[cmake] Export gtest/gtest_main and its dependencies via a special build tree only...
authorMichael Gottesman <mgottesman@apple.com>
Fri, 9 Sep 2016 19:45:34 +0000 (19:45 +0000)
committerMichael Gottesman <mgottesman@apple.com>
Fri, 9 Sep 2016 19:45:34 +0000 (19:45 +0000)
Previously, gtest/gtest_main were not exported via cmake. The intention here was
to ensure that users whom are linking against the LLVM install tree would not
get the gtest/gtest_main targets. This prevents downstream projects that link
against the LLVM build tree (i.e. Swift) from getting this dependency
information in their cmake builds. Without such dependency information, linker
issues can result on linux due to LLVMSupport being put before gtest on the
linker command line.

This commit preserves behavior that we want for the install tree, while adding
support for the build tree by:

1. The special casing for gtest/gtest_main in the add_llvm_library code is
removed in favor of a flag called "BUILDTREE_ONLY". If this is set, then the
library is communicating that it is only meant to be exported into the build
tree and is not meant to be installed or exported via the install tree. This
part is just a tweak to remove the special case, the underlying code is the
same.

2. The cmake code that exports cmake targets for the build tree has special code
to import an additional targets file called
LLVMBuildTreeOnlyExports.cmake. Additionally the extra targets are added to the
LLVMConfig.cmake's LLVM_EXPORTED_TARGETS variable. In contrast, the
"installation" cmake file uses the normal LLVM_EXPORTS_TARGETS as before and
does not include the extra exports file. This is implemented by
defining/undefining variables when performing a configure of the build/install
tree LLVMConfig.cmake files.

llvm-svn: 281085

llvm/cmake/modules/AddLLVM.cmake
llvm/cmake/modules/CMakeLists.txt
llvm/cmake/modules/LLVMConfig.cmake.in
llvm/utils/unittest/CMakeLists.txt
llvm/utils/unittest/UnitTestMain/CMakeLists.txt

index 221d9bd94526bc34f19c874956458ed1bfef9b10..05b51d96d76fa4818e0f200855f214eae5f7ed49 100644 (file)
@@ -533,26 +533,28 @@ endfunction()
 
 macro(add_llvm_library name)
   cmake_parse_arguments(ARG
-    "SHARED"
+    "SHARED;BUILDTREE_ONLY"
     ""
     ""
     ${ARGN})
-  if( BUILD_SHARED_LIBS )
-    llvm_add_library(${name} SHARED ${ARGN})
+  if( BUILD_SHARED_LIBS OR ARG_SHARED )
+    llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS})
   else()
-    llvm_add_library(${name} ${ARGN})
+    llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
   endif()
-  # The gtest libraries should not be installed or exported as a target
-  if ("${name}" STREQUAL gtest OR "${name}" STREQUAL gtest_main)
-    set(_is_gtest TRUE)
-  else()
-    set(_is_gtest FALSE)
+
+  # Libraries that are meant to only be exposed via the build tree only are
+  # never installed and are only exported as a target in the special build tree
+  # config file.
+  if (NOT ARG_BUILDTREE_ONLY)
     set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
   endif()
 
   if( EXCLUDE_FROM_ALL )
     set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
-  elseif(NOT _is_gtest)
+  elseif(ARG_BUILDTREE_ONLY)
+    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name})
+  else()
     if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
       set(install_dir lib${LLVM_LIBDIR_SUFFIX})
       if(ARG_SHARED OR BUILD_SHARED_LIBS)
index d2510b853ad68821c3eb3b03788ff1c1ba76f633..deeaaa8be8b84855e0bebdc0f859db933abf74c5 100644 (file)
@@ -1,9 +1,17 @@
 set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm)
 set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}")
 
+# First for users who use an installed LLVM, create the
+# LLVMInstallationExports.cmake
+set(LLVM_EXPORTS_FILE ${llvm_cmake_builddir}/LLVMExports.cmake)
 get_property(LLVM_EXPORTS GLOBAL PROPERTY LLVM_EXPORTS)
-export(TARGETS ${LLVM_EXPORTS}
-  FILE ${llvm_cmake_builddir}/LLVMExports.cmake)
+export(TARGETS ${LLVM_EXPORTS} FILE ${LLVM_EXPORTS_FILE})
+
+# Then for users who want to link against the LLVM build tree, provide the
+# normal targets and the build tree only targets.
+set(LLVM_BUILDTREEONLY_EXPORTS_FILE ${llvm_cmake_builddir}/LLVMBuildTreeOnlyTargets.cmake)
+get_property(LLVM_EXPORTS_BUILDTREE_ONLY GLOBAL PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY)
+export(TARGETS ${LLVM_EXPORTS_BUILDTREE_ONLY} FILE ${LLVM_BUILDTREEONLY_EXPORTS_FILE})
 
 get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
 
