blob: 6a06c1bab14de11b7b60ae8ac6a790f2d2b746e1 [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
537 // Now that the Value list is finalized for this axis, compare it with the original
538 // list, and put the difference into the default condition for the axis.
Chris Parsons51f8c392021-08-03 21:01:05 -0400539 lla.ConfigurableValues[axis][ConditionsDefaultConfigKey] = SubtractBazelLabelList(baseLabels, lla.Value)
Liz Kammer74deed42021-06-02 13:02:03 -0400540
541 // if everything ends up without includes, just delete the axis
542 if !lla.ConfigurableValues[axis].HasConfigurableValues() {
543 delete(lla.ConfigurableValues, axis)
544 }
545 }
546}
547
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400548// OtherModuleContext is a limited context that has methods with information about other modules.
549type OtherModuleContext interface {
550 ModuleFromName(name string) (blueprint.Module, bool)
551 OtherModuleType(m blueprint.Module) string
552 OtherModuleName(m blueprint.Module) string
553 OtherModuleDir(m blueprint.Module) string
554 ModuleErrorf(fmt string, args ...interface{})
555}
556
557// LabelMapper is a function that takes a OtherModuleContext and returns a (potentially changed)
558// label and whether it was changed.
559type LabelMapper func(OtherModuleContext, string) (string, bool)
560
561// LabelPartition contains descriptions of a partition for labels
562type LabelPartition struct {
563 // Extensions to include in this partition
564 Extensions []string
565 // LabelMapper is a function that can map a label to a new label, and indicate whether to include
566 // the mapped label in the partition
567 LabelMapper LabelMapper
568 // Whether to store files not included in any other partition in a group of LabelPartitions
569 // Only one partition in a group of LabelPartitions can enabled Keep_remainder
570 Keep_remainder bool
571}
572
573// LabelPartitions is a map of partition name to a LabelPartition describing the elements of the
574// partition
575type LabelPartitions map[string]LabelPartition
576
577// filter returns a pointer to a label if the label should be included in the partition or nil if
578// not.
579func (lf LabelPartition) filter(ctx OtherModuleContext, label Label) *Label {
580 if lf.LabelMapper != nil {
581 if newLabel, changed := lf.LabelMapper(ctx, label.Label); changed {
582 return &Label{newLabel, label.OriginalModuleName}
583 }
584 }
585 for _, ext := range lf.Extensions {
586 if strings.HasSuffix(label.Label, ext) {
587 return &label
588 }
589 }
590
591 return nil
592}
593
594// PartitionToLabelListAttribute is map of partition name to a LabelListAttribute
595type PartitionToLabelListAttribute map[string]LabelListAttribute
596
597type partitionToLabelList map[string]*LabelList
598
599func (p partitionToLabelList) appendIncludes(partition string, label Label) {
600 if _, ok := p[partition]; !ok {
601 p[partition] = &LabelList{}
602 }
603 p[partition].Includes = append(p[partition].Includes, label)
604}
605
606func (p partitionToLabelList) excludes(partition string, excludes []Label) {
607 if _, ok := p[partition]; !ok {
608 p[partition] = &LabelList{}
609 }
610 p[partition].Excludes = excludes
611}
612
613// PartitionLabelListAttribute partitions a LabelListAttribute into the requested partitions
614func PartitionLabelListAttribute(ctx OtherModuleContext, lla *LabelListAttribute, partitions LabelPartitions) PartitionToLabelListAttribute {
615 ret := PartitionToLabelListAttribute{}
616 var partitionNames []string
617 // Stored as a pointer to distinguish nil (no remainder partition) from empty string partition
618 var remainderPartition *string
619 for p, f := range partitions {
620 partitionNames = append(partitionNames, p)
621 if f.Keep_remainder {
622 if remainderPartition != nil {
623 panic("only one partition can store the remainder")
624 }
625 // If we take the address of p in a loop, we'll end up with the last value of p in
626 // remainderPartition, we want the requested partition
627 capturePartition := p
628 remainderPartition = &capturePartition
629 }
630 }
631
632 partitionLabelList := func(axis ConfigurationAxis, config string) {
633 value := lla.SelectValue(axis, config)
634 partitionToLabels := partitionToLabelList{}
635 for _, item := range value.Includes {
636 wasFiltered := false
637 var inPartition *string
638 for partition, f := range partitions {
639 filtered := f.filter(ctx, item)
640 if filtered == nil {
641 // did not match this filter, keep looking
642 continue
643 }
644 wasFiltered = true
645 partitionToLabels.appendIncludes(partition, *filtered)
646 // don't need to check other partitions if this filter used the item,
647 // continue checking if mapped to another name
648 if *filtered == item {
649 if inPartition != nil {
650 ctx.ModuleErrorf("%q was found in multiple partitions: %q, %q", item.Label, *inPartition, partition)
651 }
652 capturePartition := partition
653 inPartition = &capturePartition
654 }
655 }
656
657 // if not specified in a partition, add to remainder partition if one exists
658 if !wasFiltered && remainderPartition != nil {
659 partitionToLabels.appendIncludes(*remainderPartition, item)
660 }
661 }
662
663 // ensure empty lists are maintained
664 if value.Excludes != nil {
665 for _, partition := range partitionNames {
666 partitionToLabels.excludes(partition, value.Excludes)
667 }
668 }
669
670 for partition, list := range partitionToLabels {
671 val := ret[partition]
672 (&val).SetSelectValue(axis, config, *list)
673 ret[partition] = val
674 }
675 }
676
677 partitionLabelList(NoConfigAxis, "")
678 for axis, configToList := range lla.ConfigurableValues {
679 for config, _ := range configToList {
680 partitionLabelList(axis, config)
681 }
682 }
683 return ret
684}
685
Jingwen Chen5d864492021-02-24 07:20:12 -0500686// StringListAttribute corresponds to the string_list Bazel attribute type with
687// support for additional metadata, like configurations.
688type StringListAttribute struct {
689 // The base value of the string list attribute.
690 Value []string
691
Liz Kammer9abd62d2021-05-21 08:37:59 -0400692 // The configured attribute label list Values. Optional
693 // a map of independent configurability axes
694 ConfigurableValues configurableStringLists
695}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000696
Liz Kammer9abd62d2021-05-21 08:37:59 -0400697type configurableStringLists map[ConfigurationAxis]stringListSelectValues
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400698
Liz Kammer9abd62d2021-05-21 08:37:59 -0400699func (csl configurableStringLists) Append(other configurableStringLists) {
700 for axis, otherSelects := range other {
701 selects := csl[axis]
702 if selects == nil {
703 selects = make(stringListSelectValues, len(otherSelects))
704 }
705 selects.appendSelects(otherSelects)
706 csl[axis] = selects
707 }
708}
709
710func (csl configurableStringLists) setValueForAxis(axis ConfigurationAxis, config string, list []string) {
711 if csl[axis] == nil {
712 csl[axis] = make(stringListSelectValues)
713 }
714 csl[axis][config] = list
715}
716
717type stringListSelectValues map[string][]string
718
719func (sl stringListSelectValues) appendSelects(other stringListSelectValues) {
720 for k, v := range other {
721 sl[k] = append(sl[k], v...)
722 }
723}
724
725func (sl stringListSelectValues) hasConfigurableValues(other stringListSelectValues) bool {
726 for _, val := range sl {
727 if len(val) > 0 {
728 return true
729 }
730 }
731 return false
Jingwen Chen5d864492021-02-24 07:20:12 -0500732}
733
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000734// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value.
735func MakeStringListAttribute(value []string) StringListAttribute {
736 // NOTE: These strings are not necessarily unique or sorted.
Liz Kammer9abd62d2021-05-21 08:37:59 -0400737 return StringListAttribute{
738 Value: value,
739 ConfigurableValues: make(configurableStringLists),
Jingwen Chen91220d72021-03-24 02:18:33 -0400740 }
741}
742
Liz Kammer9abd62d2021-05-21 08:37:59 -0400743// HasConfigurableValues returns true if the attribute contains axis-specific string_list values.
744func (sla StringListAttribute) HasConfigurableValues() bool {
745 return len(sla.ConfigurableValues) > 0
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400746}
747
Jingwen Chened9c17d2021-04-13 07:14:55 +0000748// Append appends all values, including os and arch specific ones, from another
749// StringListAttribute to this StringListAttribute
Liz Kammer9abd62d2021-05-21 08:37:59 -0400750func (sla *StringListAttribute) Append(other StringListAttribute) {
751 sla.Value = append(sla.Value, other.Value...)
752 if sla.ConfigurableValues == nil {
753 sla.ConfigurableValues = make(configurableStringLists)
754 }
755 sla.ConfigurableValues.Append(other.ConfigurableValues)
756}
757
758// SetSelectValue set a value for a bazel select for the given axis, config and value.
759func (sla *StringListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list []string) {
760 axis.validateConfig(config)
761 switch axis.configurationType {
762 case noConfig:
763 sla.Value = list
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400764 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400765 if sla.ConfigurableValues == nil {
766 sla.ConfigurableValues = make(configurableStringLists)
767 }
768 sla.ConfigurableValues.setValueForAxis(axis, config, list)
769 default:
770 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
771 }
772}
773
774// SelectValue gets a value for a bazel select for the given axis and config.
775func (sla *StringListAttribute) SelectValue(axis ConfigurationAxis, config string) []string {
776 axis.validateConfig(config)
777 switch axis.configurationType {
778 case noConfig:
779 return sla.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400780 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400781 return sla.ConfigurableValues[axis][config]
782 default:
783 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
784 }
785}
786
787// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
788func (sla *StringListAttribute) SortedConfigurationAxes() []ConfigurationAxis {
789 keys := make([]ConfigurationAxis, 0, len(sla.ConfigurableValues))
790 for k := range sla.ConfigurableValues {
791 keys = append(keys, k)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000792 }
793
Liz Kammer9abd62d2021-05-21 08:37:59 -0400794 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
795 return keys
Jingwen Chened9c17d2021-04-13 07:14:55 +0000796}
797
Liz Kammer5fad5012021-09-09 14:08:21 -0400798// DeduplicateAxesFromBase ensures no duplication of items between the no-configuration value and
799// configuration-specific values. For example, if we would convert this StringListAttribute as:
800// ["a", "b", "c"] + select({
801// "//condition:one": ["a", "d"],
802// "//conditions:default": [],
803// })
804// after this function, we would convert this StringListAttribute as:
805// ["a", "b", "c"] + select({
806// "//condition:one": ["d"],
807// "//conditions:default": [],
808// })
809func (sla *StringListAttribute) DeduplicateAxesFromBase() {
810 base := sla.Value
811 for axis, configToList := range sla.ConfigurableValues {
812 for config, list := range configToList {
813 remaining := SubtractStrings(list, base)
814 if len(remaining) == 0 {
815 delete(sla.ConfigurableValues[axis], config)
816 } else {
817 sla.ConfigurableValues[axis][config] = remaining
818 }
819 }
820 }
821}
822
Liz Kammera060c452021-03-24 10:14:47 -0400823// TryVariableSubstitution, replace string substitution formatting within each string in slice with
824// Starlark string.format compatible tag for productVariable.
825func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
826 ret := make([]string, 0, len(slice))
827 changesMade := false
828 for _, s := range slice {
829 newS, changed := TryVariableSubstitution(s, productVariable)
830 ret = append(ret, newS)
831 changesMade = changesMade || changed
832 }
833 return ret, changesMade
834}
835
836// TryVariableSubstitution, replace string substitution formatting within s with Starlark
837// string.format compatible tag for productVariable.
838func TryVariableSubstitution(s string, productVariable string) (string, bool) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400839 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "$("+productVariable+")")
Liz Kammera060c452021-03-24 10:14:47 -0400840 return sub, s != sub
841}