[RISCV][NFC] precommit for D159399
[llvm-project.git] / mlir / CMakeLists.txt
blobbbbcb0703f20f425b276534f11cec12e1d4d6d81
1 # MLIR project.
2 cmake_minimum_required(VERSION 3.20.0)
4 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
5   set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
6 endif()
7 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
8   NO_POLICY_SCOPE)
10 # Check if MLIR is built as a standalone project.
11 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
12   project(mlir)
13   set(MLIR_STANDALONE_BUILD TRUE)
14 endif()
16 # Must go below project(..)
17 include(GNUInstallDirs)
18 set(CMAKE_CXX_STANDARD 17)
20 if(MLIR_STANDALONE_BUILD)
21   find_package(LLVM CONFIG REQUIRED)
22   set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
23   include(HandleLLVMOptions)
24   include(AddLLVM)
25   include(TableGen)
27   include_directories(${LLVM_INCLUDE_DIRS})
29   set(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest)
30   if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
31     add_subdirectory(${UNITTEST_DIR} third-party/unittest)
32   endif()
34   set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
35     "${CMAKE_CURRENT_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}")
36   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
38   set(LLVM_LIT_ARGS "-sv" CACHE STRING "Default options for lit")
39 endif()
41 set(MLIR_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
42     "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
43 mark_as_advanced(MLIR_TOOLS_INSTALL_DIR)
45 set(MLIR_MAIN_SRC_DIR     ${CMAKE_CURRENT_SOURCE_DIR}  )
46 set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
48 set(MLIR_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
49 set(MLIR_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
50 set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
51 set(MLIR_TOOLS_DIR   ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
53 # Make sure that our source directory is on the current cmake module path so
54 # that we can include cmake files from this directory.
55 list(INSERT CMAKE_MODULE_PATH 0
56   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
57   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
58   )
60 include(AddMLIR)
62 # -BSymbolic is incompatible with TypeID
63 if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]")
64   message(FATAL_ERROR " MLIR does not support `-Bsymbolic` (see http://llvm.org/pr51420 ),"
65           " try `-Bsymbolic-functions` instead.")
66 endif()
68 # Forbid implicit function declaration: this may lead to subtle bugs and we
69 # don't have a reason to support this.
70 check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
71 append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)
73 # Forbid mismatch between declaration and definition for class vs struct. This is
74 # harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems
75 # where it creeps into the ABI.
76 check_c_compiler_flag("-Werror=mismatched-tags" C_SUPPORTS_WERROR_MISMATCHED_TAGS)
77 append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_C_FLAGS)
78 append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_CXX_FLAGS)
80 # Installing the headers and docs needs to depend on generating any public
81 # tablegen'd targets.
82 # mlir-generic-headers are dialect-independent.
83 add_custom_target(mlir-generic-headers)
84 set_target_properties(mlir-generic-headers PROPERTIES FOLDER "Misc")
85 # mlir-headers may be dialect-dependent.
86 add_custom_target(mlir-headers)
87 set_target_properties(mlir-headers PROPERTIES FOLDER "Misc")
88 add_dependencies(mlir-headers mlir-generic-headers)
89 add_custom_target(mlir-doc)
91 # Only enable execution engine if the native target is available.
92 if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
93   set(MLIR_ENABLE_EXECUTION_ENGINE 1)
94 else()
95   set(MLIR_ENABLE_EXECUTION_ENGINE 0)
96 endif()
98 # Build the CUDA conversions and run according tests if the NVPTX backend
99 # is available
100 if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
101   set(MLIR_ENABLE_CUDA_CONVERSIONS 1)
102 else()
103   set(MLIR_ENABLE_CUDA_CONVERSIONS 0)
104 endif()
105 # TODO: we should use a config.h file like LLVM does
106 add_definitions(-DMLIR_CUDA_CONVERSIONS_ENABLED=${MLIR_ENABLE_CUDA_CONVERSIONS})
108 # Build the ROCm conversions and run according tests if the AMDGPU backend
109 # is available.
110 if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
111   set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
112 else()
113   set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
114 endif()
115 add_definitions(-DMLIR_ROCM_CONVERSIONS_ENABLED=${MLIR_ENABLE_ROCM_CONVERSIONS})
117 set(MLIR_ENABLE_DEPRECATED_GPU_SERIALIZATION 0 CACHE BOOL "Enable deprecated GPU serialization passes")
118 set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the mlir CUDA runner")
119 set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the mlir ROCm runner")
120 set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the mlir SPIR-V cpu runner")
121 set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the mlir Vulkan runner")
122 set(MLIR_ENABLE_NVPTXCOMPILER 0 CACHE BOOL
123     "Statically link the nvptxlibrary instead of calling ptxas as a subprocess \
124     for compiling PTX to cubin")
126 option(MLIR_INCLUDE_TESTS
127        "Generate build targets for the MLIR unit tests."
128        ${LLVM_INCLUDE_TESTS})
130 option(MLIR_INCLUDE_INTEGRATION_TESTS
131        "Generate build targets for the MLIR integration tests.")
133 set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL
134     "Installs object files needed for add_mlir_aggregate to work out of \
135     tree. Package maintainers can disable this to exclude these assets if \
136     not desired. Enabling this will result in object files being written \
137     under lib/objects-{CMAKE_BUILD_TYPE}.")
139 set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")
141 configure_file(
142   ${MLIR_MAIN_INCLUDE_DIR}/mlir/Config/mlir-config.h.cmake
143   ${MLIR_INCLUDE_DIR}/mlir/Config/mlir-config.h)
145 #-------------------------------------------------------------------------------
146 # Python Bindings Configuration
147 # Requires:
148 #   The pybind11 library can be found (set with -DPYBIND_DIR=...)
149 #   The python executable is correct (set with -DPython3_EXECUTABLE=...)
150 #-------------------------------------------------------------------------------
152 set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
153        "Enables building of Python bindings.")
154 set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
155        "Prime the python detection by searching for a full 'Development' \
156        component first (temporary while diagnosing environment specific Python \
157        detection issues)")
158 if(MLIR_ENABLE_BINDINGS_PYTHON)
159   include(MLIRDetectPythonEnv)
160   mlir_configure_python_dev_packages()
161 endif()
163 set(CMAKE_INCLUDE_CURRENT_DIR ON)
165 include_directories( "include")
166 include_directories( ${MLIR_INCLUDE_DIR})
168 # Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
169 # MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
170 # from another directory like tools
171 add_subdirectory(tools/mlir-tblgen)
172 add_subdirectory(tools/mlir-linalg-ods-gen)
173 add_subdirectory(tools/mlir-pdll)
175 set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")
176 set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")
177 set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
178 set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
180 add_subdirectory(include/mlir)
181 add_subdirectory(lib)
182 # C API needs all dialects for registration, but should be built before tests.
183 add_subdirectory(lib/CAPI)
185 if (MLIR_INCLUDE_TESTS)
186   add_definitions(-DMLIR_INCLUDE_TESTS)
187   add_custom_target(MLIRUnitTests)
188   if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
189     add_subdirectory(unittests)
190   else()
191     message(WARNING "gtest not found, unittests will not be available")
192   endif()
193   add_subdirectory(test)
194 endif()
195 # Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
196 # Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
197 add_subdirectory(tools)
199 if(MLIR_ENABLE_BINDINGS_PYTHON)
200   # Python sources: built extensions come in via lib/Bindings/Python
201   add_subdirectory(python)
202 endif()
204 if( LLVM_INCLUDE_EXAMPLES )
205   add_subdirectory(examples)
206 endif()
208 option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
209   ${LLVM_INCLUDE_DOCS})
210 if (MLIR_INCLUDE_DOCS)
211   add_subdirectory(docs)
212 endif()
214 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
215   install(DIRECTORY include/mlir include/mlir-c
216     DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
217     COMPONENT mlir-headers
218     FILES_MATCHING
219     PATTERN "*.def"
220     PATTERN "*.h"
221     PATTERN "*.inc"
222     PATTERN "*.td"
223     PATTERN "LICENSE.TXT"
224     )
226   install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c
227     DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
228     COMPONENT mlir-headers
229     FILES_MATCHING
230     PATTERN "*.def"
231     PATTERN "*.h"
232     PATTERN "*.gen"
233     PATTERN "*.inc"
234     PATTERN "*.td"
235     PATTERN "CMakeFiles" EXCLUDE
236     PATTERN "config.h" EXCLUDE
237     )
239   if (NOT LLVM_ENABLE_IDE)
240     add_llvm_install_targets(install-mlir-headers
241                              DEPENDS mlir-headers
242                              COMPONENT mlir-headers)
243   endif()
244 endif()
246 # Custom target to install all mlir libraries
247 add_custom_target(mlir-libraries)
248 set_target_properties(mlir-libraries PROPERTIES FOLDER "Misc")
250 if (NOT LLVM_ENABLE_IDE)
251   add_llvm_install_targets(install-mlir-libraries
252                            DEPENDS mlir-libraries
253                            COMPONENT mlir-libraries)
254 endif()
256 get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
257 if(MLIR_LIBS)
258   list(REMOVE_DUPLICATES MLIR_LIBS)
259   foreach(lib ${MLIR_LIBS})
260     add_dependencies(mlir-libraries ${lib})
261     if(NOT LLVM_ENABLE_IDE)
262       add_dependencies(install-mlir-libraries install-${lib})
263       add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)
264     endif()
265   endforeach()
266 endif()
268 add_subdirectory(cmake/modules)
270 if (MLIR_ENABLE_PYTHON_BENCHMARKS)
271   add_subdirectory(utils/mbr)
272 endif()
274 if(MLIR_STANDALONE_BUILD)
275   llvm_distribution_add_targets()
276 endif()