blob: 9112ab27b1c83f5001c94a8dc414120c89fa5e3c [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
41def xnnpack_optional_ruy_deps():
42 """Optional Ruy dependencies."""
43 return []
44
45def xnnpack_optional_gemmlowp_deps():
46 """Optional Gemmlowp dependencies."""
47 return []
48
49def xnnpack_optional_tflite_deps():
50 """Optional TensorFlow Lite dependencies."""
51 return []
52
53def xnnpack_optional_armcl_deps():
54 """Optional ARM ComputeLibrary dependencies."""
55 return []
56
57def xnnpack_cc_library(
58 name,
59 srcs = [],
60 x86_srcs = [],
61 aarch32_srcs = [],
62 aarch64_srcs = [],
Marat Dukhancf056b22019-10-07 10:26:29 -070063 asmjs_srcs = [],
64 wasm_srcs = [],
65 wasmsimd_srcs = [],
Marat Dukhan08c4a432019-10-03 09:29:21 -070066 copts = [],
67 x86_copts = [],
68 aarch32_copts = [],
69 aarch64_copts = [],
Marat Dukhancf056b22019-10-07 10:26:29 -070070 asmjs_copts = [],
71 wasm_copts = [],
72 wasmsimd_copts = [],
Marat Dukhan08c4a432019-10-03 09:29:21 -070073 optimized_copts = ["-O2"],
74 hdrs = [],
75 deps = []):
Marat Dukhancf056b22019-10-07 10:26:29 -070076 """C/C++/assembly library with architecture-specific configuration.
Marat Dukhan08c4a432019-10-03 09:29:21 -070077
78 Define a static library with architecture- and instruction-specific
Marat Dukhancf056b22019-10-07 10:26:29 -070079 source files and/or compiler flags.
Marat Dukhan08c4a432019-10-03 09:29:21 -070080
81 Args:
82 name: The name of the library target to define.
83 srcs: The list of architecture-independent source files.
84 x86_srcs: The list of x86-specific source files.
85 aarch32_srcs: The list of AArch32-specific source files.
86 aarch64_srcs: The list of AArch64-specific source files.
Marat Dukhancf056b22019-10-07 10:26:29 -070087 asmjs_srcs: The list of Asm.js-specific source files.
88 wasm_srcs: The list of WebAssembly/MVP-specific source files.
89 wasmsimd_srcs: The list of WebAssembly/SIMD-specific source files.
Marat Dukhan08c4a432019-10-03 09:29:21 -070090 copts: The list of compiler flags to use in all builds. -I flags for
91 include/ and src/ directories of XNNPACK are always prepended
92 before these user-specified flags.
93 x86_copts: The list of compiler flags to use in x86 builds.
94 aarch32_copts: The list of compiler flags to use in AArch32 builds.
95 aarch64_copts: The list of compiler flags to use in AArch64 builds.
Marat Dukhancf056b22019-10-07 10:26:29 -070096 asmjs_copts: The list of compiler flags to use in Asm.js builds.
97 wasm_copts: The list of compiler flags to use in WebAssembly/MVP builds.
98 wasmsimd_copts: The list of compiler flags to use in WebAssembly/SIMD
99 builds.
Marat Dukhan08c4a432019-10-03 09:29:21 -0700100 optimized_copts: The list of compiler flags to use in optimized builds.
101 Defaults to -O2.
102 hdrs: The list of header files published by this library to be textually
103 included by sources in dependent rules.
104 deps: The list of other libraries to be linked.
105 """
106 native.cc_library(
107 name = name,
108 srcs = srcs + select({
109 ":linux_k8": x86_srcs,
Marat Dukhan4e45e662019-10-03 15:40:24 -0700110 ":linux_aarch64": aarch64_srcs,
Marat Dukhan885ca242019-10-07 09:17:32 -0700111 ":macos_x86_64": x86_srcs,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700112 ":android_armv7": aarch32_srcs,
113 ":android_arm64": aarch64_srcs,
114 ":android_x86": x86_srcs,
115 ":android_x86_64": x86_srcs,
Miao Wang3fa1f012020-02-17 22:45:06 +0000116 ":ios_armv7": aarch32_srcs,
117 ":ios_arm64": aarch64_srcs,
118 ":ios_arm64e": aarch64_srcs,
119 ":ios_x86": x86_srcs,
120 ":ios_x86_64": x86_srcs,
121 ":watchos_armv7k": aarch32_srcs,
122 ":watchos_arm64_32": aarch64_srcs,
123 ":watchos_x86": x86_srcs,
124 ":watchos_x86_64": x86_srcs,
125 ":tvos_arm64": aarch64_srcs,
126 ":tvos_x86_64": x86_srcs,
Marat Dukhancf056b22019-10-07 10:26:29 -0700127 ":emscripten_asmjs": asmjs_srcs,
128 ":emscripten_wasm": wasm_srcs,
129 ":emscripten_wasmsimd": wasmsimd_srcs,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700130 "//conditions:default": [],
131 }),
132 copts = [
133 "-Iinclude",
134 "-Isrc",
135 ] + copts + select({
136 ":linux_k8": x86_copts,
Marat Dukhan4e45e662019-10-03 15:40:24 -0700137 ":linux_aarch64": aarch64_copts,
Marat Dukhan885ca242019-10-07 09:17:32 -0700138 ":macos_x86_64": x86_copts,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700139 ":android_armv7": aarch32_copts,
140 ":android_arm64": aarch64_copts,
141 ":android_x86": x86_copts,
142 ":android_x86_64": x86_copts,
Miao Wang3fa1f012020-02-17 22:45:06 +0000143 ":ios_armv7": aarch32_copts,
144 ":ios_arm64": aarch64_copts,
145 ":ios_arm64e": aarch64_copts,
146 ":ios_x86": x86_copts,
147 ":ios_x86_64": x86_copts,
148 ":watchos_armv7k": aarch32_copts,
149 ":watchos_arm64_32": aarch64_copts,
150 ":watchos_x86": x86_copts,
151 ":watchos_x86_64": x86_copts,
152 ":tvos_arm64": aarch64_copts,
153 ":tvos_x86_64": x86_copts,
Marat Dukhancf056b22019-10-07 10:26:29 -0700154 ":emscripten_asmjs": asmjs_copts,
155 ":emscripten_wasm": wasm_copts,
156 ":emscripten_wasmsimd": wasmsimd_copts,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700157 "//conditions:default": [],
158 }) + select({
159 ":optimized_build": optimized_copts,
160 "//conditions:default": [],
161 }),
Daniel Smilkov0c57d2a2019-10-07 10:06:44 -0700162 includes = ["include", "src"],
Marat Dukhan08c4a432019-10-03 09:29:21 -0700163 linkstatic = True,
164 linkopts = select({
165 ":linux_k8": ["-lpthread"],
Marat Dukhan4e45e662019-10-03 15:40:24 -0700166 ":linux_aarch64": ["-lpthread"],
Marat Dukhan08c4a432019-10-03 09:29:21 -0700167 ":android": ["-lm"],
168 "//conditions:default": [],
169 }),
170 textual_hdrs = hdrs,
171 deps = deps,
172 )
173
174def xnnpack_aggregate_library(
175 name,
176 generic_deps = [],
177 x86_deps = [],
178 aarch32_deps = [],
179 aarch64_deps = [],
180 wasm_deps = [],
181 wasmsimd_deps = []):
182 """Static library that aggregates architecture-specific dependencies.
183
184 Args:
185 name: The name of the library target to define.
186 generic_deps: The list of libraries to link on all architectures.
187 x86_deps: The list of libraries to link in x86 and x86-64 builds.
188 aarch32_deps: The list of libraries to link in AArch32 builds.
189 aarch64_deps: The list of libraries to link in AArch32 builds.
190 wasm_deps: The list of libraries to link in WebAssembly (MVP) builds.
191 wasmsimd_deps: The list of libraries to link in WebAssembly SIMD builds.
192 """
193
194 native.cc_library(
195 name = name,
196 linkstatic = True,
197 deps = generic_deps + select({
198 ":linux_k8": x86_deps,
Marat Dukhan4e45e662019-10-03 15:40:24 -0700199 ":linux_aarch64": aarch64_deps,
Marat Dukhan885ca242019-10-07 09:17:32 -0700200 ":macos_x86_64": x86_deps,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700201 ":android_armv7": aarch32_deps,
202 ":android_arm64": aarch64_deps,
203 ":android_x86": x86_deps,
204 ":android_x86_64": x86_deps,
Miao Wang3fa1f012020-02-17 22:45:06 +0000205 ":ios_armv7": aarch32_deps,
206 ":ios_arm64": aarch64_deps,
207 ":ios_arm64e": aarch64_deps,
208 ":ios_x86": x86_deps,
209 ":ios_x86_64": x86_deps,
210 ":watchos_armv7k": aarch32_deps,
211 ":watchos_arm64_32": aarch64_deps,
212 ":watchos_x86": x86_deps,
213 ":watchos_x86_64": x86_deps,
214 ":tvos_arm64": aarch64_deps,
215 ":tvos_x86_64": x86_deps,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700216 ":emscripten_wasm": wasm_deps,
217 ":emscripten_wasmsimd": wasmsimd_deps,
218 ":emscripten_asmjs": [],
219 }),
220 )
221
222def xnnpack_unit_test(name, srcs, copts = [], deps = []):
223 """Unit test binary based on Google Test.
224
225 Args:
226 name: The name of the test 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 Test library
232 (with main() function) is always added as a dependency and does not
233 need to be explicitly specified.
234 """
235
236 native.cc_test(
237 name = name,
238 srcs = srcs,
239 copts = xnnpack_std_cxxopts() + [
240 "-Iinclude",
241 "-Isrc",
242 ] + copts,
243 linkopts = select({
244 ":emscripten": xnnpack_emscripten_test_linkopts(),
245 "//conditions:default": [],
246 }),
247 linkstatic = True,
248 deps = [
249 "@com_google_googletest//:gtest_main",
250 ] + deps + select({
251 ":emscripten": xnnpack_emscripten_deps(),
252 "//conditions:default": [],
253 }),
254 )
255
256def xnnpack_binary(name, srcs, copts = [], deps = []):
257 """Minimal binary
258
259 Args:
260 name: The name of the binary target to define.
261 srcs: The list of source and header files.
262 copts: The list of additional compiler flags for the target. -I flags
263 for include/ and src/ directories of XNNPACK are always prepended
264 before these user-specified flags.
265 deps: The list of libraries to be linked.
266 """
267 native.cc_binary(
268 name = name,
269 srcs = srcs,
270 copts = [
271 "-Iinclude",
272 "-Isrc",
273 ] + copts,
274 linkopts = select({
275 ":emscripten": xnnpack_emscripten_minimal_linkopts(),
276 "//conditions:default": [],
277 }),
278 linkstatic = True,
279 deps = deps,
280 )
281
282def xnnpack_benchmark(name, srcs, copts = [], deps = []):
283 """Microbenchmark binary based on Google Benchmark
284
285 Args:
286 name: The name of the binary target to define.
287 srcs: The list of source and header files.
288 copts: The list of additional compiler flags for the target. -I flags
289 for include/ and src/ directories of XNNPACK are always prepended
290 before these user-specified flags.
291 deps: The list of additional libraries to be linked. Google Benchmark
292 library is always added as a dependency and does not need to be
293 explicitly specified.
294 """
295 native.cc_binary(
296 name = name,
297 srcs = srcs,
298 copts = xnnpack_std_cxxopts() + [
299 "-Iinclude",
300 "-Isrc",
301 ] + copts,
302 linkopts = select({
303 ":emscripten": xnnpack_emscripten_benchmark_linkopts(),
304 "//conditions:default": [],
305 }),
306 linkstatic = True,
307 deps = [
308 "@com_google_benchmark//:benchmark",
309 ] + deps + select({
310 ":emscripten": xnnpack_emscripten_deps(),
311 "//conditions:default": [],
312 }),
313 )