[cmake] Export gtest/gtest_main and its dependencies via a special build tree only cmake exports file.

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
This commit is contained in:
Michael Gottesman 2016-09-09 19:45:34 +00:00
parent 82621dcb10
commit 31e9363a3e
5 changed files with 42 additions and 14 deletions

View 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)

View 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

View 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)

View 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)

View 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
)