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,3 +1,4 @@ +include(GNUInstallPackageDir) include(ExtendPath) include(LLVMDistributionSupport) include(FindPrefixFromConfig) @@ -5,12 +6,16 @@ # 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 # the usual CMake convention seems to be ${Project}Targets.cmake. -set(CLANG_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/clang) -set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}") +set(CLANG_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/clang" CACHE STRING + "Path for CMake subdirectory for Clang (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/clang')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/clang") # Keep this in sync with llvm/cmake/CMakeLists.txt! -set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) -set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") +set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING + "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS) export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake) @@ -40,8 +45,8 @@ # Generate ClangConfig.cmake for the install tree. 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}") +extend_path(CLANG_CONFIG_CMAKE_DIR "\${CLANG_INSTALL_PREFIX}" "${CLANG_INSTALL_PACKAGE_DIR}") +extend_path(CLANG_CONFIG_LLVM_CMAKE_DIR "\${CLANG_INSTALL_PREFIX}" "${LLVM_INSTALL_PACKAGE_DIR}") get_config_exports_includes(Clang CLANG_CONFIG_INCLUDE_EXPORTS) extend_path(base_includedir "\${CLANG_INSTALL_PREFIX}" "${CMAKE_INSTALL_INCLUDEDIR}") set(CLANG_CONFIG_INCLUDE_DIRS diff --git a/cmake/Modules/FindPrefixFromConfig.cmake b/cmake/Modules/FindPrefixFromConfig.cmake --- a/cmake/Modules/FindPrefixFromConfig.cmake +++ b/cmake/Modules/FindPrefixFromConfig.cmake @@ -26,16 +26,22 @@ # 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) + if(IS_ABSOLUTE "${path_to_leave}") + set(prefix_var + "# Installation prefix is fixed absolute path" + "set(${prefix_var} \"${CMAKE_INSTALL_PREFIX}\"") + else() + 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) + endif() string(REPLACE ";" "\n" config_code "${config_code}") set("${out_var}" "${config_code}" PARENT_SCOPE) endfunction() diff --git a/cmake/Modules/GNUInstallPackageDir.cmake b/cmake/Modules/GNUInstallPackageDir.cmake new file mode 100644 --- /dev/null +++ b/cmake/Modules/GNUInstallPackageDir.cmake @@ -0,0 +1,32 @@ +# Mimmick GNUInstallDirs for one more install directory, the one where +# project's installed cmake subdirs gos. + +# These functions are internal functions vendored in from GNUInstallDirs (with +# new names) lest it changes. +# +# The purpose would appear to be making `CACHE PATH` vars in a way that +# bypasses the legacy oddity that `-D` gets canonicalized, despite +# non-canonical `CACHE PATH`s being perfectly valid. + +macro(_GNUInstallPackageDir_cache_convert_to_path var description) + get_property(_GNUInstallPackageDir_cache_type CACHE ${var} PROPERTY TYPE) + if(_GNUInstallPackageDir_cache_type STREQUAL "UNINITIALIZED") + file(TO_CMAKE_PATH "${${var}}" _GNUInstallPackageDir_cmakepath) + set_property(CACHE ${var} PROPERTY TYPE PATH) + set_property(CACHE ${var} PROPERTY VALUE "${_GNUInstallPackageDir_cmakepath}") + set_property(CACHE ${var} PROPERTY HELPSTRING "${description}") + unset(_GNUInstallPackageDir_cmakepath) + endif() + unset(_GNUInstallPackageDir_cache_type) +endmacro() + +# Create a cache variable with default for a path. +macro(_GNUInstallPackageDir_cache_path var default description) + if(NOT DEFINED ${var}) + set(${var} "${default}" CACHE PATH "${description}") + endif() + _GNUInstallPackageDir_cache_convert_to_path("${var}" "${description}") +endmacro() + +_GNUInstallPackageDir_cache_path(CMAKE_INSTALL_PACKAGEDIR "lib${LLVM_LIBDIR_SUFFIX}/cmake" + "Directories containing installed CMake modules (lib/cmake)") diff --git a/flang/cmake/modules/CMakeLists.txt b/flang/cmake/modules/CMakeLists.txt --- a/flang/cmake/modules/CMakeLists.txt +++ b/flang/cmake/modules/CMakeLists.txt @@ -1,15 +1,20 @@ +include(GNUInstallPackageDir) 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 # the usual CMake convention seems to be ${Project}Targets.cmake. -set(FLANG_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/flang) -set(flang_cmake_builddir "${CMAKE_BINARY_DIR}/${FLANG_INSTALL_PACKAGE_DIR}") +set(FLANG_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/flang" CACHE STRING + "Path for CMake subdirectory for Flang (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/flang')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(flang_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/flang") # Keep this in sync with llvm/cmake/CMakeLists.txt! -set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) -set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") +set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING + "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(llvm_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") get_property(FLANG_EXPORTS GLOBAL PROPERTY FLANG_EXPORTS) export(TARGETS ${FLANG_EXPORTS} FILE ${flang_cmake_builddir}/FlangTargets.cmake) diff --git a/lld/cmake/modules/CMakeLists.txt b/lld/cmake/modules/CMakeLists.txt --- a/lld/cmake/modules/CMakeLists.txt +++ b/lld/cmake/modules/CMakeLists.txt @@ -1,15 +1,20 @@ +include(GNUInstallPackageDir) 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 # the usual CMake convention seems to be ${Project}Targets.cmake. -set(LLD_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/lld) -set(lld_cmake_builddir "${CMAKE_BINARY_DIR}/${LLD_INSTALL_PACKAGE_DIR}") +set(LLD_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/lld" CACHE STRING + "Path for CMake subdirectory for LLD (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/lld')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(lld_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/lld") # Keep this in sync with llvm/cmake/CMakeLists.txt! -set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) -set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") +set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING + "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(llvm_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") get_property(LLD_EXPORTS GLOBAL PROPERTY LLD_EXPORTS) export(TARGETS ${LLD_EXPORTS} FILE ${lld_cmake_builddir}/LLDTargets.cmake) @@ -31,8 +36,8 @@ # Generate LLDConfig.cmake for the install tree. 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}") +extend_path(LLD_CONFIG_CMAKE_DIR "\${LLD_INSTALL_PREFIX}" "${LLD_INSTALL_PACKAGE_DIR}") +extend_path(LLD_CONFIG_LLVM_CMAKE_DIR "\${LLD_INSTALL_PREFIX}" "${LLVM_INSTALL_PACKAGE_DIR}") get_config_exports_includes(LLD LLD_CONFIG_INCLUDE_EXPORTS) extend_path(LLD_CONFIG_INCLUDE_DIRS "\${LLD_INSTALL_PREFIX}" "${CMAKE_INSTALL_INCLUDEDIR}") configure_file( diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -1049,8 +1049,11 @@ ## Part 1: Extension header to be included whenever we need extension # processing. - set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) - set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") + if(NOT DEFINED LLVM_INSTALL_PACKAGE_DIR) + message(FATAL_ERROR "LLVM_INSTALL_PACKAGE_DIR must be defined and writable. GEN_CONFIG should only be passe when building LLVM proper.") + endif() + # LLVM_INSTALL_PACKAGE_DIR might be absolute, so don't reuse below. + set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") file(WRITE "${llvm_cmake_builddir}/LLVMConfigExtensions.cmake" "set(LLVM_STATIC_EXTENSIONS ${LLVM_STATIC_EXTENSIONS})") 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,9 +1,12 @@ +include(GNUInstallPackageDir) include(ExtendPath) 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}") +set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING + "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(llvm_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") # First for users who use an installed LLVM, create the LLVMExports.cmake file. set(LLVM_EXPORTS_FILE ${llvm_cmake_builddir}/LLVMExports.cmake) diff --git a/mlir/cmake/modules/CMakeLists.txt b/mlir/cmake/modules/CMakeLists.txt --- a/mlir/cmake/modules/CMakeLists.txt +++ b/mlir/cmake/modules/CMakeLists.txt @@ -1,3 +1,4 @@ +include(GNUInstallPackageDir) include(ExtendPath) include(LLVMDistributionSupport) include(FindPrefixFromConfig) @@ -5,12 +6,16 @@ # 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 # the usual CMake convention seems to be ${Project}Targets.cmake. -set(MLIR_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/mlir) -set(mlir_cmake_builddir "${CMAKE_BINARY_DIR}/${MLIR_INSTALL_PACKAGE_DIR}") +set(MLIR_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/mlir" CACHE STRING + "Path for CMake subdirectory for Polly (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/polly')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(mlir_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/mlir") # Keep this in sync with llvm/cmake/CMakeLists.txt! -set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) -set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") +set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING + "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(llvm_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") get_property(MLIR_EXPORTS GLOBAL PROPERTY MLIR_EXPORTS) export(TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir}/MLIRTargets.cmake) @@ -50,8 +55,8 @@ # Generate MLIRConfig.cmake for the install tree. 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}") +extend_path(MLIR_CONFIG_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}" "${MLIR_INSTALL_PACKAGE_DIR}") +extend_path(MLIR_CONFIG_LLVM_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}" "${LLVM_INSTALL_PACKAGE_DIR}") get_config_exports_includes(MLIR MLIR_CONFIG_INCLUDE_EXPORTS) extend_path(base_includedir "\${MLIR_INSTALL_PREFIX}" "${CMAKE_INSTALL_INCLUDEDIR}") set(MLIR_CONFIG_INCLUDE_DIRS diff --git a/polly/cmake/CMakeLists.txt b/polly/cmake/CMakeLists.txt --- a/polly/cmake/CMakeLists.txt +++ b/polly/cmake/CMakeLists.txt @@ -1,10 +1,19 @@ # Keep this in sync with llvm/cmake/CMakeLists.txt! +include(GNUInstallPackageDir) include(ExtendPath) include(FindPrefixFromConfig) -set(LLVM_INSTALL_PACKAGE_DIR "lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") -set(POLLY_INSTALL_PACKAGE_DIR "lib${LLVM_LIBDIR_SUFFIX}/cmake/polly") +set(POLLY_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/polly" CACHE STRING + "Path for CMake subdirectory for Polly (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/polly')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(polly_cmake_builddir "${POLLY_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/polly") + +set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING + "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") +# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. +set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") + if (CMAKE_CONFIGURATION_TYPES) set(POLLY_EXPORTS_FILE_NAME "PollyExports-$>.cmake") else() @@ -28,9 +37,6 @@ set(POLLY_CONFIG_TARGET_${tgt}_TYPE ${tgt_type}) endforeach() -set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") -set(POLLY_CONFIG_LLVM_CMAKE_DIR "${llvm_cmake_builddir}") - # generate the import code for bundled/undbundled libisl versions if (NOT POLLY_BUNDLED_ISL) get_property(incl TARGET ISL PROPERTY INTERFACE_INCLUDE_DIRECTORIES) @@ -50,7 +56,8 @@ # Generate PollyConfig.cmake for the build tree. set(POLLY_CONFIG_CODE "") -set(POLLY_CONFIG_CMAKE_DIR "${CMAKE_BINARY_DIR}/${POLLY_INSTALL_PACKAGE_DIR}") +set(POLLY_CONFIG_LLVM_CMAKE_DIR "${llvm_cmake_builddir}") +set(POLLY_CONFIG_CMAKE_DIR "${polly_cmake_builddir}") set(POLLY_CONFIG_INCLUDE_DIRS ${POLLY_SOURCE_DIR}/include ${ISL_INCLUDE_DIRS} @@ -73,11 +80,11 @@ # the imported locations configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PollyConfig.cmake.in - ${POLLY_CONFIG_CMAKE_DIR}/PollyConfig.cmake + ${polly_cmake_builddir}/PollyConfig.cmake @ONLY) file(GENERATE - OUTPUT ${POLLY_CONFIG_CMAKE_DIR}/${POLLY_EXPORTS_FILE_NAME} + OUTPUT ${polly_cmake_builddir}/${POLLY_EXPORTS_FILE_NAME} CONTENT "${POLLY_EXPORTS}")