[cmake] rework LLVM_LINK_LLVM_DYLIB option handling
authorAndrew Wilkins <axwalk@gmail.com>
Sat, 5 Sep 2015 08:27:33 +0000 (08:27 +0000)
committerAndrew Wilkins <axwalk@gmail.com>
Sat, 5 Sep 2015 08:27:33 +0000 (08:27 +0000)
Summary:
This diff attempts to address the concerns raised in
http://reviews.llvm.org/D12488.

We introduce a new USE_SHARED option to llvm_config,
which, if set, causes the target to be linked against
libLLVM.

add_llvm_utility now uniformly disables linking against
libLLVM. These utilities are not intended for distribution,
and this keeps the option handling more centralised.

llvm-shlib is now processes before any other "tools"
subdirectories, ensuring the libLLVM target is defined
before its dependents.

One main difference from what was requested: llvm_config
does not prune LLVM_DYLIB_COMPONENTS from the components
passed into explicit_llvm_config. This is because the "all"
component does something special, adding additional
libraries (namely libLTO). Adding the component libraries
after libLLVM should not be a problem, as symbols will be
resolved in libLLVM first.

Finally, I'm not really happy with the
DISABLE_LLVM_LINK_LLVM option, but I'm not sure of a
better way to get the following:
 - link all tools and shared libraries to libLLVM if
   LLVM_LINK_LLVM_DYLIB is set
 - some way of explicitly *not* doing so for utilities
   and libLLVM itself
Suggestions for improvement here are particularly welcome.

Reviewers: beanz

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D12590

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

CMakeLists.txt
cmake/modules/AddLLVM.cmake
cmake/modules/LLVM-Config.cmake
cmake/modules/TableGen.cmake
tools/llvm-shlib/CMakeLists.txt

index 552ea5553b6fef5c9592d11b5118852d0e75b5d7..3518a68ac52d90b12b92af0101d772a3e1d83f8b 100644 (file)
@@ -347,6 +347,13 @@ option (LLVM_ENABLE_SPHINX "Use Sphinx to generate llvm documentation." OFF)
 option (LLVM_BUILD_EXTERNAL_COMPILER_RT
   "Build compiler-rt as an external project." OFF)
 
+# You can configure which libraries from LLVM you want to include in the
+# shared library by setting LLVM_DYLIB_COMPONENTS to a semi-colon delimited
+# list of LLVM components. All component names handled by llvm-config are valid.
+if(NOT DEFINED LLVM_DYLIB_COMPONENTS)
+  set(LLVM_DYLIB_COMPONENTS "all" CACHE STRING
+    "Semicolon-separated list of components to include in libLLVM, or \"all\".")
+endif()
 option(LLVM_LINK_LLVM_DYLIB "Link tools against the libllvm dynamic library" OFF)
 option(LLVM_BUILD_LLVM_DYLIB "Build libllvm dynamic library" ${LLVM_LINK_LLVM_DYLIB})
 option(LLVM_DYLIB_EXPORT_ALL "Export all symbols from libLLVM.dylib (default is C API only" ${LLVM_LINK_LLVM_DYLIB})
index f53b89d32abb249c6ce49b08d7452bf29a813e89..0f3b660b95f81fbd6ed402e63011ce2083f06223 100644 (file)
@@ -610,13 +610,13 @@ macro(add_llvm_executable name)
     add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
   endif(LLVM_EXPORTED_SYMBOL_FILE)
 
-  set(EXCLUDE_FROM_ALL OFF)
-  set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
   if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
-    target_link_libraries(${name} LLVM)
-  else()
-    llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
+    set(USE_SHARED USE_SHARED)
   endif()
+
+  set(EXCLUDE_FROM_ALL OFF)
+  set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
+  llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} )
   if( LLVM_COMMON_DEPENDS )
     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
   endif( LLVM_COMMON_DEPENDS )
@@ -677,7 +677,7 @@ endmacro(add_llvm_example name)
 
 
 macro(add_llvm_utility name)
