blob: ee32e73ac9200b68bd9912ef7180e634032594a3 [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
167 remainder := make(map[string]bool)
168
169 for _, s := range haystack {
170 remainder[s] = true
171 }
172 for _, s := range needle {
173 delete(remainder, s)
174 }
175
176 var strings []string
177 for s, _ := range remainder {
178 strings = append(strings, s)
179 }
180
181 sort.SliceStable(strings, func(i, j int) bool {
182 return strings[i] < strings[j]
183 })
184
185 return strings
186}
187
188// Subtract needle from haystack
189func SubtractBazelLabels(haystack []Label, needle []Label) []Label {
190 // This is really a set
191 remainder := make(map[Label]bool)
192
193 for _, label := range haystack {
194 remainder[label] = true
195 }
196 for _, label := range needle {
197 delete(remainder, label)
198 }
199
200 var labels []Label
201 for label, _ := range remainder {
202 labels = append(labels, label)
203 }
204
205 sort.SliceStable(labels, func(i, j int) bool {
206 return labels[i].Label < labels[j].Label
207 })
208
209 return labels
210}
211
Chris Parsons484e50a2021-05-13 15:13:04 -0400212// Appends two LabelLists, returning the combined list.
213func AppendBazelLabelLists(a LabelList, b LabelList) LabelList {
214 var result LabelList
215 result.Includes = append(a.Includes, b.Includes...)
216 result.Excludes = append(a.Excludes, b.Excludes...)
217 return result
218}
219
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000220// Subtract needle from haystack
221func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList {
222 var result LabelList
223 result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes)
224 // NOTE: Excludes are intentionally not subtracted
225 result.Excludes = haystack.Excludes
226 return result
227}
228
Jingwen Chenc1c26502021-04-05 10:35:13 +0000229type Attribute interface {
230 HasConfigurableValues() bool
231}
232
Liz Kammer9abd62d2021-05-21 08:37:59 -0400233type labelSelectValues map[string]*Label
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400234
Liz Kammer9abd62d2021-05-21 08:37:59 -0400235type configurableLabels map[ConfigurationAxis]labelSelectValues
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400236
Liz Kammer9abd62d2021-05-21 08:37:59 -0400237func (cl configurableLabels) setValueForAxis(axis ConfigurationAxis, config string, value *Label) {
238 if cl[axis] == nil {
239 cl[axis] = make(labelSelectValues)
240 }
241 cl[axis][config] = value
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400242}
243
244// Represents an attribute whose value is a single label
245type LabelAttribute struct {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400246 Value *Label
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400247
Liz Kammer9abd62d2021-05-21 08:37:59 -0400248 ConfigurableValues configurableLabels
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200249}
250
Liz Kammer9abd62d2021-05-21 08:37:59 -0400251// HasConfigurableValues returns whether there are configurable values set for this label.
252func (la LabelAttribute) HasConfigurableValues() bool {
253 return len(la.ConfigurableValues) > 0
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200254}
255
Liz Kammer9abd62d2021-05-21 08:37:59 -0400256// SetValue sets the base, non-configured value for the Label
257func (la *LabelAttribute) SetValue(value Label) {
258 la.SetSelectValue(NoConfigAxis, "", value)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400259}
260
Liz Kammer9abd62d2021-05-21 08:37:59 -0400261// SetSelectValue set a value for a bazel select for the given axis, config and value.
262func (la *LabelAttribute) SetSelectValue(axis ConfigurationAxis, config string, value Label) {
263 axis.validateConfig(config)
264 switch axis.configurationType {
265 case noConfig:
266 la.Value = &value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400267 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400268 if la.ConfigurableValues == nil {
269 la.ConfigurableValues = make(configurableLabels)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400270 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400271 la.ConfigurableValues.setValueForAxis(axis, config, &value)
272 default:
273 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
274 }
275}
276
277// SelectValue gets a value for a bazel select for the given axis and config.
278func (la *LabelAttribute) SelectValue(axis ConfigurationAxis, config string) Label {
279 axis.validateConfig(config)
280 switch axis.configurationType {
281 case noConfig:
282 return *la.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400283 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400284 return *la.ConfigurableValues[axis][config]
285 default:
286 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
287 }
288}
289
290// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
291func (la *LabelAttribute) SortedConfigurationAxes() []ConfigurationAxis {
292 keys := make([]ConfigurationAxis, 0, len(la.ConfigurableValues))
293 for k := range la.ConfigurableValues {
294 keys = append(keys, k)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400295 }
296
Liz Kammer9abd62d2021-05-21 08:37:59 -0400297 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
298 return keys
299}
300
Liz Kammerd366c902021-06-03 13:43:01 -0400301type configToBools map[string]bool
302
303func (ctb configToBools) setValue(config string, value *bool) {
304 if value == nil {
305 if _, ok := ctb[config]; ok {
306 delete(ctb, config)
307 }
308 return
309 }
310 ctb[config] = *value
311}
312
313type configurableBools map[ConfigurationAxis]configToBools
314
315func (cb configurableBools) setValueForAxis(axis ConfigurationAxis, config string, value *bool) {
316 if cb[axis] == nil {
317 cb[axis] = make(configToBools)
318 }
319 cb[axis].setValue(config, value)
320}
321
322// BoolAttribute represents an attribute whose value is a single bool but may be configurable..
323type BoolAttribute struct {
324 Value *bool
325
326 ConfigurableValues configurableBools
327}
328
329// HasConfigurableValues returns whether there are configurable values for this attribute.
330func (ba BoolAttribute) HasConfigurableValues() bool {
331 return len(ba.ConfigurableValues) > 0
332}
333
334// SetSelectValue sets value for the given axis/config.
335func (ba *BoolAttribute) SetSelectValue(axis ConfigurationAxis, config string, value *bool) {
336 axis.validateConfig(config)
337 switch axis.configurationType {
338 case noConfig:
339 ba.Value = value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400340 case arch, os, osArch, productVariables:
Liz Kammerd366c902021-06-03 13:43:01 -0400341 if ba.ConfigurableValues == nil {
342 ba.ConfigurableValues = make(configurableBools)
343 }
344 ba.ConfigurableValues.setValueForAxis(axis, config, value)
345 default:
346 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
347 }
348}
349
350// SelectValue gets the value for the given axis/config.
351func (ba BoolAttribute) SelectValue(axis ConfigurationAxis, config string) *bool {
352 axis.validateConfig(config)
353 switch axis.configurationType {
354 case noConfig:
355 return ba.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400356 case arch, os, osArch, productVariables:
Liz Kammerd366c902021-06-03 13:43:01 -0400357 if v, ok := ba.ConfigurableValues[axis][config]; ok {
358 return &v
359 } else {
360 return nil
361 }
362 default:
363 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
364 }
365}
366
367// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
368func (ba *BoolAttribute) SortedConfigurationAxes() []ConfigurationAxis {
369 keys := make([]ConfigurationAxis, 0, len(ba.ConfigurableValues))
370 for k := range ba.ConfigurableValues {
371 keys = append(keys, k)
372 }
373
374 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
375 return keys
376}
377
Liz Kammer9abd62d2021-05-21 08:37:59 -0400378// labelListSelectValues supports config-specific label_list typed Bazel attribute values.
379type labelListSelectValues map[string]LabelList
380
381func (ll labelListSelectValues) appendSelects(other labelListSelectValues) {
382 for k, v := range other {
383 l := ll[k]
384 (&l).Append(v)
385 ll[k] = l
386 }
387}
388
389// HasConfigurableValues returns whether there are configurable values within this set of selects.
390func (ll labelListSelectValues) HasConfigurableValues() bool {
391 for _, v := range ll {
Chris Parsons51f8c392021-08-03 21:01:05 -0400392 if v.Includes != nil {
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400393 return true
394 }
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400395 }
396 return false
397}
398
Jingwen Chen07027912021-03-15 06:02:43 -0400399// LabelListAttribute is used to represent a list of Bazel labels as an
400// attribute.
401type LabelListAttribute struct {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400402 // The non-configured attribute label list Value. Required.
Jingwen Chen07027912021-03-15 06:02:43 -0400403 Value LabelList
404
Liz Kammer9abd62d2021-05-21 08:37:59 -0400405 // The configured attribute label list Values. Optional
406 // a map of independent configurability axes
407 ConfigurableValues configurableLabelLists
Chris Parsons51f8c392021-08-03 21:01:05 -0400408
409 // If true, differentiate between "nil" and "empty" list. nil means that
410 // this attribute should not be specified at all, and "empty" means that
411 // the attribute should be explicitly specified as an empty list.
412 // This mode facilitates use of attribute defaults: an empty list should
413 // override the default.
414 ForceSpecifyEmptyList bool
Liz Kammer9abd62d2021-05-21 08:37:59 -0400415}
Jingwen Chen91220d72021-03-24 02:18:33 -0400416
Liz Kammer9abd62d2021-05-21 08:37:59 -0400417type configurableLabelLists map[ConfigurationAxis]labelListSelectValues
418
419func (cll configurableLabelLists) setValueForAxis(axis ConfigurationAxis, config string, list LabelList) {
420 if list.IsNil() {
421 if _, ok := cll[axis][config]; ok {
422 delete(cll[axis], config)
423 }
424 return
425 }
426 if cll[axis] == nil {
427 cll[axis] = make(labelListSelectValues)
428 }
429
430 cll[axis][config] = list
431}
432
433func (cll configurableLabelLists) Append(other configurableLabelLists) {
434 for axis, otherSelects := range other {
435 selects := cll[axis]
436 if selects == nil {
437 selects = make(labelListSelectValues, len(otherSelects))
438 }
439 selects.appendSelects(otherSelects)
440 cll[axis] = selects
441 }
Jingwen Chen07027912021-03-15 06:02:43 -0400442}
443
444// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
445func MakeLabelListAttribute(value LabelList) LabelListAttribute {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400446 return LabelListAttribute{
447 Value: value,
448 ConfigurableValues: make(configurableLabelLists),
449 }
450}
451
452func (lla *LabelListAttribute) SetValue(list LabelList) {
453 lla.SetSelectValue(NoConfigAxis, "", list)
454}
455
456// SetSelectValue set a value for a bazel select for the given axis, config and value.
457func (lla *LabelListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list LabelList) {
458 axis.validateConfig(config)
459 switch axis.configurationType {
460 case noConfig:
461 lla.Value = list
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400462 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400463 if lla.ConfigurableValues == nil {
464 lla.ConfigurableValues = make(configurableLabelLists)
465 }
466 lla.ConfigurableValues.setValueForAxis(axis, config, list)
467 default:
468 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
469 }
470}
471
472// SelectValue gets a value for a bazel select for the given axis and config.
473func (lla *LabelListAttribute) SelectValue(axis ConfigurationAxis, config string) LabelList {
474 axis.validateConfig(config)
475 switch axis.configurationType {
476 case noConfig:
477 return lla.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400478 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400479 return lla.ConfigurableValues[axis][config]
480 default:
481 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
482 }
483}
484
485// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
486func (lla *LabelListAttribute) SortedConfigurationAxes() []ConfigurationAxis {
487 keys := make([]ConfigurationAxis, 0, len(lla.ConfigurableValues))
488 for k := range lla.ConfigurableValues {
489 keys = append(keys, k)
490 }
491
492 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
493 return keys
Jingwen Chen07027912021-03-15 06:02:43 -0400494}
495
Jingwen Chened9c17d2021-04-13 07:14:55 +0000496// Append all values, including os and arch specific ones, from another
Jingwen Chen63930982021-03-24 10:04:33 -0400497// LabelListAttribute to this LabelListAttribute.
Liz Kammer9abd62d2021-05-21 08:37:59 -0400498func (lla *LabelListAttribute) Append(other LabelListAttribute) {
Chris Parsons51f8c392021-08-03 21:01:05 -0400499 if lla.ForceSpecifyEmptyList && !other.Value.IsNil() {
500 lla.Value.Includes = []Label{}
501 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400502 lla.Value.Append(other.Value)
503 if lla.ConfigurableValues == nil {
504 lla.ConfigurableValues = make(configurableLabelLists)
Jingwen Chen63930982021-03-24 10:04:33 -0400505 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400506 lla.ConfigurableValues.Append(other.ConfigurableValues)
Jingwen Chen63930982021-03-24 10:04:33 -0400507}
508
Liz Kammer9abd62d2021-05-21 08:37:59 -0400509// HasConfigurableValues returns true if the attribute contains axis-specific label list values.
510func (lla LabelListAttribute) HasConfigurableValues() bool {
511 return len(lla.ConfigurableValues) > 0
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400512}
513
Chris Parsons69fa9f92021-07-13 11:47:44 -0400514// IsEmpty returns true if the attribute has no values under any configuration.
515func (lla LabelListAttribute) IsEmpty() bool {
516 if len(lla.Value.Includes) > 0 {
517 return false
518 }
519 for axis, _ := range lla.ConfigurableValues {
520 if lla.ConfigurableValues[axis].HasConfigurableValues() {
521 return false
522 }
523 }
524 return true
525}
526
Liz Kammer74deed42021-06-02 13:02:03 -0400527// ResolveExcludes handles excludes across the various axes, ensuring that items are removed from
528// the base value and included in default values as appropriate.
529func (lla *LabelListAttribute) ResolveExcludes() {
530 for axis, configToLabels := range lla.ConfigurableValues {
531 baseLabels := lla.Value.deepCopy()
532 for config, val := range configToLabels {
533 // Exclude config-specific excludes from base value
534 lla.Value = SubtractBazelLabelList(lla.Value, LabelList{Includes: val.Excludes})
535
536 // add base values to config specific to add labels excluded by others in this axis
537 // then remove all config-specific excludes
538 allLabels := baseLabels.deepCopy()
539 allLabels.Append(val)
540 lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(allLabels, LabelList{Includes: val.Excludes})
541 }
542
543 // After going through all configs, delete the duplicates in the config
544 // values that are already in the base Value.
545 for config, val := range configToLabels {
546 lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(val, lla.Value)
547 }
548
549 // Now that the Value list is finalized for this axis, compare it with the original
550 // list, and put the difference into the default condition for the axis.
Chris Parsons51f8c392021-08-03 21:01:05 -0400551 lla.ConfigurableValues[axis][ConditionsDefaultConfigKey] = SubtractBazelLabelList(baseLabels, lla.Value)
Liz Kammer74deed42021-06-02 13:02:03 -0400552
553 // if everything ends up without includes, just delete the axis
554 if !lla.ConfigurableValues[axis].HasConfigurableValues() {
555 delete(lla.ConfigurableValues, axis)
556 }
557 }
558}
559
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400560// OtherModuleContext is a limited context that has methods with information about other modules.
561type OtherModuleContext interface {
562 ModuleFromName(name string) (blueprint.Module, bool)
563 OtherModuleType(m blueprint.Module) string
564 OtherModuleName(m blueprint.Module) string
565 OtherModuleDir(m blueprint.Module) string
566 ModuleErrorf(fmt string, args ...interface{})
567}
568
569// LabelMapper is a function that takes a OtherModuleContext and returns a (potentially changed)
570// label and whether it was changed.
571type LabelMapper func(OtherModuleContext, string) (string, bool)
572
573// LabelPartition contains descriptions of a partition for labels
574type LabelPartition struct {
575 // Extensions to include in this partition
576 Extensions []string
577 // LabelMapper is a function that can map a label to a new label, and indicate whether to include
578 // the mapped label in the partition
579 LabelMapper LabelMapper
580 // Whether to store files not included in any other partition in a group of LabelPartitions
581 // Only one partition in a group of LabelPartitions can enabled Keep_remainder
582 Keep_remainder bool
583}
584
585// LabelPartitions is a map of partition name to a LabelPartition describing the elements of the
586// partition
587type LabelPartitions map[string]LabelPartition
588
589// filter returns a pointer to a label if the label should be included in the partition or nil if
590// not.
591func (lf LabelPartition) filter(ctx OtherModuleContext, label Label) *Label {
592 if lf.LabelMapper != nil {
593 if newLabel, changed := lf.LabelMapper(ctx, label.Label); changed {
594 return &Label{newLabel, label.OriginalModuleName}
595 }
596 }
597 for _, ext := range lf.Extensions {
598 if strings.HasSuffix(label.Label, ext) {
599 return &label
600 }
601 }
602
603 return nil
604}
605
606// PartitionToLabelListAttribute is map of partition name to a LabelListAttribute
607type PartitionToLabelListAttribute map[string]LabelListAttribute
608
609type partitionToLabelList map[string]*LabelList
610
611func (p partitionToLabelList) appendIncludes(partition string, label Label) {
612 if _, ok := p[partition]; !ok {
613 p[partition] = &LabelList{}
614 }
615 p[partition].Includes = append(p[partition].Includes, label)
616}
617
618func (p partitionToLabelList) excludes(partition string, excludes []Label) {
619 if _, ok := p[partition]; !ok {
620 p[partition] = &LabelList{}
621 }
622 p[partition].Excludes = excludes
623}
624
625// PartitionLabelListAttribute partitions a LabelListAttribute into the requested partitions
626func PartitionLabelListAttribute(ctx OtherModuleContext, lla *LabelListAttribute, partitions LabelPartitions) PartitionToLabelListAttribute {
627 ret := PartitionToLabelListAttribute{}
628 var partitionNames []string
629 // Stored as a pointer to distinguish nil (no remainder partition) from empty string partition
630 var remainderPartition *string
631 for p, f := range partitions {
632 partitionNames = append(partitionNames, p)
633 if f.Keep_remainder {
634 if remainderPartition != nil {
635 panic("only one partition can store the remainder")
636 }
637 // If we take the address of p in a loop, we'll end up with the last value of p in
638 // remainderPartition, we want the requested partition
639 capturePartition := p
640 remainderPartition = &capturePartition
641 }
642 }
643
644 partitionLabelList := func(axis ConfigurationAxis, config string) {
645 value := lla.SelectValue(axis, config)
646 partitionToLabels := partitionToLabelList{}
647 for _, item := range value.Includes {
648 wasFiltered := false
649 var inPartition *string
650 for partition, f := range partitions {
651 filtered := f.filter(ctx, item)
652 if filtered == nil {
653 // did not match this filter, keep looking
654 continue
655 }
656 wasFiltered = true
657 partitionToLabels.appendIncludes(partition, *filtered)
658 // don't need to check other partitions if this filter used the item,
659 // continue checking if mapped to another name
660 if *filtered == item {
661 if inPartition != nil {
662 ctx.ModuleErrorf("%q was found in multiple partitions: %q, %q", item.Label, *inPartition, partition)
663 }
664 capturePartition := partition
665 inPartition = &capturePartition
666 }
667 }
668
669 // if not specified in a partition, add to remainder partition if one exists
670 if !wasFiltered && remainderPartition != nil {
671 partitionToLabels.appendIncludes(*remainderPartition, item)
672 }
673 }
674
675 // ensure empty lists are maintained
676 if value.Excludes != nil {
677 for _, partition := range partitionNames {
678 partitionToLabels.excludes(partition, value.Excludes)
679 }
680 }
681
682 for partition, list := range partitionToLabels {
683 val := ret[partition]
684 (&val).SetSelectValue(axis, config, *list)
685 ret[partition] = val
686 }
687 }
688
689 partitionLabelList(NoConfigAxis, "")
690 for axis, configToList := range lla.ConfigurableValues {
691 for config, _ := range configToList {
692 partitionLabelList(axis, config)
693 }
694 }
695 return ret
696}
697
Jingwen Chen5d864492021-02-24 07:20:12 -0500698// StringListAttribute corresponds to the string_list Bazel attribute type with
699// support for additional metadata, like configurations.
700type StringListAttribute struct {
701 // The base value of the string list attribute.
702 Value []string
703
Liz Kammer9abd62d2021-05-21 08:37:59 -0400704 // The configured attribute label list Values. Optional
705 // a map of independent configurability axes
706 ConfigurableValues configurableStringLists
707}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000708
Liz Kammer9abd62d2021-05-21 08:37:59 -0400709type configurableStringLists map[ConfigurationAxis]stringListSelectValues
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400710
Liz Kammer9abd62d2021-05-21 08:37:59 -0400711func (csl configurableStringLists) Append(other configurableStringLists) {
712 for axis, otherSelects := range other {
713 selects := csl[axis]
714 if selects == nil {
715 selects = make(stringListSelectValues, len(otherSelects))
716 }
717 selects.appendSelects(otherSelects)
718 csl[axis] = selects
719 }
720}
721
722func (csl configurableStringLists) setValueForAxis(axis ConfigurationAxis, config string, list []string) {
723 if csl[axis] == nil {
724 csl[axis] = make(stringListSelectValues)
725 }
726 csl[axis][config] = list
727}
728
729type stringListSelectValues map[string][]string
730
731func (sl stringListSelectValues) appendSelects(other stringListSelectValues) {
732 for k, v := range other {
733 sl[k] = append(sl[k], v...)
734 }
735}
736
737func (sl stringListSelectValues) hasConfigurableValues(other stringListSelectValues) bool {
738 for _, val := range sl {
739 if len(val) > 0 {
740 return true
741 }
742 }
743 return false
Jingwen Chen5d864492021-02-24 07:20:12 -0500744}
745
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000746// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value.
747func MakeStringListAttribute(value []string) StringListAttribute {
748 // NOTE: These strings are not necessarily unique or sorted.
Liz Kammer9abd62d2021-05-21 08:37:59 -0400749 return StringListAttribute{
750 Value: value,
751 ConfigurableValues: make(configurableStringLists),
Jingwen Chen91220d72021-03-24 02:18:33 -0400752 }
753}
754
Liz Kammer9abd62d2021-05-21 08:37:59 -0400755// HasConfigurableValues returns true if the attribute contains axis-specific string_list values.
756func (sla StringListAttribute) HasConfigurableValues() bool {
757 return len(sla.ConfigurableValues) > 0
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400758}
759
Jingwen Chened9c17d2021-04-13 07:14:55 +0000760// Append appends all values, including os and arch specific ones, from another
761// StringListAttribute to this StringListAttribute
Liz Kammer9abd62d2021-05-21 08:37:59 -0400762func (sla *StringListAttribute) Append(other StringListAttribute) {
763 sla.Value = append(sla.Value, other.Value...)
764 if sla.ConfigurableValues == nil {
765 sla.ConfigurableValues = make(configurableStringLists)
766 }
767 sla.ConfigurableValues.Append(other.ConfigurableValues)
768}
769
770// SetSelectValue set a value for a bazel select for the given axis, config and value.
771func (sla *StringListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list []string) {
772 axis.validateConfig(config)
773 switch axis.configurationType {
774 case noConfig:
775 sla.Value = list
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400776 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400777 if sla.ConfigurableValues == nil {
778 sla.ConfigurableValues = make(configurableStringLists)
779 }
780 sla.ConfigurableValues.setValueForAxis(axis, config, list)
781 default:
782 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
783 }
784}
785
786// SelectValue gets a value for a bazel select for the given axis and config.
787func (sla *StringListAttribute) SelectValue(axis ConfigurationAxis, config string) []string {
788 axis.validateConfig(config)
789 switch axis.configurationType {
790 case noConfig:
791 return sla.Value
Chris Parsons2dde0cb2021-10-01 14:45:30 -0400792 case arch, os, osArch, productVariables:
Liz Kammer9abd62d2021-05-21 08:37:59 -0400793 return sla.ConfigurableValues[axis][config]
794 default:
795 panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
796 }
797}
798
799// SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order.
800func (sla *StringListAttribute) SortedConfigurationAxes() []ConfigurationAxis {
801 keys := make([]ConfigurationAxis, 0, len(sla.ConfigurableValues))
802 for k := range sla.ConfigurableValues {
803 keys = append(keys, k)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000804 }
805
Liz Kammer9abd62d2021-05-21 08:37:59 -0400806 sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) })
807 return keys
Jingwen Chened9c17d2021-04-13 07:14:55 +0000808}
809
Liz Kammer5fad5012021-09-09 14:08:21 -0400810// DeduplicateAxesFromBase ensures no duplication of items between the no-configuration value and
811// configuration-specific values. For example, if we would convert this StringListAttribute as:
812// ["a", "b", "c"] + select({
813// "//condition:one": ["a", "d"],
814// "//conditions:default": [],
815// })
816// after this function, we would convert this StringListAttribute as:
817// ["a", "b", "c"] + select({
818// "//condition:one": ["d"],
819// "//conditions:default": [],
820// })
821func (sla *StringListAttribute) DeduplicateAxesFromBase() {
822 base := sla.Value
823 for axis, configToList := range sla.ConfigurableValues {
824 for config, list := range configToList {
825 remaining := SubtractStrings(list, base)
826 if len(remaining) == 0 {
827 delete(sla.ConfigurableValues[axis], config)
828 } else {
829 sla.ConfigurableValues[axis][config] = remaining
830 }
831 }
832 }
833}
834
Liz Kammera060c452021-03-24 10:14:47 -0400835// TryVariableSubstitution, replace string substitution formatting within each string in slice with
836// Starlark string.format compatible tag for productVariable.
837func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
838 ret := make([]string, 0, len(slice))
839 changesMade := false
840 for _, s := range slice {
841 newS, changed := TryVariableSubstitution(s, productVariable)
842 ret = append(ret, newS)
843 changesMade = changesMade || changed
844 }
845 return ret, changesMade
846}
847
848// TryVariableSubstitution, replace string substitution formatting within s with Starlark
849// string.format compatible tag for productVariable.
850func TryVariableSubstitution(s string, productVariable string) (string, bool) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400851 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "$("+productVariable+")")
Liz Kammera060c452021-03-24 10:14:47 -0400852 return sub, s != sub
853}