blob: 3e7469d1c8ce76e6e1fbae8622370c1a1a7ab317 [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
Marius Brehler59dcd0d2021-02-09 20:44:44 +01007cmake_minimum_required(VERSION 3.13.4)
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
Ben Vanik96fd2772021-04-19 20:30:56 -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
Ben Vanik0f14db02021-04-19 23:29:29 -0700107 VMVX
Scott Todd6132bb32020-08-11 12:04:40 -0700108 Vulkan
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700109)
110
Ben Vanik85cdd862020-11-14 11:26:12 -0800111if(IREE_HAL_DRIVERS_TO_BUILD STREQUAL "all")
112 set(IREE_HAL_DRIVERS_TO_BUILD ${IREE_ALL_HAL_DRIVERS})
Lei Zhang6171a982020-09-08 12:48:25 -0400113
114 # For Apple platforms we need to use Metal instead of Vulkan.
115 if(APPLE)
116 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Vulkan)
Lei Zhang6171a982020-09-08 12:48:25 -0400117 endif()
Thomasa98c3c02021-02-16 09:56:03 -0800118 # Remove Cuda from Android and Apple platforms.
119 if(ANDROID OR APPLE)
120 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Cuda)
121 endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700122endif()
Scott Todd6132bb32020-08-11 12:04:40 -0700123message(STATUS "Building HAL drivers: ${IREE_HAL_DRIVERS_TO_BUILD}")
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700124
125# Default every IREE_HAL_DRIVER_* to OFF
126foreach(_backend ${IREE_ALL_HAL_DRIVERS})
127 string(TOUPPER "${_backend}" uppercase_backend)
128 set(IREE_HAL_DRIVER_${uppercase_backend} OFF CACHE BOOL "" FORCE)
129endforeach()
130
131# Set IREE_HAL_DRIVER_* based on configuration
132foreach(_backend ${IREE_HAL_DRIVERS_TO_BUILD})
133 string(TOUPPER "${_backend}" uppercase_backend)
Marius Brehler30f06a02020-12-18 13:39:26 +0000134 string(REPLACE "\"" "" uppercase_backend ${uppercase_backend})
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700135 set(IREE_HAL_DRIVER_${uppercase_backend} ON CACHE BOOL "" FORCE)
136endforeach()
137
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700138# List of all target backends to be built by default:
139set(IREE_ALL_TARGET_BACKENDS
Thomasd85738e2021-02-23 11:02:55 -0800140 CUDA
Scott Todd9f0148f2021-03-17 15:14:07 -0700141 DYLIB-LLVM-AOT
142 WASM-LLVM-AOT
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400143 Metal-SPIRV
harsh-nodf6d36fe2021-05-17 15:38:31 -0700144 ROCM
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400145 Vulkan-SPIRV
Ben Vanik907f3532021-04-20 13:27:00 -0700146 VMVX
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700147)
148
Scott Todd28cacaa2021-06-29 09:31:46 -0700149if(${IREE_BUILD_COMPILER})
150 if(IREE_TARGET_BACKENDS_TO_BUILD STREQUAL "all")
151 set(IREE_TARGET_BACKENDS_TO_BUILD ${IREE_ALL_TARGET_BACKENDS})
152 endif()
153 message(STATUS "Building target backends: ${IREE_TARGET_BACKENDS_TO_BUILD}")
154else()
155 set(IREE_TARGET_BACKENDS_TO_BUILD "" CACHE STRING "" FORCE)
156 message(STATUS "Compiler is disabled, building no target backends")
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700157endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700158
159# Default every IREE_TARGET_BACKEND_* to OFF
160foreach(_backend ${IREE_ALL_TARGET_BACKENDS})
161 string(TOUPPER "${_backend}" uppercase_backend)
162 set(IREE_TARGET_BACKEND_${uppercase_backend} OFF CACHE BOOL "" FORCE)
163endforeach()
164
165# Set IREE_TARGET_BACKEND_* based on configuration
166foreach(_backend ${IREE_TARGET_BACKENDS_TO_BUILD})
167 string(TOUPPER "${_backend}" uppercase_backend)
Marius Brehler30f06a02020-12-18 13:39:26 +0000168 string(REPLACE "\"" "" uppercase_backend ${uppercase_backend})
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700169 set(IREE_TARGET_BACKEND_${uppercase_backend} ON CACHE BOOL "" FORCE)
170endforeach()
171
Ben Vanik185d30c2019-09-19 14:24:11 -0700172list(APPEND CMAKE_MODULE_PATH
173 ${CMAKE_CURRENT_LIST_DIR}/build_tools/cmake/
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800174 ${CMAKE_CURRENT_LIST_DIR}/bindings/python/build_tools/cmake/
Ben Vanik185d30c2019-09-19 14:24:11 -0700175)
Ben Vanik512d2d32019-09-20 13:22:34 -0700176
Lei Zhang0d281b72020-06-01 20:00:23 -0400177#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400178# IREE compilation toolchain configuration
179#-------------------------------------------------------------------------------
180
181# Enable using lld as the linker for C/C++ targets. This affects IREE and all
182# dependency projects.
183option(IREE_ENABLE_LLD "Use lld when linking" OFF)
184option(IREE_ENABLE_ASAN "Enable address sanitizer" OFF)
185option(IREE_ENABLE_MSAN "Enable memory sanitizer" OFF)
186option(IREE_ENABLE_TSAN "Enable thread sanitizer" OFF)
bjacobe694d952020-11-03 12:05:35 -0500187option(IREE_ENABLE_CCACHE "Use ccache if installed to speed up rebuilds." OFF)
188
Ben Vanik85cdd862020-11-14 11:26:12 -0800189if(${IREE_ENABLE_CCACHE})
bjacobe694d952020-11-03 12:05:35 -0500190 find_program(CCACHE_PROGRAM ccache)
191 if(CCACHE_PROGRAM)
192 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
193 endif()
194endif()
Lei Zhange88470f2020-09-08 13:21:09 -0400195
Stella Laurenzo5b639122021-06-18 14:44:10 -0700196
197#-------------------------------------------------------------------------------
198# IREE assertions
199# We don't love the way this is done, but we have to line it up with how LLVM
200# does it and not diverge, since all implementations and all header users must
201# have the same definition of NDEBUG.
202#
203# LLVM defaults LLVM_ENABLE_ASSERTIONS to ON for Debug builds only but then
204# conditions itself to only update flags if not building Debug. We just let
205# IREE_ENABLE_ASSERTIONS be not conditioned on anything and only update the
206# flags in appropriate build types.
207#
208# If IREE_ENABLE_ASSERTIONS is set ON manually, then
209# - NDEBUG must be undefined
210# - LLVM_ENABLE_ASSERTIONS is forced off in order to keep multiple parties
211# from mucking with globals.
212#
213# Since CMake forces NDEBUG for !Debug builds, some surgery needs to be done
214# at the top level to avoid divergence.
215#-------------------------------------------------------------------------------
216
217option(IREE_ENABLE_ASSERTIONS "Force unset of NDEBUG compile option" OFF)
218
219# Filter -DNDEBUG from CMAKE_CXX_FLAGS_* and CMAKE_C_FLAGS_* (if
220# CMAKE_BUILD_TYPE is not Debug).
221function(iree_fix_ndebug)
222 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
223 if(IREE_ENABLE_ASSERTIONS AND NOT "${uppercase_CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
224 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
225 foreach (flags_var_to_scrub
226 CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE}
227 CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE})
228 set(original_flags "${${flags_var_to_scrub}}")
229 string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
230 altered_flags "${original_flags}")
231 if(NOT "${original_flags}" STREQUAL "${altered_flags}")
232 message(STATUS
233 "IREE_ENABLE_ASSERTIONS force disabled NDEBUG for ${flags_var_to_scrub}: '${original_flags}' -> '${altered_flags}'")
234 set(${flags_var_to_scrub} "${altered_flags}" PARENT_SCOPE)
235 endif()
236 endforeach()
237
238 # Make sure that LLVM doesn't add its own logic for assertion disabling.
239 # We'd like to make sure that we are not dueling over globals.
240 set(LLVM_ENABLE_ASSERTIONS OFF PARENT_SCOPE)
241 endif()
242endfunction()
243iree_fix_ndebug()
244
Lei Zhange88470f2020-09-08 13:21:09 -0400245#-------------------------------------------------------------------------------
Lei Zhang7e253da2020-06-10 07:51:19 -0700246# IREE utility definitions
Lei Zhang0d281b72020-06-01 20:00:23 -0400247#-------------------------------------------------------------------------------
248
Ben Vanik512d2d32019-09-20 13:22:34 -0700249include(iree_macros)
Ben Vanik185d30c2019-09-19 14:24:11 -0700250include(iree_copts)
bjacobc12ba3b2020-11-03 10:32:00 -0500251include(sanitizers)
Ben Vanik6b112ef2019-10-03 10:45:14 -0700252include(iree_cc_binary)
Ben Vanik185d30c2019-09-19 14:24:11 -0700253include(iree_cc_library)
254include(iree_cc_test)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700255include(iree_tablegen_library)
Lei Zhang22f0e242020-03-30 12:09:20 -0700256include(iree_tablegen_doc)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700257include(iree_third_party_cmake_options)
CindyLiu40ed02d2021-04-22 16:16:28 +0000258include(iree_c_embed_data)
Scott Todd11adcab2019-12-18 14:10:44 -0800259include(iree_bytecode_module)
Marius Brehler46e83312021-03-25 00:11:39 +0100260include(iree_c_module)
Stella Laurenzo94363e22020-12-15 13:46:14 -0800261include(iree_python)
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800262include(iree_lit_test)
Geoffrey Martin-Noble4526dcc2020-03-09 11:59:52 -0700263include(iree_add_all_subdirs)
Geoffrey Martin-Noblee5fd5b52020-03-31 11:31:30 -0700264include(iree_check_test)
Geoffrey Martin-Noble48896122021-01-05 21:04:18 -0800265include(iree_run_binary_test)
Lei Zhangfe4403e2021-06-01 15:09:43 -0400266include(iree_mlir_benchmark_suite)
Ben Vanik185d30c2019-09-19 14:24:11 -0700267
Marius Brehlerc4b6b912020-01-15 08:44:23 -0800268set(DEFAULT_CMAKE_BUILD_TYPE "Release")
Ben Vanik85cdd862020-11-14 11:26:12 -0800269if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Marius Brehlerc4b6b912020-01-15 08:44:23 -0800270 message(STATUS "No build type selected, default to ${DEFAULT_CMAKE_BUILD_TYPE}")
271 set(CMAKE_BUILD_TYPE "${DEFAULT_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
272endif()
273
Marius Brehler06ac36e2020-01-10 14:44:11 -0800274set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
275
Ben Vanik185d30c2019-09-19 14:24:11 -0700276#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400277# IREE compilation flags
Scott Todd29d654e2020-06-11 15:24:17 -0700278#-------------------------------------------------------------------------------
279
Lei Zhangdd21f322020-09-10 10:47:33 -0400280iree_append_list_to_string(CMAKE_C_FLAGS_DEBUG ${IREE_C_FLAGS_DEBUG_LIST})
281iree_append_list_to_string(CMAKE_CXX_FLAGS_DEBUG ${IREE_CXX_FLAGS_DEBUG_LIST})
Scott Todd29d654e2020-06-11 15:24:17 -0700282
283set(CMAKE_CXX_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C++ compiler during fast builds." FORCE)
284set(CMAKE_C_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C compiler during fast builds." FORCE)
285set(CMAKE_EXE_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used for linking binaries during fast builds." FORCE)
286set(CMAKE_SHARED_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used by the shared libraries linker binaries during fast builds." FORCE)
287mark_as_advanced(
288 CMAKE_CXX_FLAGS_FASTBUILD
289 CMAKE_C_FLAGS_FASTBUILD
290 CMAKE_EXE_LINKER_FLAGS_FASTBUILD
291 CMAKE_SHARED_LINKER_FLAGS_FASTBUILD
292)
293
294include(iree_setup_toolchain)
295
296#-------------------------------------------------------------------------------
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800297# Configure python early if there are any features that need it.
298# Note that doing this early ensures that dependencies that make incidental
299# use of Python (such as LLVM) resolve the same version.
300#-------------------------------------------------------------------------------
301
302if(${IREE_BUILD_COMPILER} OR
Stella Laurenzoa4137ef2020-12-09 21:28:08 -0800303 ${IREE_BUILD_PYTHON_BINDINGS})
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800304 find_package(Python3 COMPONENTS Interpreter REQUIRED)
305endif()
306
307#-------------------------------------------------------------------------------
Niloy Sikdar47238df2021-07-28 23:37:33 +0530308# Check if git submodules have been initialized.
309# This will only run if python3 is available.
310#-------------------------------------------------------------------------------
311
312find_package(Python3 COMPONENTS Interpreter QUIET)
313if(Python3_FOUND)
314 execute_process(
315 COMMAND ${Python3_EXECUTABLE} scripts/git/check_submodule_init.py
316 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
317 RESULT_VARIABLE ret
318 )
319 if(NOT ret EQUAL "0")
320 message(FATAL_ERROR "check_submodule_init.py failed, see the logs above")
321 endif()
322endif()
323
324#-------------------------------------------------------------------------------
Stella Laurenzo382122d2020-06-11 16:18:09 -0700325# MLIR/LLVM Dependency
Stella Laurenzo382122d2020-06-11 16:18:09 -0700326#-------------------------------------------------------------------------------
327
Stella Laurenzo275215d2021-08-06 11:50:28 -0700328if(NOT IREE_BUILD_COMPILER)
329 message(STATUS "Not adding LLVM/MLIR because the configuration does not require it")
330elseif(TARGET LLVMSupport)
331 message(STATUS "Not adding IREE bundled LLVM because it has already been included")
332 if(NOT TARGET MLIRIR)
333 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 -0700334 endif()
Stella Laurenzo275215d2021-08-06 11:50:28 -0700335else()
336 message(STATUS "Adding bundled LLVM source dependency")
337 iree_set_llvm_cmake_options()
Stella Laurenzo382122d2020-06-11 16:18:09 -0700338
Stella Laurenzo275215d2021-08-06 11:50:28 -0700339 # Disable LLVM's warnings.
340 set(LLVM_ENABLE_WARNINGS OFF CACHE BOOL "don't use global flags /facepalm")
341
Ben Vanik89a77fa2020-10-07 17:19:31 -0700342 # Stash cmake build type in case LLVM messes with it.
343 set(_CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
344
Stella Laurenzo62fbef02021-08-09 20:52:48 -0700345 # Add default external projects.
Stella Laurenzo02230c12021-08-10 03:58:42 +0000346 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 -0700347 iree_add_llvm_external_project(mlir-hlo MLIR_HLO ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mlir-hlo)
348
Stella Laurenzo275215d2021-08-06 11:50:28 -0700349 add_subdirectory("third_party/llvm-project/llvm" EXCLUDE_FROM_ALL)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700350
Ben Vanik89a77fa2020-10-07 17:19:31 -0700351 # Reset CMAKE_BUILD_TYPE to its previous setting.
Stella Laurenzo382122d2020-06-11 16:18:09 -0700352 set(CMAKE_BUILD_TYPE "${_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700353
Stella Laurenzo275215d2021-08-06 11:50:28 -0700354 # Extend module path to allow submodules to use LLVM and MLIR CMake modules.
355 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/mlir")
356 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/lib/cmake/llvm/")
Marius Brehler71f76cb2020-11-10 20:56:55 +0100357
Stella Laurenzo275215d2021-08-06 11:50:28 -0700358 # Add the bundled include directories for cmake files looking for them.
359 list(APPEND LLVM_INCLUDE_DIRS
360 ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/llvm/include
361 ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/include
362 )
363 list(APPEND MLIR_INCLUDE_DIRS
364 ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/mlir/include
365 ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include
366 )
Marius Brehler71f76cb2020-11-10 20:56:55 +0100367
Stella Laurenzo62fbef02021-08-09 20:52:48 -0700368 function(_hack_llvm_include_paths)
369 set(_common_include_dirs
370 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/llvm/include>
371 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/include>
372 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/mlir/include>
373 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include>
374 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir-hlo/include>
375 )
376 # Avoid globally modifying paths by instead adding the include paths to the
377 # rules that really should have them in the first place.
378 target_include_directories(LLVMSupport PUBLIC ${_common_include_dirs})
379 target_include_directories(MLIRSupport PUBLIC ${_common_include_dirs})
380 endfunction()
381 _hack_llvm_include_paths()
Stella Laurenzo382122d2020-06-11 16:18:09 -0700382endif()
383
Stella Laurenzo382122d2020-06-11 16:18:09 -0700384#-------------------------------------------------------------------------------
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800385# Python bindings.
Ben Vanik185d30c2019-09-19 14:24:11 -0700386#-------------------------------------------------------------------------------
387
Stella Laurenzo275215d2021-08-06 11:50:28 -0700388if(IREE_BUILD_PYTHON_BINDINGS)
Stella Laurenzoc8f906c2020-07-21 13:55:50 -0700389 # Note: Optional because python libs can be manually specified.
Marius Brehleraf7b4c32020-11-24 19:19:17 +0100390 find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
Scott Todd4af17b62019-12-17 16:47:00 -0800391endif()
392
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800393#-------------------------------------------------------------------------------
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800394# Other dependencies.
395#-------------------------------------------------------------------------------
396
Ben Vanik512d2d32019-09-20 13:22:34 -0700397include(external_cc_library)
Ben Vanik2d1808b2020-07-17 19:02:16 -0700398include(flatbuffer_c_library)
Ben Vanik512d2d32019-09-20 13:22:34 -0700399
Thomas3079fb12021-05-12 20:31:20 -0700400add_subdirectory(build_tools/third_party/cuda EXCLUDE_FROM_ALL)
Ben Vanika9036492021-06-25 09:39:53 -0700401add_subdirectory(build_tools/third_party/libyaml EXCLUDE_FROM_ALL)
CindyLiu807f4572021-05-12 23:41:41 +0000402add_subdirectory(build_tools/third_party/stblib EXCLUDE_FROM_ALL)
Ben Vanika9036492021-06-25 09:39:53 -0700403add_subdirectory(build_tools/third_party/vulkan_memory_allocator EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700404
Stella Laurenzo275215d2021-08-06 11:50:28 -0700405iree_set_googletest_cmake_options()
Marius Brehlerf5022e82019-12-13 15:20:25 -0800406add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700407
408if(IREE_ENABLE_THREADING)
409 iree_set_benchmark_cmake_options()
Cindy Liu331946c2021-06-01 12:20:30 -0700410 add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700411 iree_set_cpuinfo_cmake_options()
Cindy Liu331946c2021-06-01 12:20:30 -0700412 add_subdirectory(third_party/cpuinfo EXCLUDE_FROM_ALL)
Cindy Liu331946c2021-06-01 12:20:30 -0700413endif()
Stella Laurenzo275215d2021-08-06 11:50:28 -0700414
415iree_set_flatcc_cmake_options()
416add_subdirectory(build_tools/third_party/flatcc EXCLUDE_FROM_ALL)
Ben Vanik71527c22020-07-17 13:09:57 -0700417add_subdirectory(third_party/flatcc EXCLUDE_FROM_ALL)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700418
Ben Vanik512d2d32019-09-20 13:22:34 -0700419add_subdirectory(third_party/vulkan_headers EXCLUDE_FROM_ALL)
Ben Vanik185d30c2019-09-19 14:24:11 -0700420
Scott Todd4e1a69b2021-01-25 15:29:49 -0800421# TODO(scotttodd): Iterate some more and find a better place for this.
422if (NOT CMAKE_CROSSCOMPILING)
423 install(TARGETS iree-flatcc-cli
424 COMPONENT iree-flatcc-cli
425 RUNTIME DESTINATION bin)
Lei Zhang0d281b72020-06-01 20:00:23 -0400426endif()
427
Stella Laurenzo275215d2021-08-06 11:50:28 -0700428if(IREE_BUILD_COMPILER)
Marius Brehler3eaf4442021-01-13 18:22:08 +0100429 add_subdirectory(build_tools/third_party/mlir-hlo EXCLUDE_FROM_ALL)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700430endif()
431
Stella Laurenzo275215d2021-08-06 11:50:28 -0700432if(IREE_ENABLE_EMITC)
Marius Brehler75d4bb82021-07-14 10:59:20 +0000433 add_subdirectory(build_tools/third_party/mlir-emitc EXCLUDE_FROM_ALL)
434endif()
435
Stella Laurenzo275215d2021-08-06 11:50:28 -0700436if(IREE_BUILD_TESTS)
Marius Brehler575b63a2020-01-07 09:39:24 -0800437 enable_testing(iree)
Marius Brehler29676502019-12-27 17:07:10 -0800438endif()
439
Stella Laurenzo275215d2021-08-06 11:50:28 -0700440if(IREE_BUILD_PYTHON_BINDINGS)
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800441 add_subdirectory(third_party/pybind11 EXCLUDE_FROM_ALL)
442endif()
443
Stella Laurenzo275215d2021-08-06 11:50:28 -0700444if(IREE_TARGET_BACKEND_METAL-SPIRV)
445 iree_set_spirv_cross_cmake_options()
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400446 # SPIRV-Cross is needed to cross compile SPIR-V into MSL source code.
447 add_subdirectory(third_party/spirv_cross EXCLUDE_FROM_ALL)
448endif()
449
Ben Vanik185d30c2019-09-19 14:24:11 -0700450#-------------------------------------------------------------------------------
Lei Zhang22f0e242020-03-30 12:09:20 -0700451# IREE top-level targets
452#-------------------------------------------------------------------------------
453
Lei Zhangfe4403e2021-06-01 15:09:43 -0400454if(${IREE_BUILD_BENCHMARKS})
455 # Add a top-level custom target to drive generating benchmark suites.
456 add_custom_target(iree-benchmark-suites)
457endif()
458
Lei Zhang22f0e242020-03-30 12:09:20 -0700459if(${IREE_BUILD_DOCS})
460 # Add a top-level custom target to drive generating all documentation.
461 # Register it to the default target given that IREE_BUILD_DOCS is explicitly
462 # requested.
463 add_custom_target(iree-doc ALL)
464endif()
465
466#-------------------------------------------------------------------------------
Ben Vanik185d30c2019-09-19 14:24:11 -0700467# IREE top-level libraries
468#-------------------------------------------------------------------------------
469
Marius Brehlerf5022e82019-12-13 15:20:25 -0800470add_subdirectory(build_tools/embed_data/)
471
Ben Vanik185d30c2019-09-19 14:24:11 -0700472add_subdirectory(iree/base)
Ben Vanik512d2d32019-09-20 13:22:34 -0700473add_subdirectory(iree/hal)
Marius Brehler9317d1f2020-01-08 10:34:11 -0800474add_subdirectory(iree/modules)
Ben Vanikb69d0cc2021-05-05 18:16:22 -0700475add_subdirectory(iree/runtime)
Ben Vanik512d2d32019-09-20 13:22:34 -0700476add_subdirectory(iree/schemas)
Ben Vanik01600a52020-12-15 17:18:04 -0800477add_subdirectory(iree/task)
Marius Brehler29676502019-12-27 17:07:10 -0800478add_subdirectory(iree/testing)
Marius Brehler476031a2020-03-24 16:14:30 -0700479add_subdirectory(iree/test)
not-jennibd3610e2020-12-23 17:00:22 -0800480add_subdirectory(iree/vm)
Lei Zhangff38a682021-06-08 21:12:24 -0400481
482if(${IREE_BUILD_BENCHMARKS})
Lei Zhang08cea042021-07-20 12:44:01 -0400483 add_subdirectory(benchmarks)
Lei Zhangff38a682021-06-08 21:12:24 -0400484endif()
485
raikonenfnu4b31bd92021-05-18 18:20:25 -0700486if(${IREE_BUILD_EXPERIMENTAL_ROCM})
487 add_subdirectory(build_tools/third_party/rocm EXCLUDE_FROM_ALL)
488 add_subdirectory(experimental/rocm)
raikonenfnu4b31bd92021-05-18 18:20:25 -0700489endif()
Ben Vanikcc2aff92019-09-24 10:23:55 -0700490
491if(${IREE_BUILD_COMPILER})
492 add_subdirectory(iree/compiler)
493endif()
Ben Vanik6b112ef2019-10-03 10:45:14 -0700494
495add_subdirectory(iree/tools)
496
Stella Laurenzo77a63cd2021-01-04 17:29:54 -0800497# Order constraint: The python bindings install tools targets from iree/tools
498# and must come after it.
499if(${IREE_BUILD_PYTHON_BINDINGS})
500 add_subdirectory(bindings/python)
501endif()
502
Ben Vanikcd1132f2021-01-29 15:58:17 -0800503if(${IREE_BUILD_BINDINGS_TFLITE})
504 add_subdirectory(bindings/tflite)
505endif()
506
Ben Vanik6b112ef2019-10-03 10:45:14 -0700507if(${IREE_BUILD_SAMPLES})
508 add_subdirectory(iree/samples)
509endif()
Ben Vanikb64e9182020-01-30 15:19:37 -0800510
Stella Laurenzo0f005e52020-12-27 11:54:33 -0800511if(${IREE_BUILD_EXPERIMENTAL_REMOTING})
512 # NOTE: Currently liburing is only used by the experimental remoting
513 # support, so keeping it scoped here. If this broadens, then include along
514 # with other dependencies as normal.
515 if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
516 message(STATUS "Enabling liburing")
517 add_subdirectory(build_tools/third_party/liburing EXCLUDE_FROM_ALL)
518 endif()
519 add_subdirectory(experimental/remoting)
Ben Vanikb64e9182020-01-30 15:19:37 -0800520endif()
Scott Toddee30e1b2020-02-03 16:44:15 -0800521
not-jenni603e9f12021-01-26 16:49:18 -0800522if(${IREE_BUILD_EXPERIMENTAL_JAVA_BINDINGS})
523 add_subdirectory(experimental/bindings/java)
524endif()
525
Stella Laurenzoa4137ef2020-12-09 21:28:08 -0800526if(${IREE_ENABLE_TENSORFLOW})
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800527 add_subdirectory(integrations/tensorflow)
528endif()
529
Stella Laurenzo03e48db2020-06-11 18:35:13 -0700530set(IREE_PUBLIC_INCLUDE_DIRS "${IREE_COMMON_INCLUDE_DIRS}"
531 CACHE INTERNAL "IREE: Include Directories" FORCE)