# 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 : MatchaScript.native

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

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log MatchaScript scanner.ml parser.ml parser.mli
	rm -rf *.cmx *.cmi *.cmo *.cmx *.o
	rm -rf *.diff *.err *.ll *.pretty *.prettytokens *.out
	rm -f ./frontend_tests/scannerprint.ml ./frontend_tests/*.tokens

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

OBJS = ast.cmx codegen.cmx parser.cmx scanner.cmx analyzer.cmx MatchaScript.cmx top_ast.cmx sast.cmx

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

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 :
sast.cmo :
sast.cmx :
codegen.cmo : ast.cmo sast.cmo
codegen.cmx : ast.cmx sast.cmx
top_ast.cmo :
top_ast.cmx :
MatchaScript.cmo : analyzer.cmo scanner.cmo parser.cmi codegen.cmo ast.cmo top_ast.cmo sast.cmo str.cma
MatchaScript.cmx : analyzer.cmx scanner.cmx parser.cmx codegen.cmx ast.cmx top_ast.cmx sast.cmx str.cmxa
parser.cmo : ast.cmo parser.cmi
parser.cmx : ast.cmx parser.cmi
scanner.cmo : parser.cmi
scanner.cmx : parser.cmx
analyzer.cmo: ast.cmo sast.cmo str.cma
analyzer.cmx: ast.cmx sast.cmx str.cmxa
parser.cmi : ast.cmo


# Building the tarball

TESTS = hello

FAILS = 

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

TARFILES = ast.ml codegen.ml Makefile MatchaScript.ml parser.mly top_ast.ml README scanner.mll \
	analyzer.ml sast.ml testall.sh $(TESTFILES:%=tests/%)

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