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 installed "MLIRConfig.cmake" file.
13+
114include (GNUInstallPackageDir)
215include (ExtendPath)
316include (LLVMDistributionSupport)
417include (FindPrefixFromConfig)
518
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 MLIRConfig.cmake within the
20+ # installation.
921set (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')" )
1323
1424# Keep this in sync with llvm/cmake/CMakeLists.txt!
1525set (LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR} /llvm" CACHE STRING
1626 "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR} /llvm')" )
17- # CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below.
1827string (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" )
1932set (llvm_cmake_builddir "${llvm_cmake_builddir} /cmake/llvm" )
2033
34+ # -----------------------------------------------------------------------------
35+ # Generate `<build>/lib/cmake/mlir/MLIRConfig.cmake'
36+ # -----------------------------------------------------------------------------
37+
38+ # Generate the 'MLIRTarget.cmake' file.
2139get_property (MLIR_EXPORTS GLOBAL PROPERTY MLIR_EXPORTS)
2240export (TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir} /MLIRTargets.cmake)
2341
42+ # Declare all the variables required by the 'MLIRConfig.cmake.in' template.
2443get_property (MLIR_ALL_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
2544get_property (MLIR_DIALECT_LIBS GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2645get_property (MLIR_CONVERSION_LIBS GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
@@ -36,7 +55,7 @@ set(MLIR_CONFIG_INCLUDE_DIRS
3655 "${MLIR_SOURCE_DIR} /include"
3756 "${MLIR_BINARY_DIR} /include"
3857 )
39- # Refer to the best host mlir-tbgen , which might be a host-optimized version
58+ # Refer to the best host mlir-tblgen , which might be a host-optimized version
4059set (MLIR_CONFIG_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE} " )
4160set (MLIR_CONFIG_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE} " )
4261set (MLIR_CONFIG_SRC_SHARDER_TABLEGEN_EXE "${MLIR_SRC_SHARDER_TABLEGEN_EXE} " )
@@ -49,24 +68,37 @@ configure_file(
4968 ${CMAKE_CURRENT_SOURCE_DIR} /MLIRConfigVersion.cmake.in
5069 ${mlir_cmake_builddir} /MLIRConfigVersion.cmake
5170 @ONLY)
71+
72+ # Reset the variables which are invalid for the installed version of
73+ # MLIRConfig.cmake.
5274set (MLIR_CONFIG_CMAKE_DIR)
5375set (MLIR_CONFIG_LLVM_CMAKE_DIR)
5476set (MLIR_CONFIG_INCLUDE_DIRS)
5577
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.
5880# Copy without source permissions because the source could be read-only,
5981# but we need to write into the copied folder.
60- # This should be removed in the future.
6182file (COPY .
6283 DESTINATION ${mlir_cmake_builddir}
6384 NO_SOURCE_PERMISSIONS
6485 FILES_MATCHING PATTERN *.cmake
6586 PATTERN CMakeFiles EXCLUDE
6687 )
6788
89+ # -----------------------------------------------------------------------------
6890# 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.
6998find_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'.
70102extend_path(MLIR_CONFIG_CMAKE_DIR "\$ {MLIR_INSTALL_PREFIX}" "${MLIR_INSTALL_PACKAGE_DIR} " )
71103extend_path(MLIR_CONFIG_LLVM_CMAKE_DIR "\$ {MLIR_INSTALL_PREFIX}" "${LLVM_INSTALL_PACKAGE_DIR} " )
72104get_config_exports_includes(MLIR MLIR_CONFIG_INCLUDE_EXPORTS)
0 commit comments