# "make test" Compiles everything and runs the regression tests

.PHONY : test
test : all testall.sh
	./testall.sh

# "make all" builds the executable as well as the "c_library" library designed
# to test linking external code

.PHONY : all 
all : nodable.native c_library.o
# "make microc.native" compiles the compiler
#
# The _tags file controls the operation of ocamlbuild, e.g., by including
# packages, enabling warnings
#
# See https://github.com/ocaml/ocamlbuild/blob/master/manual/manual.adoc

nodable.native :
	opam config exec -- \
	ocamlbuild -use-ocamlfind nodable.native -pkgs llvm,llvm.analysis

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log ocamlllvm *.diff c_library.o

# Testing the "c_library"
c_library : c_library.c
	cc -o c_library -DBUILD_TEST c_library.c -c -lm

# Building the tarball

TESTS = \
	add1 add2 arith2 arith3 checktreebalance comment1 comment2 complexintuse complexstruse \
	div1 div2 eq1 fib float1 float2 float3 for1 func1 func2 func3 func4 func5 func6 func7 \
	func8 func9 gcd geq1 global1 global2 global3 global4 if1 if2 if3 if4 if5 if6 \
	leq1 list1 list2 list3 list4 listappend listdec listdemo listempty listFunct1 listnest \
	listsize listupdate local1 local2 local3 mod1 mult1 mult2 neq1 node1 node2 node3 node4 node5 node6 \
	printbool printflt printnit printst recurs1 sub1 sub2 sub3 tree tree2 tree3 tree4 treeheight treetraversal \
	while1 while2

FAILS = \
	assign1 assign2 assign3 dead1 dead2 expr1 expr2 expr3 float1 float2 \
	float1 float2 for1 for2 for3 for4 for5 func1 func2 func3 func4 func5 \
	func6 func7 func8 func9 global1 global2 if1 if2 if3 nomain print printbool \
	printstr return1 return2 while1 while2
  
TESTFILES = $(TESTS:%=test-%.nd) $(TESTS:%=test-%.out) \
	    $(FAILS:%=fail-%.nd) $(FAILS:%=fail-%.err)

TARFILES = ast.ml sast.ml codegen.ml Makefile _tags nodable.ml nodableparser.mly \
	README scanner.mll semant.ml testall.sh c_library.c c_library.h\
	Dockerfile \
	$(TESTFILES:%=tests/%) 

nodable.tar.gz : $(TARFILES)
	cd .. && tar czf nodable/nodable.tar.gz \
		$(TARFILES:%=nodable/%)
