Add a shared library for LLVM, named libLLVM2.7svn.(so|dylib), and add an
authorJeffrey Yasskin <jyasskin@google.com>
Thu, 18 Feb 2010 02:36:02 +0000 (02:36 +0000)
committerJeffrey Yasskin <jyasskin@google.com>
Thu, 18 Feb 2010 02:36:02 +0000 (02:36 +0000)
--enable-shared configure flag to have the tools linked shared. (2.7svn is just
$(LLVMVersion) so it'll change to "2.7" in the release.)  Always link the
example programs shared to test that the shared library keeps working.

On my mac laptop, Debug libLLVM2.7svn.dylib is 39MB, and opt (for example) is
16M static vs 440K shared.

Two things are less than ideal here:
1) The library doesn't include any version information. Since we expect to break
the ABI with every release, this shouldn't be much of a problem. If we do
release a compatible 2.7.1, we may be able to hack its library to work with
binaries compiled against 2.7.0, or we can just ask them to recompile. I'm
hoping to get a real packaging expert to look at this for the 2.8 release.
2) llvm-config doesn't yet have an option to print link options for the shared
library. I'll add this as a subsequent patch.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96559 91177308-0d34-0410-b5e6-96231b3b80d8

Makefile
Makefile.config.in
Makefile.rules
autoconf/configure.ac
configure
test/Makefile
test/Unit/lit.cfg
test/Unit/lit.site.cfg.in
tools/llvm-shlib/Makefile [new file with mode: 0644]
unittests/Makefile.unittest

index 319bdcd0fbac522d8ddf523a361ad03e51958ccf..1819226878dcdcab13dd962dcf3879b2b438ea8e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -30,8 +30,8 @@ ifeq ($(BUILD_DIRS_ONLY),1)
   DIRS := lib/System lib/Support utils
   OPTIONAL_DIRS :=
 else
-  DIRS := lib/System lib/Support utils lib/VMCore lib tools/llvm-config \
-          tools runtime docs unittests
+  DIRS := lib/System lib/Support utils lib/VMCore lib tools/llvm-shlib \
+          tools/llvm-config tools runtime docs unittests
   OPTIONAL_DIRS := projects bindings
 endif
 
index 2cc69dcf4cf6534afb8d58a45ac37b44c08b24ef..5489b2b8de65e96d16746d960c69775e729640ca 100644 (file)
@@ -266,6 +266,9 @@ ENABLE_THREADS := @ENABLE_THREADS@
 # Do we want to build with position independent code?
 ENABLE_PIC := @ENABLE_PIC@
 
+# Do we want to link the tools shared?
+ENABLE_SHARED := @ENABLE_SHARED@
+
 # Use -fvisibility-inlines-hidden?
 ENABLE_VISIBILITY_INLINES_HIDDEN := @ENABLE_VISIBILITY_INLINES_HIDDEN@
 
@@ -276,6 +279,9 @@ ENABLE_VISIBILITY_INLINES_HIDDEN := @ENABLE_VISIBILITY_INLINES_HIDDEN@
 # Enable JIT for this platform
 TARGET_HAS_JIT = @TARGET_HAS_JIT@
 
+# Environment variable to set to change the runtime shared library search path.
+SHLIBPATH_VAR = @SHLIBPATH_VAR@
+
 # Shared library extension for host platform.
 SHLIBEXT = @SHLIBEXT@
 
index 761cc812beed2d5d55311c959b178d3f0a46f782..8cabd0b02cb00b37ecbe4da679387d6546e9c621 100644 (file)
@@ -613,11 +613,12 @@ endif
 ifneq ($(HOST_OS),Darwin)
 ifneq ($(DARWIN_MAJVERS),4)
 ifdef TOOLNAME
