[OCaml] Enable -g for debug builds.
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
index 1cff422c28d400138e52255eb91873dca1e6fb06..7b26caf6bb633bb5cf97476593ba1f34434508e3 100644 (file)
@@ -1,20 +1,20 @@
-##===- tools/ml/Makefile -----------------------------------*- Makefile -*-===##
-# 
+##===- bindings/ocaml/Makefile.ocaml -----------------------*- Makefile -*-===##
+#
 #                     The LLVM Compiler Infrastructure
 #
 # This file is distributed under the University of Illinois Open Source
 # License. See LICENSE.TXT for details.
-# 
+#
 ##===----------------------------------------------------------------------===##
-# 
-# An ocaml library is a unique project type in the context of LLVM, so rules are
+#
+# An OCaml library is a unique project type in the context of LLVM, so rules are
 # here rather than in Makefile.rules.
-# 
-# Reference materials on installing ocaml libraries:
-# 
+#
+# Reference materials on installing OCaml libraries:
+#
 #   https://fedoraproject.org/wiki/Packaging/OCaml
 #   http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
-# 
+#
 ##===----------------------------------------------------------------------===##
 
 include $(LEVEL)/Makefile.config
@@ -23,6 +23,10 @@ include $(LEVEL)/Makefile.config
 CXX.Flags += -I"$(shell $(OCAMLC) -where)"
 C.Flags += -I"$(shell $(OCAMLC) -where)"
 
+ifeq ($(ENABLE_SHARED),1)
+LINK_COMPONENTS := all
+endif
+
 include $(LEVEL)/Makefile.common
 
 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
@@ -33,11 +37,23 @@ OcamlDir := $(LibDir)/ocaml
 # Info from llvm-config and similar
 ifndef IS_CLEANING_TARGET
 ifdef UsedComponents
-UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
+UsedLibs = $(shell $(LLVM_CONFIG) --libs --system-libs $(UsedComponents))
 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
 endif
 endif
 
+# How do we link OCaml executables with LLVM?
+# 1) If this is a --enable-shared build, build stub libraries. This also allows
+#    to use LLVM from toplevels.
+# 2) If this is a --disable-shared build, embed ocamlc options for building
+#    a custom runtime and a static executable. It is not possible to use LLVM
+#    from toplevels.
+ifneq ($(ObjectsO),)
+ifeq ($(ENABLE_SHARED),1)
+OCAMLSTUBS := 1
+endif
+endif
+
 # Tools
 OCAMLCFLAGS += -I $(ObjDir) -I $(OcamlDir)
 ifndef IS_CLEANING_TARGET
@@ -52,29 +68,55 @@ OCAMLAFLAGS += $(patsubst %,-cclib %, \
                                           $(UsedLibs))
 endif
 endif
-# -g was introduced in 3.10.0.
-#ifneq ($(ENABLE_OPTIMIZED),1)
-#  OCAMLDEBUGFLAG := -g
-#endif
+
+ifneq ($(ENABLE_OPTIMIZED),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)
+
+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)
+else
 Archive.CMA  := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
                                   -o)
+endif
 
-Compile.CMX  := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
+ifdef OCAMLSTUBS
+Archive.CMXA := $(strip $(OCAMLOPT) -a $(patsubst %,-cclib %, \
+                                    $(LLVMLibsOptions) -l$(LIBRARYNAME) \
+                                    -L$(SharedLibDir) $(OCAMLRPATH)) \
+                                    $(OCAMLDEBUGFLAG) -o)
+else
 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
+endif
 
 ifdef OCAMLOPT
-Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
+Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamlLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
 else
 Archive.EXE := $(strip $(OCAMLC) -cc $(CXX) $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG:%=%.cma) -o)
 endif
 
 # Source files
+ifndef OcamlSources1
 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
+endif
+
+ifndef OcamlHeaders1
 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
+endif
 
 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
