include Makefile.conf
VERSION = 0.3.2

ALL_TARGET=cduce cduce_lib.cma
INSTALL_BINARIES=cduce$(EXE)

ifneq ($(ML_INTERFACE), false)
  ALL_TARGET += cdo2ml mlcduce_wrapper
  INSTALL_BINARIES += cdo2ml$(EXE) mlcduce_wrapper$(EXE) cduce_mktop
endif


all: $(ALL_TARGET)
ifeq ($(NATIVE),true)
all: cduce_lib.cmxa
endif

PACKAGES = ulex camlp4 num cgi pcre netstring 


# Call make with VERBOSE=true to get a trace of commands

VERBOSE=false

ifneq ($(VERBOSE), true)
   HIDE=@
else
   HIDE=
endif


SYNTAX = -I misc/ q_symbol.cmo \
  -symbol cduce_version=\"$(VERSION)\" \
  -symbol build_date=\"$(shell date +%Y-%m-%d)\" \
  -symbol session_dir=\"$(SESSION_DIR)\"

SYNTAX_PARSER = -syntax camlp4o $(SYNTAX:%=-ppopt %)

CAMLC_P = ocamlc -g
DEPEND_OCAMLDEP = misc/q_symbol.cmo
ifeq ($(PROFILE), true)
  CAMLOPT_P = ocamlopt -p
  ifeq ($(NATIVE), false) 
    CAMLC_P = ocamlcp -p a
    SYNTAX_PARSER = 
    DEPEND_OCAMLDEP =
  endif
else
  CAMLOPT_P = ocamlopt -inline 25
endif

OPT = -warn-error FPS
ANNOT =
INTERFACE =
ifeq ($(ANNOT), true)
	OPT += -passopt -dtypes
endif
ifeq ($(INTERFACE), true)
	OPT += -passopt -i
endif
OCAMLFIND = ocamlfind
CAMLC = $(OCAMLFIND) $(CAMLC_P) $(OPT) -package "$(PACKAGES)"
CAMLOPT = $(OCAMLFIND) $(CAMLOPT_P) $(OPT) -package "$(PACKAGES)"

ifeq ($(NATIVE), true)
  EXTENSION = cmx
  EXTENSION_LIB = cmxa
  CAML=ocamlopt
  COMPILE = $(CAMLOPT)
  LINK = $(CAMLOPT) -linkpkg gramlib.cmxa
  SYNTAX += -symbol ocaml_compiler=\"native\"
else
  EXTENSION = cmo
  EXTENSION_LIB = cma
  COMPILE = $(CAMLC)
  CAML=ocamlc
  LINK = $(CAMLC) -custom -linkpkg gramlib.cma
  SYNTAX += -symbol ocaml_compiler=\"bytecode\"
endif


INSTALL := install

ifeq ($(NATIVE),true)
.PHONY: cduce_lib.cma
cduce_lib.cma:
	$(HIDE)$(MAKE) NATIVE=false $@
endif

install: all install_bin install_lib install_doc

install_bin:
	@echo "Install binaries"
	$(HIDE)mkdir -p $(BINDIR)
	$(HIDE)$(INSTALL) -m755 $(INSTALL_BINARIES) $(BINDIR)/
	

install_lib:
	@echo "Build META"
	$(HIDE)(sed "s/%REQ%/$(PACKAGES)/" < META.in | sed "s/%VER%/$(VERSION)/" > META)
	$(HIDE)-$(OCAMLFIND) remove cduce
	$(HIDE)-$(OCAMLFIND) install cduce META \
         cduce_lib.cmi $(wildcard *.cma) $(wildcard *.cmxa) $(wildcard *.a) \


uninstall:
	rm -f $(BINDIR)/cduce$(EXE) \
	  $(BINDIR)/cdo2ml$(EXE) \
	  $(BINDIR)/mlcduce_wrapper$(EXE) $(BINDIR)/cduce_mktop
	rm -f $(MANDIR)/man1/cduce.1 \
	  $(MANDIR)/man1/cdo2ml.1
	rm -Rf $(DOCDIR)
	ocamlfind remove cduce

help:
	@echo "GOALS"
	@echo "  cduce : compiles the CDuce command line interpreter"
	@echo "  dtd2cduce: compiles the dtd2cduce tools"
	@echo "  doc : build the documentation"
	@echo "  all : build binaries and libraries"
	@echo "  install : install binaries, man pages, documentation"
	@echo "  clean :  back to the starting point"
	@echo "  uninstall : remove installed files"

# Source directories 

DIRS = misc parser schema typing types compile runtime driver query ocamliface
CLEAN_DIRS = $(DIRS) cdo2cmo tools tests

# Objects to build

SCHEMA_OBJS = \
 schema/schema_pcre.cmo \
 schema/schema_types.cmo \
 schema/schema_xml.cmo \
 schema/schema_common.cmo \
 schema/schema_builtin.cmo \
 schema/schema_validator.cmo \
 schema/schema_parser.cmo \
 schema/schema_converter.cmo 

