# 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

all : mscompile.native 

mscompile.native :
	ocamlbuild -use-ocamlfind -pkgs llvm,llvm.analysis -cflags -w,+a-4 \
		mscompile.native

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log *.diff mscompile scanner.ml parser.ml parser.mli
	rm -rf *.cmx *.cmi *.cmo *.ll *.cmx *.o *.s *.out *.exe *.a *.err *~ 
	rm -rf */master.dSYM/ */slave.dSYM/
	rm -rf *master *slave 

# "make clean_tests" removes all generated files from testall.sh

.PHONY : clean_tests
clean_tests :
	rm -rf testall.log *.diff *master *slave ./tests/*.ll *.err *~ *.ll .ll *.out *.s

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

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

mscompile : $(OBJS)
	ocamlfind ocamlopt -linkpkg -package llvm -package llvm.analysis $(OBJS) -o mscompile

scanner.ml : scanner.mll
	ocamllex scanner.mll

parser.ml parser.mli : parser.mly
	ocamlyacc 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
mscompile.cmo : semant.cmo scanner.cmo parser.cmi codegen.cmo ast.cmo
mscompile.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 = add1 arith1 arith2 arith3 fib for1 for2 func1 func2 func3	\
    func4 func5 func6 func8 gcd2 gcd								\
    double1	double-add vector										\
    hello if1 if2 if3 if4 if5 local1 local2 ops1 ops2 var1			\
    while1 while2                                               	\
	cancel decl-assign double-add double1 \
	remote-primes remote-doubles remote-int remote-job-get \
	remote-job-states remote-many-ints remote-struct-serialize	remote-vector-serialize \
	string-concat string1 string2 struct-field-copy struct-in-vector struct-nocopy \
	struct-return vector vector-args vector-assign vector-struct-copy-assign vector-struct-copy-free \						


FAILS = assign-double assign-string assign-string1 \
	assign1 assign2 assign3 dead1 dead2 expr1 expr2 for1 for2	\
    for3 for4 for5 func1 func2 func3 func4 func5 func6 func7 func8 func9 func10 func11	\
    func9 if1 if2 if3 nomaster return1 return2 while1 while2	\
    job-cancel job-get job-get2 job-state1 job-state2 \
    remote remote1 string-concat \
    struct1 struct2 struct3 struct4 struct5 struct6 vector vector2 vector3 vector4 \

TESTFILES = $(TESTS:%=test-%.mc) $(TESTS:%=test-%.out) \
	    $(FAILS:%=fail-%.mc) $(FAILS:%=fail-%.err)

TARFILES = ast.ml codegen.ml Makefile mscompile.ml parser.mly README scanner.mll \
	semant.ml testall.sh run.sh $(TESTFILES:%=tests/%) arcade-font.pbm \
	font2c

mscompile-llvm.tar.gz : $(TARFILES)
	cd .. && tar czf mscompile-llvm/mscompile-llvm.tar.gz \
		$(TARFILES:%=mscompile-llvm/%)
