blob: bbaa33e2f82539abf68fa9a89900b79f3ff303cf [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 Kammer57e2e7a2021-09-20 12:55:02 -040022 "strings"
23
24 "github.com/google/blueprint"
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000025)
Jingwen Chen5d864492021-02-24 07:20:12 -050026
Jingwen Chen73850672020-12-14 08:25:34 -050027// BazelTargetModuleProperties contain properties and metadata used for
28// Blueprint to BUILD file conversion.
29type BazelTargetModuleProperties struct {
30 // The Bazel rule class for this target.
Liz Kammerfc46bc12021-02-19 11:06:17 -050031 Rule_class string `blueprint:"mutated"`
Jingwen Chen40067de2021-01-26 21:58:43 -050032
33 // The target label for the bzl file containing the definition of the rule class.
Liz Kammerfc46bc12021-02-19 11:06:17 -050034 Bzl_load_location string `blueprint:"mutated"`
Jingwen Chen73850672020-12-14 08:25:34 -050035}
Liz Kammer356f7d42021-01-26 09:18:53 -050036
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
Chris Parsons51f8c392021-08-03 21:01:05 -040068func (ll *LabelList) Equals(other LabelList) bool {
69 if len(ll.Includes) != len(other.Includes) || len(ll.Excludes) != len(other.Excludes) {
70 return false
71 }
72 for i, _ := range ll.Includes {
73 if ll.Includes[i] != other.Includes[i] {
74 return false
75 }
76 }
77 for i, _ := range ll.Excludes {
78 if ll.Excludes[i] != other.Excludes[i] {
79 return false
80 }
81 }
82 return true
83}
84
Liz Kammer9abd62d2021-05-21 08:37:59 -040085func (ll *LabelList) IsNil() bool {
86 return ll.Includes == nil && ll.Excludes == nil
87}
88
Liz Kammer74deed42021-06-02 13:02:03 -040089func (ll *LabelList) deepCopy() LabelList {
90 return LabelList{
91 Includes: ll.Includes[:],
92 Excludes: ll.Excludes[:],
93 }
94}
95
Jingwen Chen63930982021-03-24 10:04:33 -040096// uniqueParentDirectories returns a list of the unique parent directories for
97// all files in ll.Includes.
98func (ll *LabelList) uniqueParentDirectories() []string {
99 dirMap := map[string]bool{}
100 for _, label := range ll.Includes {
101 dirMap[filepath.Dir(label.Label)] = true
102 }
103 dirs := []string{}
104 for dir := range dirMap {
105 dirs = append(dirs, dir)
106 }
107 return dirs
108}
109
Liz Kammer12615db2021-09-28 09:19:17 -0400110// Add inserts the label Label at the end of the LabelList.
111func (ll *LabelList) Add(label *Label) {
112 if label == nil {
113 return
114 }
115 ll.Includes = append(ll.Includes, *label)
116}
117
Liz Kammer356f7d42021-01-26 09:18:53 -0500118// Append appends the fields of other labelList to the corresponding fields of ll.
119func (ll *LabelList) Append(other LabelList) {
120 if len(ll.Includes) > 0 || len(other.Includes) > 0 {
121 ll.Includes = append(ll.Includes, other.Includes...)
122 }
123 if len(ll.Excludes) > 0 || len(other.Excludes) > 0 {
124 ll.Excludes = append(other.Excludes, other.Excludes...)
125 }
126}
Jingwen Chen5d864492021-02-24 07:20:12 -0500127
Jingwen Chened9c17d2021-04-13 07:14:55 +0000128// UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns
129// the slice in a sorted order.
130func UniqueSortedBazelLabels(originalLabels []Label) []Label {
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000131 uniqueLabelsSet := make(map[Label]bool)
132 for _, l := range originalLabels {
133 uniqueLabelsSet[l] = true
134 }
135 var uniqueLabels []Label
136 for l, _ := range uniqueLabelsSet {
137 uniqueLabels = append(uniqueLabels, l)
138 }
139 sort.SliceStable(uniqueLabels, func(i, j int) bool {
140 return uniqueLabels[i].Label < uniqueLabels[j].Label
141 })
142 return uniqueLabels
143}
144
Liz Kammer9abd62d2021-05-21 08:37:59 -0400145func FirstUniqueBazelLabels(originalLabels []Label) []Label {
146 var labels []Label
147 found := make(map[Label]bool, len(originalLabels))
148 for _, l := range originalLabels {
149 if _, ok := found[l]; ok {
150 continue
151 }
152 labels = append(labels, l)
153 found[l] = true
154 }
155 return labels
156}
157
158func FirstUniqueBazelLabelList(originalLabelList LabelList) LabelList {
159 var uniqueLabelList LabelList
160 uniqueLabelList.Includes = FirstUniqueBazelLabels(originalLabelList.Includes)
161 uniqueLabelList.Excludes = FirstUniqueBazelLabels(originalLabelList.Excludes)
162 return uniqueLabelList
163}
164
165func UniqueSortedBazelLabelList(originalLabelList LabelList) LabelList {
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000166 var uniqueLabelList LabelList
Jingwen Chened9c17d2021-04-13 07:14:55 +0000167 uniqueLabelList.Includes = UniqueSortedBazelLabels(originalLabelList.Includes)
168 uniqueLabelList.Excludes = UniqueSortedBazelLabels(originalLabelList.Excludes)
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000169 return uniqueLabelList
170}
171
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000172// Subtract needle from haystack
173func SubtractStrings(haystack []string, needle []string) []string {
174 // This is really a set
Liz Kammer9bad9d62021-10-11 15:40:35 -0400175 needleMap := make(map[string]bool)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000176 for _, s := range needle {
Liz Kammer9bad9d62021-10-11 15:40:35 -0400177 needleMap[s] = true
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000178 }
179
180 var strings []string
Liz Kammer9bad9d62021-10-11 15:40:35 -0400181 for _, s := range haystack {
182 if exclude := needleMap[s]; !exclude {
183 strings = append(strings, s)
184 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000185 }
186
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000187 return strings
188}
189
190// Subtract needle from haystack
191func SubtractBazelLabels(haystack []Label, needle []Label) []Label {
192 // This is really a set
Liz Kammer9bad9d62021-10-11 15:40:35 -0400193 needleMap := make(map[Label]bool)
194 for _, s := range needle {
195 needleMap[s] = true
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000196 }
197
198 var labels []Label
Liz Kammer9bad9d62021-10-11 15:40:35 -0400199 for _, label := range haystack {
200 if exclude := needleMap[label]; !exclude {
201 labels = append(labels, label)
202 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000203 }
204
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000205 return labels
206}
207
Chris Parsons484e50a2021-05-13 15:13:04 -0400208// Appends two LabelLists, returning the combined list.
209func AppendBazelLabelLists(a LabelList, b LabelList) LabelList {
210 var result LabelList
211 result.Includes = append(a.Includes, b.Includes...)
212 result.Excludes = append(a.Excludes, b.Excludes...)
213 return result
214}
215
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000216// Subtract needle from haystack
217func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList {
218 var result LabelList
219 result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes)
220 // NOTE: Excludes are intentionally not subtracted
221 result.Excludes = haystack.Excludes
222 return result
223}
224
Jingwen Chenc1c26502021-04-05 10:35:13 +0000225type Attribute interface {
226 HasConfigurableValues() bool
227}
228
Liz Kammer9abd62d2021-05-21 08:37:59 -0400229type labelSelectValues map[string]*Label
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400230
Liz Kammer9abd62d2021-05-21 08:37:59 -0400231type configurableLabels map[ConfigurationAxis]labelSelectValues
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400232
Liz Kammer9abd62d2021-05-21 08:37:59 -0400233func (cl configurableLabels) setValueForAxis(axis ConfigurationAxis, config string, value *Label) {
234 if cl[axis] == nil {
235 cl[axis] = make(labelSelectValues)
236 }
237 cl[axis][config] = value
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400238}
239
240// Represents an attribute whose value is a single label
241type LabelAttribute struct {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400242 Value *Label
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400243
Liz Kammer9abd62d2021-05-21 08:37:59 -0400244 ConfigurableValues configurableLabels
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200245}
246
Liz Kammer9abd62d2021-05-21 08:37:59 -0400247// HasConfigurableValues returns whether there are configurable values set for this label.
248func (la LabelAttribute) HasConfigurableValues() bool {
249 return len(la.ConfigurableValues) > 0
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200250}
251
Liz Kammer9abd62d2021-05-21 08:37:59 -0400252// SetValue sets the base, non-configured value for the Label
253func (la *LabelAttribute) SetValue(value Label) {
254 la.SetSelectValue(NoConfigAxis, "", value)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400255}
256
Liz Kammer9abd62d2021-05-21 08:37:59 -0400257// SetSelectValue set a value for a bazel select for the given axis, config and value.
258func (la *LabelAttribute) SetSelectValue(axis ConfigurationAxis, config string, value Label) {
259 axis.validateConfig(config)
260 switch axis.configurationType {
261 case noConfig:
262 la.Value = &value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400263 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400264 if la.ConfigurableValues == nil {
265 la.ConfigurableValues = make(configurableLabels)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400266 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400267 la.ConfigurableValues.setValueForAxis(axis, config, &value)
268 default:
269 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
270 }
271}
272
273// SelectValue gets a value for a bazel select for the given axis and config.
274func (la *LabelAttribute) SelectValue(axis ConfigurationAxis, config string) Label {
275 axis.validateConfig(config)
276 switch axis.configurationType {
277 case noConfig:
278 return *la.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400279 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400280 return *la.ConfigurableValues[axis][config]
281 default:
282 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
283 }
284}
285
286// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
287func (la *LabelAttribute) SortedConfigurationAxes() []ConfigurationAxis {
288 keys := make([]ConfigurationAxis, 0, len(la.ConfigurableValues))
289 for k := range la.ConfigurableValues {
290 keys = append(keys, k)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400291 }
292
Liz Kammer9abd62d2021-05-21 08:37:59 -0400293 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
294 return keys
295}
296
Liz Kammerd366c902021-06-03 13:43:01 -0400297type configToBools map[string]bool
298
299func (ctb configToBools) setValue(config string, value *bool) {
300 if value == nil {
301 if _, ok := ctb[config]; ok {
302 delete(ctb, config)
303 }
304 return
305 }
306 ctb[config] = *value
307}
308
309type configurableBools map[ConfigurationAxis]configToBools
310
311func (cb configurableBools) setValueForAxis(axis ConfigurationAxis, config string, value *bool) {
312 if cb[axis] == nil {
313 cb[axis] = make(configToBools)
314 }
315 cb[axis].setValue(config, value)
316}
317
318// BoolAttribute represents an attribute whose value is a single bool but may be configurable..
319type BoolAttribute struct {
320 Value *bool
321
322 ConfigurableValues configurableBools
323}
324
325// HasConfigurableValues returns whether there are configurable values for this attribute.
326func (ba BoolAttribute) HasConfigurableValues() bool {
327 return len(ba.ConfigurableValues) > 0
328}
329
330// SetSelectValue sets value for the given axis/config.
331func (ba *BoolAttribute) SetSelectValue(axis ConfigurationAxis, config string, value *bool) {
332 axis.validateConfig(config)
333 switch axis.configurationType {
334 case noConfig:
335 ba.Value = value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400336 case arch, os, osArch, productVariables:
Liz Kammerd366c902021-06-03 13:43:01 -0400337 if ba.ConfigurableValues == nil {
338 ba.ConfigurableValues = make(configurableBools)
339 }
340 ba.ConfigurableValues.setValueForAxis(axis, config, value)
341 default:
342 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
343 }
344}
345
346// SelectValue gets the value for the given axis/config.
347func (ba BoolAttribute) SelectValue(axis ConfigurationAxis, config string) *bool {
348 axis.validateConfig(config)
349 switch axis.configurationType {
350 case noConfig:
351 return ba.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400352 case arch, os, osArch, productVariables:
Liz Kammerd366c902021-06-03 13:43:01 -0400353 if v, ok := ba.ConfigurableValues[axis][config]; ok {
354 return &v
355 } else {
356 return nil
357 }
358 default:
359 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
360 }
361}
362
363// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
364func (ba *BoolAttribute) SortedConfigurationAxes() []ConfigurationAxis {
365 keys := make([]ConfigurationAxis, 0, len(ba.ConfigurableValues))
366 for k := range ba.ConfigurableValues {
367 keys = append(keys, k)
368 }
369
370 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
371 return keys
372}
373
Liz Kammer9abd62d2021-05-21 08:37:59 -0400374// labelListSelectValues supports config-specific label_list typed Bazel attribute values.
375type labelListSelectValues map[string]LabelList
376
Liz Kammer12615db2021-09-28 09:19:17 -0400377func (ll labelListSelectValues) addSelects(label labelSelectValues) {
378 for k, v := range label {
379 if label == nil {
380 continue
381 }
382 l := ll[k]
383 (&l).Add(v)
384 ll[k] = l
385 }
386}
387
Liz Kammer9abd62d2021-05-21 08:37:59 -0400388func (ll labelListSelectValues) appendSelects(other labelListSelectValues) {
389 for k, v := range other {
390 l := ll[k]
391 (&l).Append(v)
392 ll[k] = l
393 }
394}
395
396// HasConfigurableValues returns whether there are configurable values within this set of selects.
397func (ll labelListSelectValues) HasConfigurableValues() bool {
398 for _, v := range ll {
Chris Parsons51f8c392021-08-03 21:01:05 -0400399 if v.Includes != nil {
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400400 return true
401 }
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400402 }
403 return false
404}
405
Jingwen Chen07027912021-03-15 06:02:43 -0400406// LabelListAttribute is used to represent a list of Bazel labels as an
407// attribute.
408type LabelListAttribute struct {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400409 // The non-configured attribute label list Value. Required.
Jingwen Chen07027912021-03-15 06:02:43 -0400410 Value LabelList
411
Liz Kammer9abd62d2021-05-21 08:37:59 -0400412 // The configured attribute label list Values. Optional
413 // a map of independent configurability axes
414 ConfigurableValues configurableLabelLists
Chris Parsons51f8c392021-08-03 21:01:05 -0400415
416 // If true, differentiate between "nil" and "empty" list. nil means that
417 // this attribute should not be specified at all, and "empty" means that
418 // the attribute should be explicitly specified as an empty list.
419 // This mode facilitates use of attribute defaults: an empty list should
420 // override the default.
421 ForceSpecifyEmptyList bool
Jingwen Chen58ff6802021-11-17 12:14:41 +0000422
423 // If true, signal the intent to the code generator to emit all select keys,
424 // even if the Includes list for that key is empty. This mode facilitates
425 // specific select statements where an empty list for a non-default select
426 // key has a meaning.
427 EmitEmptyList bool
Liz Kammer9abd62d2021-05-21 08:37:59 -0400428}
Jingwen Chen91220d72021-03-24 02:18:33 -0400429
Liz Kammer9abd62d2021-05-21 08:37:59 -0400430type configurableLabelLists map[ConfigurationAxis]labelListSelectValues
431
432func (cll configurableLabelLists) setValueForAxis(axis ConfigurationAxis, config string, list LabelList) {
433 if list.IsNil() {
434 if _, ok := cll[axis][config]; ok {
435 delete(cll[axis], config)
436 }
437 return
438 }
439 if cll[axis] == nil {
440 cll[axis] = make(labelListSelectValues)
441 }
442
443 cll[axis][config] = list
444}
445
446func (cll configurableLabelLists) Append(other configurableLabelLists) {
447 for axis, otherSelects := range other {
448 selects := cll[axis]
449 if selects == nil {
450 selects = make(labelListSelectValues, len(otherSelects))
451 }
452 selects.appendSelects(otherSelects)
453 cll[axis] = selects
454 }
Jingwen Chen07027912021-03-15 06:02:43 -0400455}
456
457// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
458func MakeLabelListAttribute(value LabelList) LabelListAttribute {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400459 return LabelListAttribute{
460 Value: value,
461 ConfigurableValues: make(configurableLabelLists),
462 }
463}
464
465func (lla *LabelListAttribute) SetValue(list LabelList) {
466 lla.SetSelectValue(NoConfigAxis, "", list)
467}
468
469// SetSelectValue set a value for a bazel select for the given axis, config and value.
470func (lla *LabelListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list LabelList) {
471 axis.validateConfig(config)
472 switch axis.configurationType {
473 case noConfig:
474 lla.Value = list
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400475 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400476 if lla.ConfigurableValues == nil {
477 lla.ConfigurableValues = make(configurableLabelLists)
478 }
479 lla.ConfigurableValues.setValueForAxis(axis, config, list)
480 default:
481 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
482 }
483}
484
485// SelectValue gets a value for a bazel select for the given axis and config.
486func (lla *LabelListAttribute) SelectValue(axis ConfigurationAxis, config string) LabelList {
487 axis.validateConfig(config)
488 switch axis.configurationType {
489 case noConfig:
490 return lla.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400491 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400492 return lla.ConfigurableValues[axis][config]
493 default:
494 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
495 }
496}
497
498// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
499func (lla *LabelListAttribute) SortedConfigurationAxes() []ConfigurationAxis {
500 keys := make([]ConfigurationAxis, 0, len(lla.ConfigurableValues))
501 for k := range lla.ConfigurableValues {
502 keys = append(keys, k)
503 }
504
505 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
506 return keys
Jingwen Chen07027912021-03-15 06:02:43 -0400507}
508
Jingwen Chened9c17d2021-04-13 07:14:55 +0000509// Append all values, including os and arch specific ones, from another
Jingwen Chen63930982021-03-24 10:04:33 -0400510// LabelListAttribute to this LabelListAttribute.
Liz Kammer9abd62d2021-05-21 08:37:59 -0400511func (lla *LabelListAttribute) Append(other LabelListAttribute) {
Chris Parsons51f8c392021-08-03 21:01:05 -0400512 if lla.ForceSpecifyEmptyList && !other.Value.IsNil() {
513 lla.Value.Includes = []Label{}
514 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400515 lla.Value.Append(other.Value)
516 if lla.ConfigurableValues == nil {
517 lla.ConfigurableValues = make(configurableLabelLists)
Jingwen Chen63930982021-03-24 10:04:33 -0400518 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400519 lla.ConfigurableValues.Append(other.ConfigurableValues)
Jingwen Chen63930982021-03-24 10:04:33 -0400520}
521
Liz Kammer12615db2021-09-28 09:19:17 -0400522// Add inserts the labels for each axis of LabelAttribute at the end of corresponding axis's
523// LabelList within the LabelListAttribute
524func (lla *LabelListAttribute) Add(label *LabelAttribute) {
525 if label == nil {
526 return
527 }
528
529 lla.Value.Add(label.Value)
530 if lla.ConfigurableValues == nil && label.ConfigurableValues != nil {
531 lla.ConfigurableValues = make(configurableLabelLists)
532 }
533 for axis, _ := range label.ConfigurableValues {
534 if _, exists := lla.ConfigurableValues[axis]; !exists {
535 lla.ConfigurableValues[axis] = make(labelListSelectValues)
536 }
537 lla.ConfigurableValues[axis].addSelects(label.ConfigurableValues[axis])
538 }
539}
540
Liz Kammer9abd62d2021-05-21 08:37:59 -0400541// HasConfigurableValues returns true if the attribute contains axis-specific label list values.
542func (lla LabelListAttribute) HasConfigurableValues() bool {
543 return len(lla.ConfigurableValues) > 0
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400544}
545
Chris Parsons69fa9f92021-07-13 11:47:44 -0400546// IsEmpty returns true if the attribute has no values under any configuration.
547func (lla LabelListAttribute) IsEmpty() bool {
548 if len(lla.Value.Includes) > 0 {
549 return false
550 }
551 for axis, _ := range lla.ConfigurableValues {
552 if lla.ConfigurableValues[axis].HasConfigurableValues() {
553 return false
554 }
555 }
556 return true
557}
558
Liz Kammer74deed42021-06-02 13:02:03 -0400559// ResolveExcludes handles excludes across the various axes, ensuring that items are removed from
560// the base value and included in default values as appropriate.
561func (lla *LabelListAttribute) ResolveExcludes() {
562 for axis, configToLabels := range lla.ConfigurableValues {
563 baseLabels := lla.Value.deepCopy()
564 for config, val := range configToLabels {
565 // Exclude config-specific excludes from base value
566 lla.Value = SubtractBazelLabelList(lla.Value, LabelList{Includes: val.Excludes})
567
568 // add base values to config specific to add labels excluded by others in this axis
569 // then remove all config-specific excludes
570 allLabels := baseLabels.deepCopy()
571 allLabels.Append(val)
572 lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(allLabels, LabelList{Includes: val.Excludes})
573 }
574
575 // After going through all configs, delete the duplicates in the config
576 // values that are already in the base Value.
577 for config, val := range configToLabels {
578 lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(val, lla.Value)
579 }
580
Jingwen Chen9af49a42021-11-02 10:27:17 +0000581 // Now that the Value list is finalized for this axis, compare it with
582 // the original list, and union the difference with the default
583 // condition for the axis.
584 difference := SubtractBazelLabelList(baseLabels, lla.Value)
585 existingDefaults := lla.ConfigurableValues[axis][ConditionsDefaultConfigKey]
586 existingDefaults.Append(difference)
587 lla.ConfigurableValues[axis][ConditionsDefaultConfigKey] = FirstUniqueBazelLabelList(existingDefaults)
Liz Kammer74deed42021-06-02 13:02:03 -0400588
589 // if everything ends up without includes, just delete the axis
590 if !lla.ConfigurableValues[axis].HasConfigurableValues() {
591 delete(lla.ConfigurableValues, axis)
592 }
593 }
594}
595
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400596// OtherModuleContext is a limited context that has methods with information about other modules.
597type OtherModuleContext interface {
598 ModuleFromName(name string) (blueprint.Module, bool)
599 OtherModuleType(m blueprint.Module) string
600 OtherModuleName(m blueprint.Module) string
601 OtherModuleDir(m blueprint.Module) string
602 ModuleErrorf(fmt string, args ...interface{})
603}
604
605// LabelMapper is a function that takes a OtherModuleContext and returns a (potentially changed)
606// label and whether it was changed.
Liz Kammer12615db2021-09-28 09:19:17 -0400607type LabelMapper func(OtherModuleContext, Label) (string, bool)
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400608
609// LabelPartition contains descriptions of a partition for labels
610type LabelPartition struct {
611 // Extensions to include in this partition
612 Extensions []string
613 // LabelMapper is a function that can map a label to a new label, and indicate whether to include
614 // the mapped label in the partition
615 LabelMapper LabelMapper
616 // Whether to store files not included in any other partition in a group of LabelPartitions
617 // Only one partition in a group of LabelPartitions can enabled Keep_remainder
618 Keep_remainder bool
619}
620
621// LabelPartitions is a map of partition name to a LabelPartition describing the elements of the
622// partition
623type LabelPartitions map[string]LabelPartition
624
625// filter returns a pointer to a label if the label should be included in the partition or nil if
626// not.
627func (lf LabelPartition) filter(ctx OtherModuleContext, label Label) *Label {
628 if lf.LabelMapper != nil {
Liz Kammer12615db2021-09-28 09:19:17 -0400629 if newLabel, changed := lf.LabelMapper(ctx, label); changed {
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400630 return &Label{newLabel, label.OriginalModuleName}
631 }
632 }
633 for _, ext := range lf.Extensions {
634 if strings.HasSuffix(label.Label, ext) {
635 return &label
636 }
637 }
638
639 return nil
640}
641
642// PartitionToLabelListAttribute is map of partition name to a LabelListAttribute
643type PartitionToLabelListAttribute map[string]LabelListAttribute
644
645type partitionToLabelList map[string]*LabelList
646
647func (p partitionToLabelList) appendIncludes(partition string, label Label) {
648 if _, ok := p[partition]; !ok {
649 p[partition] = &LabelList{}
650 }
651 p[partition].Includes = append(p[partition].Includes, label)
652}
653
654func (p partitionToLabelList) excludes(partition string, excludes []Label) {
655 if _, ok := p[partition]; !ok {
656 p[partition] = &LabelList{}
657 }
658 p[partition].Excludes = excludes
659}
660
661// PartitionLabelListAttribute partitions a LabelListAttribute into the requested partitions
662func PartitionLabelListAttribute(ctx OtherModuleContext, lla *LabelListAttribute, partitions LabelPartitions) PartitionToLabelListAttribute {
663 ret := PartitionToLabelListAttribute{}
664 var partitionNames []string
665 // Stored as a pointer to distinguish nil (no remainder partition) from empty string partition
666 var remainderPartition *string
667 for p, f := range partitions {
668 partitionNames = append(partitionNames, p)
669 if f.Keep_remainder {
670 if remainderPartition != nil {
671 panic("only one partition can store the remainder")
672 }
673 // If we take the address of p in a loop, we'll end up with the last value of p in
674 // remainderPartition, we want the requested partition
675 capturePartition := p
676 remainderPartition = &capturePartition
677 }
678 }
679
680 partitionLabelList := func(axis ConfigurationAxis, config string) {
681 value := lla.SelectValue(axis, config)
682 partitionToLabels := partitionToLabelList{}
683 for _, item := range value.Includes {
684 wasFiltered := false
685 var inPartition *string
686 for partition, f := range partitions {
687 filtered := f.filter(ctx, item)
688 if filtered == nil {
689 // did not match this filter, keep looking
690 continue
691 }
692 wasFiltered = true
693 partitionToLabels.appendIncludes(partition, *filtered)
694 // don't need to check other partitions if this filter used the item,
695 // continue checking if mapped to another name
696 if *filtered == item {
697 if inPartition != nil {
698 ctx.ModuleErrorf("%q was found in multiple partitions: %q, %q", item.Label, *inPartition, partition)
699 }
700 capturePartition := partition
701 inPartition = &capturePartition
702 }
703 }
704
705 // if not specified in a partition, add to remainder partition if one exists
706 if !wasFiltered && remainderPartition != nil {
707 partitionToLabels.appendIncludes(*remainderPartition, item)
708 }
709 }
710
711 // ensure empty lists are maintained
712 if value.Excludes != nil {
713 for _, partition := range partitionNames {
714 partitionToLabels.excludes(partition, value.Excludes)
715 }
716 }
717
718 for partition, list := range partitionToLabels {
719 val := ret[partition]
720 (&val).SetSelectValue(axis, config, *list)
721 ret[partition] = val
722 }
723 }
724
725 partitionLabelList(NoConfigAxis, "")
726 for axis, configToList := range lla.ConfigurableValues {
727 for config, _ := range configToList {
728 partitionLabelList(axis, config)
729 }
730 }
731 return ret
732}
733
Jingwen Chen5d864492021-02-24 07:20:12 -0500734// StringListAttribute corresponds to the string_list Bazel attribute type with
735// support for additional metadata, like configurations.
736type StringListAttribute struct {
737 // The base value of the string list attribute.
738 Value []string
739
Liz Kammer9abd62d2021-05-21 08:37:59 -0400740 // The configured attribute label list Values. Optional
741 // a map of independent configurability axes
742 ConfigurableValues configurableStringLists
743}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000744
Liz Kammer9abd62d2021-05-21 08:37:59 -0400745type configurableStringLists map[ConfigurationAxis]stringListSelectValues
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400746
Liz Kammer9abd62d2021-05-21 08:37:59 -0400747func (csl configurableStringLists) Append(other configurableStringLists) {
748 for axis, otherSelects := range other {
749 selects := csl[axis]
750 if selects == nil {
751 selects = make(stringListSelectValues, len(otherSelects))
752 }
753 selects.appendSelects(otherSelects)
754 csl[axis] = selects
755 }
756}
757
758func (csl configurableStringLists) setValueForAxis(axis ConfigurationAxis, config string, list []string) {
759 if csl[axis] == nil {
760 csl[axis] = make(stringListSelectValues)
761 }
762 csl[axis][config] = list
763}
764
765type stringListSelectValues map[string][]string
766
767func (sl stringListSelectValues) appendSelects(other stringListSelectValues) {
768 for k, v := range other {
769 sl[k] = append(sl[k], v...)
770 }
771}
772
773func (sl stringListSelectValues) hasConfigurableValues(other stringListSelectValues) bool {
774 for _, val := range sl {
775 if len(val) > 0 {
776 return true
777 }
778 }
779 return false
Jingwen Chen5d864492021-02-24 07:20:12 -0500780}
781
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000782// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value.
783func MakeStringListAttribute(value []string) StringListAttribute {
784 // NOTE: These strings are not necessarily unique or sorted.
Liz Kammer9abd62d2021-05-21 08:37:59 -0400785 return StringListAttribute{
786 Value: value,
787 ConfigurableValues: make(configurableStringLists),
Jingwen Chen91220d72021-03-24 02:18:33 -0400788 }
789}
790
Liz Kammer9abd62d2021-05-21 08:37:59 -0400791// HasConfigurableValues returns true if the attribute contains axis-specific string_list values.
792func (sla StringListAttribute) HasConfigurableValues() bool {
793 return len(sla.ConfigurableValues) > 0
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400794}
795
Jingwen Chened9c17d2021-04-13 07:14:55 +0000796// Append appends all values, including os and arch specific ones, from another
797// StringListAttribute to this StringListAttribute
Liz Kammer9abd62d2021-05-21 08:37:59 -0400798func (sla *StringListAttribute) Append(other StringListAttribute) {
799 sla.Value = append(sla.Value, other.Value...)
800 if sla.ConfigurableValues == nil {
801 sla.ConfigurableValues = make(configurableStringLists)
802 }
803 sla.ConfigurableValues.Append(other.ConfigurableValues)
804}
805
806// SetSelectValue set a value for a bazel select for the given axis, config and value.
807func (sla *StringListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list []string) {
808 axis.validateConfig(config)
809 switch axis.configurationType {
810 case noConfig:
811 sla.Value = list
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400812 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400813 if sla.ConfigurableValues == nil {
814 sla.ConfigurableValues = make(configurableStringLists)
815 }
816 sla.ConfigurableValues.setValueForAxis(axis, config, list)
817 default:
818 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
819 }
820}
821
822// SelectValue gets a value for a bazel select for the given axis and config.
823func (sla *StringListAttribute) SelectValue(axis ConfigurationAxis, config string) []string {
824 axis.validateConfig(config)
825 switch axis.configurationType {
826 case noConfig:
827 return sla.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400828 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400829 return sla.ConfigurableValues[axis][config]
830 default:
831 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
832 }
833}
834
835// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
836func (sla *StringListAttribute) SortedConfigurationAxes() []ConfigurationAxis {
837 keys := make([]ConfigurationAxis, 0, len(sla.ConfigurableValues))
838 for k := range sla.ConfigurableValues {
839 keys = append(keys, k)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000840 }
841
Liz Kammer9abd62d2021-05-21 08:37:59 -0400842 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
843 return keys
Jingwen Chened9c17d2021-04-13 07:14:55 +0000844}
845
Liz Kammer5fad5012021-09-09 14:08:21 -0400846// DeduplicateAxesFromBase ensures no duplication of items between the no-configuration value and
847// configuration-specific values. For example, if we would convert this StringListAttribute as:
848// ["a", "b", "c"] + select({
849// "//condition:one": ["a", "d"],
850// "//conditions:default": [],
851// })
852// after this function, we would convert this StringListAttribute as:
853// ["a", "b", "c"] + select({
854// "//condition:one": ["d"],
855// "//conditions:default": [],
856// })
857func (sla *StringListAttribute) DeduplicateAxesFromBase() {
858 base := sla.Value
859 for axis, configToList := range sla.ConfigurableValues {
860 for config, list := range configToList {
861 remaining := SubtractStrings(list, base)
862 if len(remaining) == 0 {
863 delete(sla.ConfigurableValues[axis], config)
864 } else {
865 sla.ConfigurableValues[axis][config] = remaining
866 }
867 }
868 }
869}
870
Liz Kammera060c452021-03-24 10:14:47 -0400871// TryVariableSubstitution, replace string substitution formatting within each string in slice with
872// Starlark string.format compatible tag for productVariable.
873func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
874 ret := make([]string, 0, len(slice))
875 changesMade := false
876 for _, s := range slice {
877 newS, changed := TryVariableSubstitution(s, productVariable)
878 ret = append(ret, newS)
879 changesMade = changesMade || changed
880 }
881 return ret, changesMade
882}
883
884// TryVariableSubstitution, replace string substitution formatting within s with Starlark
885// string.format compatible tag for productVariable.
886func TryVariableSubstitution(s string, productVariable string) (string, bool) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400887 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "$("+productVariable+")")
Liz Kammera060c452021-03-24 10:14:47 -0400888 return sub, s != sub
889}