OBJECTS = \
 driver/config.cmo \
 misc/stats.cmo \
 misc/serialize.cmo misc/custom.cmo \
 misc/state.cmo misc/pool.cmo misc/encodings.cmo misc/bool.cmo \
 misc/pretty.cmo misc/ns.cmo misc/inttbl.cmo misc/imap.cmo \
 misc/html.cmo \
 \
 types/sortedList.cmo types/boolean.cmo types/ident.cmo \
 types/intervals.cmo \
 types/chars.cmo types/atoms.cmo \
 types/normal.cmo \
 types/types.cmo types/sample.cmo types/sequence.cmo types/patterns.cmo \
 types/builtin_defs.cmo \
 \
 compile/lambda.cmo \
 runtime/value.cmo \
 \
 parser/location.cmo  parser/url.cmo \
  \
 parser/ulexer.cmo parser/ast.cmo parser/parser.cmo \
 \
 types/externals.cmo \
 typing/typed.cmo typing/typer.cmo \
 \
 $(SCHEMA_OBJS) \
 \
 runtime/load_xml.cmo runtime/run_dispatch.cmo \
 runtime/explain.cmo \
 runtime/print_xml.cmo runtime/eval.cmo \
 compile/compile.cmo \
 compile/operators.cmo \
 \
 types/builtin.cmo \
 driver/librarian.cmo \
 driver/cduce.cmo \
 runtime/system.cmo

schema/schema_types.ml: schema/schema_types.mli
	cp $^ $@

ML_INTERFACE_OBJS = \
  ocamliface/caml_cduce.cmo \
  ocamliface/mltypes.cmo ocamliface/mlstub.cmo

ifneq ($(ML_INTERFACE), false)
  OBJECTS += $(ML_INTERFACE_OBJS)
endif

ifneq ($(CURL), false)
  OBJECTS += parser/cduce_curl.cmo
  PACKAGES += curl
endif
ifneq ($(NETCLIENT), false)
  OBJECTS += parser/cduce_netclient.cmo
  PACKAGES += netclient
endif

PXP_PACK=pxp-engine pxp-lex-iso88591
ifeq ($(PXP_WLEX), true)
  PXP_PACK += pxp-wlex-utf8
else
  PXP_PACK += pxp-lex-utf8
endif

ifeq ($(PXP),true)
  OBJECTS += runtime/cduce_pxp.cmo
  PACKAGES += $(PXP_PACK)
endif
ifeq ($(EXPAT), true)
  OBJECTS += runtime/cduce_expat.cmo
  PACKAGES += expat
endif


#CQL_OBJECTS= query/query_aggregates.cmo query/query.cmo query/query_parse.cmo 
#CQL_OBJECTS_RUN = query/query_run.cmo
CQL_OBJECTS= query/query_aggregates.cmo
CQL_OBJECTS_RUN=

OBJECTS += $(CQL_OBJECTS)

VALIDATE_OBJECTS := $(shell for o in $(OBJECTS); do echo $$o; if [ "$$o" = "schema/schema_parser.cmo" ]; then exit 0; fi; done)	# all objects until schema_parser.cmo

OBJECTS += $(CQL_OBJECTS_RUN) 
OBJECTS += driver/run.cmo
CDUCE = $(OBJECTS) driver/start.cmo 

ALL_OBJECTS = $(OBJECTS) \
 driver/start.cmo driver/examples.cmo \
 driver/webiface.cmo driver/evaluator.cmo \
 tools/validate.cmo \
 $(ML_INTERFACE_OBJS) parser/cduce_curl.cmo \
 parser/cduce_netclient.cmo \
 runtime/cduce_expat.cmo runtime/cduce_pxp.cmo

ALL_INTERFACES = schema/schema_types.mli 

DEPEND = $(ALL_OBJECTS:.cmo=.ml) $(ALL_OBJECTS:.cmo=.mli) $(ALL_INTERFACES)

INCLUDES = $(DIRS:%=-I %)

cduce: $(CDUCE:.cmo=.$(EXTENSION))
	@echo "Build $@"
	$(HIDE)$(LINK) $(INCLUDES) -o $@ $^ $(EXTRA_LINK_OPTS)

cduce_packed.ml: $(CDUCE:.cmo=.ml)
	rm -f cduce_packed.ml
	ocaml tools/pack.ml $^ > cduce_packed.ml

cduce_packed: cduce_packed.$(EXTENSION)
	$(LINK) $(INCLUDES) -o $@ $^ $(EXTRA_LINK_OPTS)


cduce_lib.$(EXTENSION_LIB): $(OBJECTS:.cmo=.$(EXTENSION))
	@echo "Pack cduce_lib.$(EXTENSION)"
	$(HIDE)$(COMPILE) -o cduce_lib.$(EXTENSION) -pack $^
	@echo "Build $@"
	$(HIDE)$(COMPILE) -a -o $@ cduce_lib.$(EXTENSION)

