# 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 : JSTEM.native
JSTEM.native :
	ocamlbuild -use-ocamlfind -pkgs llvm,llvm.analysis,llvm.bitwriter,llvm.bitreader,llvm.linker,str -cflags -w,+a-4 \
		JSTEM.native

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log *.diff microc scanner.ml parser.ml parser.mli
	rm -rf *.cmx *.cmi *.cmo *.cmx *.o *.s *.ll *.out *.exe
	cd tests; rm -rf *.err *.diff *.ll *.o *.out; make clean

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

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

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

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 $<


TESTOBJS = parser.cmo scanner.cmo

.PHONY : testbuild
testbuild : JSTEM.native test_parser_scanner

.PHONY : test_parser_scanner
test_parser_scanner : $(TESTOBJS)


test: testbuild
	cd tests; make

### Generated by "ocamldep *.ml *.mli" after building scanner.ml and parser.ml
ast.cmo :
ast.cmx :
codegen.cmo : ast.cmo
codegen.cmx : ast.cmx
microc.cmo : semant.cmo scanner.cmo parser.cmi codegen.cmo ast.cmo
microc.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
TARFILES = ast.ml codegen.ml Makefile JSTEM.ml parser.mly README scanner.mll \
	semant.ml testall.sh $(TESTFILES:%=tests/%) printbig.c arcade-font.pbm \
	font2c

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