[LLVMdev] [PATCH] Capability of Win32.DLL with ENABLE_SHARED

NAKAMURA Takumi geek4civic at gmail.com
Wed Aug 4 21:08:06 PDT 2010


Good summer, all!

This patch enables ENABLE_SHARED=1 to build DLL based LLVM toolchain.
I have checked this on Cygwin-1.5, Cygwin-1.7, mingw(msysgit) and
mingw-cross-fedora12.

I can separate this patch into some parts; cleanups, adding
definitions and adding rules.

Any feedbacks are welcome.
Have fun!

...Takumi


* Pros
  - reduction of linking time of toolchain.
  - capability of -load plugins on opt.exe
  - capability of LLVM APIs on lli.exe from bc
  - reduction of size of executables.

* Cons
  - linking into DLL spends so much time.
  - contains some ugly tweaks.

* Known issues
  - I took the way to link libstdc++.a together into LLVM.dll. It may
invoke nasal demons. :(
  - On cygwin, fork(3) on DLL sometimes fails to clang.exe crashes.
You have to do "rebaseall".
    (It would be better to get rid of fork(3) in llvm::System for
cygwin in future)
  - I have not checked llvmc.

* TODO
  - check system's stdc++.dll and use it with autoconf.
-------------- next part --------------
diff --git a/Makefile.rules b/Makefile.rules
index 66666b4..2a2128b 100644
--- a/Makefile.rules
+++ b/Makefile.rules
@@ -498,6 +498,25 @@ LLVMToolDir := $(LLVM_OBJ_ROOT)/$(BuildMode)/bin
 LLVMExmplDir:= $(LLVM_OBJ_ROOT)/$(BuildMode)/examples
 
 #--------------------------------------------------------------------
+# Locations of shared libraries
+#--------------------------------------------------------------------
+
+SharedPrefix  := lib
+SharedDir     := $(LibDir)
+LLVMSharedDir := $(LLVMLibDir)
+
+# Win32.DLL prefers to be located on the "PATH" of binaries.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+  SharedPrefix  :=
+  SharedDir     := $(ToolDir)
+  LLVMSharedDir := $(LLVMToolDir)
+endif
+
+ifeq ($(HOST_OS),Cygwin)
+  SharedPrefix  := cyg
+endif
+
+#--------------------------------------------------------------------
 # LLVM Capable Compiler
 #--------------------------------------------------------------------
 
@@ -570,12 +589,7 @@ ifeq ($(HOST_OS),Darwin)
     SharedLinkOptions += -mmacosx-version-min=$(DARWIN_VERSION)
   endif
 else
-  ifeq ($(HOST_OS),Cygwin)
-    SharedLinkOptions=-shared -nostdlib -Wl,--export-all-symbols \
-                      -Wl,--enable-auto-import -Wl,--enable-auto-image-base
-  else
-    SharedLinkOptions=-shared
-  endif
+  SharedLinkOptions=-shared
 endif
 
 ifeq ($(TARGET_OS),Darwin)
@@ -585,11 +599,13 @@ ifeq ($(TARGET_OS),Darwin)
 endif
 
 ifdef SHARED_LIBRARY
+ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
 ifneq ($(HOST_OS),Darwin)
   LD.Flags += $(RPATH) -Wl,'$$ORIGIN'
 else
 ifneq ($(DARWIN_MAJVERS),4)
-  LD.Flags += $(RPATH) -Wl,$(LibDir)
+  LD.Flags += $(RPATH) -Wl,$(SharedDir)
+endif
 endif
 endif
 endif
@@ -960,6 +976,13 @@ LLVMUsedLibs    := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(LLVMLIBS)))
 LLVMLibsPaths   := $(addprefix $(LLVMLibDir)/,$(LLVMUsedLibs))
 endif
 
+# Win32.DLL may refer to other components.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+  ifdef LOADABLE_MODULE
+    LINK_COMPONENTS := all
+  endif
+endif
+
 ifndef IS_CLEANING_TARGET
 ifdef LINK_COMPONENTS
 
@@ -972,8 +995,13 @@ $(LLVM_CONFIG):
 $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT): $(LLVM_CONFIG)
 
 ifeq ($(ENABLE_SHARED), 1)
+# We can take the "auto-import" feature to get rid of using dllimport.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+LLVMLibsOptions += -Wl,--enable-auto-import,--enable-runtime-pseudo-reloc \
+                   -L $(SharedDir)
+endif
 LLVMLibsOptions += -lLLVM-$(LLVMVersion)
