##############################################################################
# Harmony top-level OMakefile.

# Phony targets that don't produce files
.PHONY: clean veryclean unittest test

##############################################################################
# OMake configuration

NATIVE_ENABLED = true

if $(equal $(OSTYPE), Linux)
	OCAMLOPTFLAGS += -ccopt static

MAKE = make

USE_OCAMLFIND = true
if $(not $(OCAMLFIND_EXISTS))
   eprintln(This project requires ocamlfind\, but it was not found.)
   eprintln(You need to install ocamlfind and run "omake --configure".)
   exit 1

OCAMLFLAGS    = -dtypes -rectypes

if $(defined DEBUG)
	OCAMLCFLAGS += -g
	BYTE_ENABLED = true
	export

if $(or $(defined PROFILING_NATIVE_CODE), $(defined PNC))
	println(Profiling native code enabled.)
	OCAMLOPTFLAGS += -p
	NATIVE_ENABLED = true
	BYTE_ENABLED = false
	export

if $(or $(defined PROFILING_BYTE_CODE), $(defined PBC))
	println(Profiling byte code enabled.)
	OCAMLC = ocamlcp
	OCAMLCFLAGS += -g -p a
	BYTE_ENABLED = true
	export

##############################################################################
# Sub-directories

BINDIR = $(dir bin)

COMMONDIR = $(dir common)
TOOLSDIR = $(COMMONDIR)/tools
UBASEDIR = $(COMMONDIR)/ubase
HBASEDIR = $(COMMONDIR)/hbase
EXTERNDIR = $(COMMONDIR)/extern

SRCDIR = $(dir src)
LENSESDIR = $(dir lenses)
EXAMPLESDIR = $(dir examples)
UNITSDIR = $(dir examples/units)
XSUGARDIR = $(dir examples/xsugar)
DOCDIR = $(dir doc)

BOOMERANGDIR = $(dir .)
BOOMUNITS = $(glob $(UNITSDIR)/*.boom)
BOOMFILES = \
            $(glob $(LENSESDIR)/*.boom) \
            $(glob $(UNITSDIR)/*.boom) \
            $(BOOMERANGDIR)/QuickStart.src \
            $(glob $(EXAMPLESDIR)/*.boom) \
            $(glob $(EXAMPLESDIR)/*.src) \
            $(glob $(XSUGARDIR)/*.boom) \
	    $(glob $(DOCDIR)/*.src) \

BOOMEXE = $(BINDIR)/boomerang$(EXE) 
BOOMFLAGS = 

##############################################################################
# Translation of .src files

SRC2F = $(TOOLSDIR)/src2f$(EXE)
SRC2TEX = $(TOOLSDIR)/src2tex$(EXE)

%.mly: %.srcy $(SRC2F)
	-rm -f $@
	$(SRC2F) $< $@
	chmod(-w, $@)

%.mll: %.srcl $(SRC2F)
	-rm -f $@
	$(SRC2F) $< $@
	chmod(-w, $@)

##############################################################################
# CLEANUP

# what about .omakedb and omakedb.lock?

CLEAN = rm -rf *~ *.tmp *.cmx *.cmi *.cmo *.o *.annot *.opt *.omc *.a *.cmxa \
                  *.output ._d ._ncdi TAGS 

VERYCLEAN = $(CLEAN) $(BINDIR)

##############################################################################
# TESTING

runtest(f) =
  print([[5m  Test  [0m] $(f)[G)
  if $(eq $(shell-code $(BOOMEXE) $(BOOMFLAGS) $(f) > /dev/null), 0)
    println([[32m   OK   [m] $(f))
  else
    println([[41;30m FAILED [m][91m $(f) [m)

unittest: $(BOOMEXE)
	foreach(f,$(BOOMUNITS))
	  runtest($(f))

test: $(BOOMEXE)
	foreach(f,$(BOOMFILES))
	  runtest($(f))

clean:
	$(CLEAN) 

veryclean: clean
	$(VERYCLEAN)

##############################################################################
# Include sub-directories

SUBDIRS = common src lenses examples doc

.SUBDIRS: $(SUBDIRS)

