Marat Dukhan | 08c4a43 | 2019-10-03 09:29:21 -0700 | [diff] [blame] | 1 | """Build definitions and rules for XNNPACK.""" |
| 2 | |
| 3 | load(":emscripten.bzl", "xnnpack_emscripten_benchmark_linkopts", "xnnpack_emscripten_deps", "xnnpack_emscripten_minimal_linkopts", "xnnpack_emscripten_test_linkopts") |
| 4 | |
| 5 | def xnnpack_visibility(): |
| 6 | """Visibility of :XNNPACK target. |
| 7 | |
| 8 | All other targets have private visibility, and can not have external |
| 9 | dependencies. |
| 10 | """ |
Marat Dukhan | 9d056a4 | 2019-10-03 12:13:35 -0700 | [diff] [blame] | 11 | return ["//visibility:public"] |
Marat Dukhan | 08c4a43 | 2019-10-03 09:29:21 -0700 | [diff] [blame] | 12 | |
| 13 | def xnnpack_min_size_copts(): |
| 14 | """Compiler flags for size-optimized builds.""" |
| 15 | return ["-Os"] |
| 16 | |
| 17 | def xnnpack_std_copts(): |
| 18 | """Compiler flags to specify language standard for C sources.""" |
| 19 | return ["-std=c99"] |
| 20 | |
| 21 | def xnnpack_std_cxxopts(): |
| 22 | """Compiler flags to specify language standard for C++ sources.""" |
| 23 | return ["-std=gnu++11"] |
| 24 | |
| 25 | def xnnpack_optional_ruy_copts(): |
| 26 | """Compiler flags to optionally enable Ruy benchmarks.""" |
| 27 | return [] |
| 28 | |
| 29 | def xnnpack_optional_gemmlowp_copts(): |
| 30 | """Compiler flags to optionally enable Gemmlowp benchmarks.""" |
| 31 | return [] |
| 32 | |
| 33 | def xnnpack_optional_tflite_copts(): |
| 34 | """Compiler flags to optionally enable TensorFlow Lite benchmarks.""" |
| 35 | return [] |
| 36 | |
| 37 | def xnnpack_optional_armcl_copts(): |
| 38 | """Compiler flags to optionally enable ARM ComputeLibrary benchmarks.""" |
| 39 | return [] |
| 40 | |
| 41 | def xnnpack_optional_ruy_deps(): |
| 42 | """Optional Ruy dependencies.""" |
| 43 | return [] |
| 44 | |
| 45 | def xnnpack_optional_gemmlowp_deps(): |
| 46 | """Optional Gemmlowp dependencies.""" |
| 47 | return [] |
| 48 | |
| 49 | def xnnpack_optional_tflite_deps(): |
| 50 | """Optional TensorFlow Lite dependencies.""" |
| 51 | return [] |
| 52 | |
| 53 | def xnnpack_optional_armcl_deps(): |
| 54 | """Optional ARM ComputeLibrary dependencies.""" |
| 55 | return [] |
| 56 | |
| 57 | def xnnpack_cc_library( |
| 58 | name, |
| 59 | srcs = [], |
| 60 | x86_srcs = [], |
| 61 | aarch32_srcs = [], |
| 62 | aarch64_srcs = [], |
| 63 | copts = [], |
| 64 | x86_copts = [], |
| 65 | aarch32_copts = [], |
| 66 | aarch64_copts = [], |
| 67 | optimized_copts = ["-O2"], |
| 68 | hdrs = [], |
| 69 | deps = []): |
| 70 | """C/C++/assembly library with architecture-specific sources. |
| 71 | |
| 72 | Define a static library with architecture- and instruction-specific |
| 73 | source files. |
| 74 | |
| 75 | Args: |
| 76 | name: The name of the library target to define. |
| 77 | srcs: The list of architecture-independent source files. |
| 78 | x86_srcs: The list of x86-specific source files. |
| 79 | aarch32_srcs: The list of AArch32-specific source files. |
| 80 | aarch64_srcs: The list of AArch64-specific source files. |
| 81 | copts: The list of compiler flags to use in all builds. -I flags for |
| 82 | include/ and src/ directories of XNNPACK are always prepended |
| 83 | before these user-specified flags. |
| 84 | x86_copts: The list of compiler flags to use in x86 builds. |
| 85 | aarch32_copts: The list of compiler flags to use in AArch32 builds. |
| 86 | aarch64_copts: The list of compiler flags to use in AArch64 builds. |
| 87 | optimized_copts: The list of compiler flags to use in optimized builds. |
| 88 | Defaults to -O2. |
| 89 | hdrs: The list of header files published by this library to be textually |
| 90 | included by sources in dependent rules. |
| 91 | deps: The list of other libraries to be linked. |
| 92 | """ |
| 93 | native.cc_library( |
| 94 | name = name, |
| 95 | srcs = srcs + select({ |
| 96 | ":linux_k8": x86_srcs, |
Marat Dukhan | 4e45e66 | 2019-10-03 15:40:24 -0700 | [diff] [blame] | 97 | ":linux_aarch64": aarch64_srcs, |
Marat Dukhan | 885ca24 | 2019-10-07 09:17:32 -0700 | [diff] [blame] | 98 | ":macos_x86_64": x86_srcs, |
Marat Dukhan | 08c4a43 | 2019-10-03 09:29:21 -0700 | [diff] [blame] | 99 | ":android_armv7": aarch32_srcs, |
| 100 | ":android_arm64": aarch64_srcs, |
| 101 | ":android_x86": x86_srcs, |
| 102 | ":android_x86_64": x86_srcs, |
| 103 | "//conditions:default": [], |
| 104 | }), |
| 105 | copts = [ |
| 106 | "-Iinclude", |
| 107 | "-Isrc", |
| 108 | ] + copts + select({ |
| 109 | ":linux_k8": x86_copts, |
Marat Dukhan | 4e45e66 | 2019-10-03 15:40:24 -0700 | [diff] [blame] | 110 | ":linux_aarch64": aarch64_copts, |
Marat Dukhan | 885ca24 | 2019-10-07 09:17:32 -0700 | [diff] [blame] | 111 | ":macos_x86_64": x86_copts, |
Marat Dukhan | 08c4a43 | 2019-10-03 09:29:21 -0700 | [diff] [blame] | 112 | ":android_armv7": aarch32_copts, |
| 113 | ":android_arm64": aarch64_copts, |
| 114 | ":android_x86": x86_copts, |
| 115 | ":android_x86_64": x86_copts, |
| 116 | "//conditions:default": [], |
| 117 | }) + select({ |
| 118 | ":optimized_build": optimized_copts, |
| 119 | "//conditions:default": [], |
| 120 | }), |
Daniel Smilkov | 0c57d2a | 2019-10-07 10:06:44 -0700 | [diff] [blame^] | 121 | includes = ["include", "src"], |
Marat Dukhan | 08c4a43 | 2019-10-03 09:29:21 -0700 | [diff] [blame] | 122 | linkstatic = True, |
| 123 | linkopts = select({ |
| 124 | ":linux_k8": ["-lpthread"], |
Marat Dukhan | 4e45e66 | 2019-10-03 15:40:24 -0700 | [diff] [blame] | 125 | ":linux_aarch64": ["-lpthread"], |
Marat Dukhan | 08c4a43 | 2019-10-03 09:29:21 -0700 | [diff] [blame] | 126 | ":android": ["-lm"], |
| 127 | "//conditions:default": [], |
| 128 | }), |
| 129 | textual_hdrs = hdrs, |
| 130 | deps = deps, |
| 131 | ) |
| 132 | |
| 133 | def xnnpack_aggregate_library( |
| 134 | name, |
| 135 | generic_deps = [], |
| 136 | x86_deps = [], |
| 137 | aarch32_deps = [], |
| 138 | aarch64_deps = [], |
| 139 | wasm_deps = [], |
| 140 | wasmsimd_deps = []): |
| 141 | """Static library that aggregates architecture-specific dependencies. |
| 142 | |
| 143 | Args: |
| 144 | name: The name of the library target to define. |
| 145 | generic_deps: The list of libraries to link on all architectures. |
| 146 | x86_deps: The list of libraries to link in x86 and x86-64 builds. |
| 147 | aarch32_deps: The list of libraries to link in AArch32 builds. |
| 148 | aarch64_deps: The list of libraries to link in AArch32 builds. |
| 149 | wasm_deps: The list of libraries to link in WebAssembly (MVP) builds. |
| 150 | wasmsimd_deps: The list of libraries to link in WebAssembly SIMD builds. |
| 151 | """ |
| 152 | |
| 153 | native.cc_library( |
| 154 | name = name, |
| 155 | linkstatic = True, |
| 156 | deps = generic_deps + select({ |
| 157 | ":linux_k8": x86_deps, |
Marat Dukhan | 4e45e66 | 2019-10-03 15:40:24 -0700 | [diff] [blame] | 158 | ":linux_aarch64": aarch64_deps, |
Marat Dukhan | 885ca24 | 2019-10-07 09:17:32 -0700 | [diff] [blame] | 159 | ":macos_x86_64": x86_deps, |
Marat Dukhan | 08c4a43 | 2019-10-03 09:29:21 -0700 | [diff] [blame] | 160 | ":android_armv7": aarch32_deps, |
| 161 | ":android_arm64": aarch64_deps, |
| 162 | ":android_x86": x86_deps, |
| 163 | ":android_x86_64": x86_deps, |
| 164 | ":emscripten_wasm": wasm_deps, |
| 165 | ":emscripten_wasmsimd": wasmsimd_deps, |
| 166 | ":emscripten_asmjs": [], |
| 167 | }), |
| 168 | ) |
| 169 | |
| 170 | def xnnpack_unit_test(name, srcs, copts = [], deps = []): |
| 171 | """Unit test binary based on Google Test. |
| 172 | |
| 173 | Args: |
| 174 | name: The name of the test target to define. |
| 175 | srcs: The list of source and header files. |
| 176 | copts: The list of additional compiler flags for the target. -I flags |
| 177 | for include/ and src/ directories of XNNPACK are always prepended |
| 178 | before these user-specified flags. |
| 179 | deps: The list of additional libraries to be linked. Google Test library |
| 180 | (with main() function) is always added as a dependency and does not |
| 181 | need to be explicitly specified. |
| 182 | """ |
| 183 | |
| 184 | native.cc_test( |
| 185 | name = name, |
| 186 | srcs = srcs, |
| 187 | copts = xnnpack_std_cxxopts() + [ |
| 188 | "-Iinclude", |
| 189 | "-Isrc", |
| 190 | ] + copts, |
| 191 | linkopts = select({ |
| 192 | ":emscripten": xnnpack_emscripten_test_linkopts(), |
| 193 | "//conditions:default": [], |
| 194 | }), |
| 195 | linkstatic = True, |
| 196 | deps = [ |
| 197 | "@com_google_googletest//:gtest_main", |
| 198 | ] + deps + select({ |
| 199 | ":emscripten": xnnpack_emscripten_deps(), |
| 200 | "//conditions:default": [], |
| 201 | }), |
| 202 | ) |
| 203 | |
| 204 | def xnnpack_binary(name, srcs, copts = [], deps = []): |
| 205 | """Minimal binary |
| 206 | |
| 207 | Args: |
| 208 | name: The name of the binary target to define. |
| 209 | srcs: The list of source and header files. |
| 210 | copts: The list of additional compiler flags for the target. -I flags |
| 211 | for include/ and src/ directories of XNNPACK are always prepended |
| 212 | before these user-specified flags. |
| 213 | deps: The list of libraries to be linked. |
| 214 | """ |
| 215 | native.cc_binary( |
| 216 | name = name, |
| 217 | srcs = srcs, |
| 218 | copts = [ |
| 219 | "-Iinclude", |
| 220 | "-Isrc", |
| 221 | ] + copts, |
| 222 | linkopts = select({ |
| 223 | ":emscripten": xnnpack_emscripten_minimal_linkopts(), |
| 224 | "//conditions:default": [], |
| 225 | }), |
| 226 | linkstatic = True, |
| 227 | deps = deps, |
| 228 | ) |
| 229 | |
| 230 | def xnnpack_benchmark(name, srcs, copts = [], deps = []): |
| 231 | """Microbenchmark binary based on Google Benchmark |
| 232 | |
| 233 | Args: |
| 234 | name: The name of the binary target to define. |
| 235 | srcs: The list of source and header files. |
| 236 | copts: The list of additional compiler flags for the target. -I flags |
| 237 | for include/ and src/ directories of XNNPACK are always prepended |
| 238 | before these user-specified flags. |
| 239 | deps: The list of additional libraries to be linked. Google Benchmark |
| 240 | library is always added as a dependency and does not need to be |
| 241 | explicitly specified. |
| 242 | """ |
| 243 | native.cc_binary( |
| 244 | name = name, |
| 245 | srcs = srcs, |
| 246 | copts = xnnpack_std_cxxopts() + [ |
| 247 | "-Iinclude", |
| 248 | "-Isrc", |
| 249 | ] + copts, |
| 250 | linkopts = select({ |
| 251 | ":emscripten": xnnpack_emscripten_benchmark_linkopts(), |
| 252 | "//conditions:default": [], |
| 253 | }), |
| 254 | linkstatic = True, |
| 255 | deps = [ |
| 256 | "@com_google_benchmark//:benchmark", |
| 257 | ] + deps + select({ |
| 258 | ":emscripten": xnnpack_emscripten_deps(), |
| 259 | "//conditions:default": [], |
| 260 | }), |
| 261 | ) |