# ocamldbi
# Copyright (C) 2003-2004 Merjis Ltd.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: Makefile,v 1.12 2004/03/18 14:58:48 rwmj Exp $

include Makefile.config

# XXX Hack required by ocamlc.
# To work out what this should be, try:
# `shell perl -MExtUtils::Embed -e ldopts'
DYNALOADER_HACK := /usr/lib/perl/5.8.2/auto/DynaLoader/DynaLoader.a

OCAMLCINCS := -I $(PCRELIBDIR)
OCAMLOPTINCS := -w s -I $(PCRELIBDIR)
OCAMLCLIBS := pcre.cma

ifeq ($(HAVE_POSTGRES),1)
OCAMLCINCS += -I $(shell ocamlfind query postgres)
OCAMLOPTINCS += -I $(shell ocamlfind query postgres)
OCAMLCLIBS += postgres.cma
endif

ifeq ($(HAVE_MYSQL),1)
OCAMLCINCS += -I $(shell ocamlfind query mysql)
OCAMLOPTINCS += -I $(shell ocamlfind query mysql)
OCAMLCLIBS += mysql.cma
endif

ifeq ($(HAVE_PERL4CAML),1)
OCAMLCINCS += -I $(shell ocamlfind query perl)
OCAMLOPTINCS += -I $(shell ocamlfind query perl)
OCAMLCLIBS += perl4caml.cma $(DYNALOADER_HACK)
endif

ifeq ($(HAVE_OCAMLODBC),1)
OCAMLCINCS += -I $(shell ocamlfind query ocamlodbc)
OCAMLOPTINCS += -I $(shell ocamlfind query ocamlodbc)
OCAMLCLIBS += ocamlodbc.cma
endif

DBI_DRIVERS :=
DBI_MODULES :=

ifeq ($(HAVE_POSTGRES),1)
DBI_DRIVERS += dbi_postgres.cmo
DBI_MODULES += "module D1 = Dbi_postgres "
endif

ifeq ($(HAVE_POSTGRESQL),1)
DBI_DRIVERS += dbi_postgresql.cmo
DBI_MODULES += "module D1' = Dbi_postgresql "
endif

ifeq ($(HAVE_MYSQL),1)
DBI_DRIVERS += dbi_mysql.cmo
DBI_MODULES += "module D2 = Dbi_mysql "
endif

ifeq ($(HAVE_PERL4CAML),1)
DBI_DRIVERS += dbi_perl.cmo
DBI_MODULES += "module D3 = Dbi_perl "
endif

ifeq ($(HAVE_OCAMLODBC),1)
DBI_DRIVERS += dbi_odbc.cmo
DBI_MODULES += "module D4 = Dbi_ocamlodbc "
endif

OCAMLCFLAGS := -g $(OCAMLCINCS)
OCAMLOPTFLAGS := $(OCAMLOPTINCS)
OCAMLDOCFLAGS := -html -stars -sort -colorize-code $(OCAMLCINCS)

SED := sed

.PHONY: all byte opt doc examples

ifeq ($(BUILD_OPT),1)
all: byte opt html examples
else
all: byte html examples
endif

byte: META dbi.cma
opt:  META dbi.cmxa
doc:  html
examples-byte: examples/dbi_example.exe
examples-opt:  examples/dbi_example.opt

# Beware that all symbols of .cmo gathered into dbi.cma will be
# exported, so utility modules must end with "_" to minimize the risk
# of name collision.
dbi.cma:  dbi.cmo dbi_utils_.cmo $(DBI_DRIVERS)
dbi.cmxa: dbi.cmx dbi_utils_.cmx $(DBI_DRIVERS:.cmo=.cmx)

META:	META.in Makefile.config
	$(SED)  -e 's/@PACKAGE@/$(PACKAGE)/' \
		-e 's/@VERSION@/$(VERSION)/' \
		< $< > $@

# Example(s).

examples/use_db.ml:
	echo $(DBI_MODULES) > $@  ## force linking in the detected modules

examples/dbi_example.exe: dbi.cma examples/use_db.cmo examples/dbi_example.ml
	$(OCAMLC) $(OCAMLCFLAGS) $(OCAMLCLIBS) $^ -o $@

examples/dbi_example.opt: dbi.cmxa examples/use_db.cmx examples/dbi_example.ml
	$(OCAMLOPT) $(OCAMLOPTFLAGS) $(OCAMLCLIBS:.cma=.cmxa) $^ -o $@

# Generic compilation instructions.

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

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

%.cma: %.cmo
	$(OCAMLC) $(OCAMLCFLAGS) -a $^ -o $@

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

%.cmxa: %.cmx
	$(OCAMLOPT) $(OCAMLOPTFLAGS) -a $^ -o $@


.PHONY: dep depend
dep:	.depend
depend: .depend

