# 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

LFLAGS = $(shell sdl2-config --libs)
LFLAGS += -L./runtime/build -lblr_rt


all : clean ballr

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

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf  ballr scanner.ml parser.ml parser.mli
	rm -rf *.cmx *.cmi *.cmo *.cmx *.o *.s *.ll *.out *.exe

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

 OBJS = ast.cmx codegen.cmx parser.cmx scanner.cmx semant.cmx ballr.cmx
# OBJS = ast.cmo codegen_simple.cmo parser.cmo scanner.cmo ballr.cmo

ballr : clean $(OBJS)
	#ocamlc -g -I ~/.opam/system/lib/llvm $(OBJS) -o ballr
	ocamlfind ocamlopt -linkpkg -g -package llvm -package llvm.analysis $(OBJS) -o ballr

scanner.ml : scanner.mll
	ocamllex scanner.mll

parser.ml parser.mli : parser.mly
	ocamlyacc parser.mly

%.cmo : %.ml
	ocamlc -g -c -I ~/.opam/system/lib/llvm $<

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

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

%.game: ballr %.blr
	./ballr -c $(*F).blr
	clang -c $(*F).ll
	clang -g $(*F).o -o $(*F) $(LFLAGS)

%.test: 
	./ballr -c tests/$(*F).blr
	clang -c tests/$(*F).ll
	clang -g $(*F).o -o $(*F) $(LFLAGS)

### Generated by "ocamldep *.ml *.mli" after building scanner.ml and parser.ml
ast.cmo :
ast.cmx :
ballr.cmo :  semant.cmo scanner.cmo parser.cmi  ast.cmo
ballr.cmx :  semant.cmx scanner.cmx parser.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