-  add_llvm_executable(${name} ${ARGN})
+  add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
   set_target_properties(${name} PROPERTIES FOLDER "Utils")
   if( LLVM_INSTALL_UTILS )
     install (TARGETS ${name}
index 22ac71451917a21d0703b4b828ba7a5900cf216d..aa68b40076025712018c4d57e8eaecee68e7119a 100644 (file)
@@ -31,7 +31,23 @@ endfunction(is_llvm_target_library)
 
 
 macro(llvm_config executable)
-  explicit_llvm_config(${executable} ${ARGN})
+  cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
+  set(link_components ${ARG_UNPARSED_ARGUMENTS})
+
+  if(USE_SHARED)
+    # If USE_SHARED is specified, then we link against libLLVM,
+    # but also against the component libraries below. This is
+    # done in case libLLVM does not contain all of the components
+    # the target requires.
+    #
+    # TODO strip LLVM_DYLIB_COMPONENTS out of link_components.
+    # To do this, we need special handling for "all", since that
+    # may imply linking to libraries that are not included in
+    # libLLVM.
+    target_link_libraries(${executable} LLVM)
+  endif()
+
+  explicit_llvm_config(${executable} ${link_components})
 endmacro(llvm_config)
 
 
index 98646bc16df44c8ed5100383e655829782bbcf70..452a728db4feb9c5b4da5006a47fbdd1c73b69d6 100644 (file)
@@ -79,13 +79,7 @@ macro(add_tablegen target project)
     set(LLVM_ENABLE_OBJLIB ON)
   endif()
 
-  add_llvm_utility(
-    ${target} ${ARGN}
-    # libLLVM does not include the TableGen
-    # components, so we cannot link any tblgen
-    # utilities against it.
-    DISABLE_LLVM_LINK_LLVM_DYLIB)
-
+  add_llvm_utility(${target} ${ARGN})
   set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
 
   set(${project}_TABLEGEN "${target}" CACHE
index 7415722d3f6bf32418af34719f94167cc683885f..d9bd15f76248169b56d802781c927f2ff414884f 100644 (file)
@@ -2,53 +2,6 @@
 # library is enabled by setting LLVM_BUILD_LLVM_DYLIB=yes on the CMake
 # commandline. By default the shared library only exports the LLVM C API.
 
-if(LLVM_LINK_LLVM_DYLIB)
-  if(DEFINED LLVM_DYLIB_COMPONENTS)
-    # To avoid inscrutable link errors, just disallow setting
-    # LLVM_DYLIB_COMPONENTS when we're intending to link tools
-    # and shared libraries with the dylib.
-    message(FATAL_ERROR "LLVM_DYLIB_COMPONENTS must not be set when LLVM_LINK_LLVM_DYLIB is ON")
-  endif()
-  if(NOT LLVM_DYLIB_EXPORT_ALL)
-    message(FATAL_ERROR "LLVM_DYLIB_EXPORT_ALL must be ON when LLVM_LINK_LLVM_DYLIB is ON")
-  endif()
-  set(LLVM_DYLIB_COMPONENTS all)
-endif()
-
-# If LLVM_LINK_LLVM_DYLIB is not OFF, you can configure which libraries from
-# LLVM you want to include in the shared library by setting
-# LLVM_DYLIB_COMPONENTS to a semi-colon delimited list of LLVM components.
-# All component names handled by llvm-config are valid.
-if(NOT DEFINED LLVM_DYLIB_COMPONENTS)
-  set(LLVM_DYLIB_COMPONENTS
-    ${LLVM_TARGETS_TO_BUILD}
-    Analysis
-    BitReader
-    BitWriter
-    CodeGen
-    Core
-    DebugInfoDWARF
-    DebugInfoPDB
-    ExecutionEngine
-    IPO
-    IRReader
-    InstCombine
-    Instrumentation
-    Interpreter
-    Linker
-    MCDisassembler
-    MCJIT
-    ObjCARCOpts
-    Object
-    ScalarOpts
-    Support
-    Target
-    TransformUtils
-    Vectorize
-    native
-    )
-endif()
-
 add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
 
 set(SOURCES
@@ -58,6 +11,10 @@ set(SOURCES
 llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
 
 if(LLVM_LINK_LLVM_DYLIB)
+  if(NOT LLVM_DYLIB_EXPORT_ALL)
+    message(FATAL_ERROR "LLVM_DYLIB_EXPORT_ALL must be ON when LLVM_LINK_LLVM_DYLIB is ON")
+  endif()
+
   # libLLVM.so should not have any dependencies on any other LLVM
   # shared libraries. When using the "all" pseudo-component,
   # LLVM_AVAILABLE_LIBS is added to the dependencies, which may