@@ -31,11 +39,21 @@ set(LLVM_CONFIG_LIBRARY_DIRS
 set(LLVM_CONFIG_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
 set(LLVM_CONFIG_BINARY_DIR "${LLVM_BINARY_DIR}")
 set(LLVM_CONFIG_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}")
-set(LLVM_CONFIG_EXPORTS_FILE "${llvm_cmake_builddir}/LLVMExports.cmake")
+# We need to use the full path to the LLVM Exports file to make sure we get the
+# one from the build tree. This is due to our cmake files being split between
+# this source dir and the binary dir in the build tree configuration and the
+# LLVM_CONFIG_CMAKE_DIR being the source directory. In contrast in the install
+# tree, both the generated LLVMExports.cmake file and the rest of the cmake
+# source files are put in the same cmake directory.
+set(LLVM_CONFIG_EXPORTS_FILE "${LLVM_EXPORTS_FILE}")
+set(LLVM_CONFIG_EXPORTS "${LLVM_EXPORTS};${LLVM_EXPORTS_BUILDTREE_ONLY}")
+set(llvm_config_include_buildtree_only_exports
+"include(\"${LLVM_BUILDTREEONLY_EXPORTS_FILE}\")")
 configure_file(
   LLVMConfig.cmake.in
   ${llvm_cmake_builddir}/LLVMConfig.cmake
   @ONLY)
+set(llvm_config_include_buildtree_only_exports)
 
 # For compatibility with projects that include(LLVMConfig)
 # via CMAKE_MODULE_PATH, place API modules next to it.
@@ -64,6 +82,7 @@ set(LLVM_CONFIG_CMAKE_DIR "\${LLVM_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}")
 set(LLVM_CONFIG_BINARY_DIR "\${LLVM_INSTALL_PREFIX}")
 set(LLVM_CONFIG_TOOLS_BINARY_DIR "\${LLVM_INSTALL_PREFIX}/bin")
 set(LLVM_CONFIG_EXPORTS_FILE "\${LLVM_CMAKE_DIR}/LLVMExports.cmake")
+set(LLVM_CONFIG_EXPORTS "${LLVM_EXPORTS}")
 configure_file(
   LLVMConfig.cmake.in
   ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/LLVMConfig.cmake
index 5dc9a9ef582427f98df9b7d8c3975592f7db2e16..f7412bac1085482d27861e432a626f5ab1f48f43 100644 (file)
@@ -68,8 +68,9 @@ set(LLVM_TOOLS_BINARY_DIR "@LLVM_CONFIG_TOOLS_BINARY_DIR@")
 set(LLVM_TOOLS_INSTALL_DIR "@LLVM_TOOLS_INSTALL_DIR@")
 
 if(NOT TARGET LLVMSupport)
-  set(LLVM_EXPORTED_TARGETS "@LLVM_EXPORTS@")
+  set(LLVM_EXPORTED_TARGETS "@LLVM_CONFIG_EXPORTS@")
   include("@LLVM_CONFIG_EXPORTS_FILE@")
+  @llvm_config_include_buildtree_only_exports@
 endif()
 
 include(${LLVM_CMAKE_DIR}/LLVM-Config.cmake)
index c9a2cdd45c8ec18ec58567128005738c047ad5ff..933399e888a201b869f9c84b4a4ed7092bde22f0 100644 (file)
@@ -45,6 +45,9 @@ add_llvm_library(gtest
 
   LINK_COMPONENTS
   Support # Depends on llvm::raw_ostream
+
+  # This is a library meant only for the build tree.
+  BUILDTREE_ONLY
 )
 
 add_subdirectory(UnitTestMain)
index 520db4e8d2b329e071a7b56071642bd42ba414f3..32f0f25a60ffcde4f151a8bf7fedcd8b5ba2b1fb 100644 (file)
@@ -6,4 +6,7 @@ add_llvm_library(gtest_main
 
   LINK_COMPONENTS
   Support # Depends on llvm::cl
+
+  # This library is not meant to be in the install tree, only the build tree.
+  BUILDTREE_ONLY
   )