From: Stephane Glondu <steph@glondu.net>
Date: Sat, 7 Dec 2013 20:49:16 +0100
Subject: Improve Makefile

 * fix build when ocamlopt is missing
 * simplify installation (and its customization) with ocamlfind
 * install dbm.cmx
 * do not fail if Makefile.config is missing
---
 Makefile | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/Makefile b/Makefile
index 8c42364..1b329fb 100644
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,7 @@
 #                                                                       #
 #########################################################################
 
-include Makefile.config
+-include Makefile.config
 
 OCAMLC=ocamlc
 OCAMLOPT=ocamlopt
@@ -21,11 +21,18 @@ OCAMLRUN=ocamlrun
 O=o
 A=a
 SO=so
-LIBDIR=`ocamlc -where`
-STUBLIBDIR=$(LIBDIR)/stublibs
 
+HAS_OCAMLOPT := $(shell if which ocamlopt >/dev/null; then echo yes; else echo no; fi)
 
-all: libcamldbm.$(A) dbm.cma dbm.cmxa dbm.cmxs 
+TARGETS := libcamldbm.$(A) dbm.cma
+FILES := META dllcamldbm.$(SO) libcamldbm.$(A) dbm.cma dbm.cmi dbm.mli
+
+ifeq ($(HAS_OCAMLOPT),yes)
+  TARGETS += dbm.cmxa dbm.cmxs
+  FILES += dbm.cmxa dbm.cmxs dbm.cmx dbm.$(A)
+endif
+
+all: $(TARGETS)
 
 dbm.cma: dbm.cmo
 	$(OCAMLMKLIB) -o dbm -oc camldbm -linkall dbm.cmo $(DBM_LINK)
@@ -57,13 +64,7 @@ depend:
 	$(OCAMLDEP) *.ml *.mli > .depend
 
 install::
-	if test -f dllcamldbm.$(SO); then cp dllcamldbm.$(SO) $(STUBLIBDIR)/; fi 
-	cp libcamldbm.$(A) $(LIBDIR)/
-	cd $(LIBDIR) && ranlib libcamldbm.$(A)
-	cp dbm.cma dbm.cmxa dbm.cmi dbm.mli $(LIBDIR)/
-	cp dbm.$(A) $(LIBDIR)/
-	cd $(LIBDIR) && ranlib dbm.$(A)
-	if test -f dbm.cmxs; then cp dbm.cmxs $(LIBDIR)/; fi
+	ocamlfind install dbm $(FILES)
 
 clean::
 	rm -f *.cm* *.$(O) *.$(A) *.$(SO)
@@ -77,12 +78,16 @@ testdbm.opt: dbm.cmxa testdbm.ml
 clean::
 	rm -f testdbm.byte testdbm.opt testdatabase.*
 
-test: testdbm.byte testdbm.opt
+test:: testdbm.byte
 	rm -f testdatabase.*
 	ocamlrun -I . ./testdbm.byte
 	rm -f testdatabase.*
+
+ifeq ($(HAS_OCAMLOPT),yes)
+test:: testdbm.opt
+	rm -f testdatabase.*
 	./testdbm.opt
 	rm -f testdatabase.*
-	
+endif
 
 include .depend
-- 
