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 | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 110 | // Add inserts the label Label at the end of the LabelList. |
| 111 | func (ll *LabelList) Add(label *Label) { |
| 112 | if label == nil { |
| 113 | return |
| 114 | } |
| 115 | ll.Includes = append(ll.Includes, *label) |
| 116 | } |
| 117 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 118 | // Append appends the fields of other labelList to the corresponding fields of ll. |
| 119 | func (ll *LabelList) Append(other LabelList) { |
| 120 | if len(ll.Includes) > 0 || len(other.Includes) > 0 { |
| 121 | ll.Includes = append(ll.Includes, other.Includes...) |
| 122 | } |
| 123 | if len(ll.Excludes) > 0 || len(other.Excludes) > 0 { |
| 124 | ll.Excludes = append(other.Excludes, other.Excludes...) |
| 125 | } |
| 126 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 127 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 128 | // UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns |
| 129 | // the slice in a sorted order. |
| 130 | func UniqueSortedBazelLabels(originalLabels []Label) []Label { |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 131 | uniqueLabelsSet := make(map[Label]bool) |
| 132 | for _, l := range originalLabels { |
| 133 | uniqueLabelsSet[l] = true |
| 134 | } |
| 135 | var uniqueLabels []Label |
| 136 | for l, _ := range uniqueLabelsSet { |
| 137 | uniqueLabels = append(uniqueLabels, l) |
| 138 | } |
| 139 | sort.SliceStable(uniqueLabels, func(i, j int) bool { |
| 140 | return uniqueLabels[i].Label < uniqueLabels[j].Label |
| 141 | }) |
| 142 | return uniqueLabels |
| 143 | } |
| 144 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 145 | func FirstUniqueBazelLabels(originalLabels []Label) []Label { |
| 146 | var labels []Label |
| 147 | found := make(map[Label]bool, len(originalLabels)) |
| 148 | for _, l := range originalLabels { |
| 149 | if _, ok := found[l]; ok { |
| 150 | continue |
| 151 | } |
| 152 | labels = append(labels, l) |
| 153 | found[l] = true |
| 154 | } |
| 155 | return labels |
| 156 | } |
| 157 | |
| 158 | func FirstUniqueBazelLabelList(originalLabelList LabelList) LabelList { |
| 159 | var uniqueLabelList LabelList |
| 160 | uniqueLabelList.Includes = FirstUniqueBazelLabels(originalLabelList.Includes) |
| 161 | uniqueLabelList.Excludes = FirstUniqueBazelLabels(originalLabelList.Excludes) |
| 162 | return uniqueLabelList |
| 163 | } |
| 164 | |
| 165 | func UniqueSortedBazelLabelList(originalLabelList LabelList) LabelList { |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 166 | var uniqueLabelList LabelList |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 167 | uniqueLabelList.Includes = UniqueSortedBazelLabels(originalLabelList.Includes) |
| 168 | uniqueLabelList.Excludes = UniqueSortedBazelLabels(originalLabelList.Excludes) |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 169 | return uniqueLabelList |
| 170 | } |
| 171 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 172 | // Subtract needle from haystack |
| 173 | func SubtractStrings(haystack []string, needle []string) []string { |
| 174 | // This is really a set |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 175 | needleMap := make(map[string]bool) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 176 | for _, s := range needle { |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 177 | needleMap[s] = true |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | var strings []string |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 181 | for _, s := range haystack { |
| 182 | if exclude := needleMap[s]; !exclude { |
| 183 | strings = append(strings, s) |
| 184 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 187 | return strings |
| 188 | } |
| 189 | |
| 190 | // Subtract needle from haystack |
| 191 | func SubtractBazelLabels(haystack []Label, needle []Label) []Label { |
| 192 | // This is really a set |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 193 | needleMap := make(map[Label]bool) |
| 194 | for _, s := range needle { |
| 195 | needleMap[s] = true |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | var labels []Label |
Liz Kammer | 9bad9d6 | 2021-10-11 15:40:35 -0400 | [diff] [blame] | 199 | for _, label := range haystack { |
| 200 | if exclude := needleMap[label]; !exclude { |
| 201 | labels = append(labels, label) |
| 202 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 205 | return labels |
| 206 | } |
| 207 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 208 | // Appends two LabelLists, returning the combined list. |
| 209 | func AppendBazelLabelLists(a LabelList, b LabelList) LabelList { |
| 210 | var result LabelList |
| 211 | result.Includes = append(a.Includes, b.Includes...) |
| 212 | result.Excludes = append(a.Excludes, b.Excludes...) |
| 213 | return result |
| 214 | } |
| 215 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 216 | // Subtract needle from haystack |
| 217 | func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList { |
| 218 | var result LabelList |
| 219 | result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes) |
| 220 | // NOTE: Excludes are intentionally not subtracted |
| 221 | result.Excludes = haystack.Excludes |
| 222 | return result |
| 223 | } |
| 224 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 225 | type Attribute interface { |
| 226 | HasConfigurableValues() bool |
| 227 | } |
| 228 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 229 | type labelSelectValues map[string]*Label |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 230 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 231 | type configurableLabels map[ConfigurationAxis]labelSelectValues |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 232 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 233 | func (cl configurableLabels) setValueForAxis(axis ConfigurationAxis, config string, value *Label) { |
| 234 | if cl[axis] == nil { |
| 235 | cl[axis] = make(labelSelectValues) |
| 236 | } |
| 237 | cl[axis][config] = value |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // Represents an attribute whose value is a single label |
| 241 | type LabelAttribute struct { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 242 | Value *Label |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 243 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 244 | ConfigurableValues configurableLabels |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 247 | // HasConfigurableValues returns whether there are configurable values set for this label. |
| 248 | func (la LabelAttribute) HasConfigurableValues() bool { |
| 249 | return len(la.ConfigurableValues) > 0 |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 252 | // SetValue sets the base, non-configured value for the Label |
| 253 | func (la *LabelAttribute) SetValue(value Label) { |
| 254 | la.SetSelectValue(NoConfigAxis, "", value) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 255 | } |
| 256 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 257 | // SetSelectValue set a value for a bazel select for the given axis, config and value. |
| 258 | func (la *LabelAttribute) SetSelectValue(axis ConfigurationAxis, config string, value Label) { |
| 259 | axis.validateConfig(config) |
| 260 | switch axis.configurationType { |
| 261 | case noConfig: |
| 262 | la.Value = &value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 263 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 264 | if la.ConfigurableValues == nil { |
| 265 | la.ConfigurableValues = make(configurableLabels) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 266 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 267 | la.ConfigurableValues.setValueForAxis(axis, config, &value) |
| 268 | default: |
| 269 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // SelectValue gets a value for a bazel select for the given axis and config. |
| 274 | func (la *LabelAttribute) SelectValue(axis ConfigurationAxis, config string) Label { |
| 275 | axis.validateConfig(config) |
| 276 | switch axis.configurationType { |
| 277 | case noConfig: |
| 278 | return *la.Value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 279 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 280 | return *la.ConfigurableValues[axis][config] |
| 281 | default: |
| 282 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order. |
| 287 | func (la *LabelAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 288 | keys := make([]ConfigurationAxis, 0, len(la.ConfigurableValues)) |
| 289 | for k := range la.ConfigurableValues { |
| 290 | keys = append(keys, k) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 293 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 294 | return keys |
| 295 | } |
| 296 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 297 | type configToBools map[string]bool |
| 298 | |
| 299 | func (ctb configToBools) setValue(config string, value *bool) { |
| 300 | if value == nil { |
| 301 | if _, ok := ctb[config]; ok { |
| 302 | delete(ctb, config) |
| 303 | } |
| 304 | return |
| 305 | } |
| 306 | ctb[config] = *value |
| 307 | } |
| 308 | |
| 309 | type configurableBools map[ConfigurationAxis]configToBools |
| 310 | |
| 311 | func (cb configurableBools) setValueForAxis(axis ConfigurationAxis, config string, value *bool) { |
| 312 | if cb[axis] == nil { |
| 313 | cb[axis] = make(configToBools) |
| 314 | } |
| 315 | cb[axis].setValue(config, value) |
| 316 | } |
| 317 | |
| 318 | // BoolAttribute represents an attribute whose value is a single bool but may be configurable.. |
| 319 | type BoolAttribute struct { |
| 320 | Value *bool |
| 321 | |
| 322 | ConfigurableValues configurableBools |
| 323 | } |
| 324 | |
| 325 | // HasConfigurableValues returns whether there are configurable values for this attribute. |
| 326 | func (ba BoolAttribute) HasConfigurableValues() bool { |
| 327 | return len(ba.ConfigurableValues) > 0 |
| 328 | } |
| 329 | |
| 330 | // SetSelectValue sets value for the given axis/config. |
| 331 | func (ba *BoolAttribute) SetSelectValue(axis ConfigurationAxis, config string, value *bool) { |
| 332 | axis.validateConfig(config) |
| 333 | switch axis.configurationType { |
| 334 | case noConfig: |
| 335 | ba.Value = value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 336 | case arch, os, osArch, productVariables: |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 337 | if ba.ConfigurableValues == nil { |
| 338 | ba.ConfigurableValues = make(configurableBools) |
| 339 | } |
| 340 | ba.ConfigurableValues.setValueForAxis(axis, config, value) |
| 341 | default: |
| 342 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // SelectValue gets the value for the given axis/config. |
| 347 | func (ba BoolAttribute) SelectValue(axis ConfigurationAxis, config string) *bool { |
| 348 | axis.validateConfig(config) |
| 349 | switch axis.configurationType { |
| 350 | case noConfig: |
| 351 | return ba.Value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 352 | case arch, os, osArch, productVariables: |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 353 | if v, ok := ba.ConfigurableValues[axis][config]; ok { |
| 354 | return &v |
| 355 | } else { |
| 356 | return nil |
| 357 | } |
| 358 | default: |
| 359 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order. |
| 364 | func (ba *BoolAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 365 | keys := make([]ConfigurationAxis, 0, len(ba.ConfigurableValues)) |
| 366 | for k := range ba.ConfigurableValues { |
| 367 | keys = append(keys, k) |
| 368 | } |
| 369 | |
| 370 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 371 | return keys |
| 372 | } |
| 373 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 374 | // labelListSelectValues supports config-specific label_list typed Bazel attribute values. |
| 375 | type labelListSelectValues map[string]LabelList |
| 376 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 377 | func (ll labelListSelectValues) addSelects(label labelSelectValues) { |
| 378 | for k, v := range label { |
| 379 | if label == nil { |
| 380 | continue |
| 381 | } |
| 382 | l := ll[k] |
| 383 | (&l).Add(v) |
| 384 | ll[k] = l |
| 385 | } |
| 386 | } |
| 387 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 388 | func (ll labelListSelectValues) appendSelects(other labelListSelectValues) { |
| 389 | for k, v := range other { |
| 390 | l := ll[k] |
| 391 | (&l).Append(v) |
| 392 | ll[k] = l |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // HasConfigurableValues returns whether there are configurable values within this set of selects. |
| 397 | func (ll labelListSelectValues) HasConfigurableValues() bool { |
| 398 | for _, v := range ll { |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 399 | if v.Includes != nil { |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 400 | return true |
| 401 | } |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 402 | } |
| 403 | return false |
| 404 | } |
| 405 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 406 | // LabelListAttribute is used to represent a list of Bazel labels as an |
| 407 | // attribute. |
| 408 | type LabelListAttribute struct { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 409 | // The non-configured attribute label list Value. Required. |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 410 | Value LabelList |
| 411 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 412 | // The configured attribute label list Values. Optional |
| 413 | // a map of independent configurability axes |
| 414 | ConfigurableValues configurableLabelLists |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 415 | |
| 416 | // If true, differentiate between "nil" and "empty" list. nil means that |
| 417 | // this attribute should not be specified at all, and "empty" means that |
| 418 | // the attribute should be explicitly specified as an empty list. |
| 419 | // This mode facilitates use of attribute defaults: an empty list should |
| 420 | // override the default. |
| 421 | ForceSpecifyEmptyList bool |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 422 | |
| 423 | // If true, signal the intent to the code generator to emit all select keys, |
| 424 | // even if the Includes list for that key is empty. This mode facilitates |
| 425 | // specific select statements where an empty list for a non-default select |
| 426 | // key has a meaning. |
| 427 | EmitEmptyList bool |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 428 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 429 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 430 | type configurableLabelLists map[ConfigurationAxis]labelListSelectValues |
| 431 | |
| 432 | func (cll configurableLabelLists) setValueForAxis(axis ConfigurationAxis, config string, list LabelList) { |
| 433 | if list.IsNil() { |
| 434 | if _, ok := cll[axis][config]; ok { |
| 435 | delete(cll[axis], config) |
| 436 | } |
| 437 | return |
| 438 | } |
| 439 | if cll[axis] == nil { |
| 440 | cll[axis] = make(labelListSelectValues) |
| 441 | } |
| 442 | |
| 443 | cll[axis][config] = list |
| 444 | } |
| 445 | |
| 446 | func (cll configurableLabelLists) Append(other configurableLabelLists) { |
| 447 | for axis, otherSelects := range other { |
| 448 | selects := cll[axis] |
| 449 | if selects == nil { |
| 450 | selects = make(labelListSelectValues, len(otherSelects)) |
| 451 | } |
| 452 | selects.appendSelects(otherSelects) |
| 453 | cll[axis] = selects |
| 454 | } |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value. |
| 458 | func MakeLabelListAttribute(value LabelList) LabelListAttribute { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 459 | return LabelListAttribute{ |
| 460 | Value: value, |
| 461 | ConfigurableValues: make(configurableLabelLists), |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | func (lla *LabelListAttribute) SetValue(list LabelList) { |
| 466 | lla.SetSelectValue(NoConfigAxis, "", list) |
| 467 | } |
| 468 | |
| 469 | // SetSelectValue set a value for a bazel select for the given axis, config and value. |
| 470 | func (lla *LabelListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list LabelList) { |
| 471 | axis.validateConfig(config) |
| 472 | switch axis.configurationType { |
| 473 | case noConfig: |
| 474 | lla.Value = list |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 475 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 476 | if lla.ConfigurableValues == nil { |
| 477 | lla.ConfigurableValues = make(configurableLabelLists) |
| 478 | } |
| 479 | lla.ConfigurableValues.setValueForAxis(axis, config, list) |
| 480 | default: |
| 481 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // SelectValue gets a value for a bazel select for the given axis and config. |
| 486 | func (lla *LabelListAttribute) SelectValue(axis ConfigurationAxis, config string) LabelList { |
| 487 | axis.validateConfig(config) |
| 488 | switch axis.configurationType { |
| 489 | case noConfig: |
| 490 | return lla.Value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 491 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 492 | return lla.ConfigurableValues[axis][config] |
| 493 | default: |
| 494 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order. |
| 499 | func (lla *LabelListAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 500 | keys := make([]ConfigurationAxis, 0, len(lla.ConfigurableValues)) |
| 501 | for k := range lla.ConfigurableValues { |
| 502 | keys = append(keys, k) |
| 503 | } |
| 504 | |
| 505 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 506 | return keys |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 507 | } |
| 508 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 509 | // Append all values, including os and arch specific ones, from another |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 510 | // LabelListAttribute to this LabelListAttribute. |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 511 | func (lla *LabelListAttribute) Append(other LabelListAttribute) { |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 512 | if lla.ForceSpecifyEmptyList && !other.Value.IsNil() { |
| 513 | lla.Value.Includes = []Label{} |
| 514 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 515 | lla.Value.Append(other.Value) |
| 516 | if lla.ConfigurableValues == nil { |
| 517 | lla.ConfigurableValues = make(configurableLabelLists) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 518 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 519 | lla.ConfigurableValues.Append(other.ConfigurableValues) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 520 | } |
| 521 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 522 | // Add inserts the labels for each axis of LabelAttribute at the end of corresponding axis's |
| 523 | // LabelList within the LabelListAttribute |
| 524 | func (lla *LabelListAttribute) Add(label *LabelAttribute) { |
| 525 | if label == nil { |
| 526 | return |
| 527 | } |
| 528 | |
| 529 | lla.Value.Add(label.Value) |
| 530 | if lla.ConfigurableValues == nil && label.ConfigurableValues != nil { |
| 531 | lla.ConfigurableValues = make(configurableLabelLists) |
| 532 | } |
| 533 | for axis, _ := range label.ConfigurableValues { |
| 534 | if _, exists := lla.ConfigurableValues[axis]; !exists { |
| 535 | lla.ConfigurableValues[axis] = make(labelListSelectValues) |
| 536 | } |
| 537 | lla.ConfigurableValues[axis].addSelects(label.ConfigurableValues[axis]) |
| 538 | } |
| 539 | } |
| 540 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 541 | // HasConfigurableValues returns true if the attribute contains axis-specific label list values. |
| 542 | func (lla LabelListAttribute) HasConfigurableValues() bool { |
| 543 | return len(lla.ConfigurableValues) > 0 |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 546 | // IsEmpty returns true if the attribute has no values under any configuration. |
| 547 | func (lla LabelListAttribute) IsEmpty() bool { |
| 548 | if len(lla.Value.Includes) > 0 { |
| 549 | return false |
| 550 | } |
| 551 | for axis, _ := range lla.ConfigurableValues { |
| 552 | if lla.ConfigurableValues[axis].HasConfigurableValues() { |
| 553 | return false |
| 554 | } |
| 555 | } |
| 556 | return true |
| 557 | } |
| 558 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 559 | // ResolveExcludes handles excludes across the various axes, ensuring that items are removed from |
| 560 | // the base value and included in default values as appropriate. |
| 561 | func (lla *LabelListAttribute) ResolveExcludes() { |
| 562 | for axis, configToLabels := range lla.ConfigurableValues { |
| 563 | baseLabels := lla.Value.deepCopy() |
| 564 | for config, val := range configToLabels { |
| 565 | // Exclude config-specific excludes from base value |
| 566 | lla.Value = SubtractBazelLabelList(lla.Value, LabelList{Includes: val.Excludes}) |
| 567 | |
| 568 | // add base values to config specific to add labels excluded by others in this axis |
| 569 | // then remove all config-specific excludes |
| 570 | allLabels := baseLabels.deepCopy() |
| 571 | allLabels.Append(val) |
| 572 | lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(allLabels, LabelList{Includes: val.Excludes}) |
| 573 | } |
| 574 | |
| 575 | // After going through all configs, delete the duplicates in the config |
| 576 | // values that are already in the base Value. |
| 577 | for config, val := range configToLabels { |
| 578 | lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(val, lla.Value) |
| 579 | } |
| 580 | |
Jingwen Chen | 9af49a4 | 2021-11-02 10:27:17 +0000 | [diff] [blame] | 581 | // Now that the Value list is finalized for this axis, compare it with |
| 582 | // the original list, and union the difference with the default |
| 583 | // condition for the axis. |
| 584 | difference := SubtractBazelLabelList(baseLabels, lla.Value) |
| 585 | existingDefaults := lla.ConfigurableValues[axis][ConditionsDefaultConfigKey] |
| 586 | existingDefaults.Append(difference) |
| 587 | lla.ConfigurableValues[axis][ConditionsDefaultConfigKey] = FirstUniqueBazelLabelList(existingDefaults) |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 588 | |
| 589 | // if everything ends up without includes, just delete the axis |
| 590 | if !lla.ConfigurableValues[axis].HasConfigurableValues() { |
| 591 | delete(lla.ConfigurableValues, axis) |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 596 | // OtherModuleContext is a limited context that has methods with information about other modules. |
| 597 | type OtherModuleContext interface { |
| 598 | ModuleFromName(name string) (blueprint.Module, bool) |
| 599 | OtherModuleType(m blueprint.Module) string |
| 600 | OtherModuleName(m blueprint.Module) string |
| 601 | OtherModuleDir(m blueprint.Module) string |
| 602 | ModuleErrorf(fmt string, args ...interface{}) |
| 603 | } |
| 604 | |
| 605 | // LabelMapper is a function that takes a OtherModuleContext and returns a (potentially changed) |
| 606 | // label and whether it was changed. |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 607 | type LabelMapper func(OtherModuleContext, Label) (string, bool) |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 608 | |
| 609 | // LabelPartition contains descriptions of a partition for labels |
| 610 | type LabelPartition struct { |
| 611 | // Extensions to include in this partition |
| 612 | Extensions []string |
| 613 | // LabelMapper is a function that can map a label to a new label, and indicate whether to include |
| 614 | // the mapped label in the partition |
| 615 | LabelMapper LabelMapper |
| 616 | // Whether to store files not included in any other partition in a group of LabelPartitions |
| 617 | // Only one partition in a group of LabelPartitions can enabled Keep_remainder |
| 618 | Keep_remainder bool |
| 619 | } |
| 620 | |
| 621 | // LabelPartitions is a map of partition name to a LabelPartition describing the elements of the |
| 622 | // partition |
| 623 | type LabelPartitions map[string]LabelPartition |
| 624 | |
| 625 | // filter returns a pointer to a label if the label should be included in the partition or nil if |
| 626 | // not. |
| 627 | func (lf LabelPartition) filter(ctx OtherModuleContext, label Label) *Label { |
| 628 | if lf.LabelMapper != nil { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 629 | if newLabel, changed := lf.LabelMapper(ctx, label); changed { |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 630 | return &Label{newLabel, label.OriginalModuleName} |
| 631 | } |
| 632 | } |
| 633 | for _, ext := range lf.Extensions { |
| 634 | if strings.HasSuffix(label.Label, ext) { |
| 635 | return &label |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | return nil |
| 640 | } |
| 641 | |
| 642 | // PartitionToLabelListAttribute is map of partition name to a LabelListAttribute |
| 643 | type PartitionToLabelListAttribute map[string]LabelListAttribute |
| 644 | |
| 645 | type partitionToLabelList map[string]*LabelList |
| 646 | |
| 647 | func (p partitionToLabelList) appendIncludes(partition string, label Label) { |
| 648 | if _, ok := p[partition]; !ok { |
| 649 | p[partition] = &LabelList{} |
| 650 | } |
| 651 | p[partition].Includes = append(p[partition].Includes, label) |
| 652 | } |
| 653 | |
| 654 | func (p partitionToLabelList) excludes(partition string, excludes []Label) { |
| 655 | if _, ok := p[partition]; !ok { |
| 656 | p[partition] = &LabelList{} |
| 657 | } |
| 658 | p[partition].Excludes = excludes |
| 659 | } |
| 660 | |
| 661 | // PartitionLabelListAttribute partitions a LabelListAttribute into the requested partitions |
| 662 | func PartitionLabelListAttribute(ctx OtherModuleContext, lla *LabelListAttribute, partitions LabelPartitions) PartitionToLabelListAttribute { |
| 663 | ret := PartitionToLabelListAttribute{} |
| 664 | var partitionNames []string |
| 665 | // Stored as a pointer to distinguish nil (no remainder partition) from empty string partition |
| 666 | var remainderPartition *string |
| 667 | for p, f := range partitions { |
| 668 | partitionNames = append(partitionNames, p) |
| 669 | if f.Keep_remainder { |
| 670 | if remainderPartition != nil { |
| 671 | panic("only one partition can store the remainder") |
| 672 | } |
| 673 | // If we take the address of p in a loop, we'll end up with the last value of p in |
| 674 | // remainderPartition, we want the requested partition |
| 675 | capturePartition := p |
| 676 | remainderPartition = &capturePartition |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | partitionLabelList := func(axis ConfigurationAxis, config string) { |
| 681 | value := lla.SelectValue(axis, config) |
| 682 | partitionToLabels := partitionToLabelList{} |
| 683 | for _, item := range value.Includes { |
| 684 | wasFiltered := false |
| 685 | var inPartition *string |
| 686 | for partition, f := range partitions { |
| 687 | filtered := f.filter(ctx, item) |
| 688 | if filtered == nil { |
| 689 | // did not match this filter, keep looking |
| 690 | continue |
| 691 | } |
| 692 | wasFiltered = true |
| 693 | partitionToLabels.appendIncludes(partition, *filtered) |
| 694 | // don't need to check other partitions if this filter used the item, |
| 695 | // continue checking if mapped to another name |
| 696 | if *filtered == item { |
| 697 | if inPartition != nil { |
| 698 | ctx.ModuleErrorf("%q was found in multiple partitions: %q, %q", item.Label, *inPartition, partition) |
| 699 | } |
| 700 | capturePartition := partition |
| 701 | inPartition = &capturePartition |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // if not specified in a partition, add to remainder partition if one exists |
| 706 | if !wasFiltered && remainderPartition != nil { |
| 707 | partitionToLabels.appendIncludes(*remainderPartition, item) |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | // ensure empty lists are maintained |
| 712 | if value.Excludes != nil { |
| 713 | for _, partition := range partitionNames { |
| 714 | partitionToLabels.excludes(partition, value.Excludes) |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | for partition, list := range partitionToLabels { |
| 719 | val := ret[partition] |
| 720 | (&val).SetSelectValue(axis, config, *list) |
| 721 | ret[partition] = val |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | partitionLabelList(NoConfigAxis, "") |
| 726 | for axis, configToList := range lla.ConfigurableValues { |
| 727 | for config, _ := range configToList { |
| 728 | partitionLabelList(axis, config) |
| 729 | } |
| 730 | } |
| 731 | return ret |
| 732 | } |
| 733 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 734 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 735 | // support for additional metadata, like configurations. |
| 736 | type StringListAttribute struct { |
| 737 | // The base value of the string list attribute. |
| 738 | Value []string |
| 739 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 740 | // The configured attribute label list Values. Optional |
| 741 | // a map of independent configurability axes |
| 742 | ConfigurableValues configurableStringLists |
| 743 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 744 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 745 | type configurableStringLists map[ConfigurationAxis]stringListSelectValues |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 746 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 747 | func (csl configurableStringLists) Append(other configurableStringLists) { |
| 748 | for axis, otherSelects := range other { |
| 749 | selects := csl[axis] |
| 750 | if selects == nil { |
| 751 | selects = make(stringListSelectValues, len(otherSelects)) |
| 752 | } |
| 753 | selects.appendSelects(otherSelects) |
| 754 | csl[axis] = selects |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | func (csl configurableStringLists) setValueForAxis(axis ConfigurationAxis, config string, list []string) { |
| 759 | if csl[axis] == nil { |
| 760 | csl[axis] = make(stringListSelectValues) |
| 761 | } |
| 762 | csl[axis][config] = list |
| 763 | } |
| 764 | |
| 765 | type stringListSelectValues map[string][]string |
| 766 | |
| 767 | func (sl stringListSelectValues) appendSelects(other stringListSelectValues) { |
| 768 | for k, v := range other { |
| 769 | sl[k] = append(sl[k], v...) |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | func (sl stringListSelectValues) hasConfigurableValues(other stringListSelectValues) bool { |
| 774 | for _, val := range sl { |
| 775 | if len(val) > 0 { |
| 776 | return true |
| 777 | } |
| 778 | } |
| 779 | return false |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 780 | } |
| 781 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 782 | // MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value. |
| 783 | func MakeStringListAttribute(value []string) StringListAttribute { |
| 784 | // NOTE: These strings are not necessarily unique or sorted. |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 785 | return StringListAttribute{ |
| 786 | Value: value, |
| 787 | ConfigurableValues: make(configurableStringLists), |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 791 | // HasConfigurableValues returns true if the attribute contains axis-specific string_list values. |
| 792 | func (sla StringListAttribute) HasConfigurableValues() bool { |
| 793 | return len(sla.ConfigurableValues) > 0 |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 794 | } |
| 795 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 796 | // Append appends all values, including os and arch specific ones, from another |
| 797 | // StringListAttribute to this StringListAttribute |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 798 | func (sla *StringListAttribute) Append(other StringListAttribute) { |
| 799 | sla.Value = append(sla.Value, other.Value...) |
| 800 | if sla.ConfigurableValues == nil { |
| 801 | sla.ConfigurableValues = make(configurableStringLists) |
| 802 | } |
| 803 | sla.ConfigurableValues.Append(other.ConfigurableValues) |
| 804 | } |
| 805 | |
| 806 | // SetSelectValue set a value for a bazel select for the given axis, config and value. |
| 807 | func (sla *StringListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list []string) { |
| 808 | axis.validateConfig(config) |
| 809 | switch axis.configurationType { |
| 810 | case noConfig: |
| 811 | sla.Value = list |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 812 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 813 | if sla.ConfigurableValues == nil { |
| 814 | sla.ConfigurableValues = make(configurableStringLists) |
| 815 | } |
| 816 | sla.ConfigurableValues.setValueForAxis(axis, config, list) |
| 817 | default: |
| 818 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // SelectValue gets a value for a bazel select for the given axis and config. |
| 823 | func (sla *StringListAttribute) SelectValue(axis ConfigurationAxis, config string) []string { |
| 824 | axis.validateConfig(config) |
| 825 | switch axis.configurationType { |
| 826 | case noConfig: |
| 827 | return sla.Value |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 828 | case arch, os, osArch, productVariables: |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 829 | return sla.ConfigurableValues[axis][config] |
| 830 | default: |
| 831 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order. |
| 836 | func (sla *StringListAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 837 | keys := make([]ConfigurationAxis, 0, len(sla.ConfigurableValues)) |
| 838 | for k := range sla.ConfigurableValues { |
| 839 | keys = append(keys, k) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 842 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 843 | return keys |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 846 | // DeduplicateAxesFromBase ensures no duplication of items between the no-configuration value and |
| 847 | // configuration-specific values. For example, if we would convert this StringListAttribute as: |
| 848 | // ["a", "b", "c"] + select({ |
| 849 | // "//condition:one": ["a", "d"], |
| 850 | // "//conditions:default": [], |
| 851 | // }) |
| 852 | // after this function, we would convert this StringListAttribute as: |
| 853 | // ["a", "b", "c"] + select({ |
| 854 | // "//condition:one": ["d"], |
| 855 | // "//conditions:default": [], |
| 856 | // }) |
| 857 | func (sla *StringListAttribute) DeduplicateAxesFromBase() { |
| 858 | base := sla.Value |
| 859 | for axis, configToList := range sla.ConfigurableValues { |
| 860 | for config, list := range configToList { |
| 861 | remaining := SubtractStrings(list, base) |
| 862 | if len(remaining) == 0 { |
| 863 | delete(sla.ConfigurableValues[axis], config) |
| 864 | } else { |
| 865 | sla.ConfigurableValues[axis][config] = remaining |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 871 | // TryVariableSubstitution, replace string substitution formatting within each string in slice with |
| 872 | // Starlark string.format compatible tag for productVariable. |
| 873 | func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) { |
| 874 | ret := make([]string, 0, len(slice)) |
| 875 | changesMade := false |
| 876 | for _, s := range slice { |
| 877 | newS, changed := TryVariableSubstitution(s, productVariable) |
| 878 | ret = append(ret, newS) |
| 879 | changesMade = changesMade || changed |
| 880 | } |
| 881 | return ret, changesMade |
| 882 | } |
| 883 | |
| 884 | // TryVariableSubstitution, replace string substitution formatting within s with Starlark |
| 885 | // string.format compatible tag for productVariable. |
| 886 | func TryVariableSubstitution(s string, productVariable string) (string, bool) { |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 887 | sub := productVariableSubstitutionPattern.ReplaceAllString(s, "$("+productVariable+")") |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 888 | return sub, s != sub |
| 889 | } |