-ifdef EXAMPLE_TOOL
-  LD.Flags += $(RPATH) -Wl,$(ExmplDir) $(RDYNAMIC)
-else
-  LD.Flags += $(RPATH) -Wl,$(ToolDir) $(RDYNAMIC)
-endif
+  LD.Flags += $(RPATH) -Wl,'$$ORIGIN/../lib'
+  ifdef EXAMPLE_TOOL
+    LD.Flags += $(RPATH) -Wl,$(ExmplDir) $(RDYNAMIC)
+  else
+    LD.Flags += $(RPATH) -Wl,$(ToolDir) $(RDYNAMIC)
+  endif
 endif
 endif
 endif
@@ -952,11 +953,21 @@ $(LLVM_CONFIG):
 
 $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT): $(LLVM_CONFIG)
 
+ifeq ($(ENABLE_SHARED), 1)
+LLVMLibsOptions += -lLLVM$(LLVMVersion)
+LLVMLibsPaths += $(LibDir)/libLLVM$(LLVMVersion)$(SHLIBEXT)
+else
+ifeq ($(EXAMPLE_TOOL), 1)
+LLVMLibsOptions += -lLLVM$(LLVMVersion)
+LLVMLibsPaths += $(LibDir)/libLLVM$(LLVMVersion)$(SHLIBEXT)
+else
 LLVMLibsOptions += $(shell $(LLVM_CONFIG) --libs     $(LINK_COMPONENTS))
 LLVMLibsPaths   += $(LLVM_CONFIG) \
                    $(shell $(LLVM_CONFIG) --libfiles $(LINK_COMPONENTS))
 endif
 endif
+endif
+endif
 
 ###############################################################################
 # Library Build Rules: Four ways to build a library
@@ -1162,11 +1173,13 @@ endif
 #   If neither BUILD_ARCHIVE or LOADABLE_MODULE are specified, default to
 #   building an archive.
 #---------------------------------------------------------
+ifndef NO_BUILD_ARCHIVE
 ifndef BUILD_ARCHIVE
 ifndef LOADABLE_MODULE
 BUILD_ARCHIVE = 1
 endif
 endif
+endif
 
 #---------------------------------------------------------
 # Archive Library Targets:
index 40f77d828e2add6ae9bab3d436c9ad93ec622173..01ba197f540bae550a43fb4726f58f2c413fe789 100644 (file)
@@ -470,6 +470,18 @@ esac
 AC_DEFINE_UNQUOTED([ENABLE_PIC],$ENABLE_PIC,
                    [Define if position independent code is enabled])
 
