xref: /llvm-project/mlir/cmake/modules/CMakeLists.txt (revision 1b232fa0e9864dde230db8da82a906c588baf792)
1include(GNUInstallPackageDir)
2include(ExtendPath)
3include(LLVMDistributionSupport)
4include(FindPrefixFromConfig)
5
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.
9set(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.
12set(mlir_cmake_builddir "${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/mlir")
13
14# Keep this in sync with llvm/cmake/CMakeLists.txt!
15set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING
16  "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')")
17# CMAKE_INSTALL_PACKAGEDIR might be absolute, so don't reuse below.
18string(REPLACE "${CMAKE_CFG_INTDIR}" "." llvm_cmake_builddir "${LLVM_LIBRARY_DIR}")
19set(llvm_cmake_builddir "${llvm_cmake_builddir}/cmake/llvm")
20
21get_property(MLIR_EXPORTS GLOBAL PROPERTY MLIR_EXPORTS)
22export(TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir}/MLIRTargets.cmake)
23
24get_property(MLIR_ALL_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
25get_property(MLIR_DIALECT_LIBS GLOBAL PROPERTY MLIR_DIALECT_LIBS)
26get_property(MLIR_CONVERSION_LIBS GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
27get_property(MLIR_EXTENSION_LIBS GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
28get_property(MLIR_TRANSLATION_LIBS GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
29get_property(MLIR_CAPI_LIBS GLOBAL PROPERTY MLIR_CAPI_LIBS)
30
31# Generate MlirConfig.cmake for the build tree.
32set(MLIR_CONFIG_CMAKE_DIR "${mlir_cmake_builddir}")
33set(MLIR_CONFIG_LLVM_CMAKE_DIR "${llvm_cmake_builddir}")
34set(MLIR_CONFIG_INCLUDE_EXPORTS "include(\"\${MLIR_CMAKE_DIR}/MLIRTargets.cmake\")")
35set(MLIR_CONFIG_INCLUDE_DIRS
36  "${MLIR_SOURCE_DIR}/include"
37  "${MLIR_BINARY_DIR}/include"
38  )
39# Refer to the best host mlir-tbgen, which might be a host-optimized version
40set(MLIR_CONFIG_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}")
41set(MLIR_CONFIG_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}")
42set(MLIR_CONFIG_SRC_SHARDER_TABLEGEN_EXE "${MLIR_SRC_SHARDER_TABLEGEN_EXE}")
43
44configure_file(
45  ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfig.cmake.in
46  ${mlir_cmake_builddir}/MLIRConfig.cmake
47  @ONLY)
48configure_file(
49  ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfigVersion.cmake.in
50  ${mlir_cmake_builddir}/MLIRConfigVersion.cmake
51  @ONLY)
52set(MLIR_CONFIG_CMAKE_DIR)
53set(MLIR_CONFIG_LLVM_CMAKE_DIR)
54set(MLIR_CONFIG_INCLUDE_DIRS)
55
56# For compatibility with projects that include(MLIRConfig)
57# via CMAKE_MODULE_PATH, place API modules next to it.
58# Copy without source permissions because the source could be read-only,
59# but we need to write into the copied folder.
60# This should be removed in the future.
61file(COPY .
62  DESTINATION ${mlir_cmake_builddir}
63  NO_SOURCE_PERMISSIONS
64  FILES_MATCHING PATTERN *.cmake
65  PATTERN CMakeFiles EXCLUDE
66  )
67
68# Generate MLIRConfig.cmake for the install tree.
69find_prefix_from_config(MLIR_CONFIG_CODE MLIR_INSTALL_PREFIX "${MLIR_INSTALL_PACKAGE_DIR}")
70extend_path(MLIR_CONFIG_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}" "${MLIR_INSTALL_PACKAGE_DIR}")
71extend_path(MLIR_CONFIG_LLVM_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}" "${LLVM_INSTALL_PACKAGE_DIR}")
72get_config_exports_includes(MLIR MLIR_CONFIG_INCLUDE_EXPORTS)
73extend_path(base_includedir "\${MLIR_INSTALL_PREFIX}" "${CMAKE_INSTALL_INCLUDEDIR}")
74set(MLIR_CONFIG_INCLUDE_DIRS
75  "${base_includedir}"
76  )
77# Ensure that we are using the installed mlir-tblgen. This might not be MLIR_TABLEGEN_EXE
78# if we're building with a host-optimized mlir-tblgen (with LLVM_OPTIMIZED_TABLEGEN).
79set(MLIR_CONFIG_TABLEGEN_EXE mlir-tblgen)
80set(MLIR_CONFIG_PDLL_TABLEGEN_EXE mlir-pdll)
81set(MLIR_CONFIG_SRC_SHARDER_TABLEGEN_EXE mlir-src-sharder)
82
83configure_file(
84  ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfig.cmake.in
85  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfig.cmake
86  @ONLY)
87configure_file(
88  ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfigVersion.cmake.in
89  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfigVersion.cmake
90  @ONLY)
91set(MLIR_CONFIG_CODE)
92set(MLIR_CONFIG_CMAKE_DIR)
93set(MLIR_CONFIG_LLVM_CMAKE_DIR)
94set(MLIR_CONFIG_INCLUDE_DIRS)
95
96if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
97  # Not TOOLCHAIN ONLY, so install the MLIR parts as well
98  # Include the cmake files so other tools can use mlir-tblgen, etc.
99  install_distribution_exports(MLIR)
100
101  install(FILES
102    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfig.cmake
103    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfigVersion.cmake
104    ${CMAKE_CURRENT_SOURCE_DIR}/AddMLIR.cmake
105    ${CMAKE_CURRENT_SOURCE_DIR}/AddMLIRPython.cmake
106    ${CMAKE_CURRENT_SOURCE_DIR}/MLIRDetectPythonEnv.cmake
107    DESTINATION ${MLIR_INSTALL_PACKAGE_DIR}
108    COMPONENT mlir-cmake-exports)
109
110  if(NOT LLVM_ENABLE_IDE)
111    # Add a dummy target so this can be used with LLVM_DISTRIBUTION_COMPONENTS
112    add_custom_target(mlir-cmake-exports)
113    add_llvm_install_targets(install-mlir-cmake-exports
114                             COMPONENT mlir-cmake-exports)
115  endif()
116endif()
117