xref: /aosp_15_r20/external/XNNPACK/build_defs.bzl (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1"""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    """
11    return ["//visibility:public"]
12
13def xnnpack_min_size_copts():
14    """Compiler flags for size-optimized builds."""
15    return ["-Os"]
16
17def xnnpack_gcc_std_copts():
18    """GCC-like compiler flags to specify language standard for C sources."""
19    return ["-std=c99"]
20
21def xnnpack_msvc_std_copts():
22    """MSVC compiler flags to specify language standard for C sources."""
23    return ["/Drestrict="]
24
25def xnnpack_std_cxxopts():
26    """Compiler flags to specify language standard for C++ sources."""
27    return ["-std=gnu++11"]
28
29def xnnpack_optional_ruy_copts():
30    """Compiler flags to optionally enable Ruy benchmarks."""
31    return []
32
33def xnnpack_optional_gemmlowp_copts():
34    """Compiler flags to optionally enable Gemmlowp benchmarks."""
35    return []
36
37def xnnpack_optional_tflite_copts():
38    """Compiler flags to optionally enable TensorFlow Lite benchmarks."""
39    return []
40
41def xnnpack_optional_dnnl_copts():
42    """Compiler flags to optionally enable Intel DNNL benchmarks."""
43    return []
44
45def 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_dnnl_deps():
58    """Optional Intel DNNL dependencies."""
59    return []
60
61def xnnpack_cc_library(
62        name,
63        srcs = [],
64        x86_srcs = [],
65        aarch32_srcs = [],
66        aarch64_srcs = [],
67        riscv_srcs = [],
68        wasm_srcs = [],
69        wasmsimd_srcs = [],
70        wasmrelaxedsimd_srcs = [],
71        copts = [],
72        gcc_copts = [],
73        msvc_copts = [],
74        mingw_copts = [],
75        msys_copts = [],
76        gcc_x86_copts = [],
77        msvc_x86_32_copts = [],
78        msvc_x86_64_copts = [],
79        aarch32_copts = [],
80        aarch64_copts = [],
81        riscv_copts = [],
82        wasm_copts = [],
83        wasmsimd_copts = [],
84        wasmrelaxedsimd_copts = [],
85        optimized_copts = ["-O2"],
86        hdrs = [],
87        defines = [],
88        includes = [],
89        deps = [],
90        visibility = [],
91        testonly = False):
92    """C/C++/assembly library with architecture-specific configuration.
93
94    Define a static library with architecture- and instruction-specific
95    source files and/or compiler flags.
96
97    Args:
98      name: The name of the library target to define.
99      srcs: The list of architecture-independent source files.
100      x86_srcs: The list of x86-specific source files.
101      aarch32_srcs: The list of AArch32-specific source files.
102      aarch64_srcs: The list of AArch64-specific source files.
103      riscv_srcs: The list of RISC-V-specific source files.
104      wasm_srcs: The list of WebAssembly 1.0-specific source files.
105      wasmsimd_srcs: The list of WebAssembly SIMD-specific source files.
106      wasmrelaxedsimd_srcs: The list of WebAssembly Relaxed SIMD-specific
107                            source files.
108      copts: The list of compiler flags to use in all builds. -I flags for
109             include/ and src/ directories of XNNPACK are always prepended
110             before these user-specified flags.
111      gcc_copts: The list of compiler flags to use with GCC-like compilers.
112      msvc_copts: The list of compiler flags to use with MSVC compiler.
113      mingw_copts: The list of compiler flags to use with MinGW GCC compilers.
114      msys_copts: The list of compiler flags to use with MSYS (Cygwin) GCC
115                  compilers.
116      gcc_x86_copts: The list of GCC-like compiler flags to use in x86 (32-bit
117                     and 64-bit) builds.
118      msvc_x86_32_copts: The list of MSVC compiler flags to use in x86 (32-bit)
119                         builds.
120      msvc_x86_64_copts: The list of MSVC compiler flags to use in x86 (64-bit)
121                         builds.
122      aarch32_copts: The list of compiler flags to use in AArch32 builds.
123      aarch64_copts: The list of compiler flags to use in AArch64 builds.
124      riscv_copts: The list of compiler flags to use in RISC-V builds.
125      wasm_copts: The list of compiler flags to use in WebAssembly 1.0 builds.
126      wasmsimd_copts: The list of compiler flags to use in WebAssembly SIMD
127                      builds.
128      wasmrelaxedsimd_copts: The list of compiler flags to use in WebAssembly
129                             Relaxed SIMD builds.
130      optimized_copts: The list of compiler flags to use in optimized builds.
131                       Defaults to -O2.
132      hdrs: The list of header files published by this library to be textually
133            included by sources in dependent rules.
134      defines: List of predefines macros to be added to the compile line.
135      includes: List of include dirs to be added to the compile line.
136      deps: The list of other libraries to be linked.
137      visibility: The list of packages that can depend on this target.
138    """
139    native.cc_library(
140        name = name,
141        srcs = srcs + select({
142            ":aarch32": aarch32_srcs,
143            ":aarch64": aarch64_srcs,
144            ":riscv": riscv_srcs,
145            ":x86": x86_srcs,
146            ":emscripten_wasm": wasm_srcs,
147            ":emscripten_wasmsimd": wasmsimd_srcs,
148            ":emscripten_wasmrelaxedsimd": wasmrelaxedsimd_srcs,
149            "//conditions:default": [],
150        }),
151        copts = [
152            "-Iinclude",
153            "-Isrc",
154        ] + copts + select({
155            ":linux_k8": gcc_x86_copts,
156            ":linux_arm": aarch32_copts,
157            ":linux_armeabi": aarch32_copts,
158            ":linux_armhf": aarch32_copts,
159            ":linux_armv7a": aarch32_copts,
160            ":linux_arm64": aarch64_copts,
161            ":macos_x86_64": gcc_x86_copts,
162            ":macos_arm64": aarch64_copts,
163            ":windows_x86_64_clang": ["/clang:" + opt for opt in gcc_x86_copts],
164            ":windows_x86_64_mingw": mingw_copts + gcc_x86_copts,
165            ":windows_x86_64_msys": msys_copts + gcc_x86_copts,
166            ":windows_x86_64": msvc_x86_64_copts,
167            ":android_armv7": aarch32_copts,
168            ":android_arm64": aarch64_copts,
169            ":android_x86": gcc_x86_copts,
170            ":android_x86_64": gcc_x86_copts,
171            ":ios_arm64": aarch64_copts,
172            ":ios_arm64e": aarch64_copts,
173            ":ios_sim_arm64": aarch64_copts,
174            ":ios_x86_64": gcc_x86_copts,
175            ":watchos_arm64_32": aarch64_copts,
176            ":watchos_x86_64": gcc_x86_copts,
177            ":tvos_arm64": aarch64_copts,
178            ":tvos_x86_64": gcc_x86_copts,
179            ":emscripten_wasm": wasm_copts,
180            ":emscripten_wasmsimd": wasmsimd_copts,
181            ":emscripten_wasmrelaxedsimd": wasmrelaxedsimd_copts,
182            "//conditions:default": [],
183        }) + select({
184            ":windows_x86_64_clang": ["/clang:" + opt for opt in gcc_copts],
185            ":windows_x86_64_mingw": gcc_copts,
186            ":windows_x86_64_msys": gcc_copts,
187            ":windows_x86_64": msvc_copts,
188            "//conditions:default": gcc_copts,
189        }) + select({
190            ":optimized_build": optimized_copts,
191            "//conditions:default": [],
192        }),
193        defines = defines,
194        deps = deps,
195        includes = ["include", "src"] + includes,
196        linkstatic = True,
197        linkopts = select({
198            ":linux_k8": ["-lpthread"],
199            ":linux_arm": ["-lpthread"],
200            ":linux_armeabi": ["-lpthread"],
201            ":linux_armhf": ["-lpthread"],
202            ":linux_armv7a": ["-lpthread"],
203            ":linux_arm64": ["-lpthread"],
204            ":android": ["-lm"],
205            "//conditions:default": [],
206        }),
207        textual_hdrs = hdrs,
208        visibility = visibility,
209        testonly = testonly,
210    )
211
212def xnnpack_aggregate_library(
213        name,
214        generic_deps = [],
215        x86_deps = [],
216        aarch32_deps = [],
217        aarch64_deps = [],
218        riscv_deps = [],
219        wasm_deps = [],
220        wasmsimd_deps = [],
221        wasmrelaxedsimd_deps = [],
222        defines = [],
223        compatible_with = None):
224    """Static library that aggregates architecture-specific dependencies.
225
226    Args:
227      name: The name of the library target to define.
228      generic_deps: The list of libraries to link on all architectures.
229      x86_deps: The list of libraries to link in x86 and x86-64 builds.
230      aarch32_deps: The list of libraries to link in AArch32 builds.
231      aarch64_deps: The list of libraries to link in AArch64 builds.
232      riscv_deps: The list of libraries to link in RISC-V builds.
233      wasm_deps: The list of libraries to link in WebAssembly 1.0 builds.
234      wasmsimd_deps: The list of libraries to link in WebAssembly SIMD builds.
235      wasmrelaxedsimd_deps: The list of libraries to link in WebAssembly
236                            Relaxed SIMD builds.
237      defines: List of predefines macros to be added to the compile line.
238      compatible_with: The list of additional environments this rule can be built for.
239    """
240
241    native.cc_library(
242        name = name,
243        linkstatic = True,
244        deps = generic_deps + select({
245            ":aarch32": aarch32_deps,
246            ":aarch64": aarch64_deps,
247            ":x86": x86_deps,
248            ":emscripten_wasm": wasm_deps,
249            ":emscripten_wasmsimd": wasmsimd_deps,
250            ":emscripten_wasmrelaxedsimd": wasmrelaxedsimd_deps,
251            ":riscv": riscv_deps,
252        }),
253        defines = defines,
254        compatible_with = compatible_with,
255    )
256
257def xnnpack_unit_test(name, srcs, copts = [], mingw_copts = [], msys_copts = [], deps = [], tags = [], automatic = True, timeout = "short", shard_count = 1):
258    """Unit test binary based on Google Test.
259
260    Args:
261      name: The name of the test target to define.
262      srcs: The list of source and header files.
263      copts: The list of additional compiler flags for the target. -I flags
264             for include/ and src/ directories of XNNPACK are always prepended
265             before these user-specified flags.
266      mingw_copts: The list of compiler flags to use with MinGW GCC compilers.
267      msys_copts: The list of compiler flags to use with MSYS (Cygwin) GCC compilers.
268      deps: The list of additional libraries to be linked. Google Test library
269            (with main() function) is always added as a dependency and does not
270            need to be explicitly specified.
271      tags: List of arbitrary text tags.
272      automatic: Whether to create the test or testable binary.
273      timeout: How long the test is expected to run before returning.
274      shard_count: Specifies the number of parallel shards to use to run the test.
275    """
276
277    if automatic:
278        native.cc_test(
279            name = name,
280            srcs = srcs,
281            copts = xnnpack_std_cxxopts() + [
282                "-Iinclude",
283                "-Isrc",
284            ] + select({
285                ":windows_x86_64_mingw": mingw_copts,
286                ":windows_x86_64_msys": msys_copts,
287                "//conditions:default": [],
288            }) + select({
289                ":windows_x86_64_clang": ["/clang:-Wno-unused-function"],
290                ":windows_x86_64_mingw": ["-Wno-unused-function"],
291                ":windows_x86_64_msys": ["-Wno-unused-function"],
292                ":windows_x86_64": [],
293                "//conditions:default": ["-Wno-unused-function"],
294            }) + copts,
295            linkopts = select({
296                ":emscripten": xnnpack_emscripten_test_linkopts(),
297                "//conditions:default": [],
298            }),
299            linkstatic = True,
300            deps = [
301                "@com_google_googletest//:gtest_main",
302            ] + deps + select({
303                ":emscripten": xnnpack_emscripten_deps(),
304                "//conditions:default": [],
305            }),
306            tags = tags,
307            timeout = timeout,
308            shard_count = shard_count,
309        )
310    else:
311        native.cc_binary(
312            name = name,
313            srcs = srcs,
314            copts = xnnpack_std_cxxopts() + [
315                "-Iinclude",
316                "-Isrc",
317            ] + select({
318                ":windows_x86_64_mingw": mingw_copts,
319                ":windows_x86_64_msys": msys_copts,
320                "//conditions:default": [],
321            }) + select({
322                ":windows_x86_64_clang": ["/clang:-Wno-unused-function"],
323                ":windows_x86_64_mingw": ["-Wno-unused-function"],
324                ":windows_x86_64_msys": ["-Wno-unused-function"],
325                ":windows_x86_64": [],
326                "//conditions:default": ["-Wno-unused-function"],
327            }) + copts,
328            linkopts = select({
329                ":emscripten": xnnpack_emscripten_test_linkopts(),
330                "//conditions:default": [],
331            }),
332            linkstatic = True,
333            deps = [
334                "@com_google_googletest//:gtest_main",
335            ] + deps + select({
336                ":emscripten": xnnpack_emscripten_deps(),
337                "//conditions:default": [],
338            }),
339            testonly = True,
340            tags = tags,
341        )
342
343def xnnpack_binary(name, srcs, copts = [], deps = []):
344    """Minimal binary
345
346    Args:
347      name: The name of the binary target to define.
348      srcs: The list of source and header files.
349      copts: The list of additional compiler flags for the target. -I flags
350             for include/ and src/ directories of XNNPACK are always prepended
351             before these user-specified flags.
352      deps: The list of libraries to be linked.
353    """
354    native.cc_binary(
355        name = name,
356        srcs = srcs,
357        copts = [
358            "-Iinclude",
359            "-Isrc",
360        ] + copts,
361        linkopts = select({
362            ":emscripten": xnnpack_emscripten_minimal_linkopts(),
363            "//conditions:default": [],
364        }),
365        linkstatic = True,
366        deps = deps,
367    )
368
369def xnnpack_benchmark(name, srcs, copts = [], deps = [], tags = []):
370    """Microbenchmark binary based on Google Benchmark
371
372    Args:
373      name: The name of the binary target to define.
374      srcs: The list of source and header files.
375      copts: The list of additional compiler flags for the target. -I flags
376             for include/ and src/ directories of XNNPACK are always prepended
377             before these user-specified flags.
378      deps: The list of additional libraries to be linked. Google Benchmark
379            library is always added as a dependency and does not need to be
380            explicitly specified.
381    """
382    native.cc_binary(
383        name = name,
384        srcs = srcs,
385        copts = xnnpack_std_cxxopts() + [
386            "-Iinclude",
387            "-Isrc",
388        ] + select({
389            ":windows_x86_64_clang": ["/clang:-Wno-unused-function"],
390            ":windows_x86_64_mingw": ["-Wno-unused-function"],
391            ":windows_x86_64_msys": ["-Wno-unused-function"],
392            ":windows_x86_64": [],
393            "//conditions:default": ["-Wno-unused-function"],
394        }) + copts,
395        linkopts = select({
396            ":emscripten": xnnpack_emscripten_benchmark_linkopts(),
397            ":windows_x86_64_mingw": ["-lshlwapi"],
398            ":windows_x86_64_msys": ["-lshlwapi"],
399            "//conditions:default": [],
400        }),
401        linkstatic = True,
402        deps = [
403            "@com_google_benchmark//:benchmark",
404        ] + deps + select({
405            ":emscripten": xnnpack_emscripten_deps(),
406            "//conditions:default": [],
407        }),
408	tags = tags,
409    )
410