Jingwen Chen | 30f5aaa | 2020-11-19 05:38:02 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package bazel |
| 16 | |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 17 | import ( |
| 18 | "fmt" |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 19 | "path/filepath" |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 20 | "regexp" |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 21 | "sort" |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 22 | "strings" |
| 23 | |
| 24 | "github.com/google/blueprint" |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 25 | ) |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 26 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 27 | // BazelTargetModuleProperties contain properties and metadata used for |
| 28 | // Blueprint to BUILD file conversion. |
| 29 | type BazelTargetModuleProperties struct { |
| 30 | // The Bazel rule class for this target. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 31 | Rule_class string `blueprint:"mutated"` |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 32 | |
| 33 | // The target label for the bzl file containing the definition of the rule class. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 34 | Bzl_load_location string `blueprint:"mutated"` |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 35 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 36 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 37 | var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)") |
| 38 | |
Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame] | 39 | // Label is used to represent a Bazel compatible Label. Also stores the original |
| 40 | // bp text to support string replacement. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 41 | type Label struct { |
Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame] | 42 | // 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 Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // LabelList is used to represent a list of Bazel labels. |
| 63 | type LabelList struct { |
| 64 | Includes []Label |
| 65 | Excludes []Label |
| 66 | } |
| 67 | |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 68 | func (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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 85 | func (ll *LabelList) IsNil() bool { |
| 86 | return ll.Includes == nil && ll.Excludes == nil |
| 87 | } |
| 88 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 89 | func (ll *LabelList) deepCopy() LabelList { |
| 90 | return LabelList{ |
| 91 | Includes: ll.Includes[:], |
| 92 | Excludes: ll.Excludes[:], |
| 93 | } |
| 94 | } |
| 95 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 96 | // uniqueParentDirectories returns a list of the unique parent directories for |
| 97 | // all files in ll.Includes. |
| 98 | func (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 Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 110 | // Append appends the fields of other labelList to the corresponding fields of ll. |
| 111 | func (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 Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 119 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 120 | // UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns |
| 121 | // the slice in a sorted order. |
| 122 | func UniqueSortedBazelLabels(originalLabels []Label) []Label { |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 123 | 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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 137 | func 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 | |
| 150 | func 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 | |
| 157 | func UniqueSortedBazelLabelList(originalLabelList LabelList) LabelList { |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 158 | var uniqueLabelList LabelList |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 159 | uniqueLabelList.Includes = UniqueSortedBazelLabels(originalLabelList.Includes) |
| 160 | uniqueLabelList.Excludes = UniqueSortedBazelLabels(originalLabelList.Excludes) |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 161 | return uniqueLabelList |
| 162 | } |
| 163 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 164 | // Subtract needle from haystack |
| 165 | func SubtractStrings(haystack []string, needle []string) []string { |
| 166 | // This is really a set |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 167 | needleMap := make(map[string]bool) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 168 | for _, s := range needle { |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 169 | needleMap[s] = true |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | var strings []string |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 173 | for _, s := range haystack { |
| 174 | if exclude := needleMap[s]; !exclude { |
| 175 | strings = append(strings, s) |
| 176 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 179 | return strings |
| 180 | } |
| 181 | |
| 182 | // Subtract needle from haystack |
| 183 | func SubtractBazelLabels(haystack []Label, needle []Label) []Label { |
| 184 | // This is really a set |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 185 | needleMap := make(map[Label]bool) |
| 186 | for _, s := range needle { |
| 187 | needleMap[s] = true |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | var labels []Label |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 191 | for _, label := range haystack { |
| 192 | if exclude := needleMap[label]; !exclude { |
| 193 | labels = append(labels, label) |
| 194 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 197 | return labels |
| 198 | } |
| 199 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 200 | // Appends two LabelLists, returning the combined list. |
| 201 | func 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 Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 208 | // Subtract needle from haystack |
| 209 | func 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 Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 217 | type Attribute interface { |
| 218 | HasConfigurableValues() bool |
| 219 | } |
| 220 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 221 | type labelSelectValues map[string]*Label |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 222 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 223 | type configurableLabels map[ConfigurationAxis]labelSelectValues |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 224 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 225 | func (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 Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Represents an attribute whose value is a single label |
| 233 | type LabelAttribute struct { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 234 | Value *Label |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 235 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 236 | ConfigurableValues configurableLabels |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 239 | // HasConfigurableValues returns whether there are configurable values set for this label. |
| 240 | func (la LabelAttribute) HasConfigurableValues() bool { |
| 241 | return len(la.ConfigurableValues) > 0 |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 244 | // SetValue sets the base, non-configured value for the Label |
| 245 | func (la *LabelAttribute) SetValue(value Label) { |
| 246 | la.SetSelectValue(NoConfigAxis, "", value) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 249 | // SetSelectValue set a value for a bazel select for the given axis, config and value. |
| 250 | func (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 Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 255 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 256 | if la.ConfigurableValues == nil { |
| 257 | la.ConfigurableValues = make(configurableLabels) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 258 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 259 | 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. |
| 266 | func (la *LabelAttribute) SelectValue(axis ConfigurationAxis, config string) Label { |
| 267 | axis.validateConfig(config) |
| 268 | switch axis.configurationType { |
| 269 | case noConfig: |
| 270 | return *la.Value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 271 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 272 | 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. |
| 279 | func (la *LabelAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 280 | keys := make([]ConfigurationAxis, 0, len(la.ConfigurableValues)) |
| 281 | for k := range la.ConfigurableValues { |
| 282 | keys = append(keys, k) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 285 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 286 | return keys |
| 287 | } |
| 288 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 289 | type configToBools map[string]bool |
| 290 | |
| 291 | func (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 | |
| 301 | type configurableBools map[ConfigurationAxis]configToBools |
| 302 | |
| 303 | func (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.. |
| 311 | type BoolAttribute struct { |
| 312 | Value *bool |
| 313 | |
| 314 | ConfigurableValues configurableBools |
| 315 | } |
| 316 | |
| 317 | // HasConfigurableValues returns whether there are configurable values for this attribute. |
| 318 | func (ba BoolAttribute) HasConfigurableValues() bool { |
| 319 | return len(ba.ConfigurableValues) > 0 |
| 320 | } |
| 321 | |
| 322 | // SetSelectValue sets value for the given axis/config. |
| 323 | func (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 Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 328 | case arch, os, osArch, productVariables: |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 329 | 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. |
| 339 | func (ba BoolAttribute) SelectValue(axis ConfigurationAxis, config string) *bool { |
| 340 | axis.validateConfig(config) |
| 341 | switch axis.configurationType { |
| 342 | case noConfig: |
| 343 | return ba.Value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 344 | case arch, os, osArch, productVariables: |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 345 | 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. |
| 356 | func (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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 366 | // labelListSelectValues supports config-specific label_list typed Bazel attribute values. |
| 367 | type labelListSelectValues map[string]LabelList |
| 368 | |
| 369 | func (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. |
| 378 | func (ll labelListSelectValues) HasConfigurableValues() bool { |
| 379 | for _, v := range ll { |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 380 | if v.Includes != nil { |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 381 | return true |
| 382 | } |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 383 | } |
| 384 | return false |
| 385 | } |
| 386 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 387 | // LabelListAttribute is used to represent a list of Bazel labels as an |
| 388 | // attribute. |
| 389 | type LabelListAttribute struct { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 390 | // The non-configured attribute label list Value. Required. |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 391 | Value LabelList |
| 392 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 393 | // The configured attribute label list Values. Optional |
| 394 | // a map of independent configurability axes |
| 395 | ConfigurableValues configurableLabelLists |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 396 | |
| 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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 403 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 404 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 405 | type configurableLabelLists map[ConfigurationAxis]labelListSelectValues |
| 406 | |
| 407 | func (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 | |
| 421 | func (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 Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value. |
| 433 | func MakeLabelListAttribute(value LabelList) LabelListAttribute { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 434 | return LabelListAttribute{ |
| 435 | Value: value, |
| 436 | ConfigurableValues: make(configurableLabelLists), |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | func (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. |
| 445 | func (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 Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 450 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 451 | 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. |
| 461 | func (lla *LabelListAttribute) SelectValue(axis ConfigurationAxis, config string) LabelList { |
| 462 | axis.validateConfig(config) |
| 463 | switch axis.configurationType { |
| 464 | case noConfig: |
| 465 | return lla.Value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 466 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 467 | 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. |
| 474 | func (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 Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 484 | // Append all values, including os and arch specific ones, from another |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 485 | // LabelListAttribute to this LabelListAttribute. |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 486 | func (lla *LabelListAttribute) Append(other LabelListAttribute) { |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 487 | if lla.ForceSpecifyEmptyList && !other.Value.IsNil() { |
| 488 | lla.Value.Includes = []Label{} |
| 489 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 490 | lla.Value.Append(other.Value) |
| 491 | if lla.ConfigurableValues == nil { |
| 492 | lla.ConfigurableValues = make(configurableLabelLists) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 493 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 494 | lla.ConfigurableValues.Append(other.ConfigurableValues) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 497 | // HasConfigurableValues returns true if the attribute contains axis-specific label list values. |
| 498 | func (lla LabelListAttribute) HasConfigurableValues() bool { |
| 499 | return len(lla.ConfigurableValues) > 0 |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 502 | // IsEmpty returns true if the attribute has no values under any configuration. |
| 503 | func (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 Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 515 | // ResolveExcludes handles excludes across the various axes, ensuring that items are removed from |
| 516 | // the base value and included in default values as appropriate. |
| 517 | func (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 Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 539 | lla.ConfigurableValues[axis][ConditionsDefaultConfigKey] = SubtractBazelLabelList(baseLabels, lla.Value) |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 540 | |
| 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 Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 548 | // OtherModuleContext is a limited context that has methods with information about other modules. |
| 549 | type 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. |
| 559 | type LabelMapper func(OtherModuleContext, string) (string, bool) |
| 560 | |
| 561 | // LabelPartition contains descriptions of a partition for labels |
| 562 | type 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 |
| 575 | type 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. |
| 579 | func (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 |
| 595 | type PartitionToLabelListAttribute map[string]LabelListAttribute |
| 596 | |
| 597 | type partitionToLabelList map[string]*LabelList |
| 598 | |
| 599 | func (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 | |
| 606 | func (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 |
| 614 | func 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 Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 686 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 687 | // support for additional metadata, like configurations. |
| 688 | type StringListAttribute struct { |
| 689 | // The base value of the string list attribute. |
| 690 | Value []string |
| 691 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 692 | // The configured attribute label list Values. Optional |
| 693 | // a map of independent configurability axes |
| 694 | ConfigurableValues configurableStringLists |
| 695 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 696 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 697 | type configurableStringLists map[ConfigurationAxis]stringListSelectValues |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 698 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 699 | func (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 | |
| 710 | func (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 | |
| 717 | type stringListSelectValues map[string][]string |
| 718 | |
| 719 | func (sl stringListSelectValues) appendSelects(other stringListSelectValues) { |
| 720 | for k, v := range other { |
| 721 | sl[k] = append(sl[k], v...) |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | func (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 Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 732 | } |
| 733 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 734 | // MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value. |
| 735 | func MakeStringListAttribute(value []string) StringListAttribute { |
| 736 | // NOTE: These strings are not necessarily unique or sorted. |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 737 | return StringListAttribute{ |
| 738 | Value: value, |
| 739 | ConfigurableValues: make(configurableStringLists), |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 743 | // HasConfigurableValues returns true if the attribute contains axis-specific string_list values. |
| 744 | func (sla StringListAttribute) HasConfigurableValues() bool { |
| 745 | return len(sla.ConfigurableValues) > 0 |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 746 | } |
| 747 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 748 | // Append appends all values, including os and arch specific ones, from another |
| 749 | // StringListAttribute to this StringListAttribute |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 750 | func (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. |
| 759 | func (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 Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 764 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 765 | 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. |
| 775 | func (sla *StringListAttribute) SelectValue(axis ConfigurationAxis, config string) []string { |
| 776 | axis.validateConfig(config) |
| 777 | switch axis.configurationType { |
| 778 | case noConfig: |
| 779 | return sla.Value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 780 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 781 | 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. |
| 788 | func (sla *StringListAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 789 | keys := make([]ConfigurationAxis, 0, len(sla.ConfigurableValues)) |
| 790 | for k := range sla.ConfigurableValues { |
| 791 | keys = append(keys, k) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 794 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 795 | return keys |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 798 | // 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 | // }) |
| 809 | func (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 Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 823 | // TryVariableSubstitution, replace string substitution formatting within each string in slice with |
| 824 | // Starlark string.format compatible tag for productVariable. |
| 825 | func 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. |
| 838 | func TryVariableSubstitution(s string, productVariable string) (string, bool) { |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 839 | sub := productVariableSubstitutionPattern.ReplaceAllString(s, "$("+productVariable+")") |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 840 | return sub, s != sub |
| 841 | } |