# 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 : pienum.native image_ops.o

.PHONY : test
test:
	make dev;
	cd tests; make

.PHONY : pienum.native
pienum.native :
	ocamlbuild -use-ocamlfind -pkgs str,llvm,llvm.analysis,llvm.linker -cflags -w,-a-4-27 \
		pienum.native

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log *.diff pienum.native scanner.ml parser.ml parser.mli
	rm -rf *.cmx *.cmi *.cmo *.cmx *.o
	rm -rf *~ *.swp *.ll
	rm -rf image_ops *.s a.out
	rm -rf main

# This is just for the use of the developer.
# Compile Scanner. Parser and AST
.PHONY: dev
dev :
	ocamllex scanner.mll
	ocamlyacc parser.mly
	ocamlc -c ast.ml
	ocamlc -c parser.mli
	ocamlc -c scanner.ml
	ocamlc -c parser.ml
	ocamlc -c pienum.ml
	ocamlc -o pienum parser.cmo scanner.cmo pienum.cmo

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

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

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

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

# Custom C functions

image_ops.o: image_ops.c
	gcc -c image_ops.c

### Generated by "ocamldep *.ml *.mli" after building scanner.ml and parser.ml
ast.cmo :
ast.cmx :
codegen.cmo : ast.cmo
codegen.cmx : ast.cmx
pienum.cmo : semant.cmo scanner.cmo parser.cmi codegen.cmo ast.cmo
pienum.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 exceptions.ml Makefile pienum.ml parser.mly scanner.mll \
	semant.ml sast.ml image_ops.c ./tests

pienum.tar.gz : $(TARFILES)
	cd ../.. && tar czf pienum/pienum.tar.gz \
		$(TARFILES:%=pienum/%)
