diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt --- a/clang/CMakeLists.txt +++ b/clang/CMakeLists.txt @@ -190,9 +190,16 @@ set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}") endif() +if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS) + set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) +endif() + # Make sure that our source directory is on the current cmake module path so that # we can include cmake files from this directory. -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") +list(INSERT CMAKE_MODULE_PATH 0 + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" + "${LLVM_COMMON_CMAKE_UTILS}/Modules" + ) if(LLVM_ENABLE_LIBXML2) # Don't look for libxml if we're using MSan, since uninstrumented third party diff --git a/clang/cmake/modules/CMakeLists.txt b/clang/cmake/modules/CMakeLists.txt --- a/clang/cmake/modules/CMakeLists.txt +++ b/clang/cmake/modules/CMakeLists.txt @@ -1,4 +1,5 @@ include(LLVMDistributionSupport) +include(FindPrefixFromConfig) # Generate a list of CMake library targets so that other CMake projects can # link against them. LLVM calls its version of this file LLVMExports.cmake, but @@ -29,16 +30,7 @@ set(CLANG_CONFIG_LLVM_CMAKE_DIR) # Generate ClangConfig.cmake for the install tree. -set(CLANG_CONFIG_CODE " -# Compute the installation prefix from this LLVMConfig.cmake file location. -get_filename_component(CLANG_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)") -# Construct the proper number of get_filename_component(... PATH) -# calls to compute the installation prefix. -string(REGEX REPLACE "/" ";" _count "${CLANG_INSTALL_PACKAGE_DIR}") -foreach(p ${_count}) - set(CLANG_CONFIG_CODE "${CLANG_CONFIG_CODE} -get_filename_component(CLANG_INSTALL_PREFIX \"\${CLANG_INSTALL_PREFIX}\" PATH)") -endforeach(p) +find_prefix_from_config(CLANG_CONFIG_CODE CLANG_INSTALL_PREFIX "${CLANG_INSTALL_PACKAGE_DIR}") set(CLANG_CONFIG_CMAKE_DIR "\${CLANG_INSTALL_PREFIX}/${CLANG_INSTALL_PACKAGE_DIR}") set(CLANG_CONFIG_LLVM_CMAKE_DIR "\${CLANG_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}") get_config_exports_includes(Clang CLANG_CONFIG_INCLUDE_EXPORTS) diff --git a/cmake/Modules/FindPrefixFromConfig.cmake b/cmake/Modules/FindPrefixFromConfig.cmake new file mode 100644 --- /dev/null +++ b/cmake/Modules/FindPrefixFromConfig.cmake @@ -0,0 +1,41 @@ +# Find the prefix from the `*Config.cmake` file being generated. +# +# When generating an installed `*Config.cmake` file, we often want to be able +# to refer to the ancestor directory which contains all the installed files. +# +# We want to do this without baking in an absolute path when the config file is +# generated, in order to allow for a "relocatable" binary distribution that +# doesn't need to know what path it ends up being installed at when it is +# built. +# +# The solution that we know the relative path that the config file will be at +# within that prefix, like `"${prefix_var}/lib/cmake/${project}"`, so we count +# the number of components in that path to figure out how many parent dirs we +# need to traverse from the location of the config file to get to the prefix +# dir. +# +# out_var: +# variable to set the "return value" of the function, which is the code to +# include in the config file under construction. +# +# prefix_var: +# Name of the variable to define in the returned code (not directory for the +# faller!) that will contain the prefix path. +# +# path_to_leave: +# Path from the prefix to the config file, a relative path which we wish to +# go up and out from to find the prefix directory. +function(find_prefix_from_config out_var prefix_var path_to_leave) + set(config_code + "# Compute the installation prefix from this LLVMConfig.cmake file location." + "get_filename_component(${prefix_var} \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)") + # Construct the proper number of get_filename_component(... PATH) + # calls to compute the installation prefix. + string(REGEX REPLACE "/" ";" _count "${path_to_leave}") + foreach(p ${_count}) + list(APPEND config_code + "get_filename_component(${prefix_var} \"\${${prefix_var}}\" PATH)") + endforeach(p) + string(REPLACE ";" "\n" config_code "${config_code}") + set("${out_var}" "${config_code}" PARENT_SCOPE) +endfunction() diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -196,11 +196,13 @@ set(LLVM_GISEL_COV_PREFIX "${CMAKE_BINARY_DIR}/gisel-coverage-" CACHE STRING "Provide a filename prefix to collect the GlobalISel rule coverage") endif() +set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) + # Add path for custom modules -set(CMAKE_MODULE_PATH - ${CMAKE_MODULE_PATH} +list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" + "${LLVM_COMMON_CMAKE_UTILS}/Modules" ) # Generate a CompilationDatabase (compile_commands.json file) for our build, @@ -308,7 +310,6 @@ set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} ) # --prefix set(LLVM_THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../third-party) -set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) # Note: LLVM_CMAKE_DIR does not include generated files set(LLVM_CMAKE_DIR ${LLVM_MAIN_SRC_DIR}/cmake/modules) diff --git a/llvm/cmake/modules/CMakeLists.txt b/llvm/cmake/modules/CMakeLists.txt --- a/llvm/cmake/modules/CMakeLists.txt +++ b/llvm/cmake/modules/CMakeLists.txt @@ -1,4 +1,5 @@ include(LLVMDistributionSupport) +include(FindPrefixFromConfig) set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") @@ -99,16 +100,7 @@ ) # Generate LLVMConfig.cmake for the install tree. -set(LLVM_CONFIG_CODE " -# Compute the installation prefix from this LLVMConfig.cmake file location. -get_filename_component(LLVM_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)") -# Construct the proper number of get_filename_component(... PATH) -# calls to compute the installation prefix. -string(REGEX REPLACE "/" ";" _count "${LLVM_INSTALL_PACKAGE_DIR}") -foreach(p ${_count}) - set(LLVM_CONFIG_CODE "${LLVM_CONFIG_CODE} -get_filename_component(LLVM_INSTALL_PREFIX \"\${LLVM_INSTALL_PREFIX}\" PATH)") -endforeach(p) +find_prefix_from_config(LLVM_CONFIG_CODE LLVM_INSTALL_PREFIX "${LLVM_INSTALL_PACKAGE_DIR}") set(LLVM_CONFIG_INCLUDE_DIRS "\${LLVM_INSTALL_PREFIX}/include") set(LLVM_CONFIG_INCLUDE_DIR "${LLVM_CONFIG_INCLUDE_DIRS}") set(LLVM_CONFIG_MAIN_INCLUDE_DIR "${LLVM_CONFIG_INCLUDE_DIRS}")