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 | """ |
| 11 | return [] |
| 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, |
| 97 | ":android_armv7": aarch32_srcs, |
| 98 | ":android_arm64": aarch64_srcs, |
| 99 | ":android_x86": x86_srcs, |
| 100 | ":android_x86_64": x86_srcs, |
| 101 | "//conditions:default": [], |
| 102 | }), |
| 103 | copts = [ |
| 104 | "-Iinclude", |
| 105 | "-Isrc", |
| 106 | ] + copts + select({ |
| 107 | ":linux_k8": x86_copts, |
| 108 | ":android_armv7": aarch32_copts, |
| 109 | ":android_arm64": aarch64_copts, |
| 110 | ":android_x86": x86_copts, |
| 111 | ":android_x86_64": x86_copts, |
| 112 | "//conditions:default": [], |
| 113 | }) + select({ |
| 114 | ":optimized_build": optimized_copts, |
| 115 | "//conditions:default": [], |
| 116 | }), |
| 117 | linkstatic = True, |
| 118 | linkopts = select({ |
| 119 | ":linux_k8": ["-lpthread"], |
| 120 | ":android": ["-lm"], |
| 121 | "//conditions:default": [], |
| 122 | }), |
| 123 | textual_hdrs = hdrs, |
| 124 | deps = deps, |
| 125 | ) |
| 126 | |
| 127 | def xnnpack_aggregate_library( |
| 128 | name, |
| 129 | generic_deps = [], |
| 130 | x86_deps = [], |
| 131 | aarch32_deps = [], |
| 132 | aarch64_deps = [], |
| 133 | wasm_deps = [], |
| 134 | wasmsimd_deps = []): |
| 135 | """Static library that aggregates architecture-specific dependencies. |
| 136 | |
| 137 | Args: |
| 138 | name: The name of the library target to define. |
| 139 | generic_deps: The list of libraries to link on all architectures. |
| 140 | x86_deps: The list of libraries to link in x86 and x86-64 builds. |
| 141 | aarch32_deps: The list of libraries to link in AArch32 builds. |
| 142 | aarch64_deps: The list of libraries to link in AArch32 builds. |
| 143 | wasm_deps: The list of libraries to link in WebAssembly (MVP) builds. |
| 144 | wasmsimd_deps: The list of libraries to link in WebAssembly SIMD builds. |
| 145 | """ |
| 146 | |
| 147 | native.cc_library( |
| 148 | name = name, |
| 149 | linkstatic = True, |
| 150 | deps = generic_deps + select({ |
| 151 | ":linux_k8": x86_deps, |
| 152 | ":android_armv7": aarch32_deps, |
| 153 | ":android_arm64": aarch64_deps, |
| 154 | ":android_x86": x86_deps, |
| 155 | ":android_x86_64": x86_deps, |
| 156 | ":emscripten_wasm": wasm_deps, |
| 157 | ":emscripten_wasmsimd": wasmsimd_deps, |
| 158 | ":emscripten_asmjs": [], |
| 159 | }), |
| 160 | ) |
| 161 | |
| 162 | def xnnpack_unit_test(name, srcs, copts = [], deps = []): |
| 163 | """Unit test binary based on Google Test. |
| 164 | |
| 165 | Args: |
| 166 | name: The name of the test target to define. |
| 167 | srcs: The list of source and header files. |
| 168 | copts: The list of additional compiler flags for the target. -I flags |
| 169 | for include/ and src/ directories of XNNPACK are always prepended |
| 170 | before these user-specified flags. |
| 171 | deps: The list of additional libraries to be linked. Google Test library |
| 172 | (with main() function) is always added as a dependency and does not |
| 173 | need to be explicitly specified. |
| 174 | """ |
| 175 | |
| 176 | native.cc_test( |
| 177 | name = name, |
| 178 | srcs = srcs, |
| 179 | copts = xnnpack_std_cxxopts() + [ |
| 180 | "-Iinclude", |
| 181 | "-Isrc", |
| 182 | ] + copts, |
| 183 | linkopts = select({ |
| 184 | ":emscripten": xnnpack_emscripten_test_linkopts(), |
| 185 | "//conditions:default": [], |
| 186 | }), |
| 187 | linkstatic = True, |
| 188 | deps = [ |
| 189 | "@com_google_googletest//:gtest_main", |
| 190 | ] + deps + select({ |
| 191 | ":emscripten": xnnpack_emscripten_deps(), |
| 192 | "//conditions:default": [], |
| 193 | }), |
| 194 | ) |
| 195 | |
| 196 | def xnnpack_binary(name, srcs, copts = [], deps = []): |
| 197 | """Minimal binary |
| 198 | |
| 199 | Args: |
| 200 | name: The name of the binary target to define. |
| 201 | srcs: The list of source and header files. |
| 202 | copts: The list of additional compiler flags for the target. -I flags |
| 203 | for include/ and src/ directories of XNNPACK are always prepended |
| 204 | before these user-specified flags. |
| 205 | deps: The list of libraries to be linked. |
| 206 | """ |
| 207 | native.cc_binary( |
| 208 | name = name, |
| 209 | srcs = srcs, |
| 210 | copts = [ |
| 211 | "-Iinclude", |
| 212 | "-Isrc", |
| 213 | ] + copts, |
| 214 | linkopts = select({ |
| 215 | ":emscripten": xnnpack_emscripten_minimal_linkopts(), |
| 216 | "//conditions:default": [], |
| 217 | }), |
| 218 | linkstatic = True, |
| 219 | deps = deps, |
| 220 | ) |
| 221 | |
| 222 | def xnnpack_benchmark(name, srcs, copts = [], deps = []): |
| 223 | """Microbenchmark binary based on Google Benchmark |
| 224 | |
| 225 | Args: |
| 226 | name: The name of the binary target to define. |
| 227 | srcs: The list of source and header files. |
| 228 | copts: The list of additional compiler flags for the target. -I flags |
| 229 | for include/ and src/ directories of XNNPACK are always prepended |
| 230 | before these user-specified flags. |
| 231 | deps: The list of additional libraries to be linked. Google Benchmark |
| 232 | library is always added as a dependency and does not need to be |
| 233 | explicitly specified. |
| 234 | """ |
| 235 | native.cc_binary( |
| 236 | name = name, |
| 237 | srcs = srcs, |
| 238 | copts = xnnpack_std_cxxopts() + [ |
| 239 | "-Iinclude", |
| 240 | "-Isrc", |
| 241 | ] + copts, |
| 242 | linkopts = select({ |
| 243 | ":emscripten": xnnpack_emscripten_benchmark_linkopts(), |
| 244 | "//conditions:default": [], |
| 245 | }), |
| 246 | linkstatic = True, |
| 247 | deps = [ |
| 248 | "@com_google_benchmark//:benchmark", |
| 249 | ] + deps + select({ |
| 250 | ":emscripten": xnnpack_emscripten_deps(), |
| 251 | "//conditions:default": [], |
| 252 | }), |
| 253 | ) |