|
| 1 | +# This file configures the CMake package configuration file "MLIRConfig.cmake" |
| 2 | +# (see https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html). |
| 3 | +# Two different versions of the package config file are generated and copied |
| 4 | +# into different locations: |
| 5 | +# 1. One in a canonical location in the build directory, |
| 6 | +# '<build>/lib/cmake/mlir/MLIRConfig.cmake'. This version may use absolute |
| 7 | +# paths referring to locations in the build tree. |
| 8 | +# 2. One under '${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles', which is the version |
| 9 | +# *to be installed* when `cmake --install` command is run or when CPack |
| 10 | +# is invoked. This is the version that is distributed with LLVM packages. |
| 11 | +# The key difference between this and (1) is that all paths must be |
| 12 | +# relative the the instlaled "MLIRConfig.cmake" file. |
| 13 | + |
1 | 14 | include(GNUInstallPackageDir) |
2 | 15 | include(ExtendPath) |
3 | 16 | include(LLVMDistributionSupport) |
4 | 17 | include(FindPrefixFromConfig) |
5 | 18 |
|
6 | | -# Generate a list of CMake library targets so that other CMake projects can |
7 | | -# link against them. LLVM calls its version of this file LLVMExports.cmake, but |
8 | | -# the usual CMake convention seems to be ${Project}Targets.cmake. |
| 19 | +# This is a relative path to the location of MLIConfig.cmake within the |
| 20 | +# installation. |
9 | 21 | set(MLIR_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/mlir" CACHE STRING |
10 | | - "Path for CMake subdirectory for Polly (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/polly')") |
11 | | -# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. |
12 | | -set(mlir_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/mlir") |
| 22 | + "Path for CMake subdirectory for MLIR (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/mlir')") |
13 | 23 |
|
14 | 24 | # Keep this in sync with llvm/cmake/CMakeLists.txt! |
15 | 25 | set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING |
16 | 26 | "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") |
17 | | -# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below. |
18 | 27 | string(REPLACE "${CMAKE_CFG_INTDIR}" "." llvm_cmake_builddir "${LLVM_LIBRARY_DIR}") |
| 28 | + |
| 29 | +# These two variables contain absolute paths, so only use them for the |
| 30 | +# build directory 'MLIRConfig.cmake'. |
| 31 | +set(mlir_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/mlir") |
19 | 32 | set(llvm_cmake_builddir "${llvm_cmake_builddir}/cmake/llvm") |
20 | 33 |
|
| 34 | +# ----------------------------------------------------------------------------- |
| 35 | +# Generate `<build>/lib/cmake/mlir/MLIRConfig.cmake' |
| 36 | +# ----------------------------------------------------------------------------- |
| 37 | + |
| 38 | +# Generate the 'MLIRTarget.cmake' file. |
21 | 39 | get_property(MLIR_EXPORTS GLOBAL PROPERTY MLIR_EXPORTS) |
22 | 40 | export(TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir}/MLIRTargets.cmake) |
23 | 41 |
|
| 42 | +# Declare all the variables required by the 'MLIRConfig.cmake.in' template. |
24 | 43 | get_property(MLIR_ALL_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS) |
25 | 44 | get_property(MLIR_DIALECT_LIBS GLOBAL PROPERTY MLIR_DIALECT_LIBS) |
26 | 45 | get_property(MLIR_CONVERSION_LIBS GLOBAL PROPERTY MLIR_CONVERSION_LIBS) |
@@ -49,24 +68,37 @@ configure_file( |
49 | 68 | ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfigVersion.cmake.in |
50 | 69 | ${mlir_cmake_builddir}/MLIRConfigVersion.cmake |
51 | 70 | @ONLY) |
| 71 | + |
| 72 | +# Reset the variables which are invalid for the installed version of |
| 73 | +# MLIRConfig.cmake. |
52 | 74 | set(MLIR_CONFIG_CMAKE_DIR) |
53 | 75 | set(MLIR_CONFIG_LLVM_CMAKE_DIR) |
54 | 76 | set(MLIR_CONFIG_INCLUDE_DIRS) |
55 | 77 |
|
56 | | -# For compatibility with projects that include(MLIRConfig) |
57 | | -# via CMAKE_MODULE_PATH, place API modules next to it. |
| 78 | +# For compatibility with projects that add '<build>/lib/cmake/mlir' to |
| 79 | +# their CMAKE_MODULE_PATH, place API modules next to it. |
58 | 80 | # Copy without source permissions because the source could be read-only, |
59 | 81 | # but we need to write into the copied folder. |
60 | | -# This should be removed in the future. |
61 | 82 | file(COPY . |
62 | 83 | DESTINATION ${mlir_cmake_builddir} |
63 | 84 | NO_SOURCE_PERMISSIONS |
64 | 85 | FILES_MATCHING PATTERN *.cmake |
65 | 86 | PATTERN CMakeFiles EXCLUDE |
66 | 87 | ) |
67 | 88 |
|
| 89 | +# ----------------------------------------------------------------------------- |
68 | 90 | # Generate MLIRConfig.cmake for the install tree. |
| 91 | +# All path variables used in the template must be relative! |
| 92 | +# ----------------------------------------------------------------------------- |
| 93 | + |
| 94 | +# This is a helper that sets 'MLIR_CONFIG_CODE' to some CMake code for |
| 95 | +# insertion at the top of the generated 'MLIRConfig.cmake'. The code calculates |
| 96 | +# the install directory prefix relative to the installed MLIRConfig.cmake (whose |
| 97 | +# prefix is MLIR_INSTALL_PACKAGE_DIR), avoiding any use of absolute paths. |
69 | 98 | find_prefix_from_config(MLIR_CONFIG_CODE MLIR_INSTALL_PREFIX "${MLIR_INSTALL_PACKAGE_DIR}") |
| 99 | + |
| 100 | +# Generate the other required paths by setting them relative to the |
| 101 | +# variable 'MLIR_INSTALL_PREFIX'. |
70 | 102 | extend_path(MLIR_CONFIG_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}" "${MLIR_INSTALL_PACKAGE_DIR}") |
71 | 103 | extend_path(MLIR_CONFIG_LLVM_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}" "${LLVM_INSTALL_PACKAGE_DIR}") |
72 | 104 | get_config_exports_includes(MLIR MLIR_CONFIG_INCLUDE_EXPORTS) |
|
0 commit comments