# Make sure ocamlbuild can find opam-managed packages: first run
#
# eval `opam config env`

# Easiest way to build: using ocamlbuild, which in turn uses ocamlfind

.PHONY: all
all : clean bugsy.native builtins.o

.PHONY: bugsy.native
bugsy.native :
	ocamlbuild -I src -use-ocamlfind -pkgs llvm,llvm.analysis -cflags -w,+a-4-42 \
		bugsy.native

builtins.o:
	cc -c -o _build/builtins.o  src/builtins.c -lm

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log *.diff bugsy.native scanner.ml parser.ml parser.mli
	rm -rf _build/builtins.o
	rm -rf *.cmx *.cmi *.cmo *.cmx *.o *.s *.ll *.out *.exe
	rm -rf _build tmp

# More detailed: build using ocamlc/ocamlopt + ocamlfind to locate LLVM

OBJS = ast.cmx codegen.cmx parser.cmx scanner.cmx semant.cmx bugsy.cmx

bugsy : $(src/OBJS)
	ocamlfind ocamlopt -linkpkg -package llvm -package llvm.analysis $(src/OBJS) -o bugsy

scanner.ml : scanner.mll
	ocamllex src/scanner.mll

parser.ml parser.mli : parser.mly
	ocamlyacc src/parser.mly

%.cmo : %.ml
	ocamlc -c $<

%.cmi : %.mli
	ocamlc -c $<

%.cmx : %.ml
	ocamlfind ocamlopt -c -package llvm $<

### Generated by "ocamldep *.ml *.mli" after building scanner.ml and parser.ml
ast.cmo :
ast.cmx :
codegen.cmo : ast.cmo
codegen.cmx : ast.cmx
bugsy.cmo : semant.cmo scanner.cmo parser.cmi codegen.cmo ast.cmo
bugsy.cmx : semant.cmx scanner.cmx parser.cmx codegen.cmx ast.cmx
parser.cmo : ast.cmo parser.cmi
parser.cmx : ast.cmx parser.cmi
scanner.cmo : parser.cmi
scanner.cmx : parser.cmx
semant.cmo : ast.cmo
semant.cmx : ast.cmx
parser.cmi : ast.cmo

# Building the tarball

#TESTS = hello add beans bool

TESTS = add1 arith1 arith2 arith3 array1 array2 circle decrement1 decrement2 ellipse fib for1 for2 func1 func2 func3	\
    func4 func5 func6 func7 func8 gcd2 gcd global1 global2 global3	\
    hello if1 if2 if3 if4 if5 increment1 increment2 local1 local2 moveBy ops1 ops2 rectangle regagon rotateBy scaleBy square triangle var1 var2		\
    while1 while2

FAILS = array assign1 assign2 assign3 dead1 dead2 expr1 expr2 for1 for2	\
    for3 for4 for5 func1 func2 func3 func4 func5 func6 func7 func8	\
    func9 global1 global2 if1 if2 if3 nomain return1 return2 while1	\
    while2

TESTFILES = $(TESTS:%=tests/test-%.bug) $(TESTS:%=golden_set/test-%.out) \
	    $(FAILS:%=tests/fail-%.bug) $(FAILS:%=fails/fail-%.err)

EXAMPLES = fact.bug shape_test.bug \
arrays.bug animation-array-example.bug loop.bug fizzbug.bug
 

TARFILES = src/ast.ml src/codegen.ml Makefile src/bugsy.ml src/parser.mly\
					 README src/scanner.mll src/sast.ml src/semant.ml scripts/testall.sh\
					 $(TESTFILES:%=test/%) src/builtins.c scripts/bugsyc \
					 $(EXAMPLES:%=examples/%) Dockerfile

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