diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt index aa21d05882430c..7ea37850ad609e 100644 --- a/clang/CMakeLists.txt +++ b/clang/CMakeLists.txt @@ -192,9 +192,16 @@ else() set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}") endif() # standalone +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 index 561665d58cad92..3890ea14d06c6f 100644 --- 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_CMAKE_DIR) 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 index 00000000000000..aa9fb0d03413d4 --- /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/flang/cmake/modules/CMakeLists.txt b/flang/cmake/modules/CMakeLists.txt index 06466c06667474..170568c80dded0 100644 --- a/flang/cmake/modules/CMakeLists.txt +++ b/flang/cmake/modules/CMakeLists.txt @@ -1,4 +1,5 @@ include(ExtendPath) +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(FLANG_CONFIG_CMAKE_DIR) set(FLANG_CONFIG_LLVM_CMAKE_DIR) # Generate FlangConfig.cmake for the install tree. -set(FLANG_CONFIG_CODE " - # Compute the installation prefix from this LLVMConfig.cmake file location. - get_filename_component(FLANG_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 "${FLANG_INSTALL_PACKAGE_DIR}") -foreach(p ${_count}) - set(FLANG_CONFIG_CODE "${FLANG_CONFIG_CODE} - get_filename_component(FLANG_INSTALL_PREFIX \"\${FLANG_INSTALL_PREFIX}\" PATH)") -endforeach(p) +find_prefix_from_config(FLANG_CONFIG_CODE FLANG_INSTALL_PREFIX "${FLANG_INSTALL_PACKAGE_DIR}") set(FLANG_CONFIG_CMAKE_DIR "\${FLANG_INSTALL_PREFIX}/${FLANG_INSTALL_PACKAGE_DIR}") set(FLANG_CONFIG_LLVM_CMAKE_DIR "\${FLANG_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}") diff --git a/lld/cmake/modules/CMakeLists.txt b/lld/cmake/modules/CMakeLists.txt index 022824ede631d9..760c9d5f8d87da 100644 --- a/lld/cmake/modules/CMakeLists.txt +++ b/lld/cmake/modules/CMakeLists.txt @@ -1,4 +1,5 @@ include(ExtendPath) +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(LLD_CONFIG_CMAKE_DIR) set(LLD_CONFIG_LLVM_CMAKE_DIR) # Generate LLDConfig.cmake for the install tree. -set(LLD_CONFIG_CODE " -# Compute the installation prefix from this LLVMConfig.cmake file location. -get_filename_component(LLD_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 "${LLD_INSTALL_PACKAGE_DIR}") -foreach(p ${_count}) - set(LLD_CONFIG_CODE "${LLD_CONFIG_CODE} -get_filename_component(LLD_INSTALL_PREFIX \"\${LLD_INSTALL_PREFIX}\" PATH)") -endforeach(p) +find_prefix_from_config(LLD_CONFIG_CODE LLD_INSTALL_PREFIX "${LLD_INSTALL_PACKAGE_DIR}") set(LLD_CONFIG_CMAKE_DIR "\${LLD_INSTALL_PREFIX}/${LLD_INSTALL_PACKAGE_DIR}") set(LLD_CONFIG_LLVM_CMAKE_DIR "\${LLD_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}") get_config_exports_includes(LLD LLD_CONFIG_INCLUDE_EXPORTS) diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index 9548dfff0e2a72..4ddc3298e9df0f 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -200,11 +200,13 @@ if(LLVM_ENABLE_GISEL_COV) 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, @@ -312,7 +314,6 @@ set(LLVM_MAIN_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/include ) # --includedir 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 index 51b6a4fdc2843d..81088241a47ee3 100644 --- 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 @@ file(COPY . ) # 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}") diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt index 28018350cb43da..b1b79cf325428e 100644 --- a/mlir/CMakeLists.txt +++ b/mlir/CMakeLists.txt @@ -33,7 +33,16 @@ set(MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include) set(MLIR_TOOLS_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") +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(INSERT CMAKE_MODULE_PATH 0 + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" + "${LLVM_COMMON_CMAKE_UTILS}/Modules" + ) include(AddMLIR) diff --git a/mlir/cmake/modules/CMakeLists.txt b/mlir/cmake/modules/CMakeLists.txt index 65efc4a1498743..6bed3f62c93767 100644 --- a/mlir/cmake/modules/CMakeLists.txt +++ b/mlir/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 @@ -44,16 +45,7 @@ file(COPY . ) # Generate MLIRConfig.cmake for the install tree. -set(MLIR_CONFIG_CODE " -# Compute the installation prefix from this MLIRConfig.cmake file location. -get_filename_component(MLIR_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 "${MLIR_INSTALL_PACKAGE_DIR}") -foreach(p ${_count}) - set(MLIR_CONFIG_CODE "${MLIR_CONFIG_CODE} -get_filename_component(MLIR_INSTALL_PREFIX \"\${MLIR_INSTALL_PREFIX}\" PATH)") -endforeach(p) +find_prefix_from_config(MLIR_CONFIG_CODE MLIR_INSTALL_PREFIX "${MLIR_INSTALL_PACKAGE_DIR}") set(MLIR_CONFIG_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}/${MLIR_INSTALL_PACKAGE_DIR}") set(MLIR_CONFIG_LLVM_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}") get_config_exports_includes(MLIR MLIR_CONFIG_INCLUDE_EXPORTS)