2 cmake_minimum_required(VERSION 3.13.4)
4 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
5 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
7 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
10 # Check if MLIR is built as a standalone project.
11 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
13 set(MLIR_STANDALONE_BUILD TRUE)
14 if ("${CMAKE_VERSION}" VERSION_LESS "3.20.0")
16 "Your CMake version is ${CMAKE_VERSION}. Starting with LLVM 17.0.0, the "
17 "minimum version of CMake required to build LLVM will become 3.20.0, and "
18 "using an older CMake will become an error. Please upgrade your CMake to "
19 "at least 3.20.0 now to avoid issues in the future!")
23 # Must go below project(..)
24 include(GNUInstallDirs)
25 set(CMAKE_CXX_STANDARD 17)
27 if(MLIR_STANDALONE_BUILD)
28 find_package(LLVM CONFIG REQUIRED)
29 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
30 include(HandleLLVMOptions)
34 include_directories(${LLVM_INCLUDE_DIRS})
36 set(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest)
37 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
38 add_subdirectory(${UNITTEST_DIR} third-party/unittest)
41 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
42 "${CMAKE_CURRENT_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}")
43 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
45 set(LLVM_LIT_ARGS "-sv" CACHE STRING "Default options for lit")
48 set(MLIR_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
49 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
50 mark_as_advanced(MLIR_TOOLS_INSTALL_DIR)
52 set(MLIR_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
53 set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
55 set(MLIR_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
56 set(MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
57 set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
58 set(MLIR_TOOLS_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
60 # Make sure that our source directory is on the current cmake module path so
61 # that we can include cmake files from this directory.
62 list(INSERT CMAKE_MODULE_PATH 0
63 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
64 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
69 # -BSymbolic is incompatible with TypeID
70 if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]")
71 message(FATAL_ERROR " MLIR does not support `-Bsymbolic` (see http://llvm.org/pr51420 ),"
72 " try `-Bsymbolic-functions` instead.")
75 # Forbid implicit function declaration: this may lead to subtle bugs and we
76 # don't have a reason to support this.
77 check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
78 append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)
80 # Forbid mismatch between declaration and definition for class vs struct. This is
81 # harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems
82 # where it creeps into the ABI.
83 check_c_compiler_flag("-Werror=mismatched-tags" C_SUPPORTS_WERROR_MISMATCHED_TAGS)
84 append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_C_FLAGS)
85 append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_CXX_FLAGS)
87 # Installing the headers and docs needs to depend on generating any public
89 # mlir-generic-headers are dialect-independent.
90 add_custom_target(mlir-generic-headers)
91 set_target_properties(mlir-generic-headers PROPERTIES FOLDER "Misc")
92 # mlir-headers may be dialect-dependent.
93 add_custom_target(mlir-headers)
94 set_target_properties(mlir-headers PROPERTIES FOLDER "Misc")
95 add_dependencies(mlir-headers mlir-generic-headers)
96 add_custom_target(mlir-doc)
98 # Only enable execution engine if the native target is available.
99 if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
100 set(MLIR_ENABLE_EXECUTION_ENGINE 1)
102 set(MLIR_ENABLE_EXECUTION_ENGINE 0)
105 # Build the CUDA conversions and run according tests if the NVPTX backend
107 if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD AND MLIR_ENABLE_EXECUTION_ENGINE)
108 set(MLIR_ENABLE_CUDA_CONVERSIONS 1)
110 set(MLIR_ENABLE_CUDA_CONVERSIONS 0)
112 # TODO: we should use a config.h file like LLVM does
113 add_definitions(-DMLIR_CUDA_CONVERSIONS_ENABLED=${MLIR_ENABLE_CUDA_CONVERSIONS})
115 # Build the ROCm conversions and run according tests if the AMDGPU backend
117 if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD AND MLIR_ENABLE_EXECUTION_ENGINE)
118 set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
120 set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
122 add_definitions(-DMLIR_ROCM_CONVERSIONS_ENABLED=${MLIR_ENABLE_ROCM_CONVERSIONS})
124 set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the mlir CUDA runner")
125 set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the mlir ROCm runner")
126 set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the mlir SPIR-V cpu runner")
127 set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the mlir Vulkan runner")
129 option(MLIR_INCLUDE_TESTS
130 "Generate build targets for the MLIR unit tests."
131 ${LLVM_INCLUDE_TESTS})
133 option(MLIR_INCLUDE_INTEGRATION_TESTS
134 "Generate build targets for the MLIR integration tests.")
136 set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL
137 "Installs object files needed for add_mlir_aggregate to work out of \
138 tree. Package maintainers can disable this to exclude these assets if \
139 not desired. Enabling this will result in object files being written \
140 under lib/objects-{CMAKE_BUILD_TYPE}.")
142 set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")
145 ${MLIR_MAIN_INCLUDE_DIR}/mlir/Config/mlir-config.h.cmake
146 ${MLIR_INCLUDE_DIR}/mlir/Config/mlir-config.h)
148 #-------------------------------------------------------------------------------
149 # Python Bindings Configuration
151 # The pybind11 library can be found (set with -DPYBIND_DIR=...)
152 # The python executable is correct (set with -DPython3_EXECUTABLE=...)
153 #-------------------------------------------------------------------------------
155 set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
156 "Enables building of Python bindings.")
157 set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
158 "Prime the python detection by searching for a full 'Development' \
159 component first (temporary while diagnosing environment specific Python \
161 if(MLIR_ENABLE_BINDINGS_PYTHON)
162 include(MLIRDetectPythonEnv)
163 mlir_configure_python_dev_packages()
166 set(CMAKE_INCLUDE_CURRENT_DIR ON)
168 include_directories( "include")
169 include_directories( ${MLIR_INCLUDE_DIR})
171 # Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
172 # MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
173 # from another directory like tools
174 add_subdirectory(tools/mlir-tblgen)
175 add_subdirectory(tools/mlir-linalg-ods-gen)
176 add_subdirectory(tools/mlir-pdll)
178 set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")
179 set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")
180 set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
181 set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
183 add_subdirectory(include/mlir)
184 add_subdirectory(lib)
185 # C API needs all dialects for registration, but should be built before tests.
186 add_subdirectory(lib/CAPI)
188 if (MLIR_INCLUDE_TESTS)
189 add_definitions(-DMLIR_INCLUDE_TESTS)
190 add_custom_target(MLIRUnitTests)
191 if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
192 add_subdirectory(unittests)
194 message(WARNING "gtest not found, unittests will not be available")
196 add_subdirectory(test)
198 # Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
199 # Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
200 add_subdirectory(tools)
202 if(MLIR_ENABLE_BINDINGS_PYTHON)
203 # Python sources: built extensions come in via lib/Bindings/Python
204 add_subdirectory(python)
207 if( LLVM_INCLUDE_EXAMPLES )
208 add_subdirectory(examples)
211 option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
212 ${LLVM_INCLUDE_DOCS})
213 if (MLIR_INCLUDE_DOCS)
214 add_subdirectory(docs)
217 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
218 install(DIRECTORY include/mlir include/mlir-c
219 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
220 COMPONENT mlir-headers
226 PATTERN "LICENSE.TXT"
229 install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c
230 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
231 COMPONENT mlir-headers
238 PATTERN "CMakeFiles" EXCLUDE
239 PATTERN "config.h" EXCLUDE
242 if (NOT LLVM_ENABLE_IDE)
243 add_llvm_install_targets(install-mlir-headers
245 COMPONENT mlir-headers)
249 # Custom target to install all mlir libraries
250 add_custom_target(mlir-libraries)
251 set_target_properties(mlir-libraries PROPERTIES FOLDER "Misc")
253 if (NOT LLVM_ENABLE_IDE)
254 add_llvm_install_targets(install-mlir-libraries
255 DEPENDS mlir-libraries
256 COMPONENT mlir-libraries)
259 get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
261 list(REMOVE_DUPLICATES MLIR_LIBS)
262 foreach(lib ${MLIR_LIBS})
263 add_dependencies(mlir-libraries ${lib})
264 if(NOT LLVM_ENABLE_IDE)
265 add_dependencies(install-mlir-libraries install-${lib})
266 add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)
271 add_subdirectory(cmake/modules)
273 if (MLIR_ENABLE_PYTHON_BENCHMARKS)
274 add_subdirectory(utils/mbr)
277 if(MLIR_STANDALONE_BUILD)
278 llvm_distribution_add_targets()