blob: b2c30b731051e51dc3427f94c5e33c6e16b75974 [file] [log] [blame]
Ben Vanik185d30c2019-09-19 14:24:11 -07001# Copyright 2019 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Scott Toddee30e1b2020-02-03 16:44:15 -080015cmake_minimum_required(VERSION 3.13)
Geoffrey Martin-Noble98bbd962019-11-21 07:27:39 -080016if(POLICY CMP0077)
17 cmake_policy(SET CMP0077 NEW)
18endif()
Scott Toddee30e1b2020-02-03 16:44:15 -080019# Allow target_link_libraries() from other directories (since 3.13):
20# https://cmake.org/cmake/help/v3.13/policy/CMP0079.html
21if(POLICY CMP0079)
22 cmake_policy(SET CMP0079 NEW)
23endif()
Ben Vanik512d2d32019-09-20 13:22:34 -070024set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Ben Vanik185d30c2019-09-19 14:24:11 -070025
Marius Brehler178c82a2019-12-27 15:00:04 -080026project(iree CXX C)
Lei Zhang6c5907b2020-06-02 09:06:08 -070027set(CMAKE_C_STANDARD 11)
28set(CMAKE_CXX_STANDARD 14)
Ben Vanik185d30c2019-09-19 14:24:11 -070029set(IREE_IDE_FOLDER IREE)
30set_property(GLOBAL PROPERTY USE_FOLDERS ON)
31
Lei Zhang7e253da2020-06-10 07:51:19 -070032#-------------------------------------------------------------------------------
33# Project component configuration
34#-------------------------------------------------------------------------------
35
Lei Zhange9967992020-06-03 11:13:00 -040036# LINT.IfChange(iree_options)
Ben Vanik56d44bf2020-05-15 13:20:50 -070037option(IREE_ENABLE_RUNTIME_TRACING "Enables instrumented runtime tracing." OFF)
Stella Laurenzo382122d2020-06-11 16:18:09 -070038option(IREE_ENABLE_MLIR "Enables MLIR/LLVM dependencies." ON)
Marius Brehler4e45a772020-06-02 14:04:18 -070039option(IREE_ENABLE_EMITC "Enables MLIR EmitC dependencies." 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 Zhang22f0e242020-03-30 12:09:20 -070043option(IREE_BUILD_DOCS "Builds IREE docs." OFF)
Ben Vanik6b112ef2019-10-03 10:45:14 -070044option(IREE_BUILD_SAMPLES "Builds IREE sample projects." ON)
Ben Vanik512d2d32019-09-20 13:22:34 -070045option(IREE_BUILD_DEBUGGER "Builds the IREE debugger app." OFF)
Marius Brehlerf3d73c92020-01-16 16:11:52 -080046option(IREE_BUILD_PYTHON_BINDINGS "Builds the IREE python bindings" OFF)
Jenni Kilduff0d09cf32020-10-19 10:23:34 -070047option(IREE_BUILD_JAVA_BINDINGS "Builds the IREE java bindings." OFF)
Ben Vanikb64e9182020-01-30 15:19:37 -080048option(IREE_BUILD_EXPERIMENTAL "Builds experimental projects." OFF)
Ben Vanik512d2d32019-09-20 13:22:34 -070049
Marius Brehler0a4b67f2020-05-08 13:19:16 -070050set(IREE_HAL_DRIVERS_TO_BUILD "all"
Scott Todd6132bb32020-08-11 12:04:40 -070051 CACHE STRING "Semicolon-separated list of HAL drivers to build, or \"all\".")
Marius Brehler0a4b67f2020-05-08 13:19:16 -070052set(IREE_TARGET_BACKENDS_TO_BUILD "all"
Scott Todd6132bb32020-08-11 12:04:40 -070053 CACHE STRING "Semicolon-separated list of target backends to build, or \"all\".")
Lei Zhange71d9732020-06-23 10:21:09 -040054# LINT.ThenChange(
Scott Todd4e5167d2020-06-29 09:30:35 -070055# https://github.com/google/iree/tree/main/build_tools/cmake/iree_cross_compile.cmake:iree_cross_compile_options,
56# https://github.com/google/iree/tree/main/build_tools/cmake/iree_cross_compile.cmake:iree_cross_compile_invoke
Lei Zhange71d9732020-06-23 10:21:09 -040057# )
Marius Brehler0a4b67f2020-05-08 13:19:16 -070058
Ben Vanikb64e9182020-01-30 15:19:37 -080059if(${IREE_BUILD_SAMPLES} OR ${IREE_BUILD_EXPERIMENTAL})
Marius Brehleredfc57f2019-12-18 11:19:38 -080060 set(IREE_BUILD_COMPILER ON CACHE BOOL "Build the IREE compiler for sample projects." FORCE)
61endif()
62
Stella Laurenzo06034152020-01-03 11:26:31 -080063if(${IREE_BUILD_COMPILER})
Stella Laurenzo382122d2020-06-11 16:18:09 -070064 set(IREE_ENABLE_MLIR ON CACHE BOOL "Enable LLVM dependencies if the IREE compiler is build." FORCE)
Stella Laurenzo06034152020-01-03 11:26:31 -080065endif()
66
Stella Laurenzo382122d2020-06-11 16:18:09 -070067if (${IREE_ENABLE_MLIR})
68 set(IREE_MLIR_DEP_MODE "BUNDLED" CACHE STRING "One of BUNDLED (default), DISABLED, INSTALLED")
Marius Brehler4e45a772020-06-02 14:04:18 -070069endif()
70
Marius Brehlerfd8be7f2020-06-18 15:59:28 -070071if(${IREE_ENABLE_EMITC})
72 if(NOT ${IREE_ENABLE_MLIR})
73 message(FATAL_ERROR "Enabling EmitC requires setting IREE_ENABLE_MLIR to ON.")
74 endif()
75
76 string(TOUPPER "${IREE_MLIR_DEP_MODE}" uppercase_IREE_MLIR_DEP_MODE)
77 if(NOT uppercase_IREE_MLIR_DEP_MODE MATCHES "^(BUNDLED|INSTALLED)$")
78 message(FATAL_ERROR "Enabling EmitC requires IREE_MLIR_DEP_MODE set to BUNDELED or INSTALLED.")
79 endif()
80endif()
81
Ben Vanik185d30c2019-09-19 14:24:11 -070082#-------------------------------------------------------------------------------
Lei Zhang8bea2e82020-06-04 10:17:56 -040083# Target and backend configuration
Ben Vanik185d30c2019-09-19 14:24:11 -070084#-------------------------------------------------------------------------------
85
Marius Brehler0a4b67f2020-05-08 13:19:16 -070086# List of all HAL drivers to be built by default:
87set(IREE_ALL_HAL_DRIVERS
Scott Todd217904d2020-06-04 13:38:32 -070088 DyLib
Marius Brehler0a4b67f2020-05-08 13:19:16 -070089 LLVM
Lei Zhang6171a982020-09-08 12:48:25 -040090 Metal
Geoffrey Martin-Nobledfb76332020-08-07 11:22:39 -070091 VMLA
Scott Todd6132bb32020-08-11 12:04:40 -070092 Vulkan
Marius Brehler0a4b67f2020-05-08 13:19:16 -070093)
94
95if( IREE_HAL_DRIVERS_TO_BUILD STREQUAL "all" )
96 set( IREE_HAL_DRIVERS_TO_BUILD ${IREE_ALL_HAL_DRIVERS} )
Lei Zhang7d90e0e2020-07-23 16:01:13 -040097 # For cross compilation towords Android, we don't want LLVM JIT HAL driver.
98 if(ANDROID)
Han-Chung Wang8cc22a32020-10-20 08:45:33 -070099 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD LLVM)
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400100 endif()
Lei Zhang6171a982020-09-08 12:48:25 -0400101
102 # For Apple platforms we need to use Metal instead of Vulkan.
103 if(APPLE)
104 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Vulkan)
105 else()
106 # And Metal isn't available on non-Apple platforms for sure.
107 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Metal)
108 endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700109endif()
Scott Todd6132bb32020-08-11 12:04:40 -0700110message(STATUS "Building HAL drivers: ${IREE_HAL_DRIVERS_TO_BUILD}")
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700111
112# Default every IREE_HAL_DRIVER_* to OFF
113foreach(_backend ${IREE_ALL_HAL_DRIVERS})
114 string(TOUPPER "${_backend}" uppercase_backend)
115 set(IREE_HAL_DRIVER_${uppercase_backend} OFF CACHE BOOL "" FORCE)
116endforeach()
117
118# Set IREE_HAL_DRIVER_* based on configuration
119foreach(_backend ${IREE_HAL_DRIVERS_TO_BUILD})
120 string(TOUPPER "${_backend}" uppercase_backend)
121 set(IREE_HAL_DRIVER_${uppercase_backend} ON CACHE BOOL "" FORCE)
122endforeach()
123
Scott Todd6132bb32020-08-11 12:04:40 -0700124# Enable HAL driver modules based on options.
125set (IREE_HAL_DRIVER_MODULES "")
126if(${IREE_HAL_DRIVER_DYLIB})
127 list(APPEND IREE_HAL_DRIVER_MODULES iree::hal::dylib::dylib_driver_module)
128endif()
129if(${IREE_HAL_DRIVER_LLVM})
130 list(APPEND IREE_HAL_DRIVER_MODULES iree::hal::llvmjit::llvmjit_driver_module)
131endif()
Lei Zhang6171a982020-09-08 12:48:25 -0400132if(${IREE_HAL_DRIVER_METAL})
133 list(APPEND IREE_HAL_DRIVER_MODULES iree::hal::metal::metal_driver_module)
134endif()
Scott Todd6132bb32020-08-11 12:04:40 -0700135if(${IREE_HAL_DRIVER_VMLA})
136 list(APPEND IREE_HAL_DRIVER_MODULES iree::hal::vmla::vmla_driver_module)
137endif()
138if(${IREE_HAL_DRIVER_VULKAN})
139 list(APPEND IREE_HAL_DRIVER_MODULES iree::hal::vulkan::vulkan_driver_module)
140endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700141
142# List of all target backends to be built by default:
143set(IREE_ALL_TARGET_BACKENDS
Geoffrey Martin-Noblee7f90b62020-07-29 14:32:26 -0700144 # TODO(#2645): Add DYLIB-LLVM-AOT when it doesn't require an env var
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400145 LLVM-IR
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400146 Metal-SPIRV
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400147 Vulkan-SPIRV
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700148 VMLA
149)
150
151if( IREE_TARGET_BACKENDS_TO_BUILD STREQUAL "all" )
152 set( IREE_TARGET_BACKENDS_TO_BUILD ${IREE_ALL_TARGET_BACKENDS} )
153endif()
Scott Todd6132bb32020-08-11 12:04:40 -0700154message(STATUS "Building target backends: ${IREE_TARGET_BACKENDS_TO_BUILD}")
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700155
156# Default every IREE_TARGET_BACKEND_* to OFF
157foreach(_backend ${IREE_ALL_TARGET_BACKENDS})
158 string(TOUPPER "${_backend}" uppercase_backend)
159 set(IREE_TARGET_BACKEND_${uppercase_backend} OFF CACHE BOOL "" FORCE)
160endforeach()
161
162# Set IREE_TARGET_BACKEND_* based on configuration
163foreach(_backend ${IREE_TARGET_BACKENDS_TO_BUILD})
164 string(TOUPPER "${_backend}" uppercase_backend)
165 set(IREE_TARGET_BACKEND_${uppercase_backend} ON CACHE BOOL "" FORCE)
166endforeach()
167
Ben Vanik185d30c2019-09-19 14:24:11 -0700168list(APPEND CMAKE_MODULE_PATH
169 ${CMAKE_CURRENT_LIST_DIR}/build_tools/cmake/
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800170 ${CMAKE_CURRENT_LIST_DIR}/bindings/python/build_tools/cmake/
Ben Vanik185d30c2019-09-19 14:24:11 -0700171 ${CMAKE_CURRENT_LIST_DIR}/third_party/abseil-cpp/absl/copts/
172)
Ben Vanik512d2d32019-09-20 13:22:34 -0700173
Lei Zhang0d281b72020-06-01 20:00:23 -0400174#-------------------------------------------------------------------------------
175# Cross compiling configuration
176#-------------------------------------------------------------------------------
177
178if(CMAKE_CROSSCOMPILING)
Lei Zhang45695f92020-06-16 19:36:41 -0400179 message(STATUS "Detected cross compilation mode; configuring IREE on host...")
Lei Zhang0d281b72020-06-01 20:00:23 -0400180
181 # C/C++ compilers for host compilation.
182 # Note: we need to explicitly set this because IREE does not work well with
183 # GCC at the moment: https://github.com/google/iree/issues/1269
184 set(IREE_HOST_C_COMPILER "$ENV{IREE_HOST_C_COMPILER}" CACHE FILEPATH "C compiler for host compilation")
185 set(IREE_HOST_CXX_COMPILER "$ENV{IREE_HOST_CXX_COMPILER}" CACHE FILEPATH "C++ compiler for host compilation")
186
187 # Master configuration for the binary directory containing all artifacts
188 # compiled for host.
Lei Zhange71d9732020-06-23 10:21:09 -0400189 if(NOT IREE_HOST_BINARY_ROOT)
190 set(IREE_HOST_BINARY_ROOT "${CMAKE_CURRENT_BINARY_DIR}/host" CACHE FILEPATH "directory containing host artifacts")
191 endif()
Lei Zhang0d281b72020-06-01 20:00:23 -0400192
193 set(IREE_HOST_BUILD_COMPILER ON) # For iree-translate
194 set(IREE_HOST_ENABLE_LLVM ON) # For iree-tblgen
195
196 # Set the host build directory for LLVM to our directory. Otherwise it will
197 # follow its own convention.
Lei Zhang7f6dfbf2020-06-30 14:31:35 -0400198 set(LLVM_NATIVE_BUILD
199 "${IREE_HOST_BINARY_ROOT}/third_party/llvm-project/llvm"
200 CACHE FILEPATH "directory containing host artifacts for LLVM"
201 )
202
203 # And set host C/C++ compiler for LLVM. This makes cross compilation using
204 # Windows as the host platform nicer. Because we have various development
205 # evnironments on Windows (CMD, Cygwin, MSYS, etc.), LLVM can have problems
206 # figuring out the host triple and toolchain. We are passing in the host
207 # C/C++ compiler toolchain for IREE anyway; so we can give LLVM side some
208 # help here. This hides some complexity and ugliness from the users.
209 set(CROSS_TOOLCHAIN_FLAGS_NATIVE
210 "-DCMAKE_C_COMPILER=\"${IREE_HOST_C_COMPILER}\";-DCMAKE_CXX_COMPILER=\"${IREE_HOST_CXX_COMPILER}\""
211 CACHE FILEPATH "LLVM toolchain configuration for host build"
212 )
Lei Zhang0d281b72020-06-01 20:00:23 -0400213
214 include(iree_cross_compile)
215
216 # Use another CMake invocation to configure a build for host.
217 iree_create_configuration(HOST)
Lei Zhang1e6a7a72020-06-04 13:12:01 -0400218
Lei Zhang45695f92020-06-16 19:36:41 -0400219 message(STATUS "Done configuring IREE on host in ${IREE_HOST_BINARY_ROOT}")
Lei Zhang0d281b72020-06-01 20:00:23 -0400220endif()
221
222#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400223# IREE compilation toolchain configuration
224#-------------------------------------------------------------------------------
225
226# Enable using lld as the linker for C/C++ targets. This affects IREE and all
227# dependency projects.
228option(IREE_ENABLE_LLD "Use lld when linking" OFF)
229option(IREE_ENABLE_ASAN "Enable address sanitizer" OFF)
230option(IREE_ENABLE_MSAN "Enable memory sanitizer" OFF)
231option(IREE_ENABLE_TSAN "Enable thread sanitizer" OFF)
232
233#-------------------------------------------------------------------------------
Lei Zhang7e253da2020-06-10 07:51:19 -0700234# IREE utility definitions
Lei Zhang0d281b72020-06-01 20:00:23 -0400235#-------------------------------------------------------------------------------
236
Ben Vanik512d2d32019-09-20 13:22:34 -0700237include(iree_macros)
Ben Vanik185d30c2019-09-19 14:24:11 -0700238include(iree_copts)
Lei Zhang54771d42020-09-09 16:56:24 -0400239include(iree_whole_archive_link)
Ben Vanik6b112ef2019-10-03 10:45:14 -0700240include(iree_cc_binary)
Ben Vanik185d30c2019-09-19 14:24:11 -0700241include(iree_cc_library)
242include(iree_cc_test)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700243include(iree_tablegen_library)
Lei Zhang22f0e242020-03-30 12:09:20 -0700244include(iree_tablegen_doc)
Scott Todd11adcab2019-12-18 14:10:44 -0800245include(iree_cc_embed_data)
246include(iree_bytecode_module)
Stella Laurenzo66578b02020-07-20 17:34:44 -0700247include(iree_multipy)
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800248include(iree_lit_test)
Geoffrey Martin-Noble4526dcc2020-03-09 11:59:52 -0700249include(iree_add_all_subdirs)
Geoffrey Martin-Noblee5fd5b52020-03-31 11:31:30 -0700250include(iree_check_test)
Ben Vanik185d30c2019-09-19 14:24:11 -0700251
Marius Brehlerc4b6b912020-01-15 08:44:23 -0800252set(DEFAULT_CMAKE_BUILD_TYPE "Release")
253if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
254 message(STATUS "No build type selected, default to ${DEFAULT_CMAKE_BUILD_TYPE}")
255 set(CMAKE_BUILD_TYPE "${DEFAULT_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
256endif()
257
Marius Brehler06ac36e2020-01-10 14:44:11 -0800258set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
259
Ben Vanik185d30c2019-09-19 14:24:11 -0700260#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400261# IREE compilation flags
Scott Todd29d654e2020-06-11 15:24:17 -0700262#-------------------------------------------------------------------------------
263
Lei Zhangdd21f322020-09-10 10:47:33 -0400264iree_append_list_to_string(CMAKE_CXX_FLAGS ${IREE_DEFAULT_COPTS})
265iree_append_list_to_string(CMAKE_C_FLAGS_DEBUG ${IREE_C_FLAGS_DEBUG_LIST})
266iree_append_list_to_string(CMAKE_CXX_FLAGS_DEBUG ${IREE_CXX_FLAGS_DEBUG_LIST})
Scott Todd29d654e2020-06-11 15:24:17 -0700267
268set(CMAKE_CXX_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C++ compiler during fast builds." FORCE)
269set(CMAKE_C_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C compiler during fast builds." FORCE)
270set(CMAKE_EXE_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used for linking binaries during fast builds." FORCE)
271set(CMAKE_SHARED_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used by the shared libraries linker binaries during fast builds." FORCE)
272mark_as_advanced(
273 CMAKE_CXX_FLAGS_FASTBUILD
274 CMAKE_C_FLAGS_FASTBUILD
275 CMAKE_EXE_LINKER_FLAGS_FASTBUILD
276 CMAKE_SHARED_LINKER_FLAGS_FASTBUILD
277)
278
279include(iree_setup_toolchain)
280
281#-------------------------------------------------------------------------------
Stella Laurenzo382122d2020-06-11 16:18:09 -0700282# MLIR/LLVM Dependency
283# We treat the LLVM dependency specially because we support several different
284# ways to use it:
scotttodd354b6912020-06-24 16:29:35 -0700285# - Bundled (default): a source dependency directly on the
Stella Laurenzo382122d2020-06-11 16:18:09 -0700286# third_party/llvm-project submodule.
287# - External: An external (source or installed) dependency on LLVM.
288# - Provided: When IREE is used as a sub-project, it is assumed that the LLVM
289# dependency is added prior to including this configuration.
290#-------------------------------------------------------------------------------
291
Ben Vanikafd7abe2020-10-07 09:01:53 -0700292# Disable LLVM's warnings.
Ben Vanik89a77fa2020-10-07 17:19:31 -0700293set(LLVM_ENABLE_ASSERTIONS OFF CACHE BOOL "don't use global flags /facepalm")
294set(LLVM_ENABLE_WARNINGS OFF CACHE BOOL "don't use global flags /facepalm")
Ben Vanikafd7abe2020-10-07 09:01:53 -0700295
Stella Laurenzo382122d2020-06-11 16:18:09 -0700296# Adds bundled projects that must be included after the LLVM directory has
scotttodd354b6912020-06-24 16:29:35 -0700297# been added and within the scope of its settings (i.e. build type override,
Stella Laurenzo382122d2020-06-11 16:18:09 -0700298# etc).
299function(add_bundled_mlir_dependent_projects)
300 if(${IREE_ENABLE_EMITC})
301 add_subdirectory(third_party/mlir-emitc EXCLUDE_FROM_ALL)
302 endif()
303endfunction()
304
305function(add_iree_mlir_src_dep llvm_monorepo_path)
Ben Vanik89a77fa2020-10-07 17:19:31 -0700306 # Stash cmake build type in case LLVM messes with it.
307 set(_CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
308
Thomas Raoux3b6a42a2020-06-26 18:18:48 -0700309 # experimental model builder uses vulkan runner.
310 if(${IREE_BUILD_EXPERIMENTAL})
311 set(MLIR_VULKAN_RUNNER_ENABLED ON)
312 endif()
313
Stella Laurenzo382122d2020-06-11 16:18:09 -0700314 add_subdirectory("${llvm_monorepo_path}/llvm" "third_party/llvm-project/llvm" EXCLUDE_FROM_ALL)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700315
Ben Vanik89a77fa2020-10-07 17:19:31 -0700316 # Reset CMAKE_BUILD_TYPE to its previous setting.
Stella Laurenzo382122d2020-06-11 16:18:09 -0700317 set(CMAKE_BUILD_TYPE "${_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
318endfunction()
319
320if(${IREE_ENABLE_MLIR})
321 if(${IREE_MLIR_DEP_MODE} STREQUAL "DISABLED")
322 message(STATUS "Not adding MLIR/LLVM dep due to IREE_MLIR_DEP_MODE=DISABLED")
323 elseif(${IREE_MLIR_DEP_MODE} STREQUAL "BUNDLED")
Stella Laurenzo03e48db2020-06-11 18:35:13 -0700324 # TODO: See iree_copts.cmake where it sets include directories for this
325 # case. This should be cleaned up to be all in one place or the other for
326 # all modes.
Stella Laurenzo382122d2020-06-11 16:18:09 -0700327 message(STATUS "Adding bundled LLVM source dependency")
328 add_iree_mlir_src_dep("third_party/llvm-project")
329 elseif(${IREE_MLIR_DEP_MODE} STREQUAL "INSTALLED")
Stella Laurenzoa1914e72020-07-25 00:06:47 -0700330 # Deps of installed MLIR/LLVM packages.
331 find_package(ZLIB) # See: https://reviews.llvm.org/D79219
Stella Laurenzo382122d2020-06-11 16:18:09 -0700332 message(STATUS "Looking for installed MLIR/LLVM packages (configure with MLIR_DIR variable)")
333 find_package(MLIR REQUIRED CONFIG)
334 message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
335 message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
336 list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
337 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
338 include(TableGen)
339 include(AddLLVM)
340 include(AddMLIR)
341 include(HandleLLVMOptions)
342
343 # Add include/link directories
Ben Vanikafd7abe2020-10-07 09:01:53 -0700344 include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
345 include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
Stella Laurenzo382122d2020-06-11 16:18:09 -0700346 link_directories(${LLVM_BUILD_LIBRARY_DIR})
347 add_definitions(${LLVM_DEFINITIONS})
348 else()
349 message(FATAL "Unsupported IREE_MLIR_DEP_MODE=${IREE_MLIR_DEP_MODE}")
350 endif()
351
352 include(external_tablegen_library)
353 add_bundled_mlir_dependent_projects()
354endif()
355
Stella Laurenzo382122d2020-06-11 16:18:09 -0700356#-------------------------------------------------------------------------------
357# Non-LLVM Dependencies
Ben Vanik185d30c2019-09-19 14:24:11 -0700358#-------------------------------------------------------------------------------
359
Lei Zhang84787782020-06-02 08:42:25 -0700360# Use the (deprecated) FindPythonInterp/FindPythonLibs functions before
361# any of our dependencies do. See
362# https://pybind11.readthedocs.io/en/stable/faq.html#inconsistent-detection-of-python-version-in-cmake-and-pybind11
363# If one dependency finds Python 2 (the default),
364# any others that try to find Python 3 will fail.
365# (Also come on, it's $CURRENT_YEAR - please just use Python 3 already.)
Marius Brehlerdb763292020-02-06 13:03:33 -0800366if(${IREE_BUILD_COMPILER} OR ${IREE_BUILD_PYTHON_BINDINGS})
Scott Todd702743d2020-02-06 20:49:06 -0800367 find_package(PythonInterp 3 REQUIRED)
Lei Zhang84787782020-06-02 08:42:25 -0700368endif()
369if(${IREE_BUILD_PYTHON_BINDINGS})
Stella Laurenzoc8f906c2020-07-21 13:55:50 -0700370 # Note: Optional because python libs can be manually specified.
371 find_package(PythonLibs 3)
Scott Todd4af17b62019-12-17 16:47:00 -0800372endif()
373
Ben Vanikcc2aff92019-09-24 10:23:55 -0700374list(APPEND CMAKE_MODULE_PATH
375 ${CMAKE_CURRENT_LIST_DIR}/third_party/flatbuffers/CMake/
376)
377
Ben Vanik512d2d32019-09-20 13:22:34 -0700378include(external_cc_library)
Ben Vanik2d1808b2020-07-17 19:02:16 -0700379include(flatbuffer_c_library)
Ben Vanik512d2d32019-09-20 13:22:34 -0700380include(flatbuffer_cc_library)
381
Ben Vanik71527c22020-07-17 13:09:57 -0700382add_subdirectory(build_tools/third_party/flatcc EXCLUDE_FROM_ALL)
383add_subdirectory(build_tools/third_party/renderdoc_api EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700384add_subdirectory(build_tools/third_party/ruy EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700385
Ben Vanikda79d9b2020-10-01 09:52:54 -0700386add_subdirectory(third_party/cpuinfo EXCLUDE_FROM_ALL)
Marius Brehlerf5022e82019-12-13 15:20:25 -0800387add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700388add_subdirectory(third_party/abseil-cpp EXCLUDE_FROM_ALL)
389add_subdirectory(third_party/flatbuffers EXCLUDE_FROM_ALL)
Ben Vanik71527c22020-07-17 13:09:57 -0700390add_subdirectory(third_party/flatcc EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700391add_subdirectory(third_party/vulkan_headers EXCLUDE_FROM_ALL)
Ben Vanik185d30c2019-09-19 14:24:11 -0700392
Lei Zhang0d281b72020-06-01 20:00:23 -0400393if(CMAKE_CROSSCOMPILING)
Lei Zhang1e6a7a72020-06-04 13:12:01 -0400394 # We need flatc to generate some source code. When cross-compiling, we need
395 # to make sure the flatc binary is configured under host environment.
Scott Todd7f4c2d22020-10-14 14:10:22 -0700396 iree_declare_host_excutable(flatc "flatc" BUILDONLY)
397 iree_declare_host_excutable(flatcc_cli "flatcc_cli" BUILDONLY)
Lei Zhang1e6a7a72020-06-04 13:12:01 -0400398
Lei Zhang0d281b72020-06-01 20:00:23 -0400399 # Set the FLATBUFFERS_FLATC_EXECUTABLE. It controls where to find the flatc
400 # binary in BuildFlatBuffers().
Lei Zhang486e7072020-06-16 11:42:49 -0400401 iree_get_executable_path(FLATBUFFERS_FLATC_EXECUTABLE flatc)
Lei Zhang1e6a7a72020-06-04 13:12:01 -0400402
Lei Zhang0d281b72020-06-01 20:00:23 -0400403 # Add a custom target to copy the flatc to the binary directory.
404 add_custom_target(iree_host_flatc
Ben Vanik71527c22020-07-17 13:09:57 -0700405 COMMAND
406 "${CMAKE_COMMAND}" -E copy_if_different
407 "${IREE_HOST_BINARY_ROOT}/third_party/flatbuffers/flatc${IREE_HOST_EXECUTABLE_SUFFIX}"
408 "${IREE_HOST_BINARY_ROOT}/bin"
Lei Zhang0d281b72020-06-01 20:00:23 -0400409 DEPENDS iree_host_build_flatc
Lei Zhang1ff70612020-06-04 10:12:41 -0400410 COMMENT "Installing host flatc..."
Lei Zhang0d281b72020-06-01 20:00:23 -0400411 )
Ben Vanikeb53de32020-07-17 19:05:27 -0700412 add_custom_target(iree_host_flatcc_cli
Ben Vanik71527c22020-07-17 13:09:57 -0700413 COMMAND
414 "${CMAKE_COMMAND}" -E copy_if_different
Ben Vanik14257372020-07-21 08:05:18 -0700415 "${PROJECT_SOURCE_DIR}/third_party/flatcc/bin/flatcc${IREE_HOST_EXECUTABLE_SUFFIX}"
Scott Todd7f4c2d22020-10-14 14:10:22 -0700416 "${IREE_HOST_BINARY_ROOT}/bin/flatcc_cli${IREE_HOST_EXECUTABLE_SUFFIX}"
Ben Vanikeb53de32020-07-17 19:05:27 -0700417 DEPENDS iree_host_build_flatcc_cli
Ben Vanik71527c22020-07-17 13:09:57 -0700418 COMMENT "Installing host flatcc..."
419 )
Ben Vanik14257372020-07-21 08:05:18 -0700420else()
421 # TODO: unify flatc and flatcc handling to the same mechanism.
422 add_executable(iree_host_flatcc_cli ALIAS flatcc_cli)
Lei Zhang0d281b72020-06-01 20:00:23 -0400423endif()
424
Stella Laurenzo06034152020-01-03 11:26:31 -0800425if(${IREE_BUILD_COMPILER})
Marius Brehler87eaa3c2020-07-02 17:39:49 +0200426 add_subdirectory(build_tools/third_party/tensorflow/tensorflow/compiler/mlir/hlo EXCLUDE_FROM_ALL)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700427endif()
428
Scott Toddf7003552019-11-11 09:14:19 -0800429if(${IREE_BUILD_DEBUGGER} OR ${IREE_BUILD_SAMPLES})
Scott Todd5e1f44e2020-03-30 16:21:44 -0700430 # sdl2 logs are spammy - change log level while adding
431 function(include_sdl2)
432 set(CMAKE_MESSAGE_LOG_LEVEL "WARNING")
433 add_subdirectory(third_party/sdl2 EXCLUDE_FROM_ALL)
434 endfunction()
435 include_sdl2()
436
Scott Todd0d416202019-11-12 16:22:05 -0800437 add_subdirectory(build_tools/third_party/dear_imgui EXCLUDE_FROM_ALL)
Scott Toddf7003552019-11-11 09:14:19 -0800438endif()
439
Marius Brehler29676502019-12-27 17:07:10 -0800440if(${IREE_BUILD_TESTS})
441 add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
Marius Brehler575b63a2020-01-07 09:39:24 -0800442 enable_testing(iree)
Marius Brehler29676502019-12-27 17:07:10 -0800443endif()
444
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800445if(${IREE_BUILD_PYTHON_BINDINGS})
Stella Laurenzo66578b02020-07-20 17:34:44 -0700446 # NOTE: The multipy defaults come from pybind's configuration and must come
447 # after. This should be pulled in locally at some point.
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800448 add_subdirectory(third_party/pybind11 EXCLUDE_FROM_ALL)
Stella Laurenzo66578b02020-07-20 17:34:44 -0700449 iree_multipy_configure()
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800450endif()
451
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400452if(${IREE_TARGET_BACKEND_METAL-SPIRV})
453 # SPIRV-Cross is needed to cross compile SPIR-V into MSL source code.
454 add_subdirectory(third_party/spirv_cross EXCLUDE_FROM_ALL)
455endif()
456
Ben Vanik185d30c2019-09-19 14:24:11 -0700457#-------------------------------------------------------------------------------
Lei Zhang22f0e242020-03-30 12:09:20 -0700458# IREE top-level targets
459#-------------------------------------------------------------------------------
460
461if(${IREE_BUILD_DOCS})
462 # Add a top-level custom target to drive generating all documentation.
463 # Register it to the default target given that IREE_BUILD_DOCS is explicitly
464 # requested.
465 add_custom_target(iree-doc ALL)
466endif()
467
468#-------------------------------------------------------------------------------
Ben Vanik185d30c2019-09-19 14:24:11 -0700469# IREE top-level libraries
470#-------------------------------------------------------------------------------
471
Marius Brehlerf5022e82019-12-13 15:20:25 -0800472add_subdirectory(build_tools/embed_data/)
473
Ben Vanik185d30c2019-09-19 14:24:11 -0700474add_subdirectory(iree/base)
Ben Vanik512d2d32019-09-20 13:22:34 -0700475add_subdirectory(iree/hal)
Marius Brehler9317d1f2020-01-08 10:34:11 -0800476add_subdirectory(iree/modules)
Ben Vanik512d2d32019-09-20 13:22:34 -0700477add_subdirectory(iree/schemas)
Marius Brehler29676502019-12-27 17:07:10 -0800478add_subdirectory(iree/testing)
Marius Brehler476031a2020-03-24 16:14:30 -0700479add_subdirectory(iree/test)
Stella Laurenzo06034152020-01-03 11:26:31 -0800480
Stella Laurenzo382122d2020-06-11 16:18:09 -0700481if(${IREE_ENABLE_MLIR})
Stella Laurenzo06034152020-01-03 11:26:31 -0800482 # The VM requires LLVM to build its op definitions.
483 add_subdirectory(iree/vm)
484endif()
Ben Vanikcc2aff92019-09-24 10:23:55 -0700485
486if(${IREE_BUILD_COMPILER})
487 add_subdirectory(iree/compiler)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700488elseif(${IREE_ENABLE_MLIR})
Stella Laurenzo06034152020-01-03 11:26:31 -0800489 # If not building the compiler, tablegen is still needed
490 # to generate vm ops so deep include it only.
Marius Brehlerc61240a2020-04-20 11:22:59 -0700491 add_subdirectory(iree/compiler/Dialect/IREE/Tools)
Stella Laurenzo06034152020-01-03 11:26:31 -0800492 add_subdirectory(iree/compiler/Dialect/VM/Tools)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700493endif()
Ben Vanik6b112ef2019-10-03 10:45:14 -0700494
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800495if(${IREE_BUILD_PYTHON_BINDINGS})
496 add_subdirectory(bindings/python)
497endif()
498
Jenni Kilduff0d09cf32020-10-19 10:23:34 -0700499if(${IREE_BUILD_JAVA_BINDINGS})
500 add_subdirectory(bindings/java)
501 add_subdirectory(bindings/javatests)
502endif()
503
Ben Vanik6b112ef2019-10-03 10:45:14 -0700504add_subdirectory(iree/tools)
505
506if(${IREE_BUILD_SAMPLES})
507 add_subdirectory(iree/samples)
508endif()
Ben Vanikb64e9182020-01-30 15:19:37 -0800509
510if(${IREE_BUILD_EXPERIMENTAL})
511 add_subdirectory(experimental)
512endif()
Scott Toddee30e1b2020-02-03 16:44:15 -0800513
514# Note: this must be called after all libraries have been declared.
515iree_complete_binary_link_options()
Marius Brehler42cc5e62020-02-18 11:17:12 -0800516if(${IREE_BUILD_PYTHON_BINDINGS})
517 iree_complete_py_extension_link_options()
518endif()
Stella Laurenzo03e48db2020-06-11 18:35:13 -0700519
520set(IREE_PUBLIC_INCLUDE_DIRS "${IREE_COMMON_INCLUDE_DIRS}"
521 CACHE INTERNAL "IREE: Include Directories" FORCE)