blob: facbedd9a4fa0f8a19f052e8b1ae9403c6f6de22 [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 Kammer356f7d42021-01-26 09:18:53 -0500110// Append appends the fields of other labelList to the corresponding fields of ll.
111func (ll *LabelList) Append(other LabelList) {
112 if len(ll.Includes) > 0 || len(other.Includes) > 0 {
113 ll.Includes = append(ll.Includes, other.Includes...)
114 }
115 if len(ll.Excludes) > 0 || len(other.Excludes) > 0 {
116 ll.Excludes = append(other.Excludes, other.Excludes...)
117 }
118}
Jingwen Chen5d864492021-02-24 07:20:12 -0500119
Jingwen Chened9c17d2021-04-13 07:14:55 +0000120// UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns
121// the slice in a sorted order.
122func UniqueSortedBazelLabels(originalLabels []Label) []Label {
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000123 uniqueLabelsSet := make(map[Label]bool)
124 for _, l := range originalLabels {
125 uniqueLabelsSet[l] = true
126 }
127 var uniqueLabels []Label
128 for l, _ := range uniqueLabelsSet {
129 uniqueLabels = append(uniqueLabels, l)
130 }
131 sort.SliceStable(uniqueLabels, func(i, j int) bool {
132 return uniqueLabels[i].Label < uniqueLabels[j].Label
133 })
134 return uniqueLabels
135}
136
Liz Kammer9abd62d2021-05-21 08:37:59 -0400137func FirstUniqueBazelLabels(originalLabels []Label) []Label {
138 var labels []Label
139 found := make(map[Label]bool, len(originalLabels))
140 for _, l := range originalLabels {
141 if _, ok := found[l]; ok {
142 continue
143 }
144 labels = append(labels, l)
145 found[l] = true
146 }
147 return labels
148}
149
150func FirstUniqueBazelLabelList(originalLabelList LabelList) LabelList {
151 var uniqueLabelList LabelList
152 uniqueLabelList.Includes = FirstUniqueBazelLabels(originalLabelList.Includes)
153 uniqueLabelList.Excludes = FirstUniqueBazelLabels(originalLabelList.Excludes)
154 return uniqueLabelList
155}
156
157func UniqueSortedBazelLabelList(originalLabelList LabelList) LabelList {
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000158 var uniqueLabelList LabelList
Jingwen Chened9c17d2021-04-13 07:14:55 +0000159 uniqueLabelList.Includes = UniqueSortedBazelLabels(originalLabelList.Includes)
160 uniqueLabelList.Excludes = UniqueSortedBazelLabels(originalLabelList.Excludes)
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000161 return uniqueLabelList
162}
163
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000164// Subtract needle from haystack
165func SubtractStrings(haystack []string, needle []string) []string {
166 // This is really a set
Liz Kammer9bad9d62021-10-11 15:40:35 -0400167 needleMap := make(map[string]bool)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000168 for _, s := range needle {
Liz Kammer9bad9d62021-10-11 15:40:35 -0400169 needleMap[s] = true
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000170 }
171
172 var strings []string
Liz Kammer9bad9d62021-10-11 15:40:35 -0400173 for _, s := range haystack {
174 if exclude := needleMap[s]; !exclude {
175 strings = append(strings, s)
176 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000177 }
178
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000179 return strings
180}
181
182// Subtract needle from haystack
183func SubtractBazelLabels(haystack []Label, needle []Label) []Label {
184 // This is really a set
Liz Kammer9bad9d62021-10-11 15:40:35 -0400185 needleMap := make(map[Label]bool)
186 for _, s := range needle {
187 needleMap[s] = true
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000188 }
189
190 var labels []Label
Liz Kammer9bad9d62021-10-11 15:40:35 -0400191 for _, label := range haystack {
192 if exclude := needleMap[label]; !exclude {
193 labels = append(labels, label)
194 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000195 }
196
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000197 return labels
198}
199
Chris Parsons484e50a2021-05-13 15:13:04 -0400200// Appends two LabelLists, returning the combined list.
201func AppendBazelLabelLists(a LabelList, b LabelList) LabelList {
202 var result LabelList
203 result.Includes = append(a.Includes, b.Includes...)
204 result.Excludes = append(a.Excludes, b.Excludes...)
205 return result
206}
207
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000208// Subtract needle from haystack
209func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList {
210 var result LabelList
211 result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes)
212 // NOTE: Excludes are intentionally not subtracted
213 result.Excludes = haystack.Excludes
214 return result
215}
216
Jingwen Chenc1c26502021-04-05 10:35:13 +0000217type Attribute interface {
218 HasConfigurableValues() bool
219}
220
Liz Kammer9abd62d2021-05-21 08:37:59 -0400221type labelSelectValues map[string]*Label
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400222
Liz Kammer9abd62d2021-05-21 08:37:59 -0400223type configurableLabels map[ConfigurationAxis]labelSelectValues
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400224
Liz Kammer9abd62d2021-05-21 08:37:59 -0400225func (cl configurableLabels) setValueForAxis(axis ConfigurationAxis, config string, value *Label) {
226 if cl[axis] == nil {
227 cl[axis] = make(labelSelectValues)
228 }
229 cl[axis][config] = value
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400230}
231
232// Represents an attribute whose value is a single label
233type LabelAttribute struct {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400234 Value *Label
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400235
Liz Kammer9abd62d2021-05-21 08:37:59 -0400236 ConfigurableValues configurableLabels
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200237}
238
Liz Kammer9abd62d2021-05-21 08:37:59 -0400239// HasConfigurableValues returns whether there are configurable values set for this label.
240func (la LabelAttribute) HasConfigurableValues() bool {
241 return len(la.ConfigurableValues) > 0
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200242}
243
Liz Kammer9abd62d2021-05-21 08:37:59 -0400244// SetValue sets the base, non-configured value for the Label
245func (la *LabelAttribute) SetValue(value Label) {
246 la.SetSelectValue(NoConfigAxis, "", value)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400247}
248
Liz Kammer9abd62d2021-05-21 08:37:59 -0400249// SetSelectValue set a value for a bazel select for the given axis, config and value.
250func (la *LabelAttribute) SetSelectValue(axis ConfigurationAxis, config string, value Label) {
251 axis.validateConfig(config)
252 switch axis.configurationType {
253 case noConfig:
254 la.Value = &value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400255 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400256 if la.ConfigurableValues == nil {
257 la.ConfigurableValues = make(configurableLabels)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400258 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400259 la.ConfigurableValues.setValueForAxis(axis, config, &value)
260 default:
261 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
262 }
263}
264
265// SelectValue gets a value for a bazel select for the given axis and config.
266func (la *LabelAttribute) SelectValue(axis ConfigurationAxis, config string) Label {
267 axis.validateConfig(config)
268 switch axis.configurationType {
269 case noConfig:
270 return *la.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400271 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400272 return *la.ConfigurableValues[axis][config]
273 default:
274 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
275 }
276}
277
278// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
279func (la *LabelAttribute) SortedConfigurationAxes() []ConfigurationAxis {
280 keys := make([]ConfigurationAxis, 0, len(la.ConfigurableValues))
281 for k := range la.ConfigurableValues {
282 keys = append(keys, k)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400283 }
284
Liz Kammer9abd62d2021-05-21 08:37:59 -0400285 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
286 return keys
287}
288
Liz Kammerd366c902021-06-03 13:43:01 -0400289type configToBools map[string]bool
290
291func (ctb configToBools) setValue(config string, value *bool) {
292 if value == nil {
293 if _, ok := ctb[config]; ok {
294 delete(ctb, config)
295 }
296 return
297 }
298 ctb[config] = *value
299}
300
301type configurableBools map[ConfigurationAxis]configToBools
302
303func (cb configurableBools) setValueForAxis(axis ConfigurationAxis, config string, value *bool) {
304 if cb[axis] == nil {
305 cb[axis] = make(configToBools)
306 }
307 cb[axis].setValue(config, value)
308}
309
310// BoolAttribute represents an attribute whose value is a single bool but may be configurable..
311type BoolAttribute struct {
312 Value *bool
313
314 ConfigurableValues configurableBools
315}
316
317// HasConfigurableValues returns whether there are configurable values for this attribute.
318func (ba BoolAttribute) HasConfigurableValues() bool {
319 return len(ba.ConfigurableValues) > 0
320}
321
322// SetSelectValue sets value for the given axis/config.
323func (ba *BoolAttribute) SetSelectValue(axis ConfigurationAxis, config string, value *bool) {
324 axis.validateConfig(config)
325 switch axis.configurationType {
326 case noConfig:
327 ba.Value = value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400328 case arch, os, osArch, productVariables:
Liz Kammerd366c902021-06-03 13:43:01 -0400329 if ba.ConfigurableValues == nil {
330 ba.ConfigurableValues = make(configurableBools)
331 }
332 ba.ConfigurableValues.setValueForAxis(axis, config, value)
333 default:
334 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
335 }
336}
337
338// SelectValue gets the value for the given axis/config.
339func (ba BoolAttribute) SelectValue(axis ConfigurationAxis, config string) *bool {
340 axis.validateConfig(config)
341 switch axis.configurationType {
342 case noConfig:
343 return ba.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400344 case arch, os, osArch, productVariables:
Liz Kammerd366c902021-06-03 13:43:01 -0400345 if v, ok := ba.ConfigurableValues[axis][config]; ok {
346 return &v
347 } else {
348 return nil
349 }
350 default:
351 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
352 }
353}
354
355// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
356func (ba *BoolAttribute) SortedConfigurationAxes() []ConfigurationAxis {
357 keys := make([]ConfigurationAxis, 0, len(ba.ConfigurableValues))
358 for k := range ba.ConfigurableValues {
359 keys = append(keys, k)
360 }
361
362 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
363 return keys
364}
365
Liz Kammer9abd62d2021-05-21 08:37:59 -0400366// labelListSelectValues supports config-specific label_list typed Bazel attribute values.
367type labelListSelectValues map[string]LabelList
368
369func (ll labelListSelectValues) appendSelects(other labelListSelectValues) {
370 for k, v := range other {
371 l := ll[k]
372 (&l).Append(v)
373 ll[k] = l
374 }
375}
376
377// HasConfigurableValues returns whether there are configurable values within this set of selects.
378func (ll labelListSelectValues) HasConfigurableValues() bool {
379 for _, v := range ll {
Chris Parsons51f8c392021-08-03 21:01:05 -0400380 if v.Includes != nil {
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400381 return true
382 }
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400383 }
384 return false
385}
386
Jingwen Chen07027912021-03-15 06:02:43 -0400387// LabelListAttribute is used to represent a list of Bazel labels as an
388// attribute.
389type LabelListAttribute struct {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400390 // The non-configured attribute label list Value. Required.
Jingwen Chen07027912021-03-15 06:02:43 -0400391 Value LabelList
392
Liz Kammer9abd62d2021-05-21 08:37:59 -0400393 // The configured attribute label list Values. Optional
394 // a map of independent configurability axes
395 ConfigurableValues configurableLabelLists
Chris Parsons51f8c392021-08-03 21:01:05 -0400396
397 // If true, differentiate between "nil" and "empty" list. nil means that
398 // this attribute should not be specified at all, and "empty" means that
399 // the attribute should be explicitly specified as an empty list.
400 // This mode facilitates use of attribute defaults: an empty list should
401 // override the default.
402 ForceSpecifyEmptyList bool
Liz Kammer9abd62d2021-05-21 08:37:59 -0400403}
Jingwen Chen91220d72021-03-24 02:18:33 -0400404
Liz Kammer9abd62d2021-05-21 08:37:59 -0400405type configurableLabelLists map[ConfigurationAxis]labelListSelectValues
406
407func (cll configurableLabelLists) setValueForAxis(axis ConfigurationAxis, config string, list LabelList) {
408 if list.IsNil() {
409 if _, ok := cll[axis][config]; ok {
410 delete(cll[axis], config)
411 }
412 return
413 }
414 if cll[axis] == nil {
415 cll[axis] = make(labelListSelectValues)
416 }
417
418 cll[axis][config] = list
419}
420
421func (cll configurableLabelLists) Append(other configurableLabelLists) {
422 for axis, otherSelects := range other {
423 selects := cll[axis]
424 if selects == nil {
425 selects = make(labelListSelectValues, len(otherSelects))
426 }
427 selects.appendSelects(otherSelects)
428 cll[axis] = selects
429 }
Jingwen Chen07027912021-03-15 06:02:43 -0400430}
431
432// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
433func MakeLabelListAttribute(value LabelList) LabelListAttribute {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400434 return LabelListAttribute{
435 Value: value,
436 ConfigurableValues: make(configurableLabelLists),
437 }
438}
439
440func (lla *LabelListAttribute) SetValue(list LabelList) {
441 lla.SetSelectValue(NoConfigAxis, "", list)
442}
443
444// SetSelectValue set a value for a bazel select for the given axis, config and value.
445func (lla *LabelListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list LabelList) {
446 axis.validateConfig(config)
447 switch axis.configurationType {
448 case noConfig:
449 lla.Value = list
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400450 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400451 if lla.ConfigurableValues == nil {
452 lla.ConfigurableValues = make(configurableLabelLists)
453 }
454 lla.ConfigurableValues.setValueForAxis(axis, config, list)
455 default:
456 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
457 }
458}
459
460// SelectValue gets a value for a bazel select for the given axis and config.
461func (lla *LabelListAttribute) SelectValue(axis ConfigurationAxis, config string) LabelList {
462 axis.validateConfig(config)
463 switch axis.configurationType {
464 case noConfig:
465 return lla.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400466 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400467 return lla.ConfigurableValues[axis][config]
468 default:
469 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
470 }
471}
472
473// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
474func (lla *LabelListAttribute) SortedConfigurationAxes() []ConfigurationAxis {
475 keys := make([]ConfigurationAxis, 0, len(lla.ConfigurableValues))
476 for k := range lla.ConfigurableValues {
477 keys = append(keys, k)
478 }
479
480 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
481 return keys
Jingwen Chen07027912021-03-15 06:02:43 -0400482}
483
Jingwen Chened9c17d2021-04-13 07:14:55 +0000484// Append all values, including os and arch specific ones, from another
Jingwen Chen63930982021-03-24 10:04:33 -0400485// LabelListAttribute to this LabelListAttribute.
Liz Kammer9abd62d2021-05-21 08:37:59 -0400486func (lla *LabelListAttribute) Append(other LabelListAttribute) {
Chris Parsons51f8c392021-08-03 21:01:05 -0400487 if lla.ForceSpecifyEmptyList && !other.Value.IsNil() {
488 lla.Value.Includes = []Label{}
489 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400490 lla.Value.Append(other.Value)
491 if lla.ConfigurableValues == nil {
492 lla.ConfigurableValues = make(configurableLabelLists)
Jingwen Chen63930982021-03-24 10:04:33 -0400493 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400494 lla.ConfigurableValues.Append(other.ConfigurableValues)
Jingwen Chen63930982021-03-24 10:04:33 -0400495}
496
Liz Kammer9abd62d2021-05-21 08:37:59 -0400497// HasConfigurableValues returns true if the attribute contains axis-specific label list values.
498func (lla LabelListAttribute) HasConfigurableValues() bool {
499 return len(lla.ConfigurableValues) > 0
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400500}
501
Chris Parsons69fa9f92021-07-13 11:47:44 -0400502// IsEmpty returns true if the attribute has no values under any configuration.
503func (lla LabelListAttribute) IsEmpty() bool {
504 if len(lla.Value.Includes) > 0 {
505 return false
506 }
507 for axis, _ := range lla.ConfigurableValues {
508 if lla.ConfigurableValues[axis].HasConfigurableValues() {
509 return false
510 }
511 }
512 return true
513}
514
Liz Kammer74deed42021-06-02 13:02:03 -0400515// ResolveExcludes handles excludes across the various axes, ensuring that items are removed from
516// the base value and included in default values as appropriate.
517func (lla *LabelListAttribute) ResolveExcludes() {
518 for axis, configToLabels := range lla.ConfigurableValues {
519 baseLabels := lla.Value.deepCopy()
520 for config, val := range configToLabels {
521 // Exclude config-specific excludes from base value
522 lla.Value = SubtractBazelLabelList(lla.Value, LabelList{Includes: val.Excludes})
523
524 // add base values to config specific to add labels excluded by others in this axis
525 // then remove all config-specific excludes
526 allLabels := baseLabels.deepCopy()
527 allLabels.Append(val)
528 lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(allLabels, LabelList{Includes: val.Excludes})
529 }
530
531 // After going through all configs, delete the duplicates in the config
532 // values that are already in the base Value.
533 for config, val := range configToLabels {
534 lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(val, lla.Value)
535 }
536
Jingwen Chen9af49a42021-11-02 10:27:17 +0000537 // Now that the Value list is finalized for this axis, compare it with
538 // the original list, and union the difference with the default
539 // condition for the axis.
540 difference := SubtractBazelLabelList(baseLabels, lla.Value)
541 existingDefaults := lla.ConfigurableValues[axis][ConditionsDefaultConfigKey]
542 existingDefaults.Append(difference)
543 lla.ConfigurableValues[axis][ConditionsDefaultConfigKey] = FirstUniqueBazelLabelList(existingDefaults)
Liz Kammer74deed42021-06-02 13:02:03 -0400544
545 // if everything ends up without includes, just delete the axis
546 if !lla.ConfigurableValues[axis].HasConfigurableValues() {
547 delete(lla.ConfigurableValues, axis)
548 }
549 }
550}
551
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400552// OtherModuleContext is a limited context that has methods with information about other modules.
553type OtherModuleContext interface {
554 ModuleFromName(name string) (blueprint.Module, bool)
555 OtherModuleType(m blueprint.Module) string
556 OtherModuleName(m blueprint.Module) string
557 OtherModuleDir(m blueprint.Module) string
558 ModuleErrorf(fmt string, args ...interface{})
559}
560
561// LabelMapper is a function that takes a OtherModuleContext and returns a (potentially changed)
562// label and whether it was changed.
563type LabelMapper func(OtherModuleContext, string) (string, bool)
564
565// LabelPartition contains descriptions of a partition for labels
566type LabelPartition struct {
567 // Extensions to include in this partition
568 Extensions []string
569 // LabelMapper is a function that can map a label to a new label, and indicate whether to include
570 // the mapped label in the partition
571 LabelMapper LabelMapper
572 // Whether to store files not included in any other partition in a group of LabelPartitions
573 // Only one partition in a group of LabelPartitions can enabled Keep_remainder
574 Keep_remainder bool
575}
576
577// LabelPartitions is a map of partition name to a LabelPartition describing the elements of the
578// partition
579type LabelPartitions map[string]LabelPartition
580
581// filter returns a pointer to a label if the label should be included in the partition or nil if
582// not.
583func (lf LabelPartition) filter(ctx OtherModuleContext, label Label) *Label {
584 if lf.LabelMapper != nil {
585 if newLabel, changed := lf.LabelMapper(ctx, label.Label); changed {
586 return &Label{newLabel, label.OriginalModuleName}
587 }
588 }
589 for _, ext := range lf.Extensions {
590 if strings.HasSuffix(label.Label, ext) {
591 return &label
592 }
593 }
594
595 return nil
596}
597
598// PartitionToLabelListAttribute is map of partition name to a LabelListAttribute
599type PartitionToLabelListAttribute map[string]LabelListAttribute
600
601type partitionToLabelList map[string]*LabelList
602
603func (p partitionToLabelList) appendIncludes(partition string, label Label) {
604 if _, ok := p[partition]; !ok {
605 p[partition] = &LabelList{}
606 }
607 p[partition].Includes = append(p[partition].Includes, label)
608}
609
610func (p partitionToLabelList) excludes(partition string, excludes []Label) {
611 if _, ok := p[partition]; !ok {
612 p[partition] = &LabelList{}
613 }
614 p[partition].Excludes = excludes
615}
616
617// PartitionLabelListAttribute partitions a LabelListAttribute into the requested partitions
618func PartitionLabelListAttribute(ctx OtherModuleContext, lla *LabelListAttribute, partitions LabelPartitions) PartitionToLabelListAttribute {
619 ret := PartitionToLabelListAttribute{}
620 var partitionNames []string
621 // Stored as a pointer to distinguish nil (no remainder partition) from empty string partition
622 var remainderPartition *string
623 for p, f := range partitions {
624 partitionNames = append(partitionNames, p)
625 if f.Keep_remainder {
626 if remainderPartition != nil {
627 panic("only one partition can store the remainder")
628 }
629 // If we take the address of p in a loop, we'll end up with the last value of p in
630 // remainderPartition, we want the requested partition
631 capturePartition := p
632 remainderPartition = &capturePartition
633 }
634 }
635
636 partitionLabelList := func(axis ConfigurationAxis, config string) {
637 value := lla.SelectValue(axis, config)
638 partitionToLabels := partitionToLabelList{}
639 for _, item := range value.Includes {
640 wasFiltered := false
641 var inPartition *string
642 for partition, f := range partitions {
643 filtered := f.filter(ctx, item)
644 if filtered == nil {
645 // did not match this filter, keep looking
646 continue
647 }
648 wasFiltered = true
649 partitionToLabels.appendIncludes(partition, *filtered)
650 // don't need to check other partitions if this filter used the item,
651 // continue checking if mapped to another name
652 if *filtered == item {
653 if inPartition != nil {
654 ctx.ModuleErrorf("%q was found in multiple partitions: %q, %q", item.Label, *inPartition, partition)
655 }
656 capturePartition := partition
657 inPartition = &capturePartition
658 }
659 }
660
661 // if not specified in a partition, add to remainder partition if one exists
662 if !wasFiltered && remainderPartition != nil {
663 partitionToLabels.appendIncludes(*remainderPartition, item)
664 }
665 }
666
667 // ensure empty lists are maintained
668 if value.Excludes != nil {
669 for _, partition := range partitionNames {
670 partitionToLabels.excludes(partition, value.Excludes)
671 }
672 }
673
674 for partition, list := range partitionToLabels {
675 val := ret[partition]
676 (&val).SetSelectValue(axis, config, *list)
677 ret[partition] = val
678 }
679 }
680
681 partitionLabelList(NoConfigAxis, "")
682 for axis, configToList := range lla.ConfigurableValues {
683 for config, _ := range configToList {
684 partitionLabelList(axis, config)
685 }
686 }
687 return ret
688}
689
Jingwen Chen5d864492021-02-24 07:20:12 -0500690// StringListAttribute corresponds to the string_list Bazel attribute type with
691// support for additional metadata, like configurations.
692type StringListAttribute struct {
693 // The base value of the string list attribute.
694 Value []string
695
Liz Kammer9abd62d2021-05-21 08:37:59 -0400696 // The configured attribute label list Values. Optional
697 // a map of independent configurability axes
698 ConfigurableValues configurableStringLists
699}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000700
Liz Kammer9abd62d2021-05-21 08:37:59 -0400701type configurableStringLists map[ConfigurationAxis]stringListSelectValues
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400702
Liz Kammer9abd62d2021-05-21 08:37:59 -0400703func (csl configurableStringLists) Append(other configurableStringLists) {
704 for axis, otherSelects := range other {
705 selects := csl[axis]
706 if selects == nil {
707 selects = make(stringListSelectValues, len(otherSelects))
708 }
709 selects.appendSelects(otherSelects)
710 csl[axis] = selects
711 }
712}
713
714func (csl configurableStringLists) setValueForAxis(axis ConfigurationAxis, config string, list []string) {
715 if csl[axis] == nil {
716 csl[axis] = make(stringListSelectValues)
717 }
718 csl[axis][config] = list
719}
720
721type stringListSelectValues map[string][]string
722
723func (sl stringListSelectValues) appendSelects(other stringListSelectValues) {
724 for k, v := range other {
725 sl[k] = append(sl[k], v...)
726 }
727}
728
729func (sl stringListSelectValues) hasConfigurableValues(other stringListSelectValues) bool {
730 for _, val := range sl {
731 if len(val) > 0 {
732 return true
733 }
734 }
735 return false
Jingwen Chen5d864492021-02-24 07:20:12 -0500736}
737
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000738// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value.
739func MakeStringListAttribute(value []string) StringListAttribute {
740 // NOTE: These strings are not necessarily unique or sorted.
Liz Kammer9abd62d2021-05-21 08:37:59 -0400741 return StringListAttribute{
742 Value: value,
743 ConfigurableValues: make(configurableStringLists),
Jingwen Chen91220d72021-03-24 02:18:33 -0400744 }
745}
746
Liz Kammer9abd62d2021-05-21 08:37:59 -0400747// HasConfigurableValues returns true if the attribute contains axis-specific string_list values.
748func (sla StringListAttribute) HasConfigurableValues() bool {
749 return len(sla.ConfigurableValues) > 0
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400750}
751
Jingwen Chened9c17d2021-04-13 07:14:55 +0000752// Append appends all values, including os and arch specific ones, from another
753// StringListAttribute to this StringListAttribute
Liz Kammer9abd62d2021-05-21 08:37:59 -0400754func (sla *StringListAttribute) Append(other StringListAttribute) {
755 sla.Value = append(sla.Value, other.Value...)
756 if sla.ConfigurableValues == nil {
757 sla.ConfigurableValues = make(configurableStringLists)
758 }
759 sla.ConfigurableValues.Append(other.ConfigurableValues)
760}
761
762// SetSelectValue set a value for a bazel select for the given axis, config and value.
763func (sla *StringListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list []string) {
764 axis.validateConfig(config)
765 switch axis.configurationType {
766 case noConfig:
767 sla.Value = list
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400768 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400769 if sla.ConfigurableValues == nil {
770 sla.ConfigurableValues = make(configurableStringLists)
771 }
772 sla.ConfigurableValues.setValueForAxis(axis, config, list)
773 default:
774 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
775 }
776}
777
778// SelectValue gets a value for a bazel select for the given axis and config.
779func (sla *StringListAttribute) SelectValue(axis ConfigurationAxis, config string) []string {
780 axis.validateConfig(config)
781 switch axis.configurationType {
782 case noConfig:
783 return sla.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400784 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400785 return sla.ConfigurableValues[axis][config]
786 default:
787 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
788 }
789}
790
791// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
792func (sla *StringListAttribute) SortedConfigurationAxes() []ConfigurationAxis {
793 keys := make([]ConfigurationAxis, 0, len(sla.ConfigurableValues))
794 for k := range sla.ConfigurableValues {
795 keys = append(keys, k)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000796 }
797
Liz Kammer9abd62d2021-05-21 08:37:59 -0400798 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
799 return keys
Jingwen Chened9c17d2021-04-13 07:14:55 +0000800}
801
Liz Kammer5fad5012021-09-09 14:08:21 -0400802// DeduplicateAxesFromBase ensures no duplication of items between the no-configuration value and
803// configuration-specific values. For example, if we would convert this StringListAttribute as:
804// ["a", "b", "c"] + select({
805// "//condition:one": ["a", "d"],
806// "//conditions:default": [],
807// })
808// after this function, we would convert this StringListAttribute as:
809// ["a", "b", "c"] + select({
810// "//condition:one": ["d"],
811// "//conditions:default": [],
812// })
813func (sla *StringListAttribute) DeduplicateAxesFromBase() {
814 base := sla.Value
815 for axis, configToList := range sla.ConfigurableValues {
816 for config, list := range configToList {
817 remaining := SubtractStrings(list, base)
818 if len(remaining) == 0 {
819 delete(sla.ConfigurableValues[axis], config)
820 } else {
821 sla.ConfigurableValues[axis][config] = remaining
822 }
823 }
824 }
825}
826
Liz Kammera060c452021-03-24 10:14:47 -0400827// TryVariableSubstitution, replace string substitution formatting within each string in slice with
828// Starlark string.format compatible tag for productVariable.
829func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
830 ret := make([]string, 0, len(slice))
831 changesMade := false
832 for _, s := range slice {
833 newS, changed := TryVariableSubstitution(s, productVariable)
834 ret = append(ret, newS)
835 changesMade = changesMade || changed
836 }
837 return ret, changesMade
838}
839
840// TryVariableSubstitution, replace string substitution formatting within s with Starlark
841// string.format compatible tag for productVariable.
842func TryVariableSubstitution(s string, productVariable string) (string, bool) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400843 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "$("+productVariable+")")
Liz Kammera060c452021-03-24 10:14:47 -0400844 return sub, s != sub
845}