RESULT := civicc
GLOBALS := types globals stringify util
PHASES := load parse print desug context typecheck extern dimreduce boolop \
	constprop unroll index assemble peephole output
SOURCES := $(addsuffix .mli,$(GLOBALS)) $(addsuffix .ml,$(GLOBALS)) \
	lexer.mll parser.mly main.mli \
	$(patsubst %,phases/%.mli,$(PHASES)) $(patsubst %,phases/%.ml,$(PHASES)) \
	main.ml
PRE_TARGETS := types.ml $(addsuffix .cmi,$(GLOBALS))
LIBS := str unix

# Set debugging flag to enable exception backtraces for OCAMLRUNPARAM=b
OCAMLFLAGS := -g

OCAMLYACC := menhir
YFLAGS := --infer --explain

CIVAS := bin32/civas
CIVVM := bin32/civvm
CIVCC := ./civicc

DIST_TGT := civicaml.tar.gz
DIST_FILES := $(RESULT) $(SOURCES) Makefile OCamlMakefile README.md test bin32 \
	.gitignore

TESTSUITE_TGT := testsuite.tar.gz

.PHONY: all check dist testsuite myclean


all: native-code

clean:: myclean

# The Types module needs an implementation to stop ocamlc from complaining
types.ml: types.mli
	cp $< $@

check: all
	@cd test; \
		CIVAS=../$(CIVAS) \
		CIVVM=../$(CIVVM) \
		CIVCC=../$(CIVCC) \
		CFLAGS="-v 1" \
		RUN_FUNCTIONAL=1 \
		bash run.bash basic preprocess nested_funs arrays

dist: $(DIST_TGT)

$(DIST_TGT): $(DIST_FILES)
	tar -czf $@ $^ --exclude=types.ml

testsuite: $(TESTSUITE_TGT)

$(TESTSUITE_TGT): TESTSUITE_README test
	tar -czf $@ $^

myclean:
	rm -f a.out $(DIST_TGT) $(TESTSUITE_TGT)

include OCamlMakefile