.depend: *.mli *.ml */*.ml
	rm -f .depend
	ocamldep $^ > $@

ifeq ($(wildcard .depend),.depend)
include .depend
endif

# Documentation.

html:	html/index.html

INTERFACES = $(wildcard *.mli)
html/index.html: $(INTERFACES) $(INTERFACES:.mli=.cmi)
	mkdir --parents html
	-$(OCAMLDOC) $(OCAMLDOCFLAGS) -d html $(INTERFACES)

clean:
	rm -f *~ *.bak core *.o *.lo *.slo
	rm -f *.cmi *.cmo *.cmx *.cma *.so *.a
	cd examples/ && \
		rm -f *~ *.o *.cm{i,o,x} dbi_example.{exe,opt}
	rm -f META
	rm -rf html
	rm -f examples/use_db.ml # automatically generated.

# Installation.

ifeq ($(BUILD_OPT),1)
install: install-main install-byte install-opt install-dbi-interfaces \
	install-doc install-html
else
install: install-main install-byte install-dbi-interfaces \
	install-doc install-html
endif

install-byte:
	install -c -m 0644 dbi.cma $(DESTDIR)$(OCAMLDBIDIR)
install-opt:
	install -c -m 0644 dbi.cmxa dbi.a $(DESTDIR)$(OCAMLDBIDIR)

install-main:
	install -c -m 0755 -d $(DESTDIR)$(OCAMLDBIDIR)
	install -c -m 0644 META $(DESTDIR)$(OCAMLDBIDIR)

install-dbi-interfaces:
	install -c -m 0644 dbi.mli dbi.cmi $(DBI_DRIVERS:.cmo=.mli) \
		$(DBI_DRIVERS:.cmo=.cmi) $(DESTDIR)$(OCAMLDBIDIR)

install-doc:
	install -c -m 0755 -d $(DESTDIR)$(DOCDIR)
	install -c -m 0644 AUTHORS FAQ README TODO $(DESTDIR)$(DOCDIR)

install-html:
	install -c -m 0755 -d $(DESTDIR)$(DOCDIR)/html
	install -c -m 0644 html/*.html $(DESTDIR)$(DOCDIR)/html

dist:
	rm -rf $(PACKAGE)-$(VERSION)
	mkdir $(PACKAGE)-$(VERSION)
	tar -cf - -T MANIFEST | tar -C $(PACKAGE)-$(VERSION) -xf -
	tar zcf $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE)-$(VERSION)
	rm -rf $(PACKAGE)-$(VERSION)
	ls -l $(PACKAGE)-$(VERSION).tar.gz

#upload:
#	rm -rf /tmp/sv_upload
#	mkdir -p /tmp/sv_upload/unstable.pkg/$(VERSION)
#	cp $(PACKAGE)-$(VERSION).tar.gz /tmp/sv_upload/unstable.pkg/$(VERSION)
#	scp -2r /tmp/sv_upload/* rwmj@subversions.gnu.org:/upload/modcaml/
#	rm -rf /tmp/sv_upload

upload:
	rm -f $(PACKAGE)-$(VERSION).tar.gz.sig
	gpg -b $(PACKAGE)-$(VERSION).tar.gz
	ftp-upload -v -h savannah.gnu.org --dir /incoming/savannah/modcaml \
	  $(PACKAGE)-$(VERSION).tar.gz{,.sig}

check-manifest:
	@for d in `find -type d -name CVS`; \
	do \
	b=`dirname $$d`/; \
	awk -F/ '$$1 != "D" {print $$2}' $$d/Entries | \
	sed -e "s|^|$$b|" -e "s|^\./||"; \
	done | sort > .check-manifest; \
	sort MANIFEST > .orig-manifest; \
	diff -u .orig-manifest .check-manifest; \
	rm -f .orig-manifest .check-manifest

# Build Debian package.
dpkg:
	@if [ 0 != `cvs -q update | wc -l` ]; then \
	echo Please commit all changes to CVS first.; \
	exit 1; \
	fi
	$(MAKE) dist
	rm -rf /tmp/dbuild
	mkdir /tmp/dbuild
	cp $(PACKAGE)-$(VERSION).tar.gz \
	  /tmp/dbuild/ocamldbi_$(VERSION).orig.tar.gz
	export CVSROOT=`cat CVS/Root`; \
	  cd /tmp/dbuild && \
	  cvs export \
	  -d ocamldbi-$(VERSION) \
	  -D now ocamldbi
	cd /tmp/dbuild/ocamldbi-$(VERSION) && dpkg-buildpackage -rfakeroot
	rm -rf /tmp/dbuild/ocamldbi-$(VERSION)
	ls -l /tmp/dbuild

.PHONY: example-dbi \
	install-main install-dbi install-dbi-postgres install-dbi-mysql \
	install-dbi-perl install-dbi-odbc \
	install-dbi-postgres-opt install-dbi-mysql-opt \
	install-dbi-perl-opt install-dbi-odbc \
	dep depend dist upload check-manifest html
.SUFFIXES: .ml .mli .cmi .cmo