#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

CFLAGS = -DSKIP_MAIN
LDFLAGS = 

UNAME_S = $(shell uname -s)
ifeq ($(UNAME_S), Darwin)
	LDFLAGS += -I /Library/Frameworks/SDL2.framework/Headers -F /Library/Frameworks
endif

.PHONY : all
all : genesis.native printbig.o genesis.o

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

# "make clean" removes all generated files

.PHONY : clean
clean :
	ocamlbuild -clean
	rm -rf testall.log *.diff genesis scanner.ml parser.ml parser.mli
	rm -rf printbig ccode/genesis.o
	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 genesis.cmx

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

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

# Testing the "printbig" example

printbig : printbig.c
	cc -o printbig -DBUILD_TEST printbig.c

printbig.o :
	cc -c printbig.c -o printbig.o

genesis.o: ccode/genesis.c
	$(CC) -c $(CFLAGS) $< $(LDFLAGS) -o ccode/$@

tests: rtest test 

rtest: all
	@echo "Running regression tests..."
	@./testall.sh rtests/test-*.god rtests/fail-*.god

vtest: all
	@echo "Running Genesis SDL tests..."
	@./testall.sh vtests/test-*.god vtests/fail-*.god

test: all
	@echo "Running Genesis tests..."
	@./testall.sh

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

TESTS = add1 arith1 arith2 arith3 fib for1 for2 func1 func2 func3	\
    func4 func5 func6 func7 func8 gcd2 gcd global1 global2 global3	\
    hello if1 if2 if3 if4 if5 local1 local2 ops1 ops2 var1 var2		\
    while1 while2 printbig

FAILS = assign1 assign2 assign3 dead1 dead2 expr1 expr2 for1 for2	\
    for3 for4 for5 func1 func2 func3 func4 func5 func6 func7 func8	\
    func9 global1 global2 if1 if2 if3 nomain return1 return2 while1	\
    while2

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

TARFILES = ast.ml codegen.ml Makefile _tags genesis.ml parser.mly README \
        scanner.mll semant.ml testall.sh printbig.c arcade-font.pbm font2c \
	$(TESTFILES:%=tests/%) 

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