[llvm] r221591 - [CMake] Let llvm-shlib work on Linux with --whole-archive.

NAKAMURA Takumi geek4civic at gmail.com
Mon Nov 10 07:04:03 PST 2014


Author: chapuni
Date: Mon Nov 10 09:04:02 2014
New Revision: 221591

URL: http://llvm.org/viewvc/llvm-project?rev=221591&view=rev
Log:
[CMake] Let llvm-shlib work on Linux with --whole-archive.

FIXME: It should work on not only Linux but elf-targeting gnu ld.

For example if LLVM_DYLIB_COMPONENTS is "BitWriter Support", CMake emits the command line like;

  -Wl,--whole-archive
    lib/libLLVMBitWriter.a
    lib/libLLVMSupport.a *1
  -Wl,--no-whole-archive
  lib/libLLVMCore.a
  lib/libLLVMSupport.a   *2
  -lrt -ldl -ltinfo -lpthread -lm

It works since symbols in LLVMCore is resolved with not *2 but *1.

Unfortunately, --gc-sections is not powerful in this case to prune unused "visibility(default)" entries.

I am still experimenting other way not to rely on --whole-archive.

Modified:
    llvm/trunk/tools/llvm-shlib/CMakeLists.txt

Modified: llvm/trunk/tools/llvm-shlib/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-shlib/CMakeLists.txt?rev=221591&r1=221590&r2=221591&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-shlib/CMakeLists.txt (original)
+++ llvm/trunk/tools/llvm-shlib/CMakeLists.txt Mon Nov 10 09:04:02 2014
@@ -8,7 +8,7 @@
 # LLVM components. All compoenent names handled by llvm-config are valid.
 
 if(NOT DEFINED LLVM_DYLIB_COMPONENTS)
-  set(LLVM_LINK_COMPONENTS
+  set(LLVM_DYLIB_COMPONENTS
     ${LLVM_TARGETS_TO_BUILD}
     Analysis
     AsmPrinter
@@ -37,8 +37,6 @@ if(NOT DEFINED LLVM_DYLIB_COMPONENTS)
     Vectorize
     native
     )
-else()
-  set(LLVM_LINK_COMPONENTS ${LLVM_DYLIB_COMPONENTS})
 endif()
 
 add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
@@ -58,7 +56,7 @@ if(NOT DEFINED LLVM_EXPORTED_SYMBOL_FILE
 
   set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm.exports)
 
-  llvm_map_components_to_libnames(LIB_NAMES ${LLVM_LINK_COMPONENTS})
+  llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
 
   foreach (lib ${LIB_NAMES})
     
@@ -87,6 +85,14 @@ endif()
 
 add_llvm_library(LLVM SHARED ${SOURCES})
 
+if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf"
+  # GNU ld doesn't resolve symbols in the version script.
+  list(REMOVE_DUPLICATES LIB_NAMES)
+  set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
+endif()
+
+target_link_libraries(LLVM ${cmake_2_8_12_PRIVATE} ${LIB_NAMES})
+
 add_dependencies(LLVM ${LLVM_EXPORTED_SYMBOL_FILE})
 
 if (APPLE)





More information about the llvm-commits mailing list