Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 1 | # Copyright 2019 The IREE Authors |
Ben Vanik | 512d2d3 | 2019-09-20 13:22:34 -0700 | [diff] [blame] | 2 | # |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 3 | # 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 Vanik | 512d2d3 | 2019-09-20 13:22:34 -0700 | [diff] [blame] | 6 | |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 7 | include(CMakeParseArguments) |
| 8 | |
| 9 | #------------------------------------------------------------------------------- |
Ben Vanik | 57d5e2e | 2020-03-03 07:14:56 -0800 | [diff] [blame] | 10 | # Missing CMake Variables |
| 11 | #------------------------------------------------------------------------------- |
| 12 | |
| 13 | if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") |
| 14 | set(IREE_HOST_SCRIPT_EXT "bat") |
Lei Zhang | 5e88832 | 2020-06-29 16:17:39 -0400 | [diff] [blame] | 15 | # https://gitlab.kitware.com/cmake/cmake/-/issues/17553 |
| 16 | set(IREE_HOST_EXECUTABLE_SUFFIX ".exe") |
Ben Vanik | 57d5e2e | 2020-03-03 07:14:56 -0800 | [diff] [blame] | 17 | else() |
| 18 | set(IREE_HOST_SCRIPT_EXT "sh") |
Lei Zhang | 5e88832 | 2020-06-29 16:17:39 -0400 | [diff] [blame] | 19 | set(IREE_HOST_EXECUTABLE_SUFFIX "") |
Ben Vanik | 57d5e2e | 2020-03-03 07:14:56 -0800 | [diff] [blame] | 20 | endif() |
| 21 | |
bjacob | 8f5ced9 | 2023-03-20 20:20:36 -0400 | [diff] [blame^] | 22 | |
| 23 | #------------------------------------------------------------------------------- |
| 24 | # IREE_ARCH: identifies the target CPU architecture. May be empty when this is |
| 25 | # ill-defined, such as multi-architecture builds. |
| 26 | # This should be kept consistent with the C preprocessor token IREE_ARCH defined |
| 27 | # in target_platform.h. |
| 28 | #------------------------------------------------------------------------------- |
| 29 | |
| 30 | # First, get the raw CMake architecture name, not yet normalized. Even that is |
| 31 | # non-trivial: it usually is CMAKE_SYSTEM_PROCESSOR, but on some platforms, we |
| 32 | # have to read other variables instead. |
| 33 | if(CMAKE_OSX_ARCHITECTURES) |
| 34 | # Borrowing from: |
| 35 | # https://boringssl.googlesource.com/boringssl/+/c5f0e58e653d2d9afa8facc090ce09f8aaa3fa0d/CMakeLists.txt#43 |
| 36 | # https://github.com/google/XNNPACK/blob/2eb43787bfad4a99bdb613111cea8bc5a82f390d/CMakeLists.txt#L40 |
| 37 | list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES) |
| 38 | if(${NUM_ARCHES} EQUAL 1) |
| 39 | # Only one arch in CMAKE_OSX_ARCHITECTURES, use that. |
| 40 | set(_IREE_UNNORMALIZED_ARCH "${CMAKE_OSX_ARCHITECTURES}") |
| 41 | endif() |
| 42 | # Leaving _IREE_UNNORMALIZED_ARCH empty disables arch code paths. We will |
| 43 | # issue a performance warning about that below. |
| 44 | elseif(CMAKE_GENERATOR MATCHES "^Visual Studio " AND CMAKE_GENERATOR_PLATFORM) |
| 45 | # Borrowing from: |
| 46 | # https://github.com/google/XNNPACK/blob/2eb43787bfad4a99bdb613111cea8bc5a82f390d/CMakeLists.txt#L50 |
| 47 | set(_IREE_UNNORMALIZED_ARCH "${CMAKE_GENERATOR_PLATFORM}") |
| 48 | else() |
| 49 | set(_IREE_UNNORMALIZED_ARCH "${CMAKE_SYSTEM_PROCESSOR}") |
| 50 | endif() |
| 51 | |
| 52 | string(TOLOWER "${_IREE_UNNORMALIZED_ARCH}" _IREE_UNNORMALIZED_ARCH_LOWERCASE) |
| 53 | |
| 54 | # Normalize _IREE_UNNORMALIZED_ARCH into IREE_ARCH. |
| 55 | if (EMSCRIPTEN) |
| 56 | # TODO: figure what to do about the wasm target, which masquerades as x86. |
| 57 | # This is the one case where the IREE_ARCH CMake variable is currently |
| 58 | # inconsistent with the IREE_ARCH C preprocessor token. |
| 59 | set (IREE_ARCH "") |
| 60 | elseif ((_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "aarch64") OR |
| 61 | (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "arm64") OR |
| 62 | (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "arm64e") OR |
| 63 | (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "arm64ec")) |
| 64 | set (IREE_ARCH "arm_64") |
| 65 | elseif ((_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "arm") OR |
| 66 | (_IREE_UNNORMALIZED_ARCH_LOWERCASE MATCHES "^armv[5-8]")) |
| 67 | set (IREE_ARCH "arm_32") |
| 68 | elseif ((_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "x86_64") OR |
| 69 | (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "amd64") OR |
| 70 | (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "x64")) |
| 71 | set (IREE_ARCH "x86_64") |
| 72 | elseif ((_IREE_UNNORMALIZED_ARCH_LOWERCASE MATCHES "^i[3-7]86$") OR |
| 73 | (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "x86") OR |
| 74 | (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "win32")) |
| 75 | set (IREE_ARCH "x86_32") |
| 76 | elseif (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "riscv64") |
| 77 | set (IREE_ARCH "riscv_64") |
| 78 | elseif (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "riscv32") |
| 79 | set (IREE_ARCH "riscv_32") |
| 80 | elseif (_IREE_UNNORMALIZED_ARCH_LOWERCASE STREQUAL "") |
| 81 | set (IREE_ARCH "") |
| 82 | message(WARNING "Performance advisory: architecture-specific code paths " |
| 83 | "disabled because no target architecture was specified or we didn't know " |
| 84 | "which CMake variable to read. Some relevant CMake variables:\n" |
| 85 | "CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}\n" |
| 86 | "CMAKE_GENERATOR=${CMAKE_GENERATOR}\n" |
| 87 | "CMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}\n" |
| 88 | "CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}\n" |
| 89 | ) |
| 90 | else() |
| 91 | set (IREE_ARCH "") |
| 92 | message(SEND_ERROR "Unrecognized target architecture ${_IREE_UNNORMALIZED_ARCH_LOWERCASE}") |
| 93 | endif() |
| 94 | |
Ben Vanik | 57d5e2e | 2020-03-03 07:14:56 -0800 | [diff] [blame] | 95 | #------------------------------------------------------------------------------- |
Lei Zhang | 7e8589a | 2020-06-10 16:42:49 -0400 | [diff] [blame] | 96 | # General utilities |
| 97 | #------------------------------------------------------------------------------- |
| 98 | |
| 99 | # iree_to_bool |
| 100 | # |
| 101 | # Sets `variable` to `ON` if `value` is true and `OFF` otherwise. |
Lei Zhang | d643f67 | 2020-06-16 10:56:41 -0400 | [diff] [blame] | 102 | function(iree_to_bool VARIABLE VALUE) |
| 103 | if(VALUE) |
| 104 | set(${VARIABLE} "ON" PARENT_SCOPE) |
Lei Zhang | 7e8589a | 2020-06-10 16:42:49 -0400 | [diff] [blame] | 105 | else() |
Lei Zhang | d643f67 | 2020-06-16 10:56:41 -0400 | [diff] [blame] | 106 | set(${VARIABLE} "OFF" PARENT_SCOPE) |
Lei Zhang | 7e8589a | 2020-06-10 16:42:49 -0400 | [diff] [blame] | 107 | endif() |
| 108 | endfunction() |
| 109 | |
Lei Zhang | dd21f32 | 2020-09-10 10:47:33 -0400 | [diff] [blame] | 110 | # iree_append_list_to_string |
| 111 | # |
| 112 | # Joins ${ARGN} together as a string separated by " " and appends it to |
| 113 | # ${VARIABLE}. |
| 114 | function(iree_append_list_to_string VARIABLE) |
| 115 | if(NOT "${ARGN}" STREQUAL "") |
| 116 | string(JOIN " " _ARGN_STR ${ARGN}) |
| 117 | set(${VARIABLE} "${${VARIABLE}} ${_ARGN_STR}" PARENT_SCOPE) |
| 118 | endif() |
| 119 | endfunction() |
| 120 | |
Lei Zhang | 7e8589a | 2020-06-10 16:42:49 -0400 | [diff] [blame] | 121 | |
| 122 | #------------------------------------------------------------------------------- |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 123 | # Packages and Paths |
| 124 | #------------------------------------------------------------------------------- |
| 125 | |
Ben Vanik | 512d2d3 | 2019-09-20 13:22:34 -0700 | [diff] [blame] | 126 | # Sets ${PACKAGE_NS} to the IREE-root relative package name in C++ namespace |
| 127 | # format (::). |
| 128 | # |
Scott Todd | 44f95f8 | 2022-06-01 09:59:32 -0700 | [diff] [blame] | 129 | # Examples: |
| 130 | # compiler/src/iree/compiler/Utils/CMakeLists.txt -> iree::compiler::Utils |
| 131 | # runtime/src/iree/base/CMakeLists.txt -> iree::base |
| 132 | # tests/e2e/CMakeLists.txt -> iree::tests::e2e |
Ben Vanik | 512d2d3 | 2019-09-20 13:22:34 -0700 | [diff] [blame] | 133 | function(iree_package_ns PACKAGE_NS) |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 134 | # Get the relative path of the current dir (i.e. runtime/src/iree/vm). |
CindyLiu | 4f683bc | 2022-07-11 17:14:25 +0000 | [diff] [blame] | 135 | string(REPLACE ${IREE_ROOT_DIR} "" _IREE_RELATIVE_PATH ${CMAKE_CURRENT_LIST_DIR}) |
| 136 | string(SUBSTRING ${_IREE_RELATIVE_PATH} 1 -1 _IREE_RELATIVE_PATH) |
| 137 | |
| 138 | if(NOT ${CMAKE_CURRENT_LIST_DIR} MATCHES "^${IREE_ROOT_DIR}/.*") |
| 139 | # Function is being called from outside IREE. Use the source-relative path. |
| 140 | # Please check the README.md to see the potential risk. |
| 141 | string(REPLACE ${PROJECT_SOURCE_DIR} "" _SOURCE_RELATIVE_PATH ${CMAKE_CURRENT_LIST_DIR}) |
| 142 | string(SUBSTRING ${_SOURCE_RELATIVE_PATH} 1 -1 _SOURCE_RELATIVE_PATH) |
| 143 | set(_PACKAGE "${_SOURCE_RELATIVE_PATH}") |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 144 | |
Stella Laurenzo | 1e8d1fa | 2022-04-22 09:50:43 -0700 | [diff] [blame] | 145 | # If changing the directory/package mapping rules, please also implement |
| 146 | # the corresponding rule in: |
| 147 | # build_tools/bazel_to_cmake/bazel_to_cmake_targets.py |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 148 | # Some sub-trees form their own roots for package purposes. Rewrite them. |
CindyLiu | 4f683bc | 2022-07-11 17:14:25 +0000 | [diff] [blame] | 149 | elseif(_IREE_RELATIVE_PATH MATCHES "^compiler/src/(.*)") |
Stella Laurenzo | 41a2ceb | 2022-04-29 12:49:36 -0700 | [diff] [blame] | 150 | # compiler/src/iree/compiler -> iree/compiler |
| 151 | set(_PACKAGE "${CMAKE_MATCH_1}") |
CindyLiu | 4f683bc | 2022-07-11 17:14:25 +0000 | [diff] [blame] | 152 | elseif(_IREE_RELATIVE_PATH MATCHES "^runtime/src/(.*)") |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 153 | # runtime/src/iree/base -> iree/base |
| 154 | set(_PACKAGE "${CMAKE_MATCH_1}") |
CindyLiu | 4f683bc | 2022-07-11 17:14:25 +0000 | [diff] [blame] | 155 | elseif(_IREE_RELATIVE_PATH MATCHES "^tools$") |
Scott Todd | 44f95f8 | 2022-06-01 09:59:32 -0700 | [diff] [blame] | 156 | # Special case for tools/ -> "" (empty string) |
| 157 | # For example, tools/iree-compile -> iree-compile (no namespace) |
Scott Todd | f57ab75 | 2022-05-23 10:36:44 -0700 | [diff] [blame] | 158 | set(_PACKAGE "") |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 159 | else() |
Scott Todd | 44f95f8 | 2022-06-01 09:59:32 -0700 | [diff] [blame] | 160 | # Default to prefixing with iree/ |
CindyLiu | 4f683bc | 2022-07-11 17:14:25 +0000 | [diff] [blame] | 161 | set(_PACKAGE "iree/${_IREE_RELATIVE_PATH}") |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 162 | endif() |
| 163 | |
Scott Todd | f57ab75 | 2022-05-23 10:36:44 -0700 | [diff] [blame] | 164 | string(REPLACE "/" "::" _PACKAGE_NS "${_PACKAGE}") |
Stella Laurenzo | 1e8d1fa | 2022-04-22 09:50:43 -0700 | [diff] [blame] | 165 | |
| 166 | if(_DEBUG_IREE_PACKAGE_NAME) |
CindyLiu | 4f683bc | 2022-07-11 17:14:25 +0000 | [diff] [blame] | 167 | message(STATUS "iree_package_ns(): map ${_IREE_RELATIVE_PATH} -> ${_PACKAGE_NS}") |
Stella Laurenzo | 1e8d1fa | 2022-04-22 09:50:43 -0700 | [diff] [blame] | 168 | endif() |
| 169 | |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 170 | set(${PACKAGE_NS} ${_PACKAGE_NS} PARENT_SCOPE) |
Ben Vanik | 512d2d3 | 2019-09-20 13:22:34 -0700 | [diff] [blame] | 171 | endfunction() |
| 172 | |
| 173 | # Sets ${PACKAGE_NAME} to the IREE-root relative package name. |
| 174 | # |
| 175 | # Example when called from iree/base/CMakeLists.txt: |
| 176 | # iree_base |
| 177 | function(iree_package_name PACKAGE_NAME) |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 178 | iree_package_ns(_PACKAGE_NS) |
Scott Todd | f57ab75 | 2022-05-23 10:36:44 -0700 | [diff] [blame] | 179 | string(REPLACE "::" "_" _PACKAGE_NAME "${_PACKAGE_NS}") |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 180 | set(${PACKAGE_NAME} ${_PACKAGE_NAME} PARENT_SCOPE) |
Ben Vanik | 512d2d3 | 2019-09-20 13:22:34 -0700 | [diff] [blame] | 181 | endfunction() |
| 182 | |
| 183 | # Sets ${PACKAGE_PATH} to the IREE-root relative package path. |
| 184 | # |
| 185 | # Example when called from iree/base/CMakeLists.txt: |
| 186 | # iree/base |
| 187 | function(iree_package_path PACKAGE_PATH) |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 188 | iree_package_ns(_PACKAGE_NS) |
| 189 | string(REPLACE "::" "/" _PACKAGE_PATH ${_PACKAGE_NS}) |
| 190 | set(${PACKAGE_PATH} ${_PACKAGE_PATH} PARENT_SCOPE) |
| 191 | endfunction() |
| 192 | |
| 193 | # Sets ${PACKAGE_DIR} to the directory name of the current package. |
| 194 | # |
| 195 | # Example when called from iree/base/CMakeLists.txt: |
| 196 | # base |
| 197 | function(iree_package_dir PACKAGE_DIR) |
| 198 | iree_package_ns(_PACKAGE_NS) |
Scott Todd | f57ab75 | 2022-05-23 10:36:44 -0700 | [diff] [blame] | 199 | string(FIND "${_PACKAGE_NS}" "::" _END_OFFSET REVERSE) |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 200 | math(EXPR _END_OFFSET "${_END_OFFSET} + 2") |
| 201 | string(SUBSTRING ${_PACKAGE_NS} ${_END_OFFSET} -1 _PACKAGE_DIR) |
| 202 | set(${PACKAGE_DIR} ${_PACKAGE_DIR} PARENT_SCOPE) |
| 203 | endfunction() |
| 204 | |
| 205 | #------------------------------------------------------------------------------- |
| 206 | # select()-like Evaluation |
| 207 | #------------------------------------------------------------------------------- |
| 208 | |
| 209 | # Appends ${OPTS} with a list of values based on the current compiler. |
| 210 | # |
| 211 | # Example: |
| 212 | # iree_select_compiler_opts(COPTS |
| 213 | # CLANG |
| 214 | # "-Wno-foo" |
| 215 | # "-Wno-bar" |
| 216 | # CLANG_CL |
| 217 | # "/W3" |
| 218 | # GCC |
| 219 | # "-Wsome-old-flag" |
| 220 | # MSVC |
| 221 | # "/W3" |
| 222 | # ) |
| 223 | # |
| 224 | # Note that variables are allowed, making it possible to share options between |
| 225 | # different compiler targets. |
| 226 | function(iree_select_compiler_opts OPTS) |
| 227 | cmake_parse_arguments( |
| 228 | PARSE_ARGV 1 |
| 229 | _IREE_SELECTS |
| 230 | "" |
| 231 | "" |
Ben Vanik | c5f97a1 | 2022-06-09 12:51:47 -0700 | [diff] [blame] | 232 | "ALL;CLANG;CLANG_GTE_10;CLANG_CL;MSVC;GCC;CLANG_OR_GCC;MSVC_OR_CLANG_CL" |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 233 | ) |
Geoffrey Martin-Noble | 3fa4f8d | 2021-09-10 09:25:27 -0700 | [diff] [blame] | 234 | # OPTS is a variable containing the *name* of the variable being populated, so |
| 235 | # we need to dereference it twice. |
| 236 | set(_OPTS "${${OPTS}}") |
Ben Vanik | b8fe086 | 2019-09-25 09:26:03 -0700 | [diff] [blame] | 237 | list(APPEND _OPTS "${_IREE_SELECTS_ALL}") |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 238 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") |
Ben Vanik | b8fe086 | 2019-09-25 09:26:03 -0700 | [diff] [blame] | 239 | list(APPEND _OPTS "${_IREE_SELECTS_GCC}") |
| 240 | list(APPEND _OPTS "${_IREE_SELECTS_CLANG_OR_GCC}") |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 241 | elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") |
| 242 | if(MSVC) |
Ben Vanik | b8fe086 | 2019-09-25 09:26:03 -0700 | [diff] [blame] | 243 | list(APPEND _OPTS ${_IREE_SELECTS_CLANG_CL}) |
| 244 | list(APPEND _OPTS ${_IREE_SELECTS_MSVC_OR_CLANG_CL}) |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 245 | else() |
Ben Vanik | b8fe086 | 2019-09-25 09:26:03 -0700 | [diff] [blame] | 246 | list(APPEND _OPTS ${_IREE_SELECTS_CLANG}) |
Ben Vanik | c5f97a1 | 2022-06-09 12:51:47 -0700 | [diff] [blame] | 247 | if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 10) |
| 248 | list(APPEND _OPTS ${_IREE_SELECTS_CLANG_GTE_10}) |
| 249 | endif() |
Ben Vanik | b8fe086 | 2019-09-25 09:26:03 -0700 | [diff] [blame] | 250 | list(APPEND _OPTS ${_IREE_SELECTS_CLANG_OR_GCC}) |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 251 | endif() |
| 252 | elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
Ben Vanik | b8fe086 | 2019-09-25 09:26:03 -0700 | [diff] [blame] | 253 | list(APPEND _OPTS ${_IREE_SELECTS_MSVC}) |
| 254 | list(APPEND _OPTS ${_IREE_SELECTS_MSVC_OR_CLANG_CL}) |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 255 | else() |
| 256 | message(ERROR "Unknown compiler: ${CMAKE_CXX_COMPILER}") |
Ben Vanik | b8fe086 | 2019-09-25 09:26:03 -0700 | [diff] [blame] | 257 | list(APPEND _OPTS "") |
Ben Vanik | cc2aff9 | 2019-09-24 10:23:55 -0700 | [diff] [blame] | 258 | endif() |
Ben Vanik | b8fe086 | 2019-09-25 09:26:03 -0700 | [diff] [blame] | 259 | set(${OPTS} ${_OPTS} PARENT_SCOPE) |
Ben Vanik | 512d2d3 | 2019-09-20 13:22:34 -0700 | [diff] [blame] | 260 | endfunction() |
Scott Todd | 633148d | 2020-03-09 10:53:56 -0700 | [diff] [blame] | 261 | |
| 262 | #------------------------------------------------------------------------------- |
| 263 | # Data dependencies |
| 264 | #------------------------------------------------------------------------------- |
| 265 | |
| 266 | # Adds 'data' dependencies to a target. |
| 267 | # |
| 268 | # Parameters: |
| 269 | # NAME: name of the target to add data dependencies to |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 270 | # DATA: List of targets and/or files in the source tree (relative to the |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 271 | # project root). |
Scott Todd | 633148d | 2020-03-09 10:53:56 -0700 | [diff] [blame] | 272 | function(iree_add_data_dependencies) |
| 273 | cmake_parse_arguments( |
| 274 | _RULE |
| 275 | "" |
| 276 | "NAME" |
| 277 | "DATA" |
| 278 | ${ARGN} |
| 279 | ) |
| 280 | |
| 281 | if(NOT _RULE_DATA) |
| 282 | return() |
| 283 | endif() |
| 284 | |
| 285 | foreach(_DATA_LABEL ${_RULE_DATA}) |
| 286 | if(TARGET ${_DATA_LABEL}) |
| 287 | add_dependencies(${_RULE_NAME} ${_DATA_LABEL}) |
| 288 | else() |
| 289 | # Not a target, assume to be a file instead. |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 290 | set(_FILE_PATH ${_DATA_LABEL}) |
Scott Todd | 633148d | 2020-03-09 10:53:56 -0700 | [diff] [blame] | 291 | |
| 292 | # Create a target which copies the data file into the build directory. |
| 293 | # If this file is included in multiple rules, only create the target once. |
| 294 | string(REPLACE "::" "_" _DATA_TARGET ${_DATA_LABEL}) |
Stella Laurenzo | 9bde61b | 2022-04-21 15:37:14 -0700 | [diff] [blame] | 295 | string(REPLACE "/" "_" _DATA_TARGET ${_DATA_TARGET}) |
Scott Todd | 633148d | 2020-03-09 10:53:56 -0700 | [diff] [blame] | 296 | if(NOT TARGET ${_DATA_TARGET}) |
Stella Laurenzo | 749ce66 | 2021-09-21 18:34:26 -0700 | [diff] [blame] | 297 | set(_INPUT_PATH "${PROJECT_SOURCE_DIR}/${_FILE_PATH}") |
| 298 | set(_OUTPUT_PATH "${PROJECT_BINARY_DIR}/${_FILE_PATH}") |
Scott Todd | 633148d | 2020-03-09 10:53:56 -0700 | [diff] [blame] | 299 | add_custom_target(${_DATA_TARGET} |
| 300 | COMMAND ${CMAKE_COMMAND} -E copy ${_INPUT_PATH} ${_OUTPUT_PATH} |
| 301 | ) |
| 302 | endif() |
| 303 | |
| 304 | add_dependencies(${_RULE_NAME} ${_DATA_TARGET}) |
| 305 | endif() |
| 306 | endforeach() |
| 307 | endfunction() |
Lei Zhang | edc9b2a | 2020-06-04 16:53:52 -0400 | [diff] [blame] | 308 | |
Lei Zhang | 0c417da | 2020-06-29 19:36:00 -0400 | [diff] [blame] | 309 | #------------------------------------------------------------------------------- |
Scott Todd | 2deecf3 | 2022-11-18 13:06:13 -0800 | [diff] [blame] | 310 | # Emscripten |
| 311 | #------------------------------------------------------------------------------- |
| 312 | |
| 313 | # A global counter to guarantee unique names for js library files. |
| 314 | set(_LINK_JS_COUNTER 1) |
| 315 | |
| 316 | # Links a JavaScript library to a target using --js-library=file.js. |
| 317 | # |
| 318 | # This function is only supported when running under Emscripten (emcmake). |
| 319 | # This implementation is forked from `em_add_tracked_link_flag()` in |
| 320 | # https://github.com/emscripten-core/emscripten/blob/main/cmake/Modules/Platform/Emscripten.cmake |
| 321 | # with changes to be compatible with IREE project style and CMake conventions. |
| 322 | # |
| 323 | # Parameters: |
| 324 | # TARGET: Name of the target to link against |
| 325 | # SRCS: List of JavaScript source files to link |
| 326 | function(iree_link_js_library) |
| 327 | cmake_parse_arguments( |
| 328 | _RULE |
| 329 | "" |
| 330 | "TARGET" |
| 331 | "SRCS" |
| 332 | ${ARGN} |
| 333 | ) |
| 334 | |
| 335 | # Convert from aliased, possibly package-relative, names to target names. |
| 336 | iree_package_ns(_PACKAGE_NS) |
| 337 | string(REGEX REPLACE "^::" "${_PACKAGE_NS}::" _RULE_TARGET ${_RULE_TARGET}) |
| 338 | string(REPLACE "::" "_" _RULE_TARGET ${_RULE_TARGET}) |
| 339 | |
| 340 | foreach(_SRC_FILE ${_RULE_SRCS}) |
| 341 | # If the JS file is changed, we want to relink dependent binaries, but |
| 342 | # unfortunately it is not possible to make a link step depend directly on a |
| 343 | # source file. Instead, we must make a dummy no-op build target on that |
| 344 | # source file, and make the original target depend on that dummy target. |
| 345 | |
| 346 | # Sanitate the source .js filename to a good dummy filename. |
| 347 | get_filename_component(_JS_NAME "${_SRC_FILE}" NAME) |
| 348 | string(REGEX REPLACE "[/:\\\\.\ ]" "_" _DUMMY_JS_TARGET ${_JS_NAME}) |
| 349 | set(_DUMMY_LIB_NAME ${_RULE_TARGET}_${_LINK_JS_COUNTER}_${_DUMMY_JS_TARGET}) |
| 350 | set(_DUMMY_C_NAME "${CMAKE_BINARY_DIR}/${_DUMMY_JS_TARGET}_tracker.c") |
| 351 | |
| 352 | # Create a new static library target that with a single dummy .c file. |
| 353 | add_library(${_DUMMY_LIB_NAME} STATIC ${_DUMMY_C_NAME}) |
| 354 | # Make the dummy .c file depend on the .js file we are linking, so that if |
| 355 | # the .js file is edited, the dummy .c file, and hence the static library |
| 356 | # will be rebuild (no-op). This causes the main application to be |
| 357 | # relinked, which is what we want. This approach was recommended by |
| 358 | # http://www.cmake.org/pipermail/cmake/2010-May/037206.html |
| 359 | add_custom_command( |
| 360 | OUTPUT ${_DUMMY_C_NAME} |
| 361 | COMMAND ${CMAKE_COMMAND} -E touch ${_DUMMY_C_NAME} |
| 362 | DEPENDS ${_SRC_FILE} |
| 363 | ) |
| 364 | target_link_libraries(${_RULE_TARGET} |
| 365 | PUBLIC |
| 366 | ${_DUMMY_LIB_NAME} |
| 367 | ) |
| 368 | |
| 369 | # Link the js-library to the target. |
| 370 | # When a linked library starts with a "-" cmake will just add it to the |
| 371 | # linker command line as it is. The advantage of doing it this way is |
| 372 | # that the js-library will also be automatically linked to targets that |
| 373 | # depend on this target. |
| 374 | get_filename_component(_SRC_ABSOLUTE_PATH "${_SRC_FILE}" ABSOLUTE) |
| 375 | target_link_libraries(${_RULE_TARGET} |
| 376 | PUBLIC |
| 377 | "--js-library \"${_SRC_ABSOLUTE_PATH}\"" |
| 378 | ) |
| 379 | |
| 380 | math(EXPR _LINK_JS_COUNTER "${_LINK_JS_COUNTER} + 1") |
| 381 | endforeach() |
| 382 | endfunction() |
| 383 | |
| 384 | #------------------------------------------------------------------------------- |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 385 | # Tool symlinks |
| 386 | #------------------------------------------------------------------------------- |
| 387 | |
| 388 | # iree_symlink_tool |
| 389 | # |
| 390 | # Adds a command to TARGET which symlinks a tool from elsewhere |
| 391 | # (FROM_TOOL_TARGET_NAME) to a local file name (TO_EXE_NAME) in the current |
| 392 | # binary directory. |
| 393 | # |
| 394 | # Parameters: |
| 395 | # TARGET: Local target to which to add the symlink command (i.e. an |
| 396 | # iree_py_library, etc). |
| 397 | # FROM_TOOL_TARGET: Target of the tool executable that is the source of the |
| 398 | # link. |
| 399 | # TO_EXE_NAME: The executable name to output in the current binary dir. |
| 400 | function(iree_symlink_tool) |
| 401 | cmake_parse_arguments( |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 402 | _RULE |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 403 | "" |
| 404 | "TARGET;FROM_TOOL_TARGET;TO_EXE_NAME" |
| 405 | "" |
| 406 | ${ARGN} |
| 407 | ) |
| 408 | |
| 409 | # Transform TARGET |
| 410 | iree_package_ns(_PACKAGE_NS) |
| 411 | iree_package_name(_PACKAGE_NAME) |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 412 | set(_TARGET "${_PACKAGE_NAME}_${_RULE_TARGET}") |
| 413 | set(_FROM_TOOL_TARGET ${_RULE_FROM_TOOL_TARGET}) |
| 414 | set(_TO_TOOL_PATH "${CMAKE_CURRENT_BINARY_DIR}/${_RULE_TO_EXE_NAME}${CMAKE_EXECUTABLE_SUFFIX}") |
Geoffrey Martin-Noble | fd8a769 | 2021-08-20 05:54:39 -0700 | [diff] [blame] | 415 | get_filename_component(_TO_TOOL_DIR "${_TO_TOOL_PATH}" DIRECTORY) |
| 416 | |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 417 | |
| 418 | add_custom_command( |
| 419 | TARGET "${_TARGET}" |
| 420 | BYPRODUCTS |
Scott Todd | 1a1aea6 | 2022-04-28 09:12:11 -0700 | [diff] [blame] | 421 | "${CMAKE_CURRENT_BINARY_DIR}/${_RULE_TO_EXE_NAME}${CMAKE_EXECUTABLE_SUFFIX}" |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 422 | COMMAND |
Geoffrey Martin-Noble | fd8a769 | 2021-08-20 05:54:39 -0700 | [diff] [blame] | 423 | ${CMAKE_COMMAND} -E make_directory "${_TO_TOOL_DIR}" |
| 424 | COMMAND |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 425 | ${CMAKE_COMMAND} -E create_symlink |
| 426 | "$<TARGET_FILE:${_FROM_TOOL_TARGET}>" |
Geoffrey Martin-Noble | fd8a769 | 2021-08-20 05:54:39 -0700 | [diff] [blame] | 427 | "${_TO_TOOL_PATH}" |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 428 | ) |
| 429 | endfunction() |
| 430 | |
| 431 | |
| 432 | #------------------------------------------------------------------------------- |
Scott Todd | 6132bb3 | 2020-08-11 12:04:40 -0700 | [diff] [blame] | 433 | # Tests |
| 434 | #------------------------------------------------------------------------------- |
| 435 | |
Geoffrey Martin-Noble | 527b7a0 | 2021-10-27 06:19:17 -0700 | [diff] [blame] | 436 | # iree_check_defined |
| 437 | # |
| 438 | # A lightweight way to check that all the given variables are defined. Useful |
| 439 | # in cases like checking that a function has been passed all required arguments. |
| 440 | # Doesn't give usage-specific error messages, but still significantly better |
| 441 | # than no error checking. |
| 442 | # Variable names should be passed directly without quoting or dereferencing. |
| 443 | # Example: |
| 444 | # iree_check_defined(_SOME_VAR _AND_ANOTHER_VAR) |
| 445 | macro(iree_check_defined) |
| 446 | foreach(_VAR ${ARGN}) |
| 447 | if(NOT DEFINED "${_VAR}") |
| 448 | message(SEND_ERROR "${_VAR} is not defined") |
| 449 | endif() |
| 450 | endforeach() |
| 451 | endmacro() |
| 452 | |
| 453 | # iree_validate_required_arguments |
| 454 | # |
| 455 | # Validates that no arguments went unparsed or were given no values and that all |
| 456 | # required arguments have values. Expects to be called after |
| 457 | # cmake_parse_arguments and verifies that the variables it creates have been |
| 458 | # populated as appropriate. |
| 459 | function(iree_validate_required_arguments |
| 460 | PREFIX |
| 461 | REQUIRED_ONE_VALUE_KEYWORDS |
| 462 | REQUIRED_MULTI_VALUE_KEYWORDS) |
| 463 | if(DEFINED ${PREFIX}_UNPARSED_ARGUMENTS) |
| 464 | message(SEND_ERROR "Unparsed argument(s): '${${PREFIX}_UNPARSED_ARGUMENTS}'") |
| 465 | endif() |
| 466 | if(DEFINED ${PREFIX}_KEYWORDS_MISSING_VALUES) |
| 467 | message(SEND_ERROR |
| 468 | "No values for field(s) '${${PREFIX}_KEYWORDS_MISSING_VALUES}'") |
| 469 | endif() |
| 470 | |
Geoffrey Martin-Noble | 66d4889 | 2021-10-29 12:24:58 -0700 | [diff] [blame] | 471 | foreach(_KEYWORD IN LISTS REQUIRED_ONE_VALUE_KEYWORDS REQUIRED_MULTI_VALUE_KEYWORDS) |
| 472 | if(NOT DEFINED ${PREFIX}_${_KEYWORD}) |
| 473 | message(SEND_ERROR "Missing required argument ${_KEYWORD}") |
Geoffrey Martin-Noble | 527b7a0 | 2021-10-27 06:19:17 -0700 | [diff] [blame] | 474 | endif() |
| 475 | endforeach() |
| 476 | endfunction() |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 477 | |
| 478 | # iree_compile_flags_for_patform |
| 479 | # |
| 480 | # Helper function to add necessary compile flags based on platform-specific |
| 481 | # configurations. Note the flags are added for cpu backends only. |
| 482 | function(iree_compile_flags_for_platform OUT_FLAGS IN_FLAGS) |
| 483 | if(NOT (IN_FLAGS MATCHES "iree-hal-target-backends=llvm-cpu" OR |
| 484 | IN_FLAGS MATCHES "iree-hal-target-backends=vmvx")) |
| 485 | set(${OUT_FLAGS} "" PARENT_SCOPE) |
| 486 | return() |
| 487 | endif() |
| 488 | |
Ben Vanik | 338b979 | 2023-03-08 19:15:22 -0800 | [diff] [blame] | 489 | if(ANDROID AND NOT IN_FLAGS MATCHES "iree-llvmcpu-target-triple") |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 490 | # Android's CMake toolchain defines some variables that we can use to infer |
| 491 | # the appropriate target triple from the configured settings: |
| 492 | # https://developer.android.com/ndk/guides/cmake#android_platform |
| 493 | # |
| 494 | # In typical CMake fashion, the various strings are pretty fuzzy and can |
| 495 | # have multiple values like "latest", "android-25"/"25"/"android-N-MR1". |
| 496 | # |
| 497 | # From looking at the toolchain file, ANDROID_PLATFORM_LEVEL seems like it |
| 498 | # should pretty consistently be just a number we can use for target triple. |
| 499 | set(_TARGET_TRIPLE "aarch64-none-linux-android${ANDROID_PLATFORM_LEVEL}") |
Ben Vanik | 380bde7 | 2023-03-08 10:55:58 -0800 | [diff] [blame] | 500 | list(APPEND _FLAGS "--iree-llvmcpu-target-triple=${_TARGET_TRIPLE}") |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 501 | endif() |
| 502 | |
bjacob | 8f5ced9 | 2023-03-20 20:20:36 -0400 | [diff] [blame^] | 503 | if(IREE_ARCH STREQUAL "riscv_64" AND |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 504 | CMAKE_SYSTEM_NAME STREQUAL "Linux" AND |
Ben Vanik | 338b979 | 2023-03-08 19:15:22 -0800 | [diff] [blame] | 505 | NOT IN_FLAGS MATCHES "iree-llvmcpu-target-triple") |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 506 | # RV64 Linux crosscompile toolchain can support iree-compile with |
| 507 | # specific CPU flags. Add the llvm flags to support RV64 RVV codegen if |
| 508 | # llvm-target-triple is not specified. |
| 509 | list(APPEND _FLAGS ${RISCV64_TEST_DEFAULT_LLVM_FLAGS}) |
bjacob | 8f5ced9 | 2023-03-20 20:20:36 -0400 | [diff] [blame^] | 510 | elseif(IREE_ARCH STREQUAL "riscv_32" AND |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 511 | CMAKE_SYSTEM_NAME STREQUAL "Linux" AND |
Ben Vanik | 338b979 | 2023-03-08 19:15:22 -0800 | [diff] [blame] | 512 | NOT IN_FLAGS MATCHES "iree-llvmcpu-target-triple") |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 513 | # RV32 Linux crosscompile toolchain can support iree-compile with |
| 514 | # specific CPU flags. Add the llvm flags to support RV32 RVV codegen if |
| 515 | # llvm-target-triple is not specified. |
| 516 | list(APPEND _FLAGS ${RISCV32_TEST_DEFAULT_LLVM_FLAGS}) |
| 517 | endif() |
| 518 | |
Ben Vanik | 338b979 | 2023-03-08 19:15:22 -0800 | [diff] [blame] | 519 | if(EMSCRIPTEN AND NOT IN_FLAGS MATCHES "iree-llvmcpu-target-triple") |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 520 | set(_EMSCRIPTEN_TEST_DEFAULT_FLAGS |
Ben Vanik | 380bde7 | 2023-03-08 10:55:58 -0800 | [diff] [blame] | 521 | "--iree-llvmcpu-target-triple=wasm32-unknown-emscripten" |
CindyLiu | c70b699 | 2022-10-03 09:57:33 -0700 | [diff] [blame] | 522 | ) |
| 523 | list(APPEND _FLAGS ${_EMSCRIPTEN_TEST_DEFAULT_FLAGS}) |
| 524 | endif() |
| 525 | |
| 526 | set(${OUT_FLAGS} "${_FLAGS}" PARENT_SCOPE) |
| 527 | endfunction() |