# make all: 		make bytecode archive, including MT version
# make opt: 		make native archive, including MT version
# make tcl:             make TCL-enabled bytecode archive 
# make tcl_opt:         make TCL-enabled native archive
# make install: 	install bytecode archive, and if present, native archive
# make uninstall: 	uninstall package
# make clean: 		remove intermediate files
# make distclean: 	remove any superflous files
# make release: 	cleanup, create archive, tag CVS module 
#			(for developers)

#----------------------------------------------------------------------
# specific rules for this package:

OBJECTS  = equeue.cmo unixqueue.cmo
XOBJECTS = equeue.cmx unixqueue.cmx
ARCHIVE  = equeue.cma
XARCHIVE = equeue.cmxa
TCLARCHIVE = equeue_tcl.cma
TCLXARCHIVE = equeue_tcl.cmxa
TCLNATBASE = equeue_tcl_nat
TCLNATARCHIVE = $(TCLNATBASE).cma
TCLNATXARCHIVE = $(TCLNATBASE).cmxa
NAME     = equeue
all: $(ARCHIVE) unixqueue_mt.cmo
	mv unixqueue_mt.cmo unixqueue_mt.cmo.saved

opt: $(XARCHIVE) unixqueue_mt.cmx
	mv unixqueue_mt.cmx unixqueue_mt.cmx.saved
	mv unixqueue_mt.o   unixqueue_mt.o.saved

tcl: $(TCLARCHIVE) $(TCLNATARCHIVE)

tcl_opt: $(TCLXARCHIVE) $(TCLNATXARCHIVE)

.PHONY: $(ARCHIVE)
$(ARCHIVE): 
	$(MAKE) semiclean
	./ifdef unixqueue.mlp
	$(MAKE) $(OBJECTS)
	$(OCAMLC) -a -o $(ARCHIVE) $(OBJECTS)

.PHONY: $(XARCHIVE)
$(XARCHIVE):
	$(MAKE) semiclean
	./ifdef unixqueue.mlp
	$(MAKE) $(XOBJECTS)
	$(OCAMLOPT) -a -o $(XARCHIVE) $(XOBJECTS)

.PHONY: $(TCLARCHIVE)
$(TCLARCHIVE):
	$(MAKE) semiclean
	./ifdef -D TCL unixqueue.mlp
	$(MAKE) ROPTIONS='-I +labltk' $(OBJECTS)
	$(OCAMLC) -a -o $(TCLARCHIVE) $(OBJECTS)

.PHONY: $(TCLXARCHIVE)
$(TCLXARCHIVE):
	$(MAKE) semiclean
	./ifdef -D TCL unixqueue.mlp
	$(MAKE) ROPTIONS='-I +labltk' $(XOBJECTS)
	$(OCAMLOPT) -a -o $(TCLXARCHIVE) $(XOBJECTS)

.PHONY: $(TCLNATARCHIVE)
$(TCLNATARCHIVE): tcl/libtclqueue.a
	$(MAKE) semiclean
	./ifdef -D TCL -D TCL_NATIVE unixqueue.mlp
	$(MAKE) $(OBJECTS)
	if ocamlmklib; then \
		ocamlmklib -o $(TCLNATBASE) $(OBJECTS) -oc tclqueue -Ltcl -ltclqueue -ltcl ; \
	else \
		$(OCAMLFIND) $(OCAMLC) -a -o $(TCLNATARCHIVE) $(OBJECTS) -cclib -ltclqueue -cclib -ltcl ; \
	fi

.PHONY: $(TCLNATXARCHIVE)
$(TCLNATXARCHIVE): tcl/libtclqueue.a
	$(MAKE) semiclean
	./ifdef -D TCL -D TCL_NATIVE unixqueue.mlp
	$(MAKE) $(XOBJECTS)
	if ocamlmklib; then \
		ocamlmklib -o $(TCLNATBASE) $(XOBJECTS) -oc tclqueue -Ltcl -ltclqueue -ltcl ; \
	else \
		$(OCAMLFIND) $(OCAMLOPT) -a -o $(TCLNATXARCHIVE) $(XOBJECTS) -cclib -ltclqueue -cclib -ltcl ; \
	fi

tcl/libtclqueue.a:
	$(MAKE) -C tcl

unixqueue_mt.cmo: unixqueue_mt.ml unixqueue_mt.cmi
	$(OCAMLC) -thread -c unixqueue_mt.ml

unixqueue_mt.cmx: unixqueue_mt.ml unixqueue_mt.cmi
	$(OCAMLOPT) -thread -c unixqueue_mt.ml


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

DEBUG     = -g
OPTIONS   =
OCAMLC    = ocamlc $(DEBUG) $(OPTIONS) $(ROPTIONS)
OCAMLOPT  = ocamlopt $(OPTIONS) $(ROPTIONS)
OCAMLDEP  = ocamldep $(OPTIONS)
OCAMLFIND = ocamlfind

.PHONY: install
install:
	for x in *.saved; do if [ -f $$x ]; then mv $$x `basename $$x .saved`; fi; done
	{ test ! -f $(XARCHIVE) || extra="*.cmxa *.a"; }; \
	{ test ! -f unixqueue_mt.cmo || extra="$$extra unixqueue_mt.cmo"; }; \
	{ test ! -f unixqueue_mt.cmx || extra="$$extra unixqueue_mt.cmx unixqueue_mt.o"; }; \
	{ test ! -f tcl/libtclqueue.a || extra="$$extra tcl/libtclqueue.a"; }; \
	{ test ! -f tcl/dlltclqueue.* || extra="$$extra tcl/dlltclqueue.*"; }; \
	$(OCAMLFIND) install $(NAME) *.mli *.cmi *.cma META $$extra

.PHONY: uninstall
uninstall:
	$(OCAMLFIND) remove $(NAME)

.PHONY: clean
clean:
	rm -f *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa unixqueue_aux.ml \
		unixqueue.ml
	$(MAKE) -C tcl clean

.PHONY: semiclean
semiclean:
	rm -f *.cmo *.cmx *.o unixqueue_aux.ml unixqueue.ml

.PHONY: CLEAN
CLEAN: clean
	$(MAKE) -C doc CLEAN
	$(MAKE) -C tcl CLEAN
	$(MAKE) -C examples CLEAN

.PHONY: distclean
distclean: clean
	rm -f *~ depend depend.pkg
	$(MAKE) -C doc distclean
	$(MAKE) -C tcl distclean
	$(MAKE) -C examples distclean

RELEASE: META
	awk '/version/ { print substr($$3,2,length($$3)-2) }' META >RELEASE

.PHONY: dist
dist: RELEASE
	r=`head -1 RELEASE`; cd ..; gtar czf $(NAME)-$$r.tar.gz  --exclude='*/CVS*' --exclude="*/depend.pkg" --exclude="*/depend" $(NAME)

.PHONY: tag-release
tag-release: RELEASE
	r=`head -1 RELEASE | sed -e s/\\\./-/g`; cd ..; cvs tag -F $(NAME)-$$r $(NAME)

.PHONY: release
release: distclean
	$(MAKE) tag-release
	$(MAKE) dist

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

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

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

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

# The dependencies are fixed:
equeue.cmo: equeue.cmi
unixqueue.cmo: unixqueue.cmi equeue.cmi
unixqueue.cmi: equeue.cmi

