[PATCH] [OCaml] Dynamically link LLVM on --enable-shared builds

Peter Zotov whitequark at whitequark.org
Mon Nov 4 00:27:06 PST 2013


This commit significantly speeds up both bytecode and native
builds of LLVM clients (from ~20 second to sub-second link time),
and allows to invoke LLVM functions from OCaml toplevel.
    
The behavior for --disable-shared builds is unchanged.

http://llvm-reviews.chandlerc.com/D2099

Files:
  bindings/ocaml/Makefile.ocaml

Index: bindings/ocaml/Makefile.ocaml
===================================================================
--- bindings/ocaml/Makefile.ocaml
+++ bindings/ocaml/Makefile.ocaml
@@ -23,6 +23,10 @@
 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
@@ -38,6 +42,18 @@
 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
@@ -60,11 +76,23 @@
 
 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
+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)) \
+                                    $(OCAMLDEBUGFLAG) -o)
+else
 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
+endif
 
 ifdef OCAMLOPT
 Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamlLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
@@ -113,6 +141,10 @@
 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
 endif
 
+ifdef OCAMLSTUBS
+LibrarySO := $(OcamlDir)/dll$(LIBRARYNAME).so
+endif
+
 ifdef TOOLNAME
 ifdef EXAMPLE_TOOL
 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
@@ -130,6 +162,10 @@
 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
 endif
 
+ifdef OCAMLSTUBS
+DestSO   := $(PROJ_libocamldir)/dll$(LIBRARYNAME).so
+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).
@@ -187,6 +223,34 @@
 endif
 
 
+##===- Build stub library from C sources ----------------------------------===##
+
+ifdef LibrarySO
+all-local:: $(LibrarySO)
+clean-local:: clean-so
+install-local:: install-so
+uninstall-local:: uninstall-so
+
+$(LibrarySO): $(ObjectsO) $(OcamlDir)/.dir
+	$(Echo) "Building $(BuildMode) $(notdir $@)"
+	$(Verb) $(Link) $(SharedLinkOptions) $(LLVMLibsOptions) \
+			-o $@ $(ObjectsO)
+
+clean-so::
+	-$(Verb) $(RM) -f $(LibrarySO)
+
+install-so:: $(LibrarySO)
+	$(Echo) "Installing $(BuildMode) $(DestSO)"
+	$(Verb) $(MKDIR) $(PROJ_libocamldir)
+	$(Verb) $(INSTALL) $(LibrarySO) $(DestSO)
+	$(Verb)
+
+uninstall-so::
+	$(Echo) "Uninstalling $(DestSO)"
+	-$(Verb) $(RM) -f $(DestSO)
+endif
+
+
 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
 
 all-local:: build-deplibs
@@ -391,6 +455,7 @@
 	$(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
 	$(Echo) "LibraryCMA   : " '$(LibraryCMA)'
 	$(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
+	$(Echo) "LibrarySO    : " '$(LibrarySO)'
 	$(Echo) "OcamlSources1: " '$(OcamlSources1)'
 	$(Echo) "OcamlSources2: " '$(OcamlSources2)'
 	$(Echo) "OcamlSources : " '$(OcamlSources)'
@@ -404,6 +469,7 @@
 	$(Echo) "DestA        : " '$(DestA)'
 	$(Echo) "DestCMA      : " '$(DestCMA)'
 	$(Echo) "DestCMXA     : " '$(DestCMXA)'
+	$(Echo) "DestSO       : " '$(DestSO)'
 	$(Echo) "UsedLibs     : " '$(UsedLibs)'
 	$(Echo) "UsedLibNames : " '$(UsedLibNames)'
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2099.1.patch
Type: text/x-patch
Size: 4139 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131104/5f2f1912/attachment.bin>


More information about the llvm-commits mailing list