blob: 79098116b40022dd798deadd81d7b5092dca94ce [file] [log] [blame]
Marat Dukhan08c4a432019-10-03 09:29:21 -07001"""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 """
Marat Dukhan9d056a42019-10-03 12:13:35 -070011 return ["//visibility:public"]
Marat Dukhan08c4a432019-10-03 09:29:21 -070012
13def xnnpack_min_size_copts():
14 """Compiler flags for size-optimized builds."""
15 return ["-Os"]
16
17def xnnpack_std_copts():
18 """Compiler flags to specify language standard for C sources."""
19 return ["-std=c99"]
20
21def xnnpack_std_cxxopts():
22 """Compiler flags to specify language standard for C++ sources."""
23 return ["-std=gnu++11"]
24
25def xnnpack_optional_ruy_copts():
26 """Compiler flags to optionally enable Ruy benchmarks."""
27 return []
28
29def xnnpack_optional_gemmlowp_copts():
30 """Compiler flags to optionally enable Gemmlowp benchmarks."""
31 return []
32
33def xnnpack_optional_tflite_copts():
34 """Compiler flags to optionally enable TensorFlow Lite benchmarks."""
35 return []
36
37def xnnpack_optional_armcl_copts():
38 """Compiler flags to optionally enable ARM ComputeLibrary benchmarks."""
39 return []
40
Marat Dukhan8d3c6932020-03-06 20:27:27 -080041def xnnpack_optional_dnnl_copts():
42 """Compiler flags to optionally enable Intel DNNL benchmarks."""
43 return []
44
Marat Dukhan08c4a432019-10-03 09:29:21 -070045def xnnpack_optional_ruy_deps():
46 """Optional Ruy dependencies."""
47 return []
48
49def xnnpack_optional_gemmlowp_deps():
50 """Optional Gemmlowp dependencies."""
51 return []
52
53def xnnpack_optional_tflite_deps():
54 """Optional TensorFlow Lite dependencies."""
55 return []
56
57def xnnpack_optional_armcl_deps():
58 """Optional ARM ComputeLibrary dependencies."""
59 return []
60
Marat Dukhan8d3c6932020-03-06 20:27:27 -080061def xnnpack_optional_dnnl_deps():
62 """Optional Intel DNNL dependencies."""
63 return []
64
Marat Dukhan08c4a432019-10-03 09:29:21 -070065def xnnpack_cc_library(
66 name,
67 srcs = [],
68 x86_srcs = [],
69 aarch32_srcs = [],
70 aarch64_srcs = [],
Marat Dukhancf056b22019-10-07 10:26:29 -070071 asmjs_srcs = [],
72 wasm_srcs = [],
73 wasmsimd_srcs = [],
Marat Dukhan08c4a432019-10-03 09:29:21 -070074 copts = [],
75 x86_copts = [],
76 aarch32_copts = [],
77 aarch64_copts = [],
Marat Dukhancf056b22019-10-07 10:26:29 -070078 asmjs_copts = [],
79 wasm_copts = [],
80 wasmsimd_copts = [],
Marat Dukhan08c4a432019-10-03 09:29:21 -070081 optimized_copts = ["-O2"],
82 hdrs = [],
83 deps = []):
Marat Dukhancf056b22019-10-07 10:26:29 -070084 """C/C++/assembly library with architecture-specific configuration.
Marat Dukhan08c4a432019-10-03 09:29:21 -070085
86 Define a static library with architecture- and instruction-specific
Marat Dukhancf056b22019-10-07 10:26:29 -070087 source files and/or compiler flags.
Marat Dukhan08c4a432019-10-03 09:29:21 -070088
89 Args:
90 name: The name of the library target to define.
91 srcs: The list of architecture-independent source files.
92 x86_srcs: The list of x86-specific source files.
93 aarch32_srcs: The list of AArch32-specific source files.
94 aarch64_srcs: The list of AArch64-specific source files.
Marat Dukhancf056b22019-10-07 10:26:29 -070095 asmjs_srcs: The list of Asm.js-specific source files.
96 wasm_srcs: The list of WebAssembly/MVP-specific source files.
97 wasmsimd_srcs: The list of WebAssembly/SIMD-specific source files.
Marat Dukhan08c4a432019-10-03 09:29:21 -070098 copts: The list of compiler flags to use in all builds. -I flags for
99 include/ and src/ directories of XNNPACK are always prepended
100 before these user-specified flags.
101 x86_copts: The list of compiler flags to use in x86 builds.
102 aarch32_copts: The list of compiler flags to use in AArch32 builds.
103 aarch64_copts: The list of compiler flags to use in AArch64 builds.
Marat Dukhancf056b22019-10-07 10:26:29 -0700104 asmjs_copts: The list of compiler flags to use in Asm.js builds.
105 wasm_copts: The list of compiler flags to use in WebAssembly/MVP builds.
106 wasmsimd_copts: The list of compiler flags to use in WebAssembly/SIMD
107 builds.
Marat Dukhan08c4a432019-10-03 09:29:21 -0700108 optimized_copts: The list of compiler flags to use in optimized builds.
109 Defaults to -O2.
110 hdrs: The list of header files published by this library to be textually
111 included by sources in dependent rules.
112 deps: The list of other libraries to be linked.
113 """
114 native.cc_library(
115 name = name,
116 srcs = srcs + select({
117 ":linux_k8": x86_srcs,
Marat Dukhan4e45e662019-10-03 15:40:24 -0700118 ":linux_aarch64": aarch64_srcs,
Marat Dukhan885ca242019-10-07 09:17:32 -0700119 ":macos_x86_64": x86_srcs,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700120 ":android_armv7": aarch32_srcs,
121 ":android_arm64": aarch64_srcs,
122 ":android_x86": x86_srcs,
123 ":android_x86_64": x86_srcs,
Marat Dukhan1498d1d2020-02-11 20:00:05 -0800124 ":ios_armv7": aarch32_srcs,
125 ":ios_arm64": aarch64_srcs,
126 ":ios_arm64e": aarch64_srcs,
127 ":ios_x86": x86_srcs,
128 ":ios_x86_64": x86_srcs,
129 ":watchos_armv7k": aarch32_srcs,
130 ":watchos_arm64_32": aarch64_srcs,
131 ":watchos_x86": x86_srcs,
132 ":watchos_x86_64": x86_srcs,
133 ":tvos_arm64": aarch64_srcs,
134 ":tvos_x86_64": x86_srcs,
Marat Dukhancf056b22019-10-07 10:26:29 -0700135 ":emscripten_asmjs": asmjs_srcs,
136 ":emscripten_wasm": wasm_srcs,
137 ":emscripten_wasmsimd": wasmsimd_srcs,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700138 "//conditions:default": [],
139 }),
140 copts = [
141 "-Iinclude",
142 "-Isrc",
143 ] + copts + select({
144 ":linux_k8": x86_copts,
Marat Dukhan4e45e662019-10-03 15:40:24 -0700145 ":linux_aarch64": aarch64_copts,
Marat Dukhan885ca242019-10-07 09:17:32 -0700146 ":macos_x86_64": x86_copts,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700147 ":android_armv7": aarch32_copts,
148 ":android_arm64": aarch64_copts,
149 ":android_x86": x86_copts,
150 ":android_x86_64": x86_copts,
Marat Dukhan1498d1d2020-02-11 20:00:05 -0800151 ":ios_armv7": aarch32_copts,
152 ":ios_arm64": aarch64_copts,
153 ":ios_arm64e": aarch64_copts,
154 ":ios_x86": x86_copts,
155 ":ios_x86_64": x86_copts,
156 ":watchos_armv7k": aarch32_copts,
157 ":watchos_arm64_32": aarch64_copts,
158 ":watchos_x86": x86_copts,
159 ":watchos_x86_64": x86_copts,
160 ":tvos_arm64": aarch64_copts,
161 ":tvos_x86_64": x86_copts,
Marat Dukhancf056b22019-10-07 10:26:29 -0700162 ":emscripten_asmjs": asmjs_copts,
163 ":emscripten_wasm": wasm_copts,
164 ":emscripten_wasmsimd": wasmsimd_copts,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700165 "//conditions:default": [],
166 }) + select({
167 ":optimized_build": optimized_copts,
168 "//conditions:default": [],
169 }),
Daniel Smilkov0c57d2a2019-10-07 10:06:44 -0700170 includes = ["include", "src"],
Marat Dukhan08c4a432019-10-03 09:29:21 -0700171 linkstatic = True,
172 linkopts = select({
173 ":linux_k8": ["-lpthread"],
Marat Dukhan4e45e662019-10-03 15:40:24 -0700174 ":linux_aarch64": ["-lpthread"],
Marat Dukhan08c4a432019-10-03 09:29:21 -0700175 ":android": ["-lm"],
176 "//conditions:default": [],
177 }),
178 textual_hdrs = hdrs,
179 deps = deps,
180 )
181
182def xnnpack_aggregate_library(
183 name,
184 generic_deps = [],
185 x86_deps = [],
186 aarch32_deps = [],
187 aarch64_deps = [],
188 wasm_deps = [],
189 wasmsimd_deps = []):
190 """Static library that aggregates architecture-specific dependencies.
191
192 Args:
193 name: The name of the library target to define.
194 generic_deps: The list of libraries to link on all architectures.
195 x86_deps: The list of libraries to link in x86 and x86-64 builds.
196 aarch32_deps: The list of libraries to link in AArch32 builds.
197 aarch64_deps: The list of libraries to link in AArch32 builds.
198 wasm_deps: The list of libraries to link in WebAssembly (MVP) builds.
199 wasmsimd_deps: The list of libraries to link in WebAssembly SIMD builds.
200 """
201
202 native.cc_library(
203 name = name,
204 linkstatic = True,
205 deps = generic_deps + select({
206 ":linux_k8": x86_deps,
Marat Dukhan4e45e662019-10-03 15:40:24 -0700207 ":linux_aarch64": aarch64_deps,
Marat Dukhan885ca242019-10-07 09:17:32 -0700208 ":macos_x86_64": x86_deps,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700209 ":android_armv7": aarch32_deps,
210 ":android_arm64": aarch64_deps,
211 ":android_x86": x86_deps,
212 ":android_x86_64": x86_deps,
Marat Dukhan1498d1d2020-02-11 20:00:05 -0800213 ":ios_armv7": aarch32_deps,
214 ":ios_arm64": aarch64_deps,
215 ":ios_arm64e": aarch64_deps,
216 ":ios_x86": x86_deps,
217 ":ios_x86_64": x86_deps,
218 ":watchos_armv7k": aarch32_deps,
219 ":watchos_arm64_32": aarch64_deps,
220 ":watchos_x86": x86_deps,
221 ":watchos_x86_64": x86_deps,
222 ":tvos_arm64": aarch64_deps,
223 ":tvos_x86_64": x86_deps,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700224 ":emscripten_wasm": wasm_deps,
225 ":emscripten_wasmsimd": wasmsimd_deps,
226 ":emscripten_asmjs": [],
227 }),
228 )
229
230def xnnpack_unit_test(name, srcs, copts = [], deps = []):
231 """Unit test binary based on Google Test.
232
233 Args:
234 name: The name of the test 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 Test library
240 (with main() function) is always added as a dependency and does not
241 need to be explicitly specified.
242 """
243
244 native.cc_test(
245 name = name,
246 srcs = srcs,
247 copts = xnnpack_std_cxxopts() + [
248 "-Iinclude",
249 "-Isrc",
250 ] + copts,
251 linkopts = select({
252 ":emscripten": xnnpack_emscripten_test_linkopts(),
253 "//conditions:default": [],
254 }),
255 linkstatic = True,
256 deps = [
257 "@com_google_googletest//:gtest_main",
258 ] + deps + select({
259 ":emscripten": xnnpack_emscripten_deps(),
260 "//conditions:default": [],
261 }),
262 )
263
264def xnnpack_binary(name, srcs, copts = [], deps = []):
265 """Minimal binary
266
267 Args:
268 name: The name of the binary target to define.
269 srcs: The list of source and header files.
270 copts: The list of additional compiler flags for the target. -I flags
271 for include/ and src/ directories of XNNPACK are always prepended
272 before these user-specified flags.
273 deps: The list of libraries to be linked.
274 """
275 native.cc_binary(
276 name = name,
277 srcs = srcs,
278 copts = [
279 "-Iinclude",
280 "-Isrc",
281 ] + copts,
282 linkopts = select({
283 ":emscripten": xnnpack_emscripten_minimal_linkopts(),
284 "//conditions:default": [],
285 }),
286 linkstatic = True,
287 deps = deps,
288 )
289
290def xnnpack_benchmark(name, srcs, copts = [], deps = []):
291 """Microbenchmark binary based on Google Benchmark
292
293 Args:
294 name: The name of the binary target to define.
295 srcs: The list of source and header files.
296 copts: The list of additional compiler flags for the target. -I flags
297 for include/ and src/ directories of XNNPACK are always prepended
298 before these user-specified flags.
299 deps: The list of additional libraries to be linked. Google Benchmark
300 library is always added as a dependency and does not need to be
301 explicitly specified.
302 """
303 native.cc_binary(
304 name = name,
305 srcs = srcs,
306 copts = xnnpack_std_cxxopts() + [
307 "-Iinclude",
308 "-Isrc",
309 ] + copts,
310 linkopts = select({
311 ":emscripten": xnnpack_emscripten_benchmark_linkopts(),
312 "//conditions:default": [],
313 }),
314 linkstatic = True,
315 deps = [
316 "@com_google_benchmark//:benchmark",
317 ] + deps + select({
318 ":emscripten": xnnpack_emscripten_deps(),
319 "//conditions:default": [],
320 }),
321 )