+dnl Allow linking tools against the shared library.
+AC_ARG_ENABLE(shared,
+  AS_HELP_STRING([--enable-shared],
+                 [Link LLVM tools shared (default is NO)]),,
+                 enableval=default)
+case "$enableval" in
+  yes) AC_SUBST(ENABLE_SHARED,[1]) ;;
+  no)  AC_SUBST(ENABLE_SHARED,[0]) ;;
+  default) AC_SUBST(ENABLE_SHARED,[0]) ;;
+  *) AC_MSG_ERROR([Invalid setting for --enable-shared. Use "yes" or "no"]) ;;
+esac
+
 dnl Allow specific targets to be specified for building (or not)
 TARGETS_TO_BUILD=""
 AC_ARG_ENABLE([targets],AS_HELP_STRING([--enable-targets],
@@ -1265,6 +1277,10 @@ dnl Propagate the shared library extension that the libltdl checks did to
 dnl the Makefiles so we can use it there too
 AC_SUBST(SHLIBEXT,$libltdl_cv_shlibext)
 
+dnl Propagate the run-time library path variable that the libltdl
+dnl checks found to the Makefiles so we can use it there too
+AC_SUBST(SHLIBPATH_VAR,$libltdl_cv_shlibpath_var)
+
 # Translate the various configuration directories and other basic
 # information into substitutions that will end up in Makefile.config.in
 # that these configured values can be used by the makefiles
index 88050f310cacc3ad4642728d7d03f5278d2e869c..719c1b7505f6ead64b1898fc45e58004cb08ecbe 100755 (executable)
--- a/configure
+++ b/configure
@@ -689,6 +689,7 @@ TARGET_HAS_JIT
 ENABLE_DOXYGEN
 ENABLE_THREADS
 ENABLE_PIC
+ENABLE_SHARED
 TARGETS_TO_BUILD
 LLVM_ENUM_TARGETS
 LLVM_ENUM_ASM_PRINTERS
@@ -769,6 +770,7 @@ LLVMGCC_VERSION
 LLVMGCC_MAJVERS
 LLVMGCC_LANGS
 SHLIBEXT
+SHLIBPATH_VAR
 LLVM_PREFIX
 LLVM_BINDIR
 LLVM_LIBDIR
@@ -1401,6 +1403,7 @@ Optional Features:
   --enable-threads        Use threads if available (default is YES)
   --enable-pic            Build LLVM with Position Independent Code (default
                           is YES)
+  --enable-shared         Link LLVM tools shared (default is NO)
   --enable-targets        Build specific host targets: all or
                           target1,target2,... Valid targets are: host, x86,
                           x86_64, sparc, powerpc, alpha, arm, mips, spu,
@@ -4857,6 +4860,25 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+# Check whether --enable-shared was given.
+if test "${enable_shared+set}" = set; then
+  enableval=$enable_shared;
+else
+  enableval=default
+fi
+
+case "$enableval" in
+  yes) ENABLE_SHARED=1
+ ;;
+  no)  ENABLE_SHARED=0
+ ;;
+  default) ENABLE_SHARED=0
+ ;;
+  *) { { echo "$as_me:$LINENO: error: Invalid setting for --enable-shared. Use \"yes\" or \"no\"" >&5
+echo "$as_me: error: Invalid setting for --enable-shared. Use \"yes\" or \"no\"" >&2;}
+   { (exit 1); exit 1; }; } ;;
+esac
+
 TARGETS_TO_BUILD=""
 # Check whether --enable-targets was given.
 if test "${enable_targets+set}" = set; then
@@ -11035,7 +11057,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<EOF
-#line 11038 "configure"
+#line 11060 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -19595,6 +19617,9 @@ fi
 SHLIBEXT=$libltdl_cv_shlibext
 
 
+SHLIBPATH_VAR=$libltdl_cv_shlibpath_var
+
+
 # Translate the various configuration directories and other basic
 # information into substitutions that will end up in Makefile.config.in
 # that these configured values can be used by the makefiles
@@ -20652,6 +20677,7 @@ TARGET_HAS_JIT!$TARGET_HAS_JIT$ac_delim
 ENABLE_DOXYGEN!$ENABLE_DOXYGEN$ac_delim
 ENABLE_THREADS!$ENABLE_THREADS$ac_delim
 ENABLE_PIC!$ENABLE_PIC$ac_delim
+ENABLE_SHARED!$ENABLE_SHARED$ac_delim
 TARGETS_TO_BUILD!$TARGETS_TO_BUILD$ac_delim
 LLVM_ENUM_TARGETS!$LLVM_ENUM_TARGETS$ac_delim
 LLVM_ENUM_ASM_PRINTERS!$LLVM_ENUM_ASM_PRINTERS$ac_delim
@@ -20661,7 +20687,6 @@ ENABLE_CBE_PRINTF_A!$ENABLE_CBE_PRINTF_A$ac_delim
 OPTIMIZE_OPTION!$OPTIMIZE_OPTION$ac_delim
 EXTRA_OPTIONS!$EXTRA_OPTIONS$ac_delim
 BINUTILS_INCDIR!$BINUTILS_INCDIR$ac_delim
-ENABLE_LLVMC_DYNAMIC!$ENABLE_LLVMC_DYNAMIC$ac_delim
 _ACEOF
 
   if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 97; then
@@ -20703,6 +20728,7 @@ _ACEOF
 ac_delim='%!_!# '
 for ac_last_try in false false false false false :; do
   cat >conf$$subs.sed <<_ACEOF
+ENABLE_LLVMC_DYNAMIC!$ENABLE_LLVMC_DYNAMIC$ac_delim
 ENABLE_LLVMC_DYNAMIC_PLUGINS!$ENABLE_LLVMC_DYNAMIC_PLUGINS$ac_delim
 CXX!$CXX$ac_delim
 CXXFLAGS!$CXXFLAGS$ac_delim
