blob: 84dca7e26900561246a1ced335a5482c0570743b [file] [log] [blame]
Jingwen Chen30f5aaa2020-11-19 05:38:02 -05001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bazel
16
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000017import (
18 "fmt"
Jingwen Chen63930982021-03-24 10:04:33 -040019 "path/filepath"
Liz Kammera060c452021-03-24 10:14:47 -040020 "regexp"
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000021 "sort"
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040022 "strings"
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000023)
Jingwen Chen5d864492021-02-24 07:20:12 -050024
Jingwen Chen73850672020-12-14 08:25:34 -050025// BazelTargetModuleProperties contain properties and metadata used for
26// Blueprint to BUILD file conversion.
27type BazelTargetModuleProperties struct {
28 // The Bazel rule class for this target.
Liz Kammerfc46bc12021-02-19 11:06:17 -050029 Rule_class string `blueprint:"mutated"`
Jingwen Chen40067de2021-01-26 21:58:43 -050030
31 // The target label for the bzl file containing the definition of the rule class.
Liz Kammerfc46bc12021-02-19 11:06:17 -050032 Bzl_load_location string `blueprint:"mutated"`
Jingwen Chen73850672020-12-14 08:25:34 -050033}
Liz Kammer356f7d42021-01-26 09:18:53 -050034
Jingwen Chenfb4692a2021-02-07 10:05:16 -050035const BazelTargetModuleNamePrefix = "__bp2build__"
36
Liz Kammera060c452021-03-24 10:14:47 -040037var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)")
38
Jingwen Chen38e62642021-04-19 05:00:15 +000039// Label is used to represent a Bazel compatible Label. Also stores the original
40// bp text to support string replacement.
Liz Kammer356f7d42021-01-26 09:18:53 -050041type Label struct {
Jingwen Chen38e62642021-04-19 05:00:15 +000042 // The string representation of a Bazel target label. This can be a relative
43 // or fully qualified label. These labels are used for generating BUILD
44 // files with bp2build.
45 Label string
46
47 // The original Soong/Blueprint module name that the label was derived from.
48 // This is used for replacing references to the original name with the new
49 // label, for example in genrule cmds.
50 //
51 // While there is a reversible 1:1 mapping from the module name to Bazel
52 // label with bp2build that could make computing the original module name
53 // from the label automatic, it is not the case for handcrafted targets,
54 // where modules can have a custom label mapping through the { bazel_module:
55 // { label: <label> } } property.
56 //
57 // With handcrafted labels, those modules don't go through bp2build
58 // conversion, but relies on handcrafted targets in the source tree.
59 OriginalModuleName string
Liz Kammer356f7d42021-01-26 09:18:53 -050060}
61
62// LabelList is used to represent a list of Bazel labels.
63type LabelList struct {
64 Includes []Label
65 Excludes []Label
66}
67
Jingwen Chen63930982021-03-24 10:04:33 -040068// uniqueParentDirectories returns a list of the unique parent directories for
69// all files in ll.Includes.
70func (ll *LabelList) uniqueParentDirectories() []string {
71 dirMap := map[string]bool{}
72 for _, label := range ll.Includes {
73 dirMap[filepath.Dir(label.Label)] = true
74 }
75 dirs := []string{}
76 for dir := range dirMap {
77 dirs = append(dirs, dir)
78 }
79 return dirs
80}
81
Liz Kammer356f7d42021-01-26 09:18:53 -050082// Append appends the fields of other labelList to the corresponding fields of ll.
83func (ll *LabelList) Append(other LabelList) {
84 if len(ll.Includes) > 0 || len(other.Includes) > 0 {
85 ll.Includes = append(ll.Includes, other.Includes...)
86 }
87 if len(ll.Excludes) > 0 || len(other.Excludes) > 0 {
88 ll.Excludes = append(other.Excludes, other.Excludes...)
89 }
90}
Jingwen Chen5d864492021-02-24 07:20:12 -050091
Jingwen Chened9c17d2021-04-13 07:14:55 +000092// UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns
93// the slice in a sorted order.
94func UniqueSortedBazelLabels(originalLabels []Label) []Label {
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000095 uniqueLabelsSet := make(map[Label]bool)
96 for _, l := range originalLabels {
97 uniqueLabelsSet[l] = true
98 }
99 var uniqueLabels []Label
100 for l, _ := range uniqueLabelsSet {
101 uniqueLabels = append(uniqueLabels, l)
102 }
103 sort.SliceStable(uniqueLabels, func(i, j int) bool {
104 return uniqueLabels[i].Label < uniqueLabels[j].Label
105 })
106 return uniqueLabels
107}
108
109func UniqueBazelLabelList(originalLabelList LabelList) LabelList {
110 var uniqueLabelList LabelList
Jingwen Chened9c17d2021-04-13 07:14:55 +0000111 uniqueLabelList.Includes = UniqueSortedBazelLabels(originalLabelList.Includes)
112 uniqueLabelList.Excludes = UniqueSortedBazelLabels(originalLabelList.Excludes)
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000113 return uniqueLabelList
114}
115
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000116// Subtract needle from haystack
117func SubtractStrings(haystack []string, needle []string) []string {
118 // This is really a set
119 remainder := make(map[string]bool)
120
121 for _, s := range haystack {
122 remainder[s] = true
123 }
124 for _, s := range needle {
125 delete(remainder, s)
126 }
127
128 var strings []string
129 for s, _ := range remainder {
130 strings = append(strings, s)
131 }
132
133 sort.SliceStable(strings, func(i, j int) bool {
134 return strings[i] < strings[j]
135 })
136
137 return strings
138}
139
140// Subtract needle from haystack
141func SubtractBazelLabels(haystack []Label, needle []Label) []Label {
142 // This is really a set
143 remainder := make(map[Label]bool)
144
145 for _, label := range haystack {
146 remainder[label] = true
147 }
148 for _, label := range needle {
149 delete(remainder, label)
150 }
151
152 var labels []Label
153 for label, _ := range remainder {
154 labels = append(labels, label)
155 }
156
157 sort.SliceStable(labels, func(i, j int) bool {
158 return labels[i].Label < labels[j].Label
159 })
160
161 return labels
162}
163
Chris Parsons484e50a2021-05-13 15:13:04 -0400164// Appends two LabelLists, returning the combined list.
165func AppendBazelLabelLists(a LabelList, b LabelList) LabelList {
166 var result LabelList
167 result.Includes = append(a.Includes, b.Includes...)
168 result.Excludes = append(a.Excludes, b.Excludes...)
169 return result
170}
171
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000172// Subtract needle from haystack
173func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList {
174 var result LabelList
175 result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes)
176 // NOTE: Excludes are intentionally not subtracted
177 result.Excludes = haystack.Excludes
178 return result
179}
180
Jingwen Chen07027912021-03-15 06:02:43 -0400181const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400182 // ArchType names in arch.go
Jingwen Chen07027912021-03-15 06:02:43 -0400183 ARCH_ARM = "arm"
184 ARCH_ARM64 = "arm64"
Jingwen Chen91220d72021-03-24 02:18:33 -0400185 ARCH_X86 = "x86"
186 ARCH_X86_64 = "x86_64"
187
188 // OsType names in arch.go
189 OS_ANDROID = "android"
190 OS_DARWIN = "darwin"
191 OS_FUCHSIA = "fuchsia"
192 OS_LINUX = "linux_glibc"
193 OS_LINUX_BIONIC = "linux_bionic"
194 OS_WINDOWS = "windows"
Jingwen Chene32e9e02021-04-23 09:17:24 +0000195
196 // This is the string representation of the default condition wherever a
197 // configurable attribute is used in a select statement, i.e.
198 // //conditions:default for Bazel.
199 //
200 // This is consistently named "conditions_default" to mirror the Soong
201 // config variable default key in an Android.bp file, although there's no
202 // integration with Soong config variables (yet).
203 CONDITIONS_DEFAULT = "conditions_default"
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400204
205 ConditionsDefaultSelectKey = "//conditions:default"
206
207 productVariableBazelPackage = "//build/bazel/product_variables"
Jingwen Chen07027912021-03-15 06:02:43 -0400208)
209
210var (
Jingwen Chenc1c26502021-04-05 10:35:13 +0000211 // These are the list of OSes and architectures with a Bazel config_setting
212 // and constraint value equivalent. These exist in arch.go, but the android
213 // package depends on the bazel package, so a cyclic dependency prevents
214 // using those variables here.
Jingwen Chen91220d72021-03-24 02:18:33 -0400215
216 // A map of architectures to the Bazel label of the constraint_value
217 // for the @platforms//cpu:cpu constraint_setting
218 PlatformArchMap = map[string]string{
Jingwen Chene32e9e02021-04-23 09:17:24 +0000219 ARCH_ARM: "//build/bazel/platforms/arch:arm",
220 ARCH_ARM64: "//build/bazel/platforms/arch:arm64",
221 ARCH_X86: "//build/bazel/platforms/arch:x86",
222 ARCH_X86_64: "//build/bazel/platforms/arch:x86_64",
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400223 CONDITIONS_DEFAULT: ConditionsDefaultSelectKey, // The default condition of as arch select map.
Jingwen Chen91220d72021-03-24 02:18:33 -0400224 }
225
226 // A map of target operating systems to the Bazel label of the
227 // constraint_value for the @platforms//os:os constraint_setting
228 PlatformOsMap = map[string]string{
Jingwen Chene32e9e02021-04-23 09:17:24 +0000229 OS_ANDROID: "//build/bazel/platforms/os:android",
230 OS_DARWIN: "//build/bazel/platforms/os:darwin",
231 OS_FUCHSIA: "//build/bazel/platforms/os:fuchsia",
232 OS_LINUX: "//build/bazel/platforms/os:linux",
233 OS_LINUX_BIONIC: "//build/bazel/platforms/os:linux_bionic",
234 OS_WINDOWS: "//build/bazel/platforms/os:windows",
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400235 CONDITIONS_DEFAULT: ConditionsDefaultSelectKey, // The default condition of an os select map.
Jingwen Chen91220d72021-03-24 02:18:33 -0400236 }
Jingwen Chen07027912021-03-15 06:02:43 -0400237)
238
Jingwen Chenc1c26502021-04-05 10:35:13 +0000239type Attribute interface {
240 HasConfigurableValues() bool
241}
242
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200243// Represents an attribute whose value is a single label
244type LabelAttribute struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200245 Value Label
246 X86 Label
247 X86_64 Label
248 Arm Label
249 Arm64 Label
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200250}
251
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200252func (attr *LabelAttribute) GetValueForArch(arch string) Label {
253 switch arch {
254 case ARCH_ARM:
255 return attr.Arm
256 case ARCH_ARM64:
257 return attr.Arm64
258 case ARCH_X86:
259 return attr.X86
260 case ARCH_X86_64:
261 return attr.X86_64
262 case CONDITIONS_DEFAULT:
263 return attr.Value
264 default:
265 panic("Invalid arch type")
266 }
267}
268
269func (attr *LabelAttribute) SetValueForArch(arch string, value Label) {
270 switch arch {
271 case ARCH_ARM:
272 attr.Arm = value
273 case ARCH_ARM64:
274 attr.Arm64 = value
275 case ARCH_X86:
276 attr.X86 = value
277 case ARCH_X86_64:
278 attr.X86_64 = value
279 default:
280 panic("Invalid arch type")
281 }
282}
283
284func (attr LabelAttribute) HasConfigurableValues() bool {
285 return attr.Arm.Label != "" || attr.Arm64.Label != "" || attr.X86.Label != "" || attr.X86_64.Label != ""
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200286}
287
Jingwen Chen07027912021-03-15 06:02:43 -0400288// Arch-specific label_list typed Bazel attribute values. This should correspond
289// to the types of architectures supported for compilation in arch.go.
290type labelListArchValues struct {
291 X86 LabelList
292 X86_64 LabelList
293 Arm LabelList
294 Arm64 LabelList
Jingwen Chen91220d72021-03-24 02:18:33 -0400295 Common LabelList
Jingwen Chene32e9e02021-04-23 09:17:24 +0000296
297 ConditionsDefault LabelList
Jingwen Chen91220d72021-03-24 02:18:33 -0400298}
299
300type labelListOsValues struct {
301 Android LabelList
302 Darwin LabelList
303 Fuchsia LabelList
304 Linux LabelList
305 LinuxBionic LabelList
306 Windows LabelList
Jingwen Chene32e9e02021-04-23 09:17:24 +0000307
308 ConditionsDefault LabelList
Jingwen Chen07027912021-03-15 06:02:43 -0400309}
310
311// LabelListAttribute is used to represent a list of Bazel labels as an
312// attribute.
313type LabelListAttribute struct {
314 // The non-arch specific attribute label list Value. Required.
315 Value LabelList
316
317 // The arch-specific attribute label list values. Optional. If used, these
318 // are generated in a select statement and appended to the non-arch specific
319 // label list Value.
320 ArchValues labelListArchValues
Jingwen Chen91220d72021-03-24 02:18:33 -0400321
322 // The os-specific attribute label list values. Optional. If used, these
323 // are generated in a select statement and appended to the non-os specific
324 // label list Value.
325 OsValues labelListOsValues
Jingwen Chen07027912021-03-15 06:02:43 -0400326}
327
328// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
329func MakeLabelListAttribute(value LabelList) LabelListAttribute {
330 return LabelListAttribute{Value: UniqueBazelLabelList(value)}
331}
332
Jingwen Chened9c17d2021-04-13 07:14:55 +0000333// Append all values, including os and arch specific ones, from another
Jingwen Chen63930982021-03-24 10:04:33 -0400334// LabelListAttribute to this LabelListAttribute.
335func (attrs *LabelListAttribute) Append(other LabelListAttribute) {
336 for arch := range PlatformArchMap {
337 this := attrs.GetValueForArch(arch)
338 that := other.GetValueForArch(arch)
339 this.Append(that)
340 attrs.SetValueForArch(arch, this)
341 }
342
343 for os := range PlatformOsMap {
344 this := attrs.GetValueForOS(os)
345 that := other.GetValueForOS(os)
346 this.Append(that)
347 attrs.SetValueForOS(os, this)
348 }
349
350 attrs.Value.Append(other.Value)
351}
352
Jingwen Chen07027912021-03-15 06:02:43 -0400353// HasArchSpecificValues returns true if the attribute contains
354// architecture-specific label_list values.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000355func (attrs LabelListAttribute) HasConfigurableValues() bool {
356 for arch := range PlatformArchMap {
Jingwen Chen91220d72021-03-24 02:18:33 -0400357 if len(attrs.GetValueForArch(arch).Includes) > 0 {
358 return true
359 }
360 }
361
Jingwen Chenc1c26502021-04-05 10:35:13 +0000362 for os := range PlatformOsMap {
Jingwen Chen91220d72021-03-24 02:18:33 -0400363 if len(attrs.GetValueForOS(os).Includes) > 0 {
Jingwen Chen07027912021-03-15 06:02:43 -0400364 return true
365 }
366 }
367 return false
368}
369
Jingwen Chen91220d72021-03-24 02:18:33 -0400370func (attrs *LabelListAttribute) archValuePtrs() map[string]*LabelList {
371 return map[string]*LabelList{
Jingwen Chene32e9e02021-04-23 09:17:24 +0000372 ARCH_X86: &attrs.ArchValues.X86,
373 ARCH_X86_64: &attrs.ArchValues.X86_64,
374 ARCH_ARM: &attrs.ArchValues.Arm,
375 ARCH_ARM64: &attrs.ArchValues.Arm64,
376 CONDITIONS_DEFAULT: &attrs.ArchValues.ConditionsDefault,
Jingwen Chen91220d72021-03-24 02:18:33 -0400377 }
378}
379
Jingwen Chen07027912021-03-15 06:02:43 -0400380// GetValueForArch returns the label_list attribute value for an architecture.
381func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList {
Jingwen Chen91220d72021-03-24 02:18:33 -0400382 var v *LabelList
383 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400384 panic(fmt.Errorf("Unknown arch: %s", arch))
385 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400386 return *v
Jingwen Chen07027912021-03-15 06:02:43 -0400387}
388
389// SetValueForArch sets the label_list attribute value for an architecture.
390func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400391 var v *LabelList
392 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400393 panic(fmt.Errorf("Unknown arch: %s", arch))
394 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400395 *v = value
396}
397
398func (attrs *LabelListAttribute) osValuePtrs() map[string]*LabelList {
399 return map[string]*LabelList{
Jingwen Chene32e9e02021-04-23 09:17:24 +0000400 OS_ANDROID: &attrs.OsValues.Android,
401 OS_DARWIN: &attrs.OsValues.Darwin,
402 OS_FUCHSIA: &attrs.OsValues.Fuchsia,
403 OS_LINUX: &attrs.OsValues.Linux,
404 OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic,
405 OS_WINDOWS: &attrs.OsValues.Windows,
406 CONDITIONS_DEFAULT: &attrs.OsValues.ConditionsDefault,
Jingwen Chen91220d72021-03-24 02:18:33 -0400407 }
408}
409
410// GetValueForOS returns the label_list attribute value for an OS target.
411func (attrs *LabelListAttribute) GetValueForOS(os string) LabelList {
412 var v *LabelList
413 if v = attrs.osValuePtrs()[os]; v == nil {
414 panic(fmt.Errorf("Unknown os: %s", os))
415 }
416 return *v
417}
418
419// SetValueForArch sets the label_list attribute value for an OS target.
420func (attrs *LabelListAttribute) SetValueForOS(os string, value LabelList) {
421 var v *LabelList
422 if v = attrs.osValuePtrs()[os]; v == nil {
423 panic(fmt.Errorf("Unknown os: %s", os))
424 }
425 *v = value
Jingwen Chen07027912021-03-15 06:02:43 -0400426}
427
Jingwen Chen5d864492021-02-24 07:20:12 -0500428// StringListAttribute corresponds to the string_list Bazel attribute type with
429// support for additional metadata, like configurations.
430type StringListAttribute struct {
431 // The base value of the string list attribute.
432 Value []string
433
Jingwen Chenc1c26502021-04-05 10:35:13 +0000434 // The arch-specific attribute string list values. Optional. If used, these
435 // are generated in a select statement and appended to the non-arch specific
436 // label list Value.
Jingwen Chen5d864492021-02-24 07:20:12 -0500437 ArchValues stringListArchValues
Jingwen Chenc1c26502021-04-05 10:35:13 +0000438
439 // The os-specific attribute string list values. Optional. If used, these
440 // are generated in a select statement and appended to the non-os specific
441 // label list Value.
442 OsValues stringListOsValues
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400443
444 // list of product-variable string list values. Optional. if used, each will generate a select
445 // statement appended to the label list Value.
446 ProductValues []ProductVariableValues
Jingwen Chen5d864492021-02-24 07:20:12 -0500447}
448
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000449// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value.
450func MakeStringListAttribute(value []string) StringListAttribute {
451 // NOTE: These strings are not necessarily unique or sorted.
452 return StringListAttribute{Value: value}
453}
454
Jingwen Chen5d864492021-02-24 07:20:12 -0500455// Arch-specific string_list typed Bazel attribute values. This should correspond
456// to the types of architectures supported for compilation in arch.go.
457type stringListArchValues struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400458 X86 []string
459 X86_64 []string
460 Arm []string
461 Arm64 []string
Jingwen Chen91220d72021-03-24 02:18:33 -0400462 Common []string
Jingwen Chene32e9e02021-04-23 09:17:24 +0000463
464 ConditionsDefault []string
Jingwen Chen5d864492021-02-24 07:20:12 -0500465}
466
Jingwen Chenc1c26502021-04-05 10:35:13 +0000467type stringListOsValues struct {
468 Android []string
469 Darwin []string
470 Fuchsia []string
471 Linux []string
472 LinuxBionic []string
473 Windows []string
Jingwen Chene32e9e02021-04-23 09:17:24 +0000474
475 ConditionsDefault []string
Jingwen Chenc1c26502021-04-05 10:35:13 +0000476}
477
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400478// Product Variable values for StringListAttribute
479type ProductVariableValues struct {
480 ProductVariable string
481
482 Values []string
483}
484
485// SelectKey returns the appropriate select key for the receiving ProductVariableValues.
486func (p ProductVariableValues) SelectKey() string {
487 return fmt.Sprintf("%s:%s", productVariableBazelPackage, strings.ToLower(p.ProductVariable))
488}
489
Jingwen Chen91220d72021-03-24 02:18:33 -0400490// HasConfigurableValues returns true if the attribute contains
Jingwen Chen5d864492021-02-24 07:20:12 -0500491// architecture-specific string_list values.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000492func (attrs StringListAttribute) HasConfigurableValues() bool {
493 for arch := range PlatformArchMap {
Jingwen Chen5d864492021-02-24 07:20:12 -0500494 if len(attrs.GetValueForArch(arch)) > 0 {
495 return true
496 }
497 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000498
499 for os := range PlatformOsMap {
500 if len(attrs.GetValueForOS(os)) > 0 {
501 return true
502 }
503 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400504
505 return len(attrs.ProductValues) > 0
Jingwen Chen5d864492021-02-24 07:20:12 -0500506}
507
Jingwen Chen91220d72021-03-24 02:18:33 -0400508func (attrs *StringListAttribute) archValuePtrs() map[string]*[]string {
509 return map[string]*[]string{
Jingwen Chene32e9e02021-04-23 09:17:24 +0000510 ARCH_X86: &attrs.ArchValues.X86,
511 ARCH_X86_64: &attrs.ArchValues.X86_64,
512 ARCH_ARM: &attrs.ArchValues.Arm,
513 ARCH_ARM64: &attrs.ArchValues.Arm64,
514 CONDITIONS_DEFAULT: &attrs.ArchValues.ConditionsDefault,
Jingwen Chen91220d72021-03-24 02:18:33 -0400515 }
516}
517
Jingwen Chen5d864492021-02-24 07:20:12 -0500518// GetValueForArch returns the string_list attribute value for an architecture.
519func (attrs *StringListAttribute) GetValueForArch(arch string) []string {
Jingwen Chen91220d72021-03-24 02:18:33 -0400520 var v *[]string
521 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500522 panic(fmt.Errorf("Unknown arch: %s", arch))
523 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400524 return *v
Jingwen Chen5d864492021-02-24 07:20:12 -0500525}
526
527// SetValueForArch sets the string_list attribute value for an architecture.
528func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400529 var v *[]string
530 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500531 panic(fmt.Errorf("Unknown arch: %s", arch))
532 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400533 *v = value
Jingwen Chen5d864492021-02-24 07:20:12 -0500534}
Liz Kammera060c452021-03-24 10:14:47 -0400535
Jingwen Chenc1c26502021-04-05 10:35:13 +0000536func (attrs *StringListAttribute) osValuePtrs() map[string]*[]string {
537 return map[string]*[]string{
Jingwen Chene32e9e02021-04-23 09:17:24 +0000538 OS_ANDROID: &attrs.OsValues.Android,
539 OS_DARWIN: &attrs.OsValues.Darwin,
540 OS_FUCHSIA: &attrs.OsValues.Fuchsia,
541 OS_LINUX: &attrs.OsValues.Linux,
542 OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic,
543 OS_WINDOWS: &attrs.OsValues.Windows,
544 CONDITIONS_DEFAULT: &attrs.OsValues.ConditionsDefault,
Jingwen Chenc1c26502021-04-05 10:35:13 +0000545 }
546}
547
548// GetValueForOS returns the string_list attribute value for an OS target.
549func (attrs *StringListAttribute) GetValueForOS(os string) []string {
550 var v *[]string
551 if v = attrs.osValuePtrs()[os]; v == nil {
552 panic(fmt.Errorf("Unknown os: %s", os))
553 }
554 return *v
555}
556
557// SetValueForArch sets the string_list attribute value for an OS target.
558func (attrs *StringListAttribute) SetValueForOS(os string, value []string) {
559 var v *[]string
560 if v = attrs.osValuePtrs()[os]; v == nil {
561 panic(fmt.Errorf("Unknown os: %s", os))
562 }
563 *v = value
564}
565
Liz Kammere3e4a5f2021-05-10 11:39:53 -0400566func (attrs *StringListAttribute) SortedProductVariables() []ProductVariableValues {
567 vals := attrs.ProductValues[:]
568 sort.Slice(vals, func(i, j int) bool { return vals[i].ProductVariable < vals[j].ProductVariable })
569 return vals
570}
571
Jingwen Chened9c17d2021-04-13 07:14:55 +0000572// Append appends all values, including os and arch specific ones, from another
573// StringListAttribute to this StringListAttribute
574func (attrs *StringListAttribute) Append(other StringListAttribute) {
575 for arch := range PlatformArchMap {
576 this := attrs.GetValueForArch(arch)
577 that := other.GetValueForArch(arch)
578 this = append(this, that...)
579 attrs.SetValueForArch(arch, this)
580 }
581
582 for os := range PlatformOsMap {
583 this := attrs.GetValueForOS(os)
584 that := other.GetValueForOS(os)
585 this = append(this, that...)
586 attrs.SetValueForOS(os, this)
587 }
588
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400589 productValues := make(map[string][]string, 0)
590 for _, pv := range attrs.ProductValues {
591 productValues[pv.ProductVariable] = pv.Values
592 }
593 for _, pv := range other.ProductValues {
594 productValues[pv.ProductVariable] = append(productValues[pv.ProductVariable], pv.Values...)
595 }
596 attrs.ProductValues = make([]ProductVariableValues, 0, len(productValues))
597 for pv, vals := range productValues {
598 attrs.ProductValues = append(attrs.ProductValues, ProductVariableValues{
599 ProductVariable: pv,
600 Values: vals,
601 })
602 }
603
Jingwen Chened9c17d2021-04-13 07:14:55 +0000604 attrs.Value = append(attrs.Value, other.Value...)
605}
606
Liz Kammera060c452021-03-24 10:14:47 -0400607// TryVariableSubstitution, replace string substitution formatting within each string in slice with
608// Starlark string.format compatible tag for productVariable.
609func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
610 ret := make([]string, 0, len(slice))
611 changesMade := false
612 for _, s := range slice {
613 newS, changed := TryVariableSubstitution(s, productVariable)
614 ret = append(ret, newS)
615 changesMade = changesMade || changed
616 }
617 return ret, changesMade
618}
619
620// TryVariableSubstitution, replace string substitution formatting within s with Starlark
621// string.format compatible tag for productVariable.
622func TryVariableSubstitution(s string, productVariable string) (string, bool) {
623 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}")
624 return sub, s != sub
625}