diff --git a/cmake/modules/AddLLVM.cmake b/cmake/modules/AddLLVM.cmake index 221d9bd9452..05b51d96d76 100644 --- a/cmake/modules/AddLLVM.cmake +++ b/cmake/modules/AddLLVM.cmake @@ -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) diff --git a/cmake/modules/CMakeLists.txt b/cmake/modules/CMakeLists.txt index d2510b853ad..deeaaa8be8b 100644 --- a/cmake/modules/CMakeLists.txt +++ b/cmake/modules/CMakeLists.txt @@ -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 diff --git a/cmake/modules/LLVMConfig.cmake.in b/cmake/modules/LLVMConfig.cmake.in index 5dc9a9ef582..f7412bac108 100644 --- a/cmake/modules/LLVMConfig.cmake.in +++ b/cmake/modules/LLVMConfig.cmake.in @@ -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) diff --git a/utils/unittest/CMakeLists.txt b/utils/unittest/CMakeLists.txt index c9a2cdd45c8..933399e888a 100644 --- a/utils/unittest/CMakeLists.txt +++ b/utils/unittest/CMakeLists.txt @@ -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) diff --git a/utils/unittest/UnitTestMain/CMakeLists.txt b/utils/unittest/UnitTestMain/CMakeLists.txt index 520db4e8d2b..32f0f25a60f 100644 --- a/utils/unittest/UnitTestMain/CMakeLists.txt +++ b/utils/unittest/UnitTestMain/CMakeLists.txt @@ -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 )