-LLVMLibsPaths += $(LibDir)/libLLVM-$(LLVMVersion)$(SHLIBEXT)
+LLVMLibsPaths += $(SharedDir)/$(SharedPrefix)LLVM-$(LLVMVersion)$(SHLIBEXT)
 else
 LLVMLibsOptions += $(shell $(LLVM_CONFIG) --libs     $(LINK_COMPONENTS))
 LLVMLibsPaths   += $(LLVM_CONFIG) \
@@ -1018,6 +1046,21 @@ ifeq ($(HOST_OS),Darwin)
 LLVMLibsOptions += -Wl,-exported_symbols_list,$(NativeExportsFile)
 endif
 
+# GNU ld Win32 accepts .DEF files that contain "DATA" entries.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+NativeExportsFile := $(ObjDir)/$(notdir $(EXPORTED_SYMBOL_FILE:.exports=.def))
+
+# LLVMLibsOptions is invalidated at processing tools/llvm-shlib.
+SharedLinkOptions += $(NativeExportsFile)
+
+$(NativeExportsFile): $(EXPORTED_SYMBOL_FILE) $(ObjDir)/.dir
+	$(Echo) Generating $(notdir $@)
+	$(Verb) $(ECHO) "EXPORTS" > $@
+	$(Verb) $(CAT) $< >> $@
+clean-local::
+	-$(Verb) $(RM) -f $(NativeExportsFile)
+else
+
 # gold, bfd ld, etc.
 ifeq ($(HAVE_LINK_VERSION_SCRIPT),1)
 LLVMLibsOptions += -Wl,--version-script,$(NativeExportsFile)
@@ -1025,6 +1068,8 @@ endif
 
 endif
 
+endif
+
 ###############################################################################
 # Library Build Rules: Four ways to build a library
 ###############################################################################
@@ -1097,10 +1142,10 @@ ifdef LIBRARYNAME
 LIBRARYNAME := $(strip $(LIBRARYNAME))
 ifdef LOADABLE_MODULE
 LibName.A  := $(LibDir)/$(LIBRARYNAME).a
-LibName.SO := $(LibDir)/$(LIBRARYNAME)$(SHLIBEXT)
+LibName.SO := $(SharedDir)/$(LIBRARYNAME)$(SHLIBEXT)
 else
 LibName.A  := $(LibDir)/lib$(LIBRARYNAME).a
-LibName.SO := $(LibDir)/lib$(LIBRARYNAME)$(SHLIBEXT)
+LibName.SO := $(SharedDir)/$(SharedPrefix)$(LIBRARYNAME)$(SHLIBEXT)
 endif
 LibName.O  := $(LibDir)/$(LIBRARYNAME).o
 LibName.BCA:= $(LibDir)/lib$(LIBRARYNAME).bca
@@ -1125,13 +1170,13 @@ SharedLibKindMessage := "Loadable Module"
 else
 SharedLibKindMessage := "Shared Library"
 endif
-$(LibName.SO): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths) $(LibDir)/.dir
+$(LibName.SO): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths) $(SharedDir)/.dir
 	$(Echo) Linking $(BuildMode) $(SharedLibKindMessage) \
 	  $(notdir $@)
 	$(Verb) $(Link) $(SharedLinkOptions) -o $@ $(ObjectsO) \
 	  $(ProjLibsOptions) $(LLVMLibsOptions) $(LIBS)
 else
-$(LibName.SO): $(ObjectsO) $(LibDir)/.dir
+$(LibName.SO): $(ObjectsO) $(SharedDir)/.dir
 	$(Echo) Linking $(BuildMode) Shared Library $(notdir $@)
 	$(Verb) $(Link) $(SharedLinkOptions) -o $@ $(ObjectsO)
 endif
@@ -1148,21 +1193,23 @@ uninstall-local::
 	$(Echo) Uninstall circumvented with NO_INSTALL
 else
 
-ifdef LOADABLE_MODULE
-DestSharedLib = $(DESTDIR)$(PROJ_libdir)/$(LIBRARYNAME)$(SHLIBEXT)
+# Win32.DLL prefers to be located on the "PATH" of binaries.
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+DestSharedDir := $(DESTDIR)$(PROJ_bindir)
 else
-DestSharedLib = $(DESTDIR)$(PROJ_libdir)/lib$(LIBRARYNAME)$(SHLIBEXT)
+DestSharedDir := $(DESTDIR)$(PROJ_libdir)
 endif