@@ -20773,6 +20799,7 @@ LLVMGCC_VERSION!$LLVMGCC_VERSION$ac_delim
 LLVMGCC_MAJVERS!$LLVMGCC_MAJVERS$ac_delim
 LLVMGCC_LANGS!$LLVMGCC_LANGS$ac_delim
 SHLIBEXT!$SHLIBEXT$ac_delim
+SHLIBPATH_VAR!$SHLIBPATH_VAR$ac_delim
 LLVM_PREFIX!$LLVM_PREFIX$ac_delim
 LLVM_BINDIR!$LLVM_BINDIR$ac_delim
 LLVM_LIBDIR!$LLVM_LIBDIR$ac_delim
@@ -20793,7 +20820,7 @@ LIBOBJS!$LIBOBJS$ac_delim
 LTLIBOBJS!$LTLIBOBJS$ac_delim
 _ACEOF
 
-  if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 88; then
+  if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 90; then
     break
   elif $ac_last_try; then
     { { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5
index e7776f8459de05b3b574a964e6560a03cfcb0b68..5554053e9aabe2b9e0109626f5b0a1d67570a9f1 100644 (file)
@@ -200,4 +200,6 @@ Unit/lit.site.cfg: $(PROJ_OBJ_DIR)/Unit/.dir FORCE
             -e "s#@LLVM_TOOLS_DIR@#$(ToolDir)#g" \
             -e "s#@LLVMGCCDIR@#$(LLVMGCCDIR)#g" \
             -e "s#@LLVM_BUILD_MODE@#$(BuildMode)#g" \
+            -e "s#@ENABLE_SHARED@#$(ENABLE_SHARED)#g" \
+            -e "s#@SHLIBPATH_VAR@#$(SHLIBPATH_VAR)#g" \
             $(PROJ_SRC_DIR)/Unit/lit.site.cfg.in > $@
index 34372bb6cbe9381c519c0f96f9ccdb512b75f8a8..5fe07324a1ba1feda21a023b3cba9d5f79bc75a8 100644 (file)
@@ -23,7 +23,14 @@ config.test_format = lit.formats.GoogleTest(llvm_build_mode, 'Tests')
 
 ###
 
-import os
+# If necessary, point the dynamic loader at libLLVM.so.
+if config.enable_shared:
+    libdir = os.path.join(config.llvm_obj_root, config.llvm_build_mode, 'lib')
+    shlibpath = config.environment.get(config.shlibpath_var,'')
+    if shlibpath:
+        shlibpath = ':' + shlibpath
+    shlibpath = libdir + shlibpath
+    config.environment[config.shlibpath_var] = shlibpath
 
 # Check that the object root is known.
 if config.test_exec_root is None:
index c190ffa68e5f2817fdfd1ed01926755914dedcf3..51b5bc416f69bebe8124c90203ed5fbcfd98e618 100644 (file)
@@ -5,6 +5,8 @@ config.llvm_obj_root = "@LLVM_BINARY_DIR@"
 config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
 config.llvmgcc_dir = "@LLVMGCCDIR@"
 config.llvm_build_mode = "@LLVM_BUILD_MODE@"
+config.enable_shared = @ENABLE_SHARED@
+config.shlibpath_var = "@SHLIBPATH_VAR@"
 
 # Let the main config do the real work.
 lit.load_config(config, "@LLVM_SOURCE_DIR@/test/Unit/lit.cfg")
diff --git a/tools/llvm-shlib/Makefile b/tools/llvm-shlib/Makefile
new file mode 100644 (file)
index 0000000..b2f725c
--- /dev/null
@@ -0,0 +1,61 @@
+##===- tools/shlib/Makefile --------------------------------*- Makefile -*-===##
+# 
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+# 
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+LIBRARYNAME = LLVM$(LLVMVersion)
+
+NO_BUILD_ARCHIVE = 1
+LINK_LIBS_IN_SHARED = 1
+SHARED_LIBRARY = 1
+
+include $(LEVEL)/Makefile.common
+
+# Include all archives in libLLVM.(so|dylib) except the ones that have
+# their own dynamic libraries.
+Archives := $(wildcard $(LibDir)/libLLVM*.a)
+SharedLibraries := $(wildcard $(LibDir)/libLLVM*$(SHLIBEXT))
+IncludeInLibLlvm := $(filter-out $(basename $(SharedLibraries)).a, $(Archives))
+LLVMLibsOptions := $(IncludeInLibLlvm:$(LibDir)/lib%.a=-l%)
+LLVMLibsPaths   := $(IncludeInLibLlvm)
+
+$(LibName.SO): $(LLVMLibsPaths)
+
+ifeq ($(HOST_OS),Darwin)
+    # set dylib internal version number to llvmCore submission number
+    ifdef LLVM_SUBMIT_VERSION
+        LLVMLibsOptions := $(LLVMLibsOptions) -Wl,-current_version \
+                        -Wl,$(LLVM_SUBMIT_VERSION).$(LLVM_SUBMIT_SUBVERSION) \
+                        -Wl,-compatibility_version -Wl,1
+    endif
+    # Include everything from the .a's into the shared library.
+    LLVMLibsOptions    := $(LLVMLibsOptions)  \
+                          -Wl,-all_load
+    # extra options to override libtool defaults 
+    LLVMLibsOptions    := $(LLVMLibsOptions)  \
+                         -avoid-version \
+                         -Wl,-dead_strip \
+                         -Wl,-seg1addr -Wl,0xE0000000 
+
+    # Mac OS X 10.4 and earlier tools do not allow a second -install_name on command line
+    DARWIN_VERS := $(shell echo $(TARGET_TRIPLE) | sed 's/.*darwin\([0-9]*\).*/\1/')
+    ifneq ($(DARWIN_VERS),8)
+       LLVMLibsOptions    := $(LLVMLibsOptions)  \
+                            -Wl,-install_name \
+                            -Wl,"@executable_path/../lib/lib$(LIBRARYNAME)$(SHLIBEXT)"
+    endif
+endif
+
+ifeq ($(HOST_OS), Linux)
+    # Include everything from the .a's into the shared library.
+    LLVMLibsOptions := -Wl,--whole-archive $(LLVMLibsOptions) \
+                       -Wl,--no-whole-archive
+    # Don't allow unresolved symbols, and warn if we'll need to modify
+    # the text segment when loading libLLVM.so.
+    LLVMLibsOptions += -Wl,--no-undefined,--warn-shared-textrel
+endif
index 6fbef54691db9070fb1f96b4b6c3821877bc9466..5094a7e1c3ec79bc26891ee533eb12730f687cbd 100644 (file)
@@ -28,6 +28,10 @@ CPP.Flags += -I$(LLVM_SRC_ROOT)/utils/unittest/googletest/include/
 CPP.Flags += $(NO_VARIADIC_MACROS)
 TESTLIBS = -lGoogleTest -lUnitTestMain
 
+ifeq ($(ENABLE_SHARED), 1)
+Run.Shared := $(SHLIBPATH_VAR)="$(LibDir)$${$(SHLIBPATH_VAR):+:}$$$(SHLIBPATH_VAR)"
+endif
+
 $(LLVMUnitTestExe): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths)
        $(Echo) Linking $(BuildMode) unit test $(TESTNAME) $(StripWarnMsg)
        $(Verb) $(Link) -o $@ $(TOOLLINKOPTS) $(ObjectsO) $(ProjLibsOptions) \
@@ -38,6 +42,6 @@ $(LLVMUnitTestExe): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths)
 all:: $(LLVMUnitTestExe)
 
 unitcheck:: $(LLVMUnitTestExe)
-       $(LLVMUnitTestExe)
+       $(Run.Shared) $(LLVMUnitTestExe)
 
 endif