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

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

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log *.diff ml scanner.ml parser.ml parser.mli
	rm -rf *.cmx *.cmi *.cmo *.cmx *.o *.s *.err *.ll *.out pretty

# "make pretty" generates pretty print ast generator

POBJS = parser.cmo scanner.cmo ast.cmo pretty.cmo

.PHONY : pretty
pretty : $(POBJS)
	ocamlc -o pretty $(POBJS)

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

OBJS = ast.cmx codegen.cmx parser.cmx scanner.cmx semant.cmx ml.cmx prep.cmx

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

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
ml.cmo : semant.cmo scanner.cmo parser.cmi codegen.cmo ast.cmo prep.cmo
ml.cmx : semant.cmx scanner.cmx parser.cmx codegen.cmx ast.cmx prep.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
prep.cmo:
prep.cmx:

# Building the tarball

TARFILES = ast.ml codegen.ml Makefile ml.ml parser.mly README scanner.mll \
	semant.ml testall.sh demo.mxl demo.sh exceptions.ml pretty.ml stdlib.mxl \
	README file tests demo2.mxl demo3.mxl prep.ml 

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