blob: db1d303eabf912bc65e8d3258a7d71f32d5773d1 [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,
Marat Dukhancf056b22019-10-07 10:26:29 -0700116 ":emscripten_asmjs": asmjs_srcs,
117 ":emscripten_wasm": wasm_srcs,
118 ":emscripten_wasmsimd": wasmsimd_srcs,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700119 "//conditions:default": [],
120 }),
121 copts = [
122 "-Iinclude",
123 "-Isrc",
124 ] + copts + select({
125 ":linux_k8": x86_copts,
Marat Dukhan4e45e662019-10-03 15:40:24 -0700126 ":linux_aarch64": aarch64_copts,
Marat Dukhan885ca242019-10-07 09:17:32 -0700127 ":macos_x86_64": x86_copts,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700128 ":android_armv7": aarch32_copts,
129 ":android_arm64": aarch64_copts,
130 ":android_x86": x86_copts,
131 ":android_x86_64": x86_copts,
Marat Dukhancf056b22019-10-07 10:26:29 -0700132 ":emscripten_asmjs": asmjs_copts,
133 ":emscripten_wasm": wasm_copts,
134 ":emscripten_wasmsimd": wasmsimd_copts,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700135 "//conditions:default": [],
136 }) + select({
137 ":optimized_build": optimized_copts,
138 "//conditions:default": [],
139 }),
Daniel Smilkov0c57d2a2019-10-07 10:06:44 -0700140 includes = ["include", "src"],
Marat Dukhan08c4a432019-10-03 09:29:21 -0700141 linkstatic = True,
142 linkopts = select({
143 ":linux_k8": ["-lpthread"],
Marat Dukhan4e45e662019-10-03 15:40:24 -0700144 ":linux_aarch64": ["-lpthread"],
Marat Dukhan08c4a432019-10-03 09:29:21 -0700145 ":android": ["-lm"],
146 "//conditions:default": [],
147 }),
148 textual_hdrs = hdrs,
149 deps = deps,
150 )
151
152def xnnpack_aggregate_library(
153 name,
154 generic_deps = [],
155 x86_deps = [],
156 aarch32_deps = [],
157 aarch64_deps = [],
158 wasm_deps = [],
159 wasmsimd_deps = []):
160 """Static library that aggregates architecture-specific dependencies.
161
162 Args:
163 name: The name of the library target to define.
164 generic_deps: The list of libraries to link on all architectures.
165 x86_deps: The list of libraries to link in x86 and x86-64 builds.
166 aarch32_deps: The list of libraries to link in AArch32 builds.
167 aarch64_deps: The list of libraries to link in AArch32 builds.
168 wasm_deps: The list of libraries to link in WebAssembly (MVP) builds.
169 wasmsimd_deps: The list of libraries to link in WebAssembly SIMD builds.
170 """
171
172 native.cc_library(
173 name = name,
174 linkstatic = True,
175 deps = generic_deps + select({
176 ":linux_k8": x86_deps,
Marat Dukhan4e45e662019-10-03 15:40:24 -0700177 ":linux_aarch64": aarch64_deps,
Marat Dukhan885ca242019-10-07 09:17:32 -0700178 ":macos_x86_64": x86_deps,
Marat Dukhan08c4a432019-10-03 09:29:21 -0700179 ":android_armv7": aarch32_deps,
180 ":android_arm64": aarch64_deps,
181 ":android_x86": x86_deps,
182 ":android_x86_64": x86_deps,
183 ":emscripten_wasm": wasm_deps,
184 ":emscripten_wasmsimd": wasmsimd_deps,
185 ":emscripten_asmjs": [],
186 }),
187 )
188
189def xnnpack_unit_test(name, srcs, copts = [], deps = []):
190 """Unit test binary based on Google Test.
191
192 Args:
193 name: The name of the test target to define.
194 srcs: The list of source and header files.
195 copts: The list of additional compiler flags for the target. -I flags
196 for include/ and src/ directories of XNNPACK are always prepended
197 before these user-specified flags.
198 deps: The list of additional libraries to be linked. Google Test library
199 (with main() function) is always added as a dependency and does not
200 need to be explicitly specified.
201 """
202
203 native.cc_test(
204 name = name,
205 srcs = srcs,
206 copts = xnnpack_std_cxxopts() + [
207 "-Iinclude",
208 "-Isrc",
209 ] + copts,
210 linkopts = select({
211 ":emscripten": xnnpack_emscripten_test_linkopts(),
212 "//conditions:default": [],
213 }),
214 linkstatic = True,
215 deps = [
216 "@com_google_googletest//:gtest_main",
217 ] + deps + select({
218 ":emscripten": xnnpack_emscripten_deps(),
219 "//conditions:default": [],
220 }),
221 )
222
223def xnnpack_binary(name, srcs, copts = [], deps = []):
224 """Minimal binary
225
226 Args:
227 name: The name of the binary target to define.
228 srcs: The list of source and header files.
229 copts: The list of additional compiler flags for the target. -I flags
230 for include/ and src/ directories of XNNPACK are always prepended
231 before these user-specified flags.
232 deps: The list of libraries to be linked.
233 """
234 native.cc_binary(
235 name = name,
236 srcs = srcs,
237 copts = [
238 "-Iinclude",
239 "-Isrc",
240 ] + copts,
241 linkopts = select({
242 ":emscripten": xnnpack_emscripten_minimal_linkopts(),
243 "//conditions:default": [],
244 }),
245 linkstatic = True,
246 deps = deps,
247 )
248
249def xnnpack_benchmark(name, srcs, copts = [], deps = []):
250 """Microbenchmark binary based on Google Benchmark
251
252 Args:
253 name: The name of the binary target to define.
254 srcs: The list of source and header files.
255 copts: The list of additional compiler flags for the target. -I flags
256 for include/ and src/ directories of XNNPACK are always prepended
257 before these user-specified flags.
258 deps: The list of additional libraries to be linked. Google Benchmark
259 library is always added as a dependency and does not need to be
260 explicitly specified.
261 """
262 native.cc_binary(
263 name = name,
264 srcs = srcs,
265 copts = xnnpack_std_cxxopts() + [
266 "-Iinclude",
267 "-Isrc",
268 ] + copts,
269 linkopts = select({
270 ":emscripten": xnnpack_emscripten_benchmark_linkopts(),
271 "//conditions:default": [],
272 }),
273 linkstatic = True,
274 deps = [
275 "@com_google_benchmark//:benchmark",
276 ] + deps + select({
277 ":emscripten": xnnpack_emscripten_deps(),
278 "//conditions:default": [],
279 }),
280 )