[OCaml] Allow out-of-tree builds of LLVM bindings.
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
index d069c454cd0ffb6a156f5c6a51c456395488f019..1f65a7b8f905f6900f8301d90e5cf7cf6feb8bf3 100644 (file)
 
 include $(LEVEL)/Makefile.config
 
+# We have our own rules for building static libraries.
+NO_BUILD_ARCHIVE = 1
+
 # CFLAGS needs to be set before Makefile.rules is included.
-CXX.Flags += -I"$(shell $(OCAMLC) -where)"
-C.Flags += -I"$(shell $(OCAMLC) -where)"
+CXX.Flags += -I"$(shell $(OCAMLFIND) c -where)"
+C.Flags += -I"$(shell $(OCAMLFIND) c -where)"
 
 ifeq ($(ENABLE_SHARED),1)
 LINK_COMPONENTS := all
@@ -29,6 +32,12 @@ endif
 
 include $(LEVEL)/Makefile.common
 
+# Used in out-of-tree builds of OCaml bindings only.
+ifdef SYSTEM_LLVM_CONFIG
+LLVM_CONFIG = $(SYSTEM_LLVM_CONFIG)
+LLVMLibsOptions += $(shell $(LLVM_CONFIG) --ldflags)
+endif
+
 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
@@ -50,12 +59,25 @@ endif
 #    from toplevels.
 ifneq ($(ObjectsO),)
 ifeq ($(ENABLE_SHARED),1)
-OCAMLSTUBS := 1
+OCAMLSTUBS     := 1
+OCAMLSTUBFLAGS := $(patsubst %,-cclib %, $(LLVMLibsOptions) -l$(LIBRARYNAME))
+endif
+endif
+
+# Avoid the need for LD_LIBRARY_PATH
+ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+ifneq ($(HOST_OS),Darwin)
+OCAMLRPATH     := $(RPATH) -Wl,'$$ORIGIN/../../lib'
 endif
 endif
 
+# See http://caml.inria.fr/mantis/view.php?id=6642
+OCAMLORIGIN    := -ccopt -L'$$CAMLORIGIN/..' \
+                  -ccopt $(RPATH) -ccopt -Wl,'$$CAMLORIGIN/..'
+
 # Tools
-OCAMLCFLAGS += -I $(ObjDir) -I $(OcamlDir)
+OCAMLCFLAGS += -I $(OcamlDir) $(addprefix -package ,$(FindlibPackages))
+
 ifndef IS_CLEANING_TARGET
 ifneq ($(ObjectsO),)
 OCAMLAFLAGS += $(patsubst %,-cclib %, \
@@ -69,44 +91,29 @@ OCAMLAFLAGS += $(patsubst %,-cclib %, \
 endif
 endif
 
-ifneq ($(ENABLE_OPTIMIZED),1)
+ifneq ($(DEBUG_SYMBOLS),1)
   OCAMLDEBUGFLAG := -g
 endif
 
-Compile.CMI  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
-Compile.CMO  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
-Compile.CMX  := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
+Compile.CMI  := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
+Compile.CMO  := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
+Compile.CMX  := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
 
 ifdef OCAMLSTUBS
-# Avoid the need for LD_LIBRARY_PATH
-ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
-ifneq ($(HOST_OS),Darwin)
-OCAMLRPATH   := $(RPATH) -Wl,'$(SharedLibDir)'
-endif
-endif
-endif
-
-ifdef OCAMLSTUBS
-Archive.CMA  := $(strip $(OCAMLC) -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
-                                  -o)
+# -dllib is engaged with ocamlc builds, $(OCAMLSTUBFLAGS) in ocamlc -custom builds.
+Archive.CMA  := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLSTUBFLAGS) \
+                                                                                                                                                        $(OCAMLDEBUGFLAG) $(OCAMLORIGIN) -o)
 else
-Archive.CMA  := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
-                                  -o)
+Archive.CMA  := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
+                                       $(OCAMLORIGIN) -o)
 endif
 
 ifdef OCAMLSTUBS