+DestSharedLib := $(DestSharedDir)/$(SharedPrefix)$(LIBRARYNAME)$(SHLIBEXT)
 
 install-local:: $(DestSharedLib)
 
-$(DestSharedLib): $(LibName.SO) $(DESTDIR)$(PROJ_libdir)
+$(DestSharedLib): $(LibName.SO) $(DestSharedDir)
 	$(Echo) Installing $(BuildMode) Shared Library $(DestSharedLib)
 	$(Verb) $(INSTALL) $(LibName.SO) $(DestSharedLib)
 
 uninstall-local::
 	$(Echo) Uninstalling $(BuildMode) Shared Library $(DestSharedLib)
-	-$(Verb) $(RM) -f $(DESTDIR)$(PROJ_libdir)/lib$(LIBRARYNAME).*
+	-$(Verb) $(RM) -f $(DestSharedDir)/$(SharedPrefix)$(LIBRARYNAME).*
 endif
 endif
 
diff --git a/tools/llvm-shlib/Makefile b/tools/llvm-shlib/Makefile
index 899406c..5238130 100644
--- a/tools/llvm-shlib/Makefile
+++ b/tools/llvm-shlib/Makefile
@@ -15,6 +15,17 @@ NO_BUILD_ARCHIVE = 1
 LINK_LIBS_IN_SHARED = 1
 SHARED_LIBRARY = 1
 
+include $(LEVEL)/Makefile.config
+
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+    EXPORTED_SYMBOL_FILE = $(ObjDir)/$(LIBRARYNAME).exports
+
+    # It is needed to force static-stdc++.a linked.
+    # FIXME: It should be omitted when configure detects system's stdc++.dll.
+    SHLIB_FRAG_NAMES += stdc++.a.o
+
+endif
+
 include $(LEVEL)/Makefile.common
 
 # Include all archives in libLLVM.(so|dylib) except the ones that have
@@ -57,3 +68,44 @@ ifeq ($(HOST_OS), Linux)
     # Don't allow unresolved symbols.
     LLVMLibsOptions += -Wl,--no-undefined
 endif
+
+ifeq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
+
+SHLIB_STUBS := $(addprefix $(ObjDir)/, $(SHLIB_FRAG_NAMES))
+SHLIB_FRAGS := $(patsubst %.a.o, $(ObjDir)/%.syms.txt, $(LIBRARYNAME).a.o $(SHLIB_FRAG_NAMES))
+LLVMLibsOptions := $(SHLIB_STUBS) $(LLVMLibsOptions)
+
+$(LibName.SO): $(SHLIB_STUBS)
+
+%.syms.txt: %.a.o
+	$(Echo) Collecting global symbols of $(notdir $*)
+	$(Verb) $(NM_PATH) -g $< > $@
+
+$(ObjDir)/$(LIBRARYNAME).exports: $(SHLIB_FRAGS) $(ObjDir)/.dir
+	$(Echo) Generating exports for $(LIBRARYNAME)
+	$(Verb) ($(SED) -n \
+			-e "s/^.* T _\([^.][^.]*\)$$/\1/p" \
+			-e "s/^.* [BDR] _\([^.][^.]*\)$$/\1 DATA/p" \
+			$(SHLIB_FRAGS) \
+		 | sort -u) > $@
+
+$(ObjDir)/$(LIBRARYNAME).a.o: $(LLVMLibsPaths) $(ObjDir)/.dir
+	$(Echo) Linking all LLVMLibs together for $(LIBRARYNAME)
+	$(Verb) $(Link) -nostartfiles -Wl,-r -nodefaultlibs -o $@ \
+			-Wl,--whole-archive $(LLVMLibsPaths) \
+			-Wl,--no-whole-archive
+
+$(ObjDir)/stdc++.a.o: $(ObjDir)/.dir
+	$(Echo) Linking all libs together for static libstdc++.a
+	$(Verb) $(Link) -nostartfiles -Wl,-r -nodefaultlibs -o $@ \
+			-Wl,--whole-archive -lstdc++ \
+			-Wl,--no-whole-archive
+# FIXME: workaround to invalidate -lstdc++
+	$(Echo) Making dummy -lstdc++ to lib
+	$(Verb) $(AR) rc $(ToolDir)/libstdc++.dll.a
+# FIXME: Is install-local needed?
+
+clean-local::
+	$(Verb) $(RM) -f $(ToolDir)/libstdc++.dll.a
+
+endif


More information about the llvm-dev mailing list