#
# This file is part of the Witchcraft Compiler Collection
# Copyright 2016 Jonathan Brossard
#
# Homepage: https://github.com/endrazine/wcc/
#
# This file is licensed under MIT License.
#

CFLAGS := -masm=intel -W -Wall -Wextra -O0 -mpreferred-stack-boundary=12 -mstackrealign -ggdb -g3 -Wno-unused-but-set-variable -Wno-unused-parameter -I./include/sflib/ -I./include -I../../include/ -rdynamic -Wl,-E -Wl,-z,now -m32 #--sanitize=address


all::
	cd openlibm && make CFLAGS="-pie -fpie -fPIC -m32" ARCH=i386
	cd lua && make linux CFLAGS="-march=i386 -m32" CC="$(CC)"
	$(CC) $(CFLAGS) wsh.c -o wsh.o -c -ldl -lreadline -pie -fpie -fPIC
	$(CC) $(CFLAGS) wshmain.c -o wshmain.o -c -ldl -lreadline -pie -fpie -fPIC
	$(CC) $(CFLAGS) helper.c -o helper.o -c -pie -fpie -fPIC
	$(CC) $(CFLAGS) linenoise/linenoise.c -o linenoise.o -c -pie -fpie -fPIC
	$(CC) $(CFLAGS) wsh.o helper.o linenoise.o -shared -fPIC -o libwitch.so
	ar cr libwitch.a wsh.o helper.o linenoise.o
	$(CC) $(CFLAGS)  wsh.o helper.o linenoise.o wshmain.o -o wsh-i386 ./lua/src/liblua.a  -lgsl -lgslcblas /usr/lib32/libc.so -lm -ldl /usr/lib/i386-linux-gnu/libiberty.a 
	cp wsh-i386 ../../bin/

dep::
	sudo apt-get install libreadline6:i386 libreadline6-dev:i386 --reinstall

clean::
	rm wsh-i386 helper.o wsh.o wshmain.o libwitch.so libwitch.a linenoise.o -f
	cd openlibm && make clean
	cd lua && make clean

requirements::
	sudo apt-get install libreadline-dev:i386
	sudo apt-get install libiberty-dev:i386
	sudo apt-get install libgsl-dev:i386


deepclean:
	cd openlibm && make clean
	cd lua && make clean
	make clean

install::
	mkdir -p $(DESTDIR)/usr/share/wcc/
	cp -r ./scripts $(DESTDIR)/usr/share/wcc/
	cp wsh-i386 $(DESTDIR)/usr/bin/wsh-i386

uninstall::
	rm -rf $(DESTDIR)/usr/share/wcc/
	rm -f $(DESTDIR)/usr/bin/wsh-i386