-Archive.CMXA := $(strip $(OCAMLOPT) -a $(patsubst %,-cclib %, \
-                                    $(LLVMLibsOptions) -l$(LIBRARYNAME) \
-                                    -L$(SharedLibDir) $(OCAMLRPATH)) \
-                                    $(OCAMLDEBUGFLAG) -o)
+Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLSTUBFLAGS) $(OCAMLDEBUGFLAG) \
+                                         $(OCAMLORIGIN) -o)
 else
-Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
-endif
-
-ifdef OCAMLOPT
-Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamlLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
-else
-Archive.EXE := $(strip $(OCAMLC) -cc $(CXX) $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG:%=%.cma) -o)
+Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
+                                         $(OCAMLORIGIN) -o)
 endif
 
 # Source files
@@ -190,7 +197,7 @@ $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
 ifdef LIBRARYNAME
 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
                                    $(OcamlDir)/.dir $(ObjDir)/.dir
-       $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
+       $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
 
 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
 endif
@@ -198,7 +205,7 @@ endif
 ifdef TOOLNAME
 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
                                 $(OcamlDir)/.dir $(ObjDir)/.dir
-       $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
+       $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
 
 -include $(ObjDir)/$(TOOLNAME).ocamldep
 endif
@@ -242,8 +249,8 @@ uninstall-local:: uninstall-shared
 
 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
        $(Echo) "Building $(BuildMode) $(notdir $@)"
-       $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \
-                       -o $@ $(ObjectsO)
+       $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) -o $@ $(ObjectsO) \
+                       $(LLVMLibsOptions)
 
 clean-shared::
        -$(Verb) $(RM) -f $(SharedLib)
@@ -260,8 +267,9 @@ uninstall-shared::
 endif
 
 
-##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
+##===- Deposit dependent libraries adjacent to OCaml libs -----------------===##
 
+ifndef SYSTEM_LLVM_CONFIG
 all-local:: build-deplibs
 clean-local:: clean-deplibs
 install-local:: install-deplibs
@@ -286,7 +294,7 @@ install-deplibs:
 
 uninstall-deplibs:
        $(Verb) $(RM) -f $(DestLibs)
-
+endif
 
 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
 
@@ -367,8 +375,8 @@ endif
 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
 
 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
-# If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
-ifdef OCAMLOPT
+# If unavailable, 'configure' will set HAVE_OCAMLOPT to 0 in Makefile.config.
+ifeq ($(HAVE_OCAMLOPT),1)
 
 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
        $(Verb) $(CP) -f $< $@
@@ -418,31 +426,11 @@ uninstall-cmxa::
 endif
 endif
 
-##===- Build executables --------------------------------------------------===##
-
-ifdef TOOLNAME
-all-local:: $(OutputEXE)
-clean-local:: clean-exe
-
-$(OutputEXE): $(ToolEXE) $(OcamlDir)/.dir
-       $(Verb) $(CP) -f $< $@
-
-ifndef OCAMLOPT
-$(ToolEXE): $(ObjectsCMO) $(OcamlDir)/.dir
-       $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
-       $(Verb) $(Archive.EXE) $@ $(ObjectsCMO)
-else
-$(ToolEXE): $(ObjectsCMX) $(OcamlDir)/.dir
-       $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
-       $(Verb) $(Archive.EXE) $@ $(ObjectsCMX)
-endif
-endif
-
 ##===- Generate documentation ---------------------------------------------===##
 
 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
        $(Echo) "Documenting $(notdir $@)"
-       $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
+       $(Verb) $(OCAMLFIND) doc -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
 
 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
 
@@ -453,15 +441,17 @@ printcamlvars::
        $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
        $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
        $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
-       $(Echo) "OCAMLC       : " '$(OCAMLC)'
-       $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
-       $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
+       $(Echo) "OCAMLRPATH   : " '$(OCAMLRPATH)'
+       $(Echo) "OCAMLSTUBS   : " '$(OCAMLSTUBS)'
+       $(Echo) "OCAMLSTUBFLAGS : " '$(OCAMLSTUBFLAGS)'
+       $(Echo) "OCAMLFIND    : " '$(OCAMLFIND)'
        $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
        $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
        $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
        $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
        $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
        $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
+       $(Echo) "LibraryA     : " '$(LibraryA)'
        $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
        $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
        $(Echo) "SharedLib    : " '$(SharedLib)'