dtd2cduce: tools/dtd2cduce.ml
	@echo "Build $@"
	$(HIDE)$(OCAMLFIND) $(CAML) -o $@ -ccopt -static -package "$(PXP_PACK) cgi" -linkpkg $^

cduce_validate: $(VALIDATE_OBJECTS:.cmo=.$(EXTENSION)) tools/validate.$(EXTENSION)
	@echo "Build $@"
	$(HIDE)$(LINK) $(INCLUDES) -o $@ $^

mlcduce_wrapper: $(OBJECTS) ocamliface/mlcduce_wrapper.ml
	@echo "Build $@"
	$(HIDE)$(CAMLC) -linkpkg $(INCLUDES) -o $@ odyl.cma camlp4.cma pr_o.cmo $^ $(EXTRA_LINK_OPTS)

cdo2ml: ocamliface/cdo2ml.ml
	@echo "Build $@"
	$(HIDE)ocamlc -o $@ -pp camlp4o -I +camlp4 odyl.cma camlp4.cma pr_o.cmo $^

.PHONY: compute_depend
compute_depend: $(DEPEND_OCAMLDEP)
	echo $(DEPEND)
	@echo "Computing dependencies ..."
	ocamlfind ocamldep -package "$(PACKAGES)" \
		$(INCLUDES) $(SYNTAX_PARSER) $(DEPEND) > depend
clean:
	for i in $(CLEAN_DIRS); do \
	(cd $$i; rm -f *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa *~); \
	done
	rm -f ocamliface/cmi2ml ocamliface/asttypes.ml
	rm -f `find . -name "*~"`
	rm -f *.cmi *.cmo *.cma *.cmx *.a *.cmxa *.o *~ META
	rm -f cduce$(EXE) ocamlprof.dump
	rm -f dtd2cduce$(EXE) webiface$(EXE) cduce_validate$(EXE) \
		cdo2ml$(EXE) mlcduce_wrapper$(EXE) evaluator$(EXE)
	rm -Rf prepro package
	rm -f web/www/*.html web/*~
	rm -f web/*.cdo
	rm -f configure.log
	rm -rf web/doc
	rm -f META

distclean: clean
	rm -f Makefile.conf

ocamliface/mltypes.$(EXTENSION): ocamliface/caml_cduce.$(EXTENSION)

$(ALL_OBJECTS:.cmo=.$(EXTENSION)): misc/q_symbol.cmo
$(ALL_INTERFACES): misc/q_symbol.cmo

.SUFFIXES: .ml .mli .cmo .cmi .cmx

misc/q_symbol.cmo: misc/q_symbol.ml
	@echo "Build $@"		  
	$(HIDE)$(CAMLC) -c -pp 'camlp4o pa_extend.cmo q_MLast.cmo' $<

.ml.cmo:
	@echo "Build $@"
	$(HIDE)$(CAMLC) -c $(INCLUDES) $(SYNTAX_PARSER) $<

.ml.cmx:
	@echo "Build $@"
	$(HIDE)$(CAMLOPT) -c $(SYNTAX_PARSER) $(INCLUDES) $<

.mli.cmi:
	@echo "Build $@"
	$(HIDE)$(CAMLC) -c $(SYNTAX_PARSER) $(INCLUDES) $<

include depend

# Documentation

doc: cduce web/site.cdo
	mkdir -p web/doc
	(cd web; ../cduce --run site.cdo --arg doc.xml -o doc)

web/site.cdo: cduce web/xhtml.cdo web/site.cd
	./cduce -I web/ --compile web/site.cd

web/xhtml.cdo: cduce web/xhtml.cd
	./cduce -I web/ --compile web/xhtml.cd

install_doc: doc
	$(HIDE)mkdir -p $(MANDIR)/man1
	$(HIDE)(cd doc; $(MAKE) install INSTALL=$(INSTALL) MANDIR=$(MANDIR) HIDE=$(HIDE))
	mkdir -p $(DOCDIR)/img
	cp web/doc/*.html $(DOCDIR)/
	cp CHANGES INSTALL INSTALL.WIN32 README AUTHORS LICENSE $(DOCDIR)/
	cp web/img/left.gif web/img/right.gif $(DOCDIR)/img


### Pack OCaml modules for the CDuce/OCaml interface


ocamliface/caml_cduce.cmo:
	@cd ocamliface; \
	$(MAKE) PREFIX=$(ML_MODULES) MODEL=$(ML_INTERFACE) \
	        HIDE=$(HIDE) caml_cduce.cmo

ocamliface/caml_cduce.cmx:
	@cd ocamliface; \
	$(MAKE) PREFIX=$(ML_MODULES) MODEL=$(ML_INTERFACE) \
		HIDE=$(HIDE) caml_cduce.cmx
