-
Notifications
You must be signed in to change notification settings - Fork 2
/
abi.bzl
136 lines (121 loc) · 4.41 KB
/
abi.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# SPDX-License-Identifier: GPL-2.0 OR Apache-2.0
# Copyright (C) 2023 The Android Open Source Project
"""
ABI aware build rules.
"""
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
visibility("private")
_ALL_ABIS = ["arm", "arm64", "x86", "x86_64"]
def _copy_with_abi(
name,
visibility = None,
path_prefix = None,
abis = None,
out = None):
if not path_prefix:
path_prefix = ""
if not abis:
abis = _ALL_ABIS
if not out:
out = name
for abi in abis:
copy_file(
name = "{name}_{abi}".format(name = name, abi = abi),
src = ":{name}".format(name = name),
out = paths.join(path_prefix, abi, out),
allow_symlink = True,
visibility = visibility,
)
def cc_binary_with_abi(
name,
path_prefix = None,
abis = None,
visibility = None,
out = None,
**kwargs):
"""A cc_binary replacement that generates output in each subdirectory named by abi.
For example:
```
cc_binary_with_abi(
name = "a_binary",
abis = ["x86_64", "arm64"],
path_prefix = "my/path",
)
```
generates 2 rules:
* Rule a_binary_x86_64: Builds the cc_binary and put output in my/path/x86_64/a_binary.
* Rule a_binary_arm64: Builds the cc_binary and put output in my/path/arm64/a_binary.
Args:
name: the name of the build rule.
path_prefix: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The path prefix to attach to output.
abis: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The intended abis to generate. Default is arm64 & x86_64.
visibility: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The visibility attribute on a target controls whether the target can be used in other packages.
out: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The output filename. Default is `name`.
**kwargs: the rest args that cc_binary uses.
"""
native.cc_binary(
name = name,
visibility = visibility,
**kwargs
)
_copy_with_abi(
name = name,
path_prefix = path_prefix,
abis = abis,
visibility = visibility,
out = out,
)
def sh_binary_with_abi(
name,
path_prefix = None,
abis = None,
visibility = None,
out = None,
**kwargs):
"""A sh_binary replacement that generates output in each subdirectory named by abi.
For example:
```
sh_binary_with_abi(
name = "a_binary",
abis = ["x86_64", "arm64"],
path_prefix = "my/path",
)
```
generates 2 rules:
* Rule a_binary_x86_64: Copies a_binary and put output in my/path/x86_64/a_binary.
* Rule a_binary_arm64: Copies a_binary and put output in my/path/arm64/a_binary.
Args:
name: the name of the build rule.
path_prefix: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The path prefix to attach to output.
abis: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The intended abis to generate. Default is arm64 & x86_64.
visibility: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The visibility attribute on a target controls whether the target can be used in other packages.
out: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
The output filename. Default is `name`.
**kwargs: the rest args that native_binary uses.
"""
if not out:
out = name
# Uses native_binary instead of sh_binary because sh_binary is not
# compatible with copy_file (sh_binary generates more than 1 outs).
native_binary(
name = name,
visibility = visibility,
out = out,
**kwargs
)
_copy_with_abi(
name = name,
path_prefix = path_prefix,
abis = abis,
visibility = visibility,
out = out,
)