From: Stefano Zacchiroli <zack@debian.org>
Date: Sat, 3 Jul 2010 18:35:28 +0200
Subject: [PATCH] Makefiles fixes

Patch upstream Makefiles in order to:
 - install stuff properly in debian/ subdirs
 - built native code stuff only if ocamlopt is available
 - install camlidlruntime.h header in /usr/include/ocaml/<version>/

Author: Stefano Zacchiroli <zack@debian.org>
---
 Makefile              |    3 +++
 config/Makefile.unix  |   22 ++++++++++++----------
 lib/Makefile          |   17 +++++++++++++----
 runtime/Makefile.unix |    2 +-
 4 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile
index 45c18d2..b1e6d17 100644
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,9 @@ all:
 	cd lib; $(MAKE) all
 	cd tools; $(MAKE) all
 
+opt:
+	cd lib; $(MAKE) opt
+
 install:
 	cd compiler; $(MAKE) install
 	cd runtime; $(MAKE) install
diff --git a/config/Makefile.unix b/config/Makefile.unix
index cf4549c..8e1eae4 100644
--- a/config/Makefile.unix
+++ b/config/Makefile.unix
@@ -19,7 +19,7 @@ OSTYPE=unix
 
 # How to invoke the C preprocessor
 # Works on most Unix systems:
-CPP=/lib/cpp
+CPP=cpp
 # Alternatives:
 # CPP=cpp
 # CPP=/usr/ccs/lib/cpp
@@ -30,23 +30,25 @@ RANLIB=ranlib
 # If ranlib is not needed:
 #RANLIB=:
 
-# Location of the Objective Caml library in your installation
-OCAMLLIB=/usr/local/lib/ocaml
+# Location of the header files
+DEBIANOCAMLINCDIR=$(shell ocamlc -where)
+OCAMLINCDIR=$(DESTDIR)$(DEBIANOCAMLINCDIR)/caml
 
+# Location of the Objective Caml library in your installation
+OCAMLLIB=$(DESTDIR)$(shell ocamlc -where)
 # Where to install the binaries
-BINDIR=/usr/local/bin
+BINDIR=$(DESTDIR)/usr/bin
 
 # The Objective Caml compilers (the defaults below should be OK)
-OCAMLC=ocamlc -g
+# OCAMLC=ocamlc -g
+OCAMLC=ocamlc
 OCAMLOPT=ocamlopt
 OCAMLYACC=ocamlyacc -v
 OCAMLLEX=ocamllex
 OCAMLDEP=ocamldep
 
 # Extra flags to pass to the C compiler
-CFLAGS=-Wall -g
+CFLAGS=-I$(DEBIANOCAMLINCDIR)
 
-# Suffixes for executables and libraries (do not change)
-EXE=
-LIBEXT=a
-OBJEXT=o
+# Extension for lib files (do not change)
+LIB=.a
diff --git a/lib/Makefile b/lib/Makefile
index 7ce04da..43c77fe 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -19,8 +19,11 @@ BYTELIB=com.cma
 NATIVEOBJS=$(BYTEOBJS:.cmo=.cmx)
 NATIVELIB=$(BYTELIB:.cma=.cmxa)
 INTERFACES=$(BYTEOBJS:.cmo=.cmi)
+LIBEXT=a
 
-all: $(BYTELIB) $(NATIVELIB)
+all: $(BYTELIB)
+
+opt: $(NATIVELIB)
 
 $(BYTELIB): $(BYTEOBJS)
 	$(OCAMLC) -a -o $(BYTELIB) $(BYTEOBJS)
@@ -29,8 +32,14 @@ $(NATIVELIB): $(NATIVEOBJS)
 	$(OCAMLOPT) -a -o $(NATIVELIB) $(NATIVEOBJS)
 
 install:
-	cp $(INTERFACES) $(BYTELIB) $(NATIVELIB) $(NATIVELIB:.cmxa=.$(LIBEXT)) $(OCAMLLIB)
-	cd $(OCAMLLIB); $(RANLIB) $(NATIVELIB:.cmxa=.$(LIBEXT))
+	cp $(INTERFACES) $(BYTELIB) $(OCAMLLIB)
+	if [ -x /usr/bin/ocamlopt ]; then	\
+		cp $(NATIVELIB) $(NATIVELIB:.cmxa=$(LIB)) $(OCAMLLIB);	\
+		cd $(OCAMLLIB);	\
+		$(RANLIB) $(NATIVELIB:.cmxa=$(LIB));	\
+	else	\
+		true;	\
+	fi
 
 .SUFFIXES: .mli .ml .cmi .cmo .cmx
 
@@ -43,7 +52,7 @@ install:
 
 # Clean up
 clean::
-	rm -f *.cm[ioax] $.cmxa *~
+	-$(RM) -f *.cm[ioax] *.cmxa *.a *.o *~
 
 # Dependencies
 depend:
diff --git a/runtime/Makefile.unix b/runtime/Makefile.unix
index 5617349..737b048 100644
--- a/runtime/Makefile.unix
+++ b/runtime/Makefile.unix
@@ -22,7 +22,7 @@ libcamlidl.a: $(OBJS)
 	$(RANLIB) $@
 
 install:
-	cp camlidlruntime.h $(OCAMLLIB)/caml/camlidlruntime.h
+	cp camlidlruntime.h $(OCAMLINCDIR)/camlidlruntime.h
 	cp libcamlidl.a $(OCAMLLIB)/libcamlidl.a
 	cd $(OCAMLLIB); $(RANLIB) libcamlidl.a
 
-- 
