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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
// Copyright 2021 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bazel
import (
"fmt"
"math"
"sort"
"strings"
)
const (
// ArchType names in arch.go
archArm = "arm"
archArm64 = "arm64"
archRiscv64 = "riscv64"
archX86 = "x86"
archX86_64 = "x86_64"
// OsType names in arch.go
OsAndroid = "android"
OsDarwin = "darwin"
OsLinux = "linux_glibc"
osLinuxMusl = "linux_musl"
osLinuxBionic = "linux_bionic"
OsWindows = "windows"
// Targets in arch.go
osArchAndroidArm = "android_arm"
OsArchAndroidArm64 = "android_arm64"
osArchAndroidRiscv64 = "android_riscv64"
osArchAndroidX86 = "android_x86"
osArchAndroidX86_64 = "android_x86_64"
osArchDarwinArm64 = "darwin_arm64"
osArchDarwinX86_64 = "darwin_x86_64"
osArchLinuxX86 = "linux_glibc_x86"
osArchLinuxX86_64 = "linux_glibc_x86_64"
osArchLinuxMuslArm = "linux_musl_arm"
osArchLinuxMuslArm64 = "linux_musl_arm64"
osArchLinuxMuslX86 = "linux_musl_x86"
osArchLinuxMuslX86_64 = "linux_musl_x86_64"
osArchLinuxBionicArm64 = "linux_bionic_arm64"
osArchLinuxBionicX86_64 = "linux_bionic_x86_64"
osArchWindowsX86 = "windows_x86"
osArchWindowsX86_64 = "windows_x86_64"
// This is the string representation of the default condition wherever a
// configurable attribute is used in a select statement, i.e.
// //conditions:default for Bazel.
//
// This is consistently named "conditions_default" to mirror the Soong
// config variable default key in an Android.bp file, although there's no
// integration with Soong config variables (yet).
ConditionsDefaultConfigKey = "conditions_default"
ConditionsDefaultSelectKey = "//conditions:default"
productVariableBazelPackage = "//build/bazel/product_config/config_settings"
AndroidAndInApex = "android-in_apex"
AndroidPlatform = "system"
InApex = "in_apex"
NonApex = "non_apex"
ErrorproneDisabled = "errorprone_disabled"
// TODO: b/294868620 - Remove when completing the bug
SanitizersEnabled = "sanitizers_enabled"
)
func PowerSetWithoutEmptySet[T any](items []T) [][]T {
resultSize := int(math.Pow(2, float64(len(items))))
powerSet := make([][]T, 0, resultSize-1)
for i := 1; i < resultSize; i++ {
combination := make([]T, 0)
for j := 0; j < len(items); j++ {
if (i>>j)%2 == 1 {
combination = append(combination, items[j])
}
}
powerSet = append(powerSet, combination)
}
return powerSet
}
func createPlatformArchMap() map[string]string {
// Copy of archFeatures from android/arch_list.go because the bazel
// package can't access the android package
archFeatures := map[string][]string{
"arm": {
"neon",
},
"arm64": {
"dotprod",
},
"riscv64": {},
"x86": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"avx2",
"avx512",
"popcnt",
"movbe",
},
"x86_64": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"avx2",
"avx512",
"popcnt",
},
}
result := make(map[string]string)
for arch, allFeatures := range archFeatures {
result[arch] = "//build/bazel/platforms/arch:" + arch
// Sometimes we want to select on multiple features being active, so
// add the power set of all possible features to the map. More details
// in android.ModuleBase.GetArchVariantProperties
for _, features := range PowerSetWithoutEmptySet(allFeatures) {
sort.Strings(features)
archFeaturesName := arch + "-" + strings.Join(features, "-")
result[archFeaturesName] = "//build/bazel/platforms/arch/variants:" + archFeaturesName
}
}
result[ConditionsDefaultConfigKey] = ConditionsDefaultSelectKey
return result
}
var (
// These are the list of OSes and architectures with a Bazel config_setting
// and constraint value equivalent. These exist in arch.go, but the android
// package depends on the bazel package, so a cyclic dependency prevents
// using those variables here.
// A map of architectures to the Bazel label of the constraint_value
// for the @platforms//cpu:cpu constraint_setting
platformArchMap = createPlatformArchMap()
// A map of target operating systems to the Bazel label of the
// constraint_value for the @platforms//os:os constraint_setting
platformOsMap = map[string]string{
OsAndroid: "//build/bazel/platforms/os:android",
OsDarwin: "//build/bazel/platforms/os:darwin",
OsLinux: "//build/bazel/platforms/os:linux_glibc",
osLinuxMusl: "//build/bazel/platforms/os:linux_musl",
osLinuxBionic: "//build/bazel/platforms/os:linux_bionic",
OsWindows: "//build/bazel/platforms/os:windows",
ConditionsDefaultConfigKey: ConditionsDefaultSelectKey, // The default condition of an os select map.
}
platformOsArchMap = map[string]string{
osArchAndroidArm: "//build/bazel/platforms/os_arch:android_arm",
OsArchAndroidArm64: "//build/bazel/platforms/os_arch:android_arm64",
osArchAndroidRiscv64: "//build/bazel/platforms/os_arch:android_riscv64",
osArchAndroidX86: "//build/bazel/platforms/os_arch:android_x86",
osArchAndroidX86_64: "//build/bazel/platforms/os_arch:android_x86_64",
osArchDarwinArm64: "//build/bazel/platforms/os_arch:darwin_arm64",
osArchDarwinX86_64: "//build/bazel/platforms/os_arch:darwin_x86_64",
osArchLinuxX86: "//build/bazel/platforms/os_arch:linux_glibc_x86",
osArchLinuxX86_64: "//build/bazel/platforms/os_arch:linux_glibc_x86_64",
osArchLinuxMuslArm: "//build/bazel/platforms/os_arch:linux_musl_arm",
osArchLinuxMuslArm64: "//build/bazel/platforms/os_arch:linux_musl_arm64",
osArchLinuxMuslX86: "//build/bazel/platforms/os_arch:linux_musl_x86",
osArchLinuxMuslX86_64: "//build/bazel/platforms/os_arch:linux_musl_x86_64",
osArchLinuxBionicArm64: "//build/bazel/platforms/os_arch:linux_bionic_arm64",
osArchLinuxBionicX86_64: "//build/bazel/platforms/os_arch:linux_bionic_x86_64",
osArchWindowsX86: "//build/bazel/platforms/os_arch:windows_x86",
osArchWindowsX86_64: "//build/bazel/platforms/os_arch:windows_x86_64",
ConditionsDefaultConfigKey: ConditionsDefaultSelectKey, // The default condition of an os select map.
}
// Map where keys are OsType names, and values are slices containing the archs
// that that OS supports.
// These definitions copied from arch.go.
// TODO(cparsons): Source from arch.go; this task is nontrivial, as it currently results
// in a cyclic dependency.
osToArchMap = map[string][]string{
OsAndroid: {archArm, archArm64, archRiscv64, archX86, archX86_64},
OsLinux: {archX86, archX86_64},
osLinuxMusl: {archX86, archX86_64},
OsDarwin: {archArm64, archX86_64},
osLinuxBionic: {archArm64, archX86_64},
// TODO(cparsons): According to arch.go, this should contain archArm, archArm64, as well.
OsWindows: {archX86, archX86_64},
}
osAndInApexMap = map[string]string{
AndroidAndInApex: "//build/bazel/rules/apex:android-in_apex",
AndroidPlatform: "//build/bazel/rules/apex:system",
OsDarwin: "//build/bazel/platforms/os:darwin",
OsLinux: "//build/bazel/platforms/os:linux_glibc",
osLinuxMusl: "//build/bazel/platforms/os:linux_musl",
osLinuxBionic: "//build/bazel/platforms/os:linux_bionic",
OsWindows: "//build/bazel/platforms/os:windows",
ConditionsDefaultConfigKey: ConditionsDefaultSelectKey,
}
inApexMap = map[string]string{
InApex: "//build/bazel/rules/apex:in_apex",
NonApex: "//build/bazel/rules/apex:non_apex",
ConditionsDefaultConfigKey: ConditionsDefaultSelectKey,
}
errorProneMap = map[string]string{
ErrorproneDisabled: "//build/bazel/rules/java/errorprone:errorprone_globally_disabled",
ConditionsDefaultConfigKey: ConditionsDefaultSelectKey,
}
// TODO: b/294868620 - Remove when completing the bug
sanitizersEnabledMap = map[string]string{
SanitizersEnabled: "//build/bazel/rules/cc:sanitizers_enabled",
ConditionsDefaultConfigKey: ConditionsDefaultSelectKey,
}
)
// basic configuration types
type configurationType int
const (
noConfig configurationType = iota
arch
os
osArch
productVariables
osAndInApex
inApex
errorProneDisabled
// TODO: b/294868620 - Remove when completing the bug
sanitizersEnabled
)
func osArchString(os string, arch string) string {
return fmt.Sprintf("%s_%s", os, arch)
}
func (ct configurationType) String() string {
return map[configurationType]string{
noConfig: "no_config",
arch: "arch",
os: "os",
osArch: "arch_os",
productVariables: "product_variables",
osAndInApex: "os_in_apex",
inApex: "in_apex",
errorProneDisabled: "errorprone_disabled",
// TODO: b/294868620 - Remove when completing the bug
sanitizersEnabled: "sanitizers_enabled",
}[ct]
}
func (ct configurationType) validateConfig(config string) {
switch ct {
case noConfig:
if config != "" {
panic(fmt.Errorf("Cannot specify config with %s, but got %s", ct, config))
}
case arch:
if _, ok := platformArchMap[config]; !ok {
panic(fmt.Errorf("Unknown arch: %s", config))
}
case os:
if _, ok := platformOsMap[config]; !ok {
panic(fmt.Errorf("Unknown os: %s", config))
}
case osArch:
if _, ok := platformOsArchMap[config]; !ok {
panic(fmt.Errorf("Unknown os+arch: %s", config))
}
case productVariables:
// do nothing
case osAndInApex:
// do nothing
// this axis can contain additional per-apex keys
case inApex:
if _, ok := inApexMap[config]; !ok {
panic(fmt.Errorf("Unknown in_apex config: %s", config))
}
case errorProneDisabled:
if _, ok := errorProneMap[config]; !ok {
panic(fmt.Errorf("Unknown errorprone config: %s", config))
}
// TODO: b/294868620 - Remove when completing the bug
case sanitizersEnabled:
if _, ok := sanitizersEnabledMap[config]; !ok {
panic(fmt.Errorf("Unknown sanitizers_enabled config: %s", config))
}
default:
panic(fmt.Errorf("Unrecognized ConfigurationType %d", ct))
}
}
// SelectKey returns the Bazel select key for a given configurationType and config string.
func (ca ConfigurationAxis) SelectKey(config string) string {
ca.validateConfig(config)
switch ca.configurationType {
case noConfig:
panic(fmt.Errorf("SelectKey is unnecessary for noConfig ConfigurationType "))
case arch:
return platformArchMap[config]
case os:
return platformOsMap[config]
case osArch:
return platformOsArchMap[config]
case productVariables:
if config == ConditionsDefaultConfigKey {
return ConditionsDefaultSelectKey
}
return fmt.Sprintf("%s:%s", productVariableBazelPackage, config)
case osAndInApex:
if ret, exists := osAndInApexMap[config]; exists {
return ret
}
return config
case inApex:
return inApexMap[config]
case errorProneDisabled:
return errorProneMap[config]
// TODO: b/294868620 - Remove when completing the bug
case sanitizersEnabled:
return sanitizersEnabledMap[config]
default:
panic(fmt.Errorf("Unrecognized ConfigurationType %d", ca.configurationType))
}
}
var (
// Indicating there is no configuration axis
NoConfigAxis = ConfigurationAxis{configurationType: noConfig}
// An axis for architecture-specific configurations
ArchConfigurationAxis = ConfigurationAxis{configurationType: arch}
// An axis for os-specific configurations
OsConfigurationAxis = ConfigurationAxis{configurationType: os}
// An axis for arch+os-specific configurations
OsArchConfigurationAxis = ConfigurationAxis{configurationType: osArch}
// An axis for os+in_apex-specific configurations
OsAndInApexAxis = ConfigurationAxis{configurationType: osAndInApex}
// An axis for in_apex-specific configurations
InApexAxis = ConfigurationAxis{configurationType: inApex}
ErrorProneAxis = ConfigurationAxis{configurationType: errorProneDisabled}
// TODO: b/294868620 - Remove when completing the bug
SanitizersEnabledAxis = ConfigurationAxis{configurationType: sanitizersEnabled}
)
// ProductVariableConfigurationAxis returns an axis for the given product variable
func ProductVariableConfigurationAxis(archVariant bool, variable string) ConfigurationAxis {
return ConfigurationAxis{
configurationType: productVariables,
subType: variable,
archVariant: archVariant,
}
}
// ConfigurationAxis is an independent axis for configuration, there should be no overlap between
// elements within an axis.
type ConfigurationAxis struct {
configurationType
// some configuration types (e.g. productVariables) have multiple independent axes, subType helps
// distinguish between them without needing to list all 17 product variables.
subType string
archVariant bool
}
func (ca *ConfigurationAxis) less(other ConfigurationAxis) bool {
if ca.configurationType == other.configurationType {
return ca.subType < other.subType
}
return ca.configurationType < other.configurationType
}
|