blob: 6205283f1de282b3467e5b76d60bf3123a85163c [file] [log] [blame]
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07001# Copyright 2019 The IREE Authors
Ben Vanik185d30c2019-09-19 14:24:11 -07002#
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07003# Licensed under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ben Vanik185d30c2019-09-19 14:24:11 -07006
Stella Laurenzo27c448e2021-09-24 17:53:21 -07007cmake_minimum_required(VERSION 3.16.3)
Ben Vanik552c5f42021-06-21 17:39:14 -07008
Scott Todd9fa14f92021-03-09 10:43:38 -08009# Do not set MSVC warning flags like /W3 by default (since 3.15):
10# https://cmake.org/cmake/help/v3.15/policy/CMP0092.html
11if(POLICY CMP0092)
12 cmake_policy(SET CMP0092 NEW)
13endif()
Ben Vanik552c5f42021-06-21 17:39:14 -070014
15# LLVM requires CMP0116 for tblgen: https://reviews.llvm.org/D101083
16# CMP0116: Ninja generators transform `DEPFILE`s from `add_custom_command()`
17# New in CMake 3.20. https://cmake.org/cmake/help/latest/policy/CMP0116.html
18set(CMAKE_POLICY_DEFAULT_CMP0116 OLD)
19
Ben Vanik512d2d32019-09-20 13:22:34 -070020set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Ben Vanik185d30c2019-09-19 14:24:11 -070021
Stella Laurenzo688670f2021-09-24 18:16:25 -070022project(IREE ASM C CXX)
Lei Zhang6c5907b2020-06-02 09:06:08 -070023set(CMAKE_C_STANDARD 11)
24set(CMAKE_CXX_STANDARD 14)
Ben Vanik185d30c2019-09-19 14:24:11 -070025set(IREE_IDE_FOLDER IREE)
26set_property(GLOBAL PROPERTY USE_FOLDERS ON)
27
Ben Vanik96fd2772021-04-19 20:30:56 -070028if(${MSVC})
29 enable_language(ASM_MASM)
30else()
31 enable_language(ASM)
32endif()
33
Lei Zhang7e253da2020-06-10 07:51:19 -070034#-------------------------------------------------------------------------------
35# Project component configuration
36#-------------------------------------------------------------------------------
37
Ben Vanik56d44bf2020-05-15 13:20:50 -070038option(IREE_ENABLE_RUNTIME_TRACING "Enables instrumented runtime tracing." OFF)
Scott Todd1cd9f0a2021-07-29 10:05:44 -070039option(IREE_ENABLE_COMPILER_TRACING "Enables instrumented compiler tracing." OFF)
Ben Vanik512d2d32019-09-20 13:22:34 -070040
Marius Brehleredfc57f2019-12-18 11:19:38 -080041option(IREE_BUILD_COMPILER "Builds the IREE compiler." ON)
Ben Vanik512d2d32019-09-20 13:22:34 -070042option(IREE_BUILD_TESTS "Builds IREE unit tests." ON)
Lei Zhangfe4403e2021-06-01 15:09:43 -040043option(IREE_BUILD_BENCHMARKS "Builds IREE benchmark suites." OFF)
Lei Zhang22f0e242020-03-30 12:09:20 -070044option(IREE_BUILD_DOCS "Builds IREE docs." OFF)
Ben Vanik6b112ef2019-10-03 10:45:14 -070045option(IREE_BUILD_SAMPLES "Builds IREE sample projects." ON)
Ben Vanikcd1132f2021-01-29 15:58:17 -080046
Stella Laurenzo6a0313b2021-05-04 22:45:59 +000047option(IREE_BUILD_TENSORFLOW_ALL "Builds all TensorFlow compiler frontends." OFF)
Stella Laurenzoa4137ef2020-12-09 21:28:08 -080048option(IREE_BUILD_TENSORFLOW_COMPILER "Builds TensorFlow compiler frontend." OFF)
Stella Laurenzo55fcfab2020-12-10 13:17:25 -080049option(IREE_BUILD_TFLITE_COMPILER "Builds the TFLite compiler frontend." OFF)
Stella Laurenzoa4137ef2020-12-09 21:28:08 -080050option(IREE_BUILD_XLA_COMPILER "Builds TensorFlow XLA compiler frontend." OFF)
Cindy Liu331946c2021-06-01 12:20:30 -070051option(IREE_ENABLE_THREADING "Builds IREE in with thread library support." ON)
Ben Vanik512d2d32019-09-20 13:22:34 -070052
Marius Brehler0a4b67f2020-05-08 13:19:16 -070053set(IREE_HAL_DRIVERS_TO_BUILD "all"
Scott Todd6132bb32020-08-11 12:04:40 -070054 CACHE STRING "Semicolon-separated list of HAL drivers to build, or \"all\".")
Marius Brehler0a4b67f2020-05-08 13:19:16 -070055set(IREE_TARGET_BACKENDS_TO_BUILD "all"
Scott Todd6132bb32020-08-11 12:04:40 -070056 CACHE STRING "Semicolon-separated list of target backends to build, or \"all\".")
Stella Laurenzo04b7c372020-12-10 00:05:31 +000057
Stella Laurenzobb1de962021-01-05 08:18:33 -080058# Properties controlling version and naming of release artifacts.
59set(IREE_RELEASE_PACKAGE_SUFFIX "-dev" CACHE STRING "Suffix to append to distributed package names")
60set(IREE_RELEASE_VERSION "0.1a1" CACHE STRING "Version to embed in distributed packages")
61set(IREE_RELEASE_REVISION "HEAD" CACHE STRING "Version control revision information to embed in distributed packages")
62
Stella Laurenzoa4137ef2020-12-09 21:28:08 -080063# Master enable for tensorflow build support.
64# Note that this is a normal CMake variable used to gate build features (not
65# a cache variable that is user-settable).
66set(IREE_ENABLE_TENSORFLOW OFF)
Stella Laurenzo6a0313b2021-05-04 22:45:59 +000067if(${IREE_BUILD_TENSORFLOW_ALL} OR
68 ${IREE_BUILD_TENSORFLOW_COMPILER} OR
Stella Laurenzo55fcfab2020-12-10 13:17:25 -080069 ${IREE_BUILD_TFLITE_COMPILER} OR
70 ${IREE_BUILD_XLA_COMPILER})
Stella Laurenzoa4137ef2020-12-09 21:28:08 -080071 set(IREE_ENABLE_TENSORFLOW ON)
72endif()
73
Ben Vanikcd1132f2021-01-29 15:58:17 -080074option(IREE_BUILD_BINDINGS_TFLITE "Builds the IREE TFLite C API compatibility shim" ON)
Kojo Acquah5bf39942021-04-22 16:01:28 -070075option(IREE_BUILD_BINDINGS_TFLITE_JAVA "Builds the IREE TFLite Java bindings with the C API compatibility shim" ON)
Ben Vanikcd1132f2021-01-29 15:58:17 -080076
Stella Laurenzo04b7c372020-12-10 00:05:31 +000077# Default python bindings to enabled for some features.
Stella Laurenzoa4137ef2020-12-09 21:28:08 -080078if(${IREE_ENABLE_TENSORFLOW})
Stella Laurenzo04b7c372020-12-10 00:05:31 +000079 option(IREE_BUILD_PYTHON_BINDINGS "Builds the IREE python bindings" ON)
80else()
81 option(IREE_BUILD_PYTHON_BINDINGS "Builds the IREE python bindings" OFF)
82endif()
83
Stella Laurenzo0f005e52020-12-27 11:54:33 -080084#-------------------------------------------------------------------------------
85# Experimental project flags
86#-------------------------------------------------------------------------------
87
Stella Laurenzo0f005e52020-12-27 11:54:33 -080088option(IREE_BUILD_EXPERIMENTAL_REMOTING "Builds experimental remoting support." OFF)
not-jenni603e9f12021-01-26 16:49:18 -080089option(IREE_BUILD_EXPERIMENTAL_JAVA_BINDINGS "Builds the experimental java bindings." OFF)
raikonenfnu4b31bd92021-05-18 18:20:25 -070090option(IREE_BUILD_EXPERIMENTAL_ROCM "Builds the experimental ROCM Backend." OFF)
Stella Laurenzo0a8a9442021-06-14 22:42:37 -070091option(IREE_ENABLE_NEW_INTEGRATION_TESTS "Enables new integration tests and disables old." OFF)
Stella Laurenzo0f005e52020-12-27 11:54:33 -080092
93#-------------------------------------------------------------------------------
94# Derived flags based on primary options
95#-------------------------------------------------------------------------------
96
Marius Brehler5400f0f2021-08-11 01:41:34 +020097option(IREE_ENABLE_EMITC "Enables MLIR EmitC dependencies." ${IREE_BUILD_COMPILER})
Marius Brehlerfd8be7f2020-06-18 15:59:28 -070098
Ben Vanik185d30c2019-09-19 14:24:11 -070099#-------------------------------------------------------------------------------
Lei Zhang8bea2e82020-06-04 10:17:56 -0400100# Target and backend configuration
Ben Vanik185d30c2019-09-19 14:24:11 -0700101#-------------------------------------------------------------------------------
102
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700103# List of all HAL drivers to be built by default:
104set(IREE_ALL_HAL_DRIVERS
Thomasa98c3c02021-02-16 09:56:03 -0800105 Cuda
Scott Todd217904d2020-06-04 13:38:32 -0700106 DyLib
CindyLiua5479652021-09-29 16:32:18 +0000107 Dylib_Sync
Ben Vanik0f14db02021-04-19 23:29:29 -0700108 VMVX
CindyLiua5479652021-09-29 16:32:18 +0000109 VMVX_Sync
Scott Todd6132bb32020-08-11 12:04:40 -0700110 Vulkan
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700111)
112
Ben Vanik85cdd862020-11-14 11:26:12 -0800113if(IREE_HAL_DRIVERS_TO_BUILD STREQUAL "all")
114 set(IREE_HAL_DRIVERS_TO_BUILD ${IREE_ALL_HAL_DRIVERS})
Lei Zhang6171a982020-09-08 12:48:25 -0400115
116 # For Apple platforms we need to use Metal instead of Vulkan.
117 if(APPLE)
118 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Vulkan)
Lei Zhang6171a982020-09-08 12:48:25 -0400119 endif()
Thomasa98c3c02021-02-16 09:56:03 -0800120 # Remove Cuda from Android and Apple platforms.
121 if(ANDROID OR APPLE)
122 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Cuda)
123 endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700124endif()
Scott Todd6132bb32020-08-11 12:04:40 -0700125message(STATUS "Building HAL drivers: ${IREE_HAL_DRIVERS_TO_BUILD}")
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700126
127# Default every IREE_HAL_DRIVER_* to OFF
128foreach(_backend ${IREE_ALL_HAL_DRIVERS})
129 string(TOUPPER "${_backend}" uppercase_backend)
130 set(IREE_HAL_DRIVER_${uppercase_backend} OFF CACHE BOOL "" FORCE)
131endforeach()
132
133# Set IREE_HAL_DRIVER_* based on configuration
134foreach(_backend ${IREE_HAL_DRIVERS_TO_BUILD})
135 string(TOUPPER "${_backend}" uppercase_backend)
Marius Brehler30f06a02020-12-18 13:39:26 +0000136 string(REPLACE "\"" "" uppercase_backend ${uppercase_backend})
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700137 set(IREE_HAL_DRIVER_${uppercase_backend} ON CACHE BOOL "" FORCE)
138endforeach()
139
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700140# List of all target backends to be built by default:
141set(IREE_ALL_TARGET_BACKENDS
Thomasd85738e2021-02-23 11:02:55 -0800142 CUDA
Scott Todd9f0148f2021-03-17 15:14:07 -0700143 DYLIB-LLVM-AOT
144 WASM-LLVM-AOT
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400145 Metal-SPIRV
harsh-nodf6d36fe2021-05-17 15:38:31 -0700146 ROCM
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400147 Vulkan-SPIRV
Ben Vanik907f3532021-04-20 13:27:00 -0700148 VMVX
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700149)
150
Scott Todd28cacaa2021-06-29 09:31:46 -0700151if(${IREE_BUILD_COMPILER})
152 if(IREE_TARGET_BACKENDS_TO_BUILD STREQUAL "all")
153 set(IREE_TARGET_BACKENDS_TO_BUILD ${IREE_ALL_TARGET_BACKENDS})
154 endif()
155 message(STATUS "Building target backends: ${IREE_TARGET_BACKENDS_TO_BUILD}")
156else()
157 set(IREE_TARGET_BACKENDS_TO_BUILD "" CACHE STRING "" FORCE)
158 message(STATUS "Compiler is disabled, building no target backends")
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700159endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700160
161# Default every IREE_TARGET_BACKEND_* to OFF
162foreach(_backend ${IREE_ALL_TARGET_BACKENDS})
163 string(TOUPPER "${_backend}" uppercase_backend)
164 set(IREE_TARGET_BACKEND_${uppercase_backend} OFF CACHE BOOL "" FORCE)
165endforeach()
166
167# Set IREE_TARGET_BACKEND_* based on configuration
168foreach(_backend ${IREE_TARGET_BACKENDS_TO_BUILD})
169 string(TOUPPER "${_backend}" uppercase_backend)
Marius Brehler30f06a02020-12-18 13:39:26 +0000170 string(REPLACE "\"" "" uppercase_backend ${uppercase_backend})
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700171 set(IREE_TARGET_BACKEND_${uppercase_backend} ON CACHE BOOL "" FORCE)
172endforeach()
173
Ben Vanik185d30c2019-09-19 14:24:11 -0700174list(APPEND CMAKE_MODULE_PATH
175 ${CMAKE_CURRENT_LIST_DIR}/build_tools/cmake/
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800176 ${CMAKE_CURRENT_LIST_DIR}/bindings/python/build_tools/cmake/
Ben Vanik185d30c2019-09-19 14:24:11 -0700177)
Ben Vanik512d2d32019-09-20 13:22:34 -0700178
Lei Zhang0d281b72020-06-01 20:00:23 -0400179#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400180# IREE compilation toolchain configuration
181#-------------------------------------------------------------------------------
182
183# Enable using lld as the linker for C/C++ targets. This affects IREE and all
184# dependency projects.
185option(IREE_ENABLE_LLD "Use lld when linking" OFF)
186option(IREE_ENABLE_ASAN "Enable address sanitizer" OFF)
187option(IREE_ENABLE_MSAN "Enable memory sanitizer" OFF)
188option(IREE_ENABLE_TSAN "Enable thread sanitizer" OFF)
bjacobe694d952020-11-03 12:05:35 -0500189option(IREE_ENABLE_CCACHE "Use ccache if installed to speed up rebuilds." OFF)
190
Ben Vanik85cdd862020-11-14 11:26:12 -0800191if(${IREE_ENABLE_CCACHE})
bjacobe694d952020-11-03 12:05:35 -0500192 find_program(CCACHE_PROGRAM ccache)
193 if(CCACHE_PROGRAM)
194 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
195 endif()
196endif()
Lei Zhange88470f2020-09-08 13:21:09 -0400197
Geoffrey Martin-Noble3fa4f8d2021-09-10 09:25:27 -0700198option(IREE_DEV_MODE "Configure settings to optimize for IREE development (as opposed to CI or release)" OFF)
Stella Laurenzo5b639122021-06-18 14:44:10 -0700199
200#-------------------------------------------------------------------------------
201# IREE assertions
202# We don't love the way this is done, but we have to line it up with how LLVM
203# does it and not diverge, since all implementations and all header users must
204# have the same definition of NDEBUG.
205#
206# LLVM defaults LLVM_ENABLE_ASSERTIONS to ON for Debug builds only but then
207# conditions itself to only update flags if not building Debug. We just let
208# IREE_ENABLE_ASSERTIONS be not conditioned on anything and only update the
209# flags in appropriate build types.
210#
211# If IREE_ENABLE_ASSERTIONS is set ON manually, then
212# - NDEBUG must be undefined
213# - LLVM_ENABLE_ASSERTIONS is forced off in order to keep multiple parties
214# from mucking with globals.
215#
216# Since CMake forces NDEBUG for !Debug builds, some surgery needs to be done
217# at the top level to avoid divergence.
218#-------------------------------------------------------------------------------
219
220option(IREE_ENABLE_ASSERTIONS "Force unset of NDEBUG compile option" OFF)
221
222# Filter -DNDEBUG from CMAKE_CXX_FLAGS_* and CMAKE_C_FLAGS_* (if
223# CMAKE_BUILD_TYPE is not Debug).
224function(iree_fix_ndebug)
225 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
226 if(IREE_ENABLE_ASSERTIONS AND NOT "${uppercase_CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
227 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
228 foreach (flags_var_to_scrub
229 CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE}
230 CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE})
231 set(original_flags "${${flags_var_to_scrub}}")
232 string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
233 altered_flags "${original_flags}")
234 if(NOT "${original_flags}" STREQUAL "${altered_flags}")
235 message(STATUS
236 "IREE_ENABLE_ASSERTIONS force disabled NDEBUG for ${flags_var_to_scrub}: '${original_flags}' -> '${altered_flags}'")
237 set(${flags_var_to_scrub} "${altered_flags}" PARENT_SCOPE)
238 endif()
239 endforeach()
240
241 # Make sure that LLVM doesn't add its own logic for assertion disabling.
242 # We'd like to make sure that we are not dueling over globals.
243 set(LLVM_ENABLE_ASSERTIONS OFF PARENT_SCOPE)
244 endif()
245endfunction()
246iree_fix_ndebug()
247
Lei Zhange88470f2020-09-08 13:21:09 -0400248#-------------------------------------------------------------------------------
Lei Zhang7e253da2020-06-10 07:51:19 -0700249# IREE utility definitions
Lei Zhang0d281b72020-06-01 20:00:23 -0400250#-------------------------------------------------------------------------------
251
Ben Vanik512d2d32019-09-20 13:22:34 -0700252include(iree_macros)
Ben Vanik185d30c2019-09-19 14:24:11 -0700253include(iree_copts)
bjacobc12ba3b2020-11-03 10:32:00 -0500254include(sanitizers)
Ben Vanik6b112ef2019-10-03 10:45:14 -0700255include(iree_cc_binary)
Ben Vanik185d30c2019-09-19 14:24:11 -0700256include(iree_cc_library)
257include(iree_cc_test)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700258include(iree_tablegen_library)
Lei Zhang22f0e242020-03-30 12:09:20 -0700259include(iree_tablegen_doc)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700260include(iree_third_party_cmake_options)
CindyLiu40ed02d2021-04-22 16:16:28 +0000261include(iree_c_embed_data)
Scott Todd11adcab2019-12-18 14:10:44 -0800262include(iree_bytecode_module)
Marius Brehler46e83312021-03-25 00:11:39 +0100263include(iree_c_module)
Stella Laurenzo94363e22020-12-15 13:46:14 -0800264include(iree_python)
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800265include(iree_lit_test)
Geoffrey Martin-Noble4526dcc2020-03-09 11:59:52 -0700266include(iree_add_all_subdirs)
Geoffrey Martin-Noblee5fd5b52020-03-31 11:31:30 -0700267include(iree_check_test)
Geoffrey Martin-Noble48896122021-01-05 21:04:18 -0800268include(iree_run_binary_test)
Lei Zhangfe4403e2021-06-01 15:09:43 -0400269include(iree_mlir_benchmark_suite)
Ben Vanik185d30c2019-09-19 14:24:11 -0700270
Marius Brehlerc4b6b912020-01-15 08:44:23 -0800271set(DEFAULT_CMAKE_BUILD_TYPE "Release")
Ben Vanik85cdd862020-11-14 11:26:12 -0800272if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Marius Brehlerc4b6b912020-01-15 08:44:23 -0800273 message(STATUS "No build type selected, default to ${DEFAULT_CMAKE_BUILD_TYPE}")
274 set(CMAKE_BUILD_TYPE "${DEFAULT_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
275endif()
276
Marius Brehler06ac36e2020-01-10 14:44:11 -0800277set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
278
Ben Vanik185d30c2019-09-19 14:24:11 -0700279#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400280# IREE compilation flags
Scott Todd29d654e2020-06-11 15:24:17 -0700281#-------------------------------------------------------------------------------
282
Lei Zhangdd21f322020-09-10 10:47:33 -0400283iree_append_list_to_string(CMAKE_C_FLAGS_DEBUG ${IREE_C_FLAGS_DEBUG_LIST})
284iree_append_list_to_string(CMAKE_CXX_FLAGS_DEBUG ${IREE_CXX_FLAGS_DEBUG_LIST})
Scott Todd29d654e2020-06-11 15:24:17 -0700285
286set(CMAKE_CXX_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C++ compiler during fast builds." FORCE)
287set(CMAKE_C_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C compiler during fast builds." FORCE)
288set(CMAKE_EXE_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used for linking binaries during fast builds." FORCE)
289set(CMAKE_SHARED_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used by the shared libraries linker binaries during fast builds." FORCE)
290mark_as_advanced(
291 CMAKE_CXX_FLAGS_FASTBUILD
292 CMAKE_C_FLAGS_FASTBUILD
293 CMAKE_EXE_LINKER_FLAGS_FASTBUILD
294 CMAKE_SHARED_LINKER_FLAGS_FASTBUILD
295)
296
297include(iree_setup_toolchain)
298
299#-------------------------------------------------------------------------------
Stella Laurenzo29032b82021-10-14 15:21:44 -0700300# Python
301# If building features that require Python development, find them early in
302# one invocation (some CMake versions are sensitive to resolving out of order).
303# Otherwise, for features that just require the interpreter, find that alone.
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800304#-------------------------------------------------------------------------------
305
Stella Laurenzo29032b82021-10-14 15:21:44 -0700306if(IREE_BUILD_PYTHON_BINDINGS)
307 # After CMake 3.18, we are able to limit the scope of the search to just
308 # Development.Module. Searching for Development will fail in situations where
309 # the Python libraries are not available. When possible, limit to just
310 # Development.Module.
311 # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
312 if(CMAKE_VERSION VERSION_LESS "3.18.0")
313 message(WARNING
314 "This version of CMake is not compatible with statically built Python "
315 "installations. If Python fails to detect below this may apply to you. "
316 "Recommend upgrading to at least CMake 3.18. "
317 "Detected current version: ${CMAKE_VERSION}"
318 )
319 find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
320 else()
321 find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)
322 endif()
323elseif(IREE_BUILD_COMPILER)
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800324 find_package(Python3 COMPONENTS Interpreter REQUIRED)
325endif()
326
327#-------------------------------------------------------------------------------
Niloy Sikdar47238df2021-07-28 23:37:33 +0530328# Check if git submodules have been initialized.
329# This will only run if python3 is available.
330#-------------------------------------------------------------------------------
331
332find_package(Python3 COMPONENTS Interpreter QUIET)
333if(Python3_FOUND)
334 execute_process(
335 COMMAND ${Python3_EXECUTABLE} scripts/git/check_submodule_init.py
336 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
337 RESULT_VARIABLE ret
338 )
339 if(NOT ret EQUAL "0")
340 message(FATAL_ERROR "check_submodule_init.py failed, see the logs above")
341 endif()
342endif()
343
344#-------------------------------------------------------------------------------
Stella Laurenzo382122d2020-06-11 16:18:09 -0700345# MLIR/LLVM Dependency
Stella Laurenzo382122d2020-06-11 16:18:09 -0700346#-------------------------------------------------------------------------------
347
Stella Laurenzo275215d2021-08-06 11:50:28 -0700348if(NOT IREE_BUILD_COMPILER)
349 message(STATUS "Not adding LLVM/MLIR because the configuration does not require it")
350elseif(TARGET LLVMSupport)
351 message(STATUS "Not adding IREE bundled LLVM because it has already been included")
352 if(NOT TARGET MLIRIR)
353 message(FATAL_ERROR "Detected externally provided LLVM project but could not find MLIR projects (is it enabled/installed?)")
Stella Laurenzo382122d2020-06-11 16:18:09 -0700354 endif()
Stella Laurenzo275215d2021-08-06 11:50:28 -0700355else()
356 message(STATUS "Adding bundled LLVM source dependency")
357 iree_set_llvm_cmake_options()
Stella Laurenzo382122d2020-06-11 16:18:09 -0700358
Stella Laurenzo688670f2021-09-24 18:16:25 -0700359 # Enable MLIR Python bindings if IREE Python bindings enabled.
360 if(IREE_BUILD_PYTHON_BINDINGS)
361 set(MLIR_ENABLE_BINDINGS_PYTHON ON CACHE BOOL "" FORCE)
362 set(MHLO_ENABLE_BINDINGS_PYTHON ON CACHE BOOL "" FORCE)
363 endif()
364
Stella Laurenzo275215d2021-08-06 11:50:28 -0700365 # Disable LLVM's warnings.
366 set(LLVM_ENABLE_WARNINGS OFF CACHE BOOL "don't use global flags /facepalm")
367
Ben Vanik89a77fa2020-10-07 17:19:31 -0700368 # Stash cmake build type in case LLVM messes with it.
369 set(_CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
370
Stella Laurenzo62fbef02021-08-09 20:52:48 -0700371 # Add default external projects.
Stella Laurenzo02230c12021-08-10 03:58:42 +0000372 iree_add_llvm_external_project(mlir-iree-dialects MLIR_IREE_DIALECTS ${CMAKE_CURRENT_SOURCE_DIR}/llvm-external-projects/iree-dialects)
Stella Laurenzo62fbef02021-08-09 20:52:48 -0700373 iree_add_llvm_external_project(mlir-hlo MLIR_HLO ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mlir-hlo)
374
Stella Laurenzo275215d2021-08-06 11:50:28 -0700375 add_subdirectory("third_party/llvm-project/llvm" EXCLUDE_FROM_ALL)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700376
Ben Vanik89a77fa2020-10-07 17:19:31 -0700377 # Reset CMAKE_BUILD_TYPE to its previous setting.
Stella Laurenzo382122d2020-06-11 16:18:09 -0700378 set(CMAKE_BUILD_TYPE "${_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700379
Stella Laurenzo275215d2021-08-06 11:50:28 -0700380 # Extend module path to allow submodules to use LLVM and MLIR CMake modules.
381 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/mlir")
382 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/lib/cmake/llvm/")
Marius Brehler71f76cb2020-11-10 20:56:55 +0100383
Stella Laurenzo275215d2021-08-06 11:50:28 -0700384 # Add the bundled include directories for cmake files looking for them.
385 list(APPEND LLVM_INCLUDE_DIRS
386 ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/llvm/include
387 ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/include
388 )
389 list(APPEND MLIR_INCLUDE_DIRS
390 ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/mlir/include
391 ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include
392 )
Marius Brehler71f76cb2020-11-10 20:56:55 +0100393
Stella Laurenzo62fbef02021-08-09 20:52:48 -0700394 function(_hack_llvm_include_paths)
395 set(_common_include_dirs
396 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/llvm/include>
397 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/include>
398 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/mlir/include>
399 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include>
400 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir-hlo/include>
401 )
402 # Avoid globally modifying paths by instead adding the include paths to the
403 # rules that really should have them in the first place.
404 target_include_directories(LLVMSupport PUBLIC ${_common_include_dirs})
405 target_include_directories(MLIRSupport PUBLIC ${_common_include_dirs})
406 endfunction()
407 _hack_llvm_include_paths()
Stella Laurenzo382122d2020-06-11 16:18:09 -0700408endif()
409
Stella Laurenzo382122d2020-06-11 16:18:09 -0700410#-------------------------------------------------------------------------------
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800411# Other dependencies.
412#-------------------------------------------------------------------------------
413
Ben Vanik512d2d32019-09-20 13:22:34 -0700414include(external_cc_library)
Ben Vanik2d1808b2020-07-17 19:02:16 -0700415include(flatbuffer_c_library)
Ben Vanik512d2d32019-09-20 13:22:34 -0700416
Thomas3079fb12021-05-12 20:31:20 -0700417add_subdirectory(build_tools/third_party/cuda EXCLUDE_FROM_ALL)
Ben Vanika9036492021-06-25 09:39:53 -0700418add_subdirectory(build_tools/third_party/libyaml EXCLUDE_FROM_ALL)
CindyLiu807f4572021-05-12 23:41:41 +0000419add_subdirectory(build_tools/third_party/stblib EXCLUDE_FROM_ALL)
Ben Vanika9036492021-06-25 09:39:53 -0700420add_subdirectory(build_tools/third_party/vulkan_memory_allocator EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700421
Stella Laurenzo275215d2021-08-06 11:50:28 -0700422iree_set_googletest_cmake_options()
Marius Brehlerf5022e82019-12-13 15:20:25 -0800423add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700424
425if(IREE_ENABLE_THREADING)
426 iree_set_benchmark_cmake_options()
Cindy Liu331946c2021-06-01 12:20:30 -0700427 add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700428 iree_set_cpuinfo_cmake_options()
Cindy Liu331946c2021-06-01 12:20:30 -0700429 add_subdirectory(third_party/cpuinfo EXCLUDE_FROM_ALL)
Cindy Liu331946c2021-06-01 12:20:30 -0700430endif()
Stella Laurenzo275215d2021-08-06 11:50:28 -0700431
432iree_set_flatcc_cmake_options()
433add_subdirectory(build_tools/third_party/flatcc EXCLUDE_FROM_ALL)
Ben Vanik71527c22020-07-17 13:09:57 -0700434add_subdirectory(third_party/flatcc EXCLUDE_FROM_ALL)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700435
Ben Vanik512d2d32019-09-20 13:22:34 -0700436add_subdirectory(third_party/vulkan_headers EXCLUDE_FROM_ALL)
Ben Vanik185d30c2019-09-19 14:24:11 -0700437
Scott Todd4e1a69b2021-01-25 15:29:49 -0800438# TODO(scotttodd): Iterate some more and find a better place for this.
439if (NOT CMAKE_CROSSCOMPILING)
440 install(TARGETS iree-flatcc-cli
441 COMPONENT iree-flatcc-cli
442 RUNTIME DESTINATION bin)
Lei Zhang0d281b72020-06-01 20:00:23 -0400443endif()
444
Stella Laurenzo275215d2021-08-06 11:50:28 -0700445if(IREE_BUILD_COMPILER)
Marius Brehler3eaf4442021-01-13 18:22:08 +0100446 add_subdirectory(build_tools/third_party/mlir-hlo EXCLUDE_FROM_ALL)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700447endif()
448
Stella Laurenzo275215d2021-08-06 11:50:28 -0700449if(IREE_ENABLE_EMITC)
Marius Brehler75d4bb82021-07-14 10:59:20 +0000450 add_subdirectory(build_tools/third_party/mlir-emitc EXCLUDE_FROM_ALL)
451endif()
452
Stella Laurenzo275215d2021-08-06 11:50:28 -0700453if(IREE_BUILD_TESTS)
Marius Brehler575b63a2020-01-07 09:39:24 -0800454 enable_testing(iree)
Marius Brehler29676502019-12-27 17:07:10 -0800455endif()
456
MaheshRavishankar1547ac22021-09-23 15:22:46 -0700457if(IREE_BUILD_PYTHON_BINDINGS)
Stella Laurenzo95ad84c2021-09-24 14:56:01 -0700458 if(NOT TARGET pybind11::module)
459 message(STATUS "Using bundled pybind11")
460 add_subdirectory(third_party/pybind11 EXCLUDE_FROM_ALL)
461 else()
462 message(STATUS "Not including bundled pybind11 (already configured)")
463 endif()
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800464endif()
465
Stella Laurenzo275215d2021-08-06 11:50:28 -0700466if(IREE_TARGET_BACKEND_METAL-SPIRV)
467 iree_set_spirv_cross_cmake_options()
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400468 # SPIRV-Cross is needed to cross compile SPIR-V into MSL source code.
469 add_subdirectory(third_party/spirv_cross EXCLUDE_FROM_ALL)
470endif()
471
Ben Vanik185d30c2019-09-19 14:24:11 -0700472#-------------------------------------------------------------------------------
Lei Zhang22f0e242020-03-30 12:09:20 -0700473# IREE top-level targets
474#-------------------------------------------------------------------------------
475
Lei Zhangfe4403e2021-06-01 15:09:43 -0400476if(${IREE_BUILD_BENCHMARKS})
477 # Add a top-level custom target to drive generating benchmark suites.
478 add_custom_target(iree-benchmark-suites)
479endif()
480
Lei Zhang22f0e242020-03-30 12:09:20 -0700481if(${IREE_BUILD_DOCS})
482 # Add a top-level custom target to drive generating all documentation.
483 # Register it to the default target given that IREE_BUILD_DOCS is explicitly
484 # requested.
485 add_custom_target(iree-doc ALL)
486endif()
487
488#-------------------------------------------------------------------------------
Ben Vanik185d30c2019-09-19 14:24:11 -0700489# IREE top-level libraries
490#-------------------------------------------------------------------------------
491
Marius Brehlerf5022e82019-12-13 15:20:25 -0800492add_subdirectory(build_tools/embed_data/)
493
Ben Vanik185d30c2019-09-19 14:24:11 -0700494add_subdirectory(iree/base)
Ben Vanik512d2d32019-09-20 13:22:34 -0700495add_subdirectory(iree/hal)
Marius Brehler9317d1f2020-01-08 10:34:11 -0800496add_subdirectory(iree/modules)
Ben Vanikb69d0cc2021-05-05 18:16:22 -0700497add_subdirectory(iree/runtime)
Ben Vanik512d2d32019-09-20 13:22:34 -0700498add_subdirectory(iree/schemas)
Ben Vanik01600a52020-12-15 17:18:04 -0800499add_subdirectory(iree/task)
Marius Brehler29676502019-12-27 17:07:10 -0800500add_subdirectory(iree/testing)
Marius Brehler476031a2020-03-24 16:14:30 -0700501add_subdirectory(iree/test)
not-jennibd3610e2020-12-23 17:00:22 -0800502add_subdirectory(iree/vm)
Lei Zhangff38a682021-06-08 21:12:24 -0400503
504if(${IREE_BUILD_BENCHMARKS})
Lei Zhang08cea042021-07-20 12:44:01 -0400505 add_subdirectory(benchmarks)
Lei Zhangff38a682021-06-08 21:12:24 -0400506endif()
507
raikonenfnu4b31bd92021-05-18 18:20:25 -0700508if(${IREE_BUILD_EXPERIMENTAL_ROCM})
509 add_subdirectory(build_tools/third_party/rocm EXCLUDE_FROM_ALL)
510 add_subdirectory(experimental/rocm)
raikonenfnu4b31bd92021-05-18 18:20:25 -0700511endif()
Ben Vanikcc2aff92019-09-24 10:23:55 -0700512
513if(${IREE_BUILD_COMPILER})
514 add_subdirectory(iree/compiler)
515endif()
Ben Vanik6b112ef2019-10-03 10:45:14 -0700516
517add_subdirectory(iree/tools)
518
Stella Laurenzo77a63cd2021-01-04 17:29:54 -0800519# Order constraint: The python bindings install tools targets from iree/tools
520# and must come after it.
521if(${IREE_BUILD_PYTHON_BINDINGS})
522 add_subdirectory(bindings/python)
523endif()
524
Ben Vanikcd1132f2021-01-29 15:58:17 -0800525if(${IREE_BUILD_BINDINGS_TFLITE})
526 add_subdirectory(bindings/tflite)
527endif()
528
Ben Vanik6b112ef2019-10-03 10:45:14 -0700529if(${IREE_BUILD_SAMPLES})
530 add_subdirectory(iree/samples)
531endif()
Ben Vanikb64e9182020-01-30 15:19:37 -0800532
Stella Laurenzo0f005e52020-12-27 11:54:33 -0800533if(${IREE_BUILD_EXPERIMENTAL_REMOTING})
534 # NOTE: Currently liburing is only used by the experimental remoting
535 # support, so keeping it scoped here. If this broadens, then include along
536 # with other dependencies as normal.
537 if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
538 message(STATUS "Enabling liburing")
539 add_subdirectory(build_tools/third_party/liburing EXCLUDE_FROM_ALL)
540 endif()
541 add_subdirectory(experimental/remoting)
Ben Vanikb64e9182020-01-30 15:19:37 -0800542endif()
Scott Toddee30e1b2020-02-03 16:44:15 -0800543
not-jenni603e9f12021-01-26 16:49:18 -0800544if(${IREE_BUILD_EXPERIMENTAL_JAVA_BINDINGS})
545 add_subdirectory(experimental/bindings/java)
546endif()
547
Stella Laurenzoa4137ef2020-12-09 21:28:08 -0800548if(${IREE_ENABLE_TENSORFLOW})
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800549 add_subdirectory(integrations/tensorflow)
550endif()
551
Stella Laurenzo03e48db2020-06-11 18:35:13 -0700552set(IREE_PUBLIC_INCLUDE_DIRS "${IREE_COMMON_INCLUDE_DIRS}"
553 CACHE INTERNAL "IREE: Include Directories" FORCE)
Stella Laurenzo688670f2021-09-24 18:16:25 -0700554
555# Include the iree-compiler-api sub-project. We do this for development
556# and CI so that developers have access to the API and tools it provides
557# (otherwise, they would need to build multiple top-level projects).
558# However, logically, iree-compiler-api depends on iree, and for deployment
559# is always built standalone, taking responsibility to include iree and LLVM
560# as sub-projects.
561# The dependency mode is controlled by the variables
562# IREE_COMPILER_API_STANDALONE, which will be set if iree-compiler-api is
563# top-level. Otherwise, we set IREE_COMPILER_API_SUB_PROJECT, indicating it
564# is being embedded as a sub project.
565if(IREE_BUILD_COMPILER AND IREE_BUILD_PYTHON_BINDINGS)
566 if(NOT IREE_COMPILER_API_STANDALONE)
567 message(STATUS "Including iree-compiler-api as a sub-project")
568 set(IREE_COMPILER_API_SUB_PROJECT ON)
569 add_subdirectory(llvm-external-projects/iree-compiler-api
570 "${CMAKE_CURRENT_BINARY_DIR}/compiler-api")
Stella Laurenzo48a43302021-09-28 17:49:33 -0700571
572 # Write out a .env file to make IDEs and developers happy.
573 # Yes, we are writing this to the source dir. It is only for IDEs and if
574 # it gets clobbered, it is fine.
575 set(_pythonpath_env "PYTHONPATH=$<SHELL_PATH:${CMAKE_CURRENT_BINARY_DIR}/compiler-api/python_package;${CMAKE_CURRENT_BINARY_DIR}/bindings/python>\n")
576 file(GENERATE OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/.env"
577 CONTENT "${_pythonpath_env}"
578 )
579 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env"
580 CONTENT "${_pythonpath_env}"
581 )
582
583 # TODO: Remove this after about Dec-2021
584 if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/bindings/python/iree/compiler")
585 message(FATAL_ERROR "You are running in a build directory which needs some manual cleanup. Please delete the directory ${CMAKE_CURRENT_BINARY_DIR}/bindings/python/iree/compiler")
586 endif()
Stella Laurenzo688670f2021-09-24 18:16:25 -0700587 endif()
588endif()