# Inclusion of Makefile.conf may fail when cleaning up:

-include $(TOP_DIR)/Makefile.conf

# Makefile.conf: written by "configure".

# How to invoke compilers and tools:
# (May be moved to Makefile.conf if necessary)

OCAMLC    = $(USE_OCAMLFIND) ocamlc -g $(OCAMLC_OPTIONS) $(INCLUDES) $(PP_OPTIONS)
OCAMLOPT  = $(USE_OCAMLFIND) ocamlopt -inline 10 $(OCAMLOPT_OPTIONS) $(INCLUDES) $(PP_OPTIONS)
OCAMLDEP  = $(USE_OCAMLFIND) ocamldep $(OCAMLDEP_OPTIONS) $(PP_OPTIONS)
OCAMLFIND = ocamlfind
OCAMLYACC = ocamlyacc
OCAMLLEX  = ocamllex
CAMLP4    = camlp4
OCAMLMKLIB = ocamlmklib

TOOLS_DIR = $(TOP_DIR)/tools
COLLECT_FILES = $(TOOLS_DIR)/collect_files

# Note: In Makefile.conf, we have either
# PP_OPTIONS=$(PP_OPTIONS_FINDLIB) or
# PP_OPTIONS=$(PP_OPTIONS_NON_FINDLIB).
# (PP_OPTIONS: preprocessing options (camlp4))

# Standard definitions and rules

XOBJECTS = $(OBJECTS:.cmo=.cmx)
ARCHIVE  = $(PKGNAME).cma
XARCHIVE = $(PKGNAME).cmxa

all: $(ARCHIVE)

opt: $(XARCHIVE)

$(ARCHIVE): $(OBJECTS) 
	$(OCAMLC) -a -o $(ARCHIVE) $(OBJECTS)

$(XARCHIVE): $(XOBJECTS) 
	$(OCAMLOPT) -a -o $(XARCHIVE) $(XOBJECTS)

# Files to remove everywhere by "make clean":

CLEAN_LIST = *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa packlist-*

# Generic build rules:

.SUFFIXES: .cmo .cmi .cmx .ml .mli .mll .mly .mtml .c .o

.ml.cmx:
	$(OCAMLOPT) -c $<

.ml.cmo:
	$(OCAMLC) -c $<

.mtml.cmx:
	ml=`basename $< .mtml`.ml; \
	cp $< $$ml; \
	$(OCAMLOPT) -c $(THREAD) $$ml; \
	code=$$?; \
	rm $$ml; \
	exit $$code

.mtml.cmo:
	ml=`basename $< .mtml`.ml; \
	cp $< $$ml; \
	$(OCAMLC) -c $(THREAD) $$ml; \
	code=$$?; \
	rm $$ml; \
	exit $$code

.mli.cmi:
	$(OCAMLC) -c $<

.mll.ml:
	$(OCAMLLEX) $<

.mly.ml:
	$(OCAMLYACC) $<

.c.o:
	$(OCAMLC) -c -ccopt -O $<

# Install rules:

.PHONY: install
install: install-$(INSTMETHOD) $(INSTOTHER)

.PHONY: uninstall
uninstall: $(UNINSTOTHER) uninstall-$(INSTMETHOD) 

.PHONY: install-findlib
install-findlib:
	files=`$(COLLECT_FILES) *.mli *.cmi *.cma *.cmxa *.a META $(INSTALL_EXTRA)` && \
	$(OCAMLFIND) install $(PKGNAME) $$files

.PHONY: uninstall-findlib
uninstall-findlib:
	$(OCAMLFIND) remove $(PKGNAME)


.PHONY: install-conventional
install-conventional:
	test -n $(LIBDIR)
	mkdir -p $(LIBDIR)
	touch packlist-$(PKGNAME)
	files=`$(COLLECT_FILES) *.mli *.cmi *.cma *.cmxa *.a META $(INSTALL_EXTRA) packlist-$(PKGNAME)` && \
	ls $$files | \
		{ while read f; do echo "$(LIBDIR)/$$f"; done } >packlist-$(PKGNAME) && \
	cp $$files $(LIBDIR)


# Note: Before ocamlnet 0.96, the packlist contained only relative path
# names. Because of this, the "cd $(LIBDIR)" is quite important.

.PHONY: uninstall-conventional
uninstall-conventional:
	test -d $(LIBDIR)
	files=`cat $(LIBDIR)/packlist-$(PKGNAME)` && \
	cd $(LIBDIR) && rm -f $$files

#----------------------------------------------------------------------
# general rules:

depend:: *.ml *.mli
	$(OCAMLDEP) *.ml *.mli >depend

.PHONY: clean
clean::
	rm -f $(CLEAN_LIST)

.PHONY: distclean
distclean::
	rm -f $(CLEAN_LIST) META
	rm -f *~ depend
