OCAMLMAKEFILE = ./OCamlMakefile
RESULT=lus2lic

LIBS = str unix
OCAMLC=ocamlc

ifndef SOURCES
SOURCES =  \
	./version.ml \
	./verbose.mli \
	./verbose.ml \
	./global.ml \
	./ident.mli \
	./ident.ml \
	./lxm.mli \
	./lxm.ml \
	./errors.ml \
	./predef.ml \
	./syntaxTreeCore.ml \
	./syntaxTree.ml \
	./solveIdent.mli \
	./solveIdent.ml \
	./parserUtils.ml \
	./parser.mly \
	./lexer.mll \
	./syntaxTabUtils.mli \
	./syntaxTabUtils.ml \
	./instanciateModel.mli \
	./instanciateModel.ml \
	./symbolTab.mli \
	./symbolTab.ml \
	./eff.ml \
	./name.mli \
	./name.ml \
	./polymorphism.ml \
	./licDump.ml \
	./unifyType.mli \
	./unifyType.ml \
	./unifyClock.mli \
	./unifyClock.ml \
	./syntaxTab.mli \
	./syntaxTab.ml \
	./predefEvalType.mli \
	./predefEvalType.ml \
	./predefEvalConst.mli \
	./predefEvalConst.ml \
	./predefEvalClock.mli \
	./predefEvalClock.ml \
	./evalConst.mli \
	./evalConst.ml \
	./evalType.mli \
	./evalType.ml \
	./evalClock.mli \
	./evalClock.ml \
	./structArrayExpand.mli \
	./structArrayExpand.ml \
	./uniqueOutput.mli \
	./uniqueOutput.ml \
	./split.mli \
	./split.ml \
	./nodesExpand.mli \
	./nodesExpand.ml \
	./inline.mli \
	./inline.ml \
	./getEff.mli \
	./getEff.ml \
	./lazyCompiler.ml \
	./lazyCompiler.mli \
	./compile.ml \
	./main.ml
endif

# Be sure to build those files before doing something else
# since they are needed by $(RESULT)
PRE_TARGETS=./version.ml

MLONLY_SOURCES=$(filter %.ml %.mll %.mly, $(SOURCES))

all:
	make bc

##############################################################################"
# searching for untested code
test/ocamlprof.dump: pbc
prof: test/ocamlprof.dump
	make test || true
	mkdir prof || rm prof/*
	for d in ${SOURCES}; do \
	   ocamlprof -F "ocamlprof: " -f test/ocamlprof.dump $$d > prof/$$d ;\
	done

# Upsate the test coverage rate
coverage:
	rm test/ocamlprof.dump || true
	make clean
	make prof
	grep -n -e  "(\* ocamlprof: 0 \*)" prof/*.ml | grep -v "assert false" > dead.grep
	grep -n -e  "(\* ocamlprof: " prof/*.ml | grep -v "assert false" > all.grep
	DEAD_HITS=`wc -l dead.grep | cut -f 1 -d " "`;\
	ALL_HITS=`wc -l all.grep  | cut -f 1 -d " "`;\
	SOL=`echo "scale=2 ; $$DEAD_HITS * 100 / $$ALL_HITS " | bc ` ;\
	echo "$$DEAD_HITS / $$ALL_HITS = $$SOL %" > untested_line_counter
	make clean
	cat untested_line_counter

##############################################################################"

# for using the debugger
debug:
	mkdir -p mli
	mv *.mli mli/
	make MLONLY=yes SOURCES="$(MLONLY_SOURCES)" dc || true
	mv mli/*.mli .




include $(OCAMLMAKEFILE)




tags:
	otags  $(SOURCES)  $(shell ocamlc -where)/*.mli

.PHONY: test

test: $(RESULT)
	cd test; make test

utest: $(RESULT)
	cd test; make utest

diff:
	cd ..; make diff

cia:
	cd ..; make cia

ci:
	cd ..; make ci

wc:
	wc $(SOURCES)

# Specific rule (version)

.PHONY: version.ml
version.ml: 
	echo "(* Automatically generated from src/Makefile *) " > version.ml
	echo "let tool = \"lus2lic\"" >> version.ml
	echo "let branch = \"$(shell utils/get_branch_name)\"" >> version.ml
	echo "let commit = \"$(shell utils/get_commit_number)\"" >> version.ml
	echo "let sha_1 = \"$(shell utils/get_sha_1)"\">> version.ml
	echo "let str = (branch ^ \".\" ^ commit)">> version.ml