@@ -108,6 +150,10 @@ OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
 endif
 
+ifdef OCAMLSTUBS
+SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
+endif
+
 ifdef TOOLNAME
 ifdef EXAMPLE_TOOL
 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
@@ -125,6 +171,10 @@ DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
 endif
 
+ifdef OCAMLSTUBS
+DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
+endif
+
 ##===- Dependencies -------------------------------------------------------===##
 # Copy the sources into the intermediate directory because older ocamlc doesn't
 # support -o except when linking (outputs are placed next to inputs).
@@ -174,7 +224,7 @@ install-a:: $(LibraryA)
        $(Echo) "Installing $(BuildMode) $(DestA)"
        $(Verb) $(MKDIR) $(PROJ_libocamldir)
        $(Verb) $(INSTALL) $(LibraryA) $(DestA)
-       $(Verb) 
+       $(Verb)
 
 uninstall-a::
        $(Echo) "Uninstalling $(DestA)"
@@ -182,6 +232,34 @@ uninstall-a::
 endif
 
 
+##===- Build stub library from C sources ----------------------------------===##
+
+ifdef SharedLib
+all-local:: $(SharedLib)
+clean-local:: clean-shared
+install-local:: install-shared
+uninstall-local:: uninstall-shared
+
+$(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
+       $(Echo) "Building $(BuildMode) $(notdir $@)"
+       $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \
+                       -o $@ $(ObjectsO)
+
+clean-shared::
+       -$(Verb) $(RM) -f $(SharedLib)
+
+install-shared:: $(SharedLib)
+       $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
+       $(Verb) $(MKDIR) $(PROJ_libocamldir)
+       $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
+       $(Verb)
+
+uninstall-shared::
+       $(Echo) "Uninstalling $(DestSharedLib)"
+       -$(Verb) $(RM) -f $(DestSharedLib)
+endif
+
+
 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
 
 all-local:: build-deplibs
@@ -233,14 +311,14 @@ clean-cmis::
 # Also install the .mli's (headers) as documentation.
 install-cmis: $(OutputsCMI) $(OcamlHeaders)
        $(Verb) $(MKDIR) $(PROJ_libocamldir)
-       $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
-         $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
-         $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
-       done
        $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
          $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
          $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
        done
+       $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
+         $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
+         $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
+       done
 
 uninstall-cmis::
        $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
@@ -352,11 +430,11 @@ $(OutputEXE): $(ToolEXE) $(OcamlDir)/.dir
 ifndef OCAMLOPT
 $(ToolEXE): $(ObjectsCMO) $(OcamlDir)/.dir
        $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
-       $(Verb) $(Archive.EXE) $@ $<
+       $(Verb) $(Archive.EXE) $@ $(ObjectsCMO)
 else
 $(ToolEXE): $(ObjectsCMX) $(OcamlDir)/.dir
        $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
-       $(Verb) $(Archive.EXE) $@ $<
+       $(Verb) $(Archive.EXE) $@ $(ObjectsCMX)
 endif
 endif
 
@@ -386,6 +464,7 @@ printcamlvars::
        $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
        $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
        $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
+       $(Echo) "SharedLib    : " '$(SharedLib)'
        $(Echo) "OcamlSources1: " '$(OcamlSources1)'
        $(Echo) "OcamlSources2: " '$(OcamlSources2)'
        $(Echo) "OcamlSources : " '$(OcamlSources)'
@@ -399,6 +478,7 @@ printcamlvars::
        $(Echo) "DestA        : " '$(DestA)'
        $(Echo) "DestCMA      : " '$(DestCMA)'
        $(Echo) "DestCMXA     : " '$(DestCMXA)'
+       $(Echo) "DestSharedLib: " '$(DestSharedLib)'
        $(Echo) "UsedLibs     : " '$(UsedLibs)'
        $(Echo) "UsedLibNames : " '$(UsedLibNames)'