1"""Build definitions and rules for XNNPACK.""" 2 3load(":emscripten.bzl", "xnnpack_emscripten_benchmark_linkopts", "xnnpack_emscripten_deps", "xnnpack_emscripten_minimal_linkopts", "xnnpack_emscripten_test_linkopts") 4 5def xnnpack_visibility(): 6 """Visibility of :XNNPACK target. 7 8 All other targets have private visibility, and can not have external 9 dependencies. 10 """ 11 return ["//visibility:public"] 12 13def xnnpack_min_size_copts(): 14 """Compiler flags for size-optimized builds.""" 15 return ["-Os"] 16 17def xnnpack_gcc_std_copts(): 18 """GCC-like compiler flags to specify language standard for C sources.""" 19 return ["-std=c99"] 20 21def xnnpack_msvc_std_copts(): 22 """MSVC compiler flags to specify language standard for C sources.""" 23 return ["/Drestrict="] 24 25def xnnpack_std_cxxopts(): 26 """Compiler flags to specify language standard for C++ sources.""" 27 return ["-std=gnu++11"] 28 29def xnnpack_optional_ruy_copts(): 30 """Compiler flags to optionally enable Ruy benchmarks.""" 31 return [] 32 33def xnnpack_optional_gemmlowp_copts(): 34 """Compiler flags to optionally enable Gemmlowp benchmarks.""" 35 return [] 36 37def xnnpack_optional_tflite_copts(): 38 """Compiler flags to optionally enable TensorFlow Lite benchmarks.""" 39 return [] 40 41def xnnpack_optional_armcl_copts(): 42 """Compiler flags to optionally enable ARM ComputeLibrary benchmarks.""" 43 return [] 44 45def xnnpack_optional_dnnl_copts(): 46 """Compiler flags to optionally enable Intel DNNL benchmarks.""" 47 return [] 48 49def xnnpack_optional_ruy_deps(): 50 """Optional Ruy dependencies.""" 51 return [] 52 53def xnnpack_optional_gemmlowp_deps(): 54 """Optional Gemmlowp dependencies.""" 55 return [] 56 57def xnnpack_optional_tflite_deps(): 58 """Optional TensorFlow Lite dependencies.""" 59 return [] 60 61def xnnpack_optional_armcl_deps(): 62 """Optional ARM ComputeLibrary dependencies.""" 63 return [] 64 65def xnnpack_optional_dnnl_deps(): 66 """Optional Intel DNNL dependencies.""" 67 return [] 68 69def xnnpack_cc_library( 70 name, 71 srcs = [], 72 x86_srcs = [], 73 aarch32_srcs = [], 74 aarch64_srcs = [], 75 wasm_srcs = [], 76 wasmsimd_srcs = [], 77 copts = [], 78 gcc_copts = [], 79 msvc_copts = [], 80 mingw_copts = [], 81 msys_copts = [], 82 gcc_x86_copts = [], 83 msvc_x86_32_copts = [], 84 msvc_x86_64_copts = [], 85 apple_aarch32_copts = [], 86 aarch32_copts = [], 87 aarch64_copts = [], 88 wasm_copts = [], 89 wasmsimd_copts = [], 90 optimized_copts = ["-O2"], 91 hdrs = [], 92 defines = [], 93 includes = [], 94 deps = [], 95 visibility = []): 96 """C/C++/assembly library with architecture-specific configuration. 97 98 Define a static library with architecture- and instruction-specific 99 source files and/or compiler flags. 100 101 Args: 102 name: The name of the library target to define. 103 srcs: The list of architecture-independent source files. 104 x86_srcs: The list of x86-specific source files. 105 aarch32_srcs: The list of AArch32-specific source files. 106 aarch64_srcs: The list of AArch64-specific source files. 107 wasm_srcs: The list of WebAssembly/MVP-specific source files. 108 wasmsimd_srcs: The list of WebAssembly/SIMD-specific source files. 109 copts: The list of compiler flags to use in all builds. -I flags for 110 include/ and src/ directories of XNNPACK are always prepended 111 before these user-specified flags. 112 gcc_copts: The list of compiler flags to use with GCC-like compilers. 113 msvc_copts: The list of compiler flags to use with MSVC compiler. 114 mingw_copts: The list of compiler flags to use with MinGW GCC compilers. 115 msys_copts: The list of compiler flags to use with MSYS (Cygwin) GCC 116 compilers. 117 gcc_x86_copts: The list of GCC-like compiler flags to use in x86 (32-bit 118 and 64-bit) builds. 119 msvc_x86_32_copts: The list of MSVC compiler flags to use in x86 (32-bit) 120 builds. 121 msvc_x86_64_copts: The list of MSVC compiler flags to use in x86 (64-bit) 122 builds. 123 apple_aarch32_copts: The list of compiler flags to use in AArch32 builds 124 with Apple Clang. 125 aarch32_copts: The list of compiler flags to use in AArch32 builds. 126 aarch64_copts: The list of compiler flags to use in AArch64 builds. 127 wasm_copts: The list of compiler flags to use in WebAssembly/MVP builds. 128 wasmsimd_copts: The list of compiler flags to use in WebAssembly/SIMD 129 builds. 130 optimized_copts: The list of compiler flags to use in optimized builds. 131 Defaults to -O2. 132 hdrs: The list of header files published by this library to be textually 133 included by sources in dependent rules. 134 defines: List of predefines macros to be added to the compile line. 135 includes: List of include dirs to be added to the compile line. 136 deps: The list of other libraries to be linked. 137 visibility: The list of packages that can depend on this target. 138 """ 139 native.cc_library( 140 name = name, 141 srcs = srcs + select({ 142 ":linux_k8": x86_srcs, 143 ":linux_arm": aarch32_srcs, 144 ":linux_armeabi": aarch32_srcs, 145 ":linux_armhf": aarch32_srcs, 146 ":linux_armv7a": aarch32_srcs, 147 ":linux_aarch64": aarch64_srcs, 148 ":macos_x86_64": x86_srcs, 149 ":macos_arm64": aarch64_srcs, 150 ":windows_x86_64_clang": x86_srcs, 151 ":windows_x86_64_mingw": x86_srcs, 152 ":windows_x86_64_msys": x86_srcs, 153 ":windows_x86_64": x86_srcs, 154 ":android_armv7": aarch32_srcs, 155 ":android_arm64": aarch64_srcs, 156 ":android_x86": x86_srcs, 157 ":android_x86_64": x86_srcs, 158 ":ios_armv7": aarch32_srcs, 159 ":ios_arm64": aarch64_srcs, 160 ":ios_arm64e": aarch64_srcs, 161 ":ios_x86": x86_srcs, 162 ":ios_x86_64": x86_srcs, 163 ":watchos_armv7k": aarch32_srcs, 164 ":watchos_arm64_32": aarch64_srcs, 165 ":watchos_x86": x86_srcs, 166 ":watchos_x86_64": x86_srcs, 167 ":tvos_arm64": aarch64_srcs, 168 ":tvos_x86_64": x86_srcs, 169 ":emscripten_wasm": wasm_srcs, 170 ":emscripten_wasmsimd": wasmsimd_srcs, 171 "//conditions:default": [], 172 }), 173 copts = [ 174 "-Iinclude", 175 "-Isrc", 176 ] + copts + select({ 177 ":linux_k8": gcc_x86_copts, 178 ":linux_arm": aarch32_copts, 179 ":linux_armeabi": aarch32_copts, 180 ":linux_armhf": aarch32_copts, 181 ":linux_armv7a": aarch32_copts, 182 ":linux_aarch64": aarch64_copts, 183 ":macos_x86_64": gcc_x86_copts, 184 ":macos_arm64": aarch64_copts, 185 ":windows_x86_64_clang": ["/clang:" + opt for opt in gcc_x86_copts], 186 ":windows_x86_64_mingw": mingw_copts + gcc_x86_copts, 187 ":windows_x86_64_msys": msys_copts + gcc_x86_copts, 188 ":windows_x86_64": msvc_x86_64_copts, 189 ":android_armv7": aarch32_copts, 190 ":android_arm64": aarch64_copts, 191 ":android_x86": gcc_x86_copts, 192 ":android_x86_64": gcc_x86_copts, 193 ":ios_armv7": apple_aarch32_copts, 194 ":ios_arm64": aarch64_copts, 195 ":ios_arm64e": aarch64_copts, 196 ":ios_x86": gcc_x86_copts, 197 ":ios_x86_64": gcc_x86_copts, 198 ":watchos_armv7k": apple_aarch32_copts, 199 ":watchos_arm64_32": aarch64_copts, 200 ":watchos_x86": gcc_x86_copts, 201 ":watchos_x86_64": gcc_x86_copts, 202 ":tvos_arm64": aarch64_copts, 203 ":tvos_x86_64": gcc_x86_copts, 204 ":emscripten_wasm": wasm_copts, 205 ":emscripten_wasmsimd": wasmsimd_copts, 206 "//conditions:default": [], 207 }) + select({ 208 ":windows_x86_64_clang": ["/clang:" + opt for opt in gcc_copts], 209 ":windows_x86_64_mingw": gcc_copts, 210 ":windows_x86_64_msys": gcc_copts, 211 ":windows_x86_64": msvc_copts, 212 "//conditions:default": gcc_copts, 213 }) + select({ 214 ":optimized_build": optimized_copts, 215 "//conditions:default": [], 216 }), 217 defines = defines, 218 deps = deps, 219 includes = ["include", "src"] + includes, 220 linkstatic = True, 221 linkopts = select({ 222 ":linux_k8": ["-lpthread"], 223 ":linux_arm": ["-lpthread"], 224 ":linux_armeabi": ["-lpthread"], 225 ":linux_armhf": ["-lpthread"], 226 ":linux_armv7a": ["-lpthread"], 227 ":linux_aarch64": ["-lpthread"], 228 ":android": ["-lm"], 229 "//conditions:default": [], 230 }), 231 textual_hdrs = hdrs, 232 visibility = visibility, 233 ) 234 235def xnnpack_aggregate_library( 236 name, 237 generic_deps = [], 238 x86_deps = [], 239 aarch32_deps = [], 240 aarch64_deps = [], 241 wasm_deps = [], 242 wasmsimd_deps = []): 243 """Static library that aggregates architecture-specific dependencies. 244 245 Args: 246 name: The name of the library target to define. 247 generic_deps: The list of libraries to link on all architectures. 248 x86_deps: The list of libraries to link in x86 and x86-64 builds. 249 aarch32_deps: The list of libraries to link in AArch32 builds. 250 aarch64_deps: The list of libraries to link in AArch32 builds. 251 wasm_deps: The list of libraries to link in WebAssembly (MVP) builds. 252 wasmsimd_deps: The list of libraries to link in WebAssembly SIMD builds. 253 """ 254 255 native.cc_library( 256 name = name, 257 linkstatic = True, 258 deps = generic_deps + select({ 259 ":linux_k8": x86_deps, 260 ":linux_arm": aarch32_deps, 261 ":linux_armeabi": aarch32_deps, 262 ":linux_armhf": aarch32_deps, 263 ":linux_armv7a": aarch32_deps, 264 ":linux_aarch64": aarch64_deps, 265 ":macos_x86_64": x86_deps, 266 ":macos_arm64": aarch64_deps, 267 ":windows_x86_64_clang": x86_deps, 268 ":windows_x86_64_mingw": x86_deps, 269 ":windows_x86_64_msys": x86_deps, 270 ":windows_x86_64": x86_deps, 271 ":android_armv7": aarch32_deps, 272 ":android_arm64": aarch64_deps, 273 ":android_x86": x86_deps, 274 ":android_x86_64": x86_deps, 275 ":ios_armv7": aarch32_deps, 276 ":ios_arm64": aarch64_deps, 277 ":ios_arm64e": aarch64_deps, 278 ":ios_x86": x86_deps, 279 ":ios_x86_64": x86_deps, 280 ":watchos_armv7k": aarch32_deps, 281 ":watchos_arm64_32": aarch64_deps, 282 ":watchos_x86": x86_deps, 283 ":watchos_x86_64": x86_deps, 284 ":tvos_arm64": aarch64_deps, 285 ":tvos_x86_64": x86_deps, 286 ":emscripten_wasm": wasm_deps, 287 ":emscripten_wasmsimd": wasmsimd_deps, 288 }), 289 ) 290 291def xnnpack_unit_test(name, srcs, copts = [], mingw_copts = [], msys_copts = [], deps = [], tags = [], automatic = True, timeout = "short"): 292 """Unit test binary based on Google Test. 293 294 Args: 295 name: The name of the test target to define. 296 srcs: The list of source and header files. 297 copts: The list of additional compiler flags for the target. -I flags 298 for include/ and src/ directories of XNNPACK are always prepended 299 before these user-specified flags. 300 mingw_copts: The list of compiler flags to use with MinGW GCC compilers. 301 msys_copts: The list of compiler flags to use with MSYS (Cygwin) GCC compilers. 302 deps: The list of additional libraries to be linked. Google Test library 303 (with main() function) is always added as a dependency and does not 304 need to be explicitly specified. 305 tags: List of arbitrary text tags. 306 automatic: Whether to create the test or testable binary. 307 timeout: How long the test is expected to run before returning. 308 """ 309 310 if automatic: 311 native.cc_test( 312 name = name, 313 srcs = srcs, 314 copts = xnnpack_std_cxxopts() + [ 315 "-Iinclude", 316 "-Isrc", 317 ] + select({ 318 ":windows_x86_64_mingw": mingw_copts, 319 ":windows_x86_64_msys": msys_copts, 320 "//conditions:default": [], 321 }) + select({ 322 ":windows_x86_64_clang": ["/clang:-Wno-unused-function"], 323 ":windows_x86_64_mingw": ["-Wno-unused-function"], 324 ":windows_x86_64_msys": ["-Wno-unused-function"], 325 ":windows_x86_64": [], 326 "//conditions:default": ["-Wno-unused-function"], 327 }) + copts, 328 linkopts = select({ 329 ":emscripten": xnnpack_emscripten_test_linkopts(), 330 "//conditions:default": [], 331 }), 332 linkstatic = True, 333 deps = [ 334 "@com_google_googletest//:gtest_main", 335 ] + deps + select({ 336 ":emscripten": xnnpack_emscripten_deps(), 337 "//conditions:default": [], 338 }), 339 tags = tags, 340 timeout = timeout, 341 ) 342 else: 343 native.cc_binary( 344 name = name, 345 srcs = srcs, 346 copts = xnnpack_std_cxxopts() + [ 347 "-Iinclude", 348 "-Isrc", 349 ] + select({ 350 ":windows_x86_64_mingw": mingw_copts, 351 ":windows_x86_64_msys": msys_copts, 352 "//conditions:default": [], 353 }) + select({ 354 ":windows_x86_64_clang": ["/clang:-Wno-unused-function"], 355 ":windows_x86_64_mingw": ["-Wno-unused-function"], 356 ":windows_x86_64_msys": ["-Wno-unused-function"], 357 ":windows_x86_64": [], 358 "//conditions:default": ["-Wno-unused-function"], 359 }) + copts, 360 linkopts = select({ 361 ":emscripten": xnnpack_emscripten_test_linkopts(), 362 "//conditions:default": [], 363 }), 364 linkstatic = True, 365 deps = [ 366 "@com_google_googletest//:gtest_main", 367 ] + deps + select({ 368 ":emscripten": xnnpack_emscripten_deps(), 369 "//conditions:default": [], 370 }), 371 testonly = True, 372 tags = tags, 373 ) 374 375def xnnpack_binary(name, srcs, copts = [], deps = []): 376 """Minimal binary 377 378 Args: 379 name: The name of the binary target to define. 380 srcs: The list of source and header files. 381 copts: The list of additional compiler flags for the target. -I flags 382 for include/ and src/ directories of XNNPACK are always prepended 383 before these user-specified flags. 384 deps: The list of libraries to be linked. 385 """ 386 native.cc_binary( 387 name = name, 388 srcs = srcs, 389 copts = [ 390 "-Iinclude", 391 "-Isrc", 392 ] + copts, 393 linkopts = select({ 394 ":emscripten": xnnpack_emscripten_minimal_linkopts(), 395 "//conditions:default": [], 396 }), 397 linkstatic = True, 398 deps = deps, 399 ) 400 401def xnnpack_benchmark(name, srcs, copts = [], deps = [], tags = []): 402 """Microbenchmark binary based on Google Benchmark 403 404 Args: 405 name: The name of the binary target to define. 406 srcs: The list of source and header files. 407 copts: The list of additional compiler flags for the target. -I flags 408 for include/ and src/ directories of XNNPACK are always prepended 409 before these user-specified flags. 410 deps: The list of additional libraries to be linked. Google Benchmark 411 library is always added as a dependency and does not need to be 412 explicitly specified. 413 """ 414 native.cc_binary( 415 name = name, 416 srcs = srcs, 417 copts = xnnpack_std_cxxopts() + [ 418 "-Iinclude", 419 "-Isrc", 420 ] + select({ 421 ":windows_x86_64_clang": ["/clang:-Wno-unused-function"], 422 ":windows_x86_64_mingw": ["-Wno-unused-function"], 423 ":windows_x86_64_msys": ["-Wno-unused-function"], 424 ":windows_x86_64": [], 425 "//conditions:default": ["-Wno-unused-function"], 426 }) + copts, 427 linkopts = select({ 428 ":emscripten": xnnpack_emscripten_benchmark_linkopts(), 429 ":windows_x86_64_mingw": ["-lshlwapi"], 430 ":windows_x86_64_msys": ["-lshlwapi"], 431 "//conditions:default": [], 432 }), 433 linkstatic = True, 434 deps = [ 435 "@com_google_benchmark//:benchmark", 436 ] + deps + select({ 437 ":emscripten": xnnpack_emscripten_deps(), 438 "//conditions:default": [], 439 }), 440 tags = tags, 441 ) 442