# -*- makefile -*-
.PHONY: all clean debug doc .headers

OCAMLBUILD=ocamlbuild -use-ocamlfind
ODIR=`ocamlc -where`

# Menhir can be told to produce a parser that explains what
# it is doing.
MENHIROPT_COMMON=\
   -yaccflag --explain -yaccflag --inspection -yaccflag --table \
   -yaccflag --dump -tag debug

ifeq ($(DEBUGPARSING), yes)
  MENHIROPT=$(MENHIROPT_COMMON) -yaccflag --trace
else
  MENHIROPT=$(MENHIROPT_COMMON)
endif

OCAMLBUILDFLAGS_COMMON = $(MENHIROPT)

# In Emacs, use classic display to enable error jumping.
TERM = $(shell echo $$TERM)
ifeq ($(TERM), dumb)
 OCAMLBUILDFLAGS = $(OCAMLBUILDFLAGS_COMMON) -tag debug -classic-display
else
 OCAMLBUILDFLAGS = $(OCAMLBUILDFLAGS_COMMON)
endif

OCAMLBUILD = ocamlbuild -use-ocamlfind $(OCAMLBUILDFLAGS)

all:
	echo "(* This file is automatically generated. *)" > version.ml
	echo "let current=\"`cat ../VERSION`\"" >> version.ml
	# echo "let commitdate = \"`git log -1 --pretty=format:%ad`\"" >> version.ml
	# echo "let commithash = \"`git log -1 --pretty=format:%H`\"" >> version.ml
	if (which ocamlopt > /dev/null); then \
		echo "compilation to native code"; \
		$(OCAMLBUILD) morbigDriver.native \
			morbig.cma morbig.cmxa morbig.cmxs CAPI.native.o libmorbigcstub.a \
		&& cp `ocamlc -where`/libasmrun.a _build/libocaml.a; \
	else \
		echo "compilation to byte code" ;\
		$(OCAMLBUILD) morbigDriver.byte morbig.cma libmorbigcstub.a \
		&& cp `ocamlc -where`/libocamlrun.a _build/libocaml.a; \
	fi && \
	ar -rc _build/libmorbigc.a		\
	  _build/libocaml.a			\
	  $(ODIR)/libcamlstr.a			\
	  $(ODIR)/dynlink.a			\
	  _build/libmorbigcstub.a		\
	  _build/CAPI.native.o

debug:
	$(OCAMLBUILD) -tag debug morbigDriver.byte morbig.cma

clean:
	$(OCAMLBUILD) -clean

doc:
	$(OCAMLBUILD) morbig.docdir/index.html

headers:
	headache -h .header *.ml *.mli *.mll *.mly
