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" |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 22 | "strings" |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 23 | ) |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 24 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 25 | // BazelTargetModuleProperties contain properties and metadata used for |
| 26 | // Blueprint to BUILD file conversion. |
| 27 | type BazelTargetModuleProperties struct { |
| 28 | // The Bazel rule class for this target. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 29 | Rule_class string `blueprint:"mutated"` |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 30 | |
| 31 | // 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] | 32 | Bzl_load_location string `blueprint:"mutated"` |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 33 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 34 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 35 | const BazelTargetModuleNamePrefix = "__bp2build__" |
| 36 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 37 | func StripNamePrefix(moduleName string) string { |
| 38 | return strings.TrimPrefix(moduleName, BazelTargetModuleNamePrefix) |
| 39 | } |
| 40 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 41 | var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)") |
| 42 | |
Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame] | 43 | // Label is used to represent a Bazel compatible Label. Also stores the original |
| 44 | // bp text to support string replacement. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 45 | type Label struct { |
Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame] | 46 | // The string representation of a Bazel target label. This can be a relative |
| 47 | // or fully qualified label. These labels are used for generating BUILD |
| 48 | // files with bp2build. |
| 49 | Label string |
| 50 | |
| 51 | // The original Soong/Blueprint module name that the label was derived from. |
| 52 | // This is used for replacing references to the original name with the new |
| 53 | // label, for example in genrule cmds. |
| 54 | // |
| 55 | // While there is a reversible 1:1 mapping from the module name to Bazel |
| 56 | // label with bp2build that could make computing the original module name |
| 57 | // from the label automatic, it is not the case for handcrafted targets, |
| 58 | // where modules can have a custom label mapping through the { bazel_module: |
| 59 | // { label: <label> } } property. |
| 60 | // |
| 61 | // With handcrafted labels, those modules don't go through bp2build |
| 62 | // conversion, but relies on handcrafted targets in the source tree. |
| 63 | OriginalModuleName string |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // LabelList is used to represent a list of Bazel labels. |
| 67 | type LabelList struct { |
| 68 | Includes []Label |
| 69 | Excludes []Label |
| 70 | } |
| 71 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 72 | func (ll *LabelList) IsNil() bool { |
| 73 | return ll.Includes == nil && ll.Excludes == nil |
| 74 | } |
| 75 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 76 | func (ll *LabelList) deepCopy() LabelList { |
| 77 | return LabelList{ |
| 78 | Includes: ll.Includes[:], |
| 79 | Excludes: ll.Excludes[:], |
| 80 | } |
| 81 | } |
| 82 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 83 | // uniqueParentDirectories returns a list of the unique parent directories for |
| 84 | // all files in ll.Includes. |
| 85 | func (ll *LabelList) uniqueParentDirectories() []string { |
| 86 | dirMap := map[string]bool{} |
| 87 | for _, label := range ll.Includes { |
| 88 | dirMap[filepath.Dir(label.Label)] = true |
| 89 | } |
| 90 | dirs := []string{} |
| 91 | for dir := range dirMap { |
| 92 | dirs = append(dirs, dir) |
| 93 | } |
| 94 | return dirs |
| 95 | } |
| 96 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 97 | // Append appends the fields of other labelList to the corresponding fields of ll. |
| 98 | func (ll *LabelList) Append(other LabelList) { |
| 99 | if len(ll.Includes) > 0 || len(other.Includes) > 0 { |
| 100 | ll.Includes = append(ll.Includes, other.Includes...) |
| 101 | } |
| 102 | if len(ll.Excludes) > 0 || len(other.Excludes) > 0 { |
| 103 | ll.Excludes = append(other.Excludes, other.Excludes...) |
| 104 | } |
| 105 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 106 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 107 | // UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns |
| 108 | // the slice in a sorted order. |
| 109 | func UniqueSortedBazelLabels(originalLabels []Label) []Label { |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 110 | uniqueLabelsSet := make(map[Label]bool) |
| 111 | for _, l := range originalLabels { |
| 112 | uniqueLabelsSet[l] = true |
| 113 | } |
| 114 | var uniqueLabels []Label |
| 115 | for l, _ := range uniqueLabelsSet { |
| 116 | uniqueLabels = append(uniqueLabels, l) |
| 117 | } |
| 118 | sort.SliceStable(uniqueLabels, func(i, j int) bool { |
| 119 | return uniqueLabels[i].Label < uniqueLabels[j].Label |
| 120 | }) |
| 121 | return uniqueLabels |
| 122 | } |
| 123 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 124 | func FirstUniqueBazelLabels(originalLabels []Label) []Label { |
| 125 | var labels []Label |
| 126 | found := make(map[Label]bool, len(originalLabels)) |
| 127 | for _, l := range originalLabels { |
| 128 | if _, ok := found[l]; ok { |
| 129 | continue |
| 130 | } |
| 131 | labels = append(labels, l) |
| 132 | found[l] = true |
| 133 | } |
| 134 | return labels |
| 135 | } |
| 136 | |
| 137 | func FirstUniqueBazelLabelList(originalLabelList LabelList) LabelList { |
| 138 | var uniqueLabelList LabelList |
| 139 | uniqueLabelList.Includes = FirstUniqueBazelLabels(originalLabelList.Includes) |
| 140 | uniqueLabelList.Excludes = FirstUniqueBazelLabels(originalLabelList.Excludes) |
| 141 | return uniqueLabelList |
| 142 | } |
| 143 | |
| 144 | func UniqueSortedBazelLabelList(originalLabelList LabelList) LabelList { |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 145 | var uniqueLabelList LabelList |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 146 | uniqueLabelList.Includes = UniqueSortedBazelLabels(originalLabelList.Includes) |
| 147 | uniqueLabelList.Excludes = UniqueSortedBazelLabels(originalLabelList.Excludes) |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 148 | return uniqueLabelList |
| 149 | } |
| 150 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 151 | // Subtract needle from haystack |
| 152 | func SubtractStrings(haystack []string, needle []string) []string { |
| 153 | // This is really a set |
| 154 | remainder := make(map[string]bool) |
| 155 | |
| 156 | for _, s := range haystack { |
| 157 | remainder[s] = true |
| 158 | } |
| 159 | for _, s := range needle { |
| 160 | delete(remainder, s) |
| 161 | } |
| 162 | |
| 163 | var strings []string |
| 164 | for s, _ := range remainder { |
| 165 | strings = append(strings, s) |
| 166 | } |
| 167 | |
| 168 | sort.SliceStable(strings, func(i, j int) bool { |
| 169 | return strings[i] < strings[j] |
| 170 | }) |
| 171 | |
| 172 | return strings |
| 173 | } |
| 174 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 175 | // Map a function over all labels in a LabelList. |
| 176 | func MapLabelList(mapOver LabelList, mapFn func(string) string) LabelList { |
| 177 | var includes []Label |
| 178 | for _, inc := range mapOver.Includes { |
| 179 | mappedLabel := Label{Label: mapFn(inc.Label), OriginalModuleName: inc.OriginalModuleName} |
| 180 | includes = append(includes, mappedLabel) |
| 181 | } |
| 182 | // mapFn is not applied over excludes, but they are propagated as-is. |
| 183 | return LabelList{Includes: includes, Excludes: mapOver.Excludes} |
| 184 | } |
| 185 | |
| 186 | // Map a function over all Labels in a LabelListAttribute |
| 187 | func MapLabelListAttribute(mapOver LabelListAttribute, mapFn func(string) string) LabelListAttribute { |
| 188 | var result LabelListAttribute |
| 189 | |
| 190 | result.Value = MapLabelList(mapOver.Value, mapFn) |
| 191 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 192 | for axis, configToLabels := range mapOver.ConfigurableValues { |
| 193 | for config, value := range configToLabels { |
| 194 | result.SetSelectValue(axis, config, MapLabelList(value, mapFn)) |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | return result |
| 199 | } |
| 200 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 201 | // Return all needles in a given haystack, where needleFn is true for needles. |
| 202 | func FilterLabelList(haystack LabelList, needleFn func(string) bool) LabelList { |
| 203 | var includes []Label |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 204 | for _, inc := range haystack.Includes { |
| 205 | if needleFn(inc.Label) { |
| 206 | includes = append(includes, inc) |
| 207 | } |
| 208 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 209 | // needleFn is not applied over excludes, but they are propagated as-is. |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 210 | return LabelList{Includes: includes, Excludes: haystack.Excludes} |
| 211 | } |
| 212 | |
| 213 | // Return all needles in a given haystack, where needleFn is true for needles. |
| 214 | func FilterLabelListAttribute(haystack LabelListAttribute, needleFn func(string) bool) LabelListAttribute { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 215 | result := MakeLabelListAttribute(FilterLabelList(haystack.Value, needleFn)) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 216 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 217 | for config, selects := range haystack.ConfigurableValues { |
| 218 | newSelects := make(labelListSelectValues, len(selects)) |
| 219 | for k, v := range selects { |
| 220 | newSelects[k] = FilterLabelList(v, needleFn) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 221 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 222 | result.ConfigurableValues[config] = newSelects |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return result |
| 226 | } |
| 227 | |
| 228 | // Subtract needle from haystack |
| 229 | func SubtractBazelLabelListAttribute(haystack LabelListAttribute, needle LabelListAttribute) LabelListAttribute { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 230 | result := MakeLabelListAttribute(SubtractBazelLabelList(haystack.Value, needle.Value)) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 231 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 232 | for config, selects := range haystack.ConfigurableValues { |
| 233 | newSelects := make(labelListSelectValues, len(selects)) |
| 234 | needleSelects := needle.ConfigurableValues[config] |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 235 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 236 | for k, v := range selects { |
| 237 | newSelects[k] = SubtractBazelLabelList(v, needleSelects[k]) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 238 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 239 | result.ConfigurableValues[config] = newSelects |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 242 | return result |
| 243 | } |
| 244 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 245 | // Subtract needle from haystack |
| 246 | func SubtractBazelLabels(haystack []Label, needle []Label) []Label { |
| 247 | // This is really a set |
| 248 | remainder := make(map[Label]bool) |
| 249 | |
| 250 | for _, label := range haystack { |
| 251 | remainder[label] = true |
| 252 | } |
| 253 | for _, label := range needle { |
| 254 | delete(remainder, label) |
| 255 | } |
| 256 | |
| 257 | var labels []Label |
| 258 | for label, _ := range remainder { |
| 259 | labels = append(labels, label) |
| 260 | } |
| 261 | |
| 262 | sort.SliceStable(labels, func(i, j int) bool { |
| 263 | return labels[i].Label < labels[j].Label |
| 264 | }) |
| 265 | |
| 266 | return labels |
| 267 | } |
| 268 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 269 | // Appends two LabelLists, returning the combined list. |
| 270 | func AppendBazelLabelLists(a LabelList, b LabelList) LabelList { |
| 271 | var result LabelList |
| 272 | result.Includes = append(a.Includes, b.Includes...) |
| 273 | result.Excludes = append(a.Excludes, b.Excludes...) |
| 274 | return result |
| 275 | } |
| 276 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 277 | // Subtract needle from haystack |
| 278 | func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList { |
| 279 | var result LabelList |
| 280 | result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes) |
| 281 | // NOTE: Excludes are intentionally not subtracted |
| 282 | result.Excludes = haystack.Excludes |
| 283 | return result |
| 284 | } |
| 285 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 286 | type Attribute interface { |
| 287 | HasConfigurableValues() bool |
| 288 | } |
| 289 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 290 | type labelSelectValues map[string]*Label |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 291 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 292 | type configurableLabels map[ConfigurationAxis]labelSelectValues |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 293 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 294 | func (cl configurableLabels) setValueForAxis(axis ConfigurationAxis, config string, value *Label) { |
| 295 | if cl[axis] == nil { |
| 296 | cl[axis] = make(labelSelectValues) |
| 297 | } |
| 298 | cl[axis][config] = value |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // Represents an attribute whose value is a single label |
| 302 | type LabelAttribute struct { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 303 | Value *Label |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 304 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 305 | ConfigurableValues configurableLabels |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 308 | // HasConfigurableValues returns whether there are configurable values set for this label. |
| 309 | func (la LabelAttribute) HasConfigurableValues() bool { |
| 310 | return len(la.ConfigurableValues) > 0 |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 313 | // SetValue sets the base, non-configured value for the Label |
| 314 | func (la *LabelAttribute) SetValue(value Label) { |
| 315 | la.SetSelectValue(NoConfigAxis, "", value) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 318 | // SetSelectValue set a value for a bazel select for the given axis, config and value. |
| 319 | func (la *LabelAttribute) SetSelectValue(axis ConfigurationAxis, config string, value Label) { |
| 320 | axis.validateConfig(config) |
| 321 | switch axis.configurationType { |
| 322 | case noConfig: |
| 323 | la.Value = &value |
| 324 | case arch, os, osArch, productVariables: |
| 325 | if la.ConfigurableValues == nil { |
| 326 | la.ConfigurableValues = make(configurableLabels) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 327 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 328 | la.ConfigurableValues.setValueForAxis(axis, config, &value) |
| 329 | default: |
| 330 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // SelectValue gets a value for a bazel select for the given axis and config. |
| 335 | func (la *LabelAttribute) SelectValue(axis ConfigurationAxis, config string) Label { |
| 336 | axis.validateConfig(config) |
| 337 | switch axis.configurationType { |
| 338 | case noConfig: |
| 339 | return *la.Value |
| 340 | case arch, os, osArch, productVariables: |
| 341 | return *la.ConfigurableValues[axis][config] |
| 342 | default: |
| 343 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order. |
| 348 | func (la *LabelAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 349 | keys := make([]ConfigurationAxis, 0, len(la.ConfigurableValues)) |
| 350 | for k := range la.ConfigurableValues { |
| 351 | keys = append(keys, k) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 354 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 355 | return keys |
| 356 | } |
| 357 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 358 | type configToBools map[string]bool |
| 359 | |
| 360 | func (ctb configToBools) setValue(config string, value *bool) { |
| 361 | if value == nil { |
| 362 | if _, ok := ctb[config]; ok { |
| 363 | delete(ctb, config) |
| 364 | } |
| 365 | return |
| 366 | } |
| 367 | ctb[config] = *value |
| 368 | } |
| 369 | |
| 370 | type configurableBools map[ConfigurationAxis]configToBools |
| 371 | |
| 372 | func (cb configurableBools) setValueForAxis(axis ConfigurationAxis, config string, value *bool) { |
| 373 | if cb[axis] == nil { |
| 374 | cb[axis] = make(configToBools) |
| 375 | } |
| 376 | cb[axis].setValue(config, value) |
| 377 | } |
| 378 | |
| 379 | // BoolAttribute represents an attribute whose value is a single bool but may be configurable.. |
| 380 | type BoolAttribute struct { |
| 381 | Value *bool |
| 382 | |
| 383 | ConfigurableValues configurableBools |
| 384 | } |
| 385 | |
| 386 | // HasConfigurableValues returns whether there are configurable values for this attribute. |
| 387 | func (ba BoolAttribute) HasConfigurableValues() bool { |
| 388 | return len(ba.ConfigurableValues) > 0 |
| 389 | } |
| 390 | |
| 391 | // SetSelectValue sets value for the given axis/config. |
| 392 | func (ba *BoolAttribute) SetSelectValue(axis ConfigurationAxis, config string, value *bool) { |
| 393 | axis.validateConfig(config) |
| 394 | switch axis.configurationType { |
| 395 | case noConfig: |
| 396 | ba.Value = value |
| 397 | case arch, os, osArch, productVariables: |
| 398 | if ba.ConfigurableValues == nil { |
| 399 | ba.ConfigurableValues = make(configurableBools) |
| 400 | } |
| 401 | ba.ConfigurableValues.setValueForAxis(axis, config, value) |
| 402 | default: |
| 403 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // SelectValue gets the value for the given axis/config. |
| 408 | func (ba BoolAttribute) SelectValue(axis ConfigurationAxis, config string) *bool { |
| 409 | axis.validateConfig(config) |
| 410 | switch axis.configurationType { |
| 411 | case noConfig: |
| 412 | return ba.Value |
| 413 | case arch, os, osArch, productVariables: |
| 414 | if v, ok := ba.ConfigurableValues[axis][config]; ok { |
| 415 | return &v |
| 416 | } else { |
| 417 | return nil |
| 418 | } |
| 419 | default: |
| 420 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order. |
| 425 | func (ba *BoolAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 426 | keys := make([]ConfigurationAxis, 0, len(ba.ConfigurableValues)) |
| 427 | for k := range ba.ConfigurableValues { |
| 428 | keys = append(keys, k) |
| 429 | } |
| 430 | |
| 431 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 432 | return keys |
| 433 | } |
| 434 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 435 | // labelListSelectValues supports config-specific label_list typed Bazel attribute values. |
| 436 | type labelListSelectValues map[string]LabelList |
| 437 | |
| 438 | func (ll labelListSelectValues) appendSelects(other labelListSelectValues) { |
| 439 | for k, v := range other { |
| 440 | l := ll[k] |
| 441 | (&l).Append(v) |
| 442 | ll[k] = l |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // HasConfigurableValues returns whether there are configurable values within this set of selects. |
| 447 | func (ll labelListSelectValues) HasConfigurableValues() bool { |
| 448 | for _, v := range ll { |
| 449 | if len(v.Includes) > 0 { |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 450 | return true |
| 451 | } |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 452 | } |
| 453 | return false |
| 454 | } |
| 455 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 456 | // LabelListAttribute is used to represent a list of Bazel labels as an |
| 457 | // attribute. |
| 458 | type LabelListAttribute struct { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 459 | // The non-configured attribute label list Value. Required. |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 460 | Value LabelList |
| 461 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 462 | // The configured attribute label list Values. Optional |
| 463 | // a map of independent configurability axes |
| 464 | ConfigurableValues configurableLabelLists |
| 465 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 466 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 467 | type configurableLabelLists map[ConfigurationAxis]labelListSelectValues |
| 468 | |
| 469 | func (cll configurableLabelLists) setValueForAxis(axis ConfigurationAxis, config string, list LabelList) { |
| 470 | if list.IsNil() { |
| 471 | if _, ok := cll[axis][config]; ok { |
| 472 | delete(cll[axis], config) |
| 473 | } |
| 474 | return |
| 475 | } |
| 476 | if cll[axis] == nil { |
| 477 | cll[axis] = make(labelListSelectValues) |
| 478 | } |
| 479 | |
| 480 | cll[axis][config] = list |
| 481 | } |
| 482 | |
| 483 | func (cll configurableLabelLists) Append(other configurableLabelLists) { |
| 484 | for axis, otherSelects := range other { |
| 485 | selects := cll[axis] |
| 486 | if selects == nil { |
| 487 | selects = make(labelListSelectValues, len(otherSelects)) |
| 488 | } |
| 489 | selects.appendSelects(otherSelects) |
| 490 | cll[axis] = selects |
| 491 | } |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value. |
| 495 | func MakeLabelListAttribute(value LabelList) LabelListAttribute { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 496 | return LabelListAttribute{ |
| 497 | Value: value, |
| 498 | ConfigurableValues: make(configurableLabelLists), |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | func (lla *LabelListAttribute) SetValue(list LabelList) { |
| 503 | lla.SetSelectValue(NoConfigAxis, "", list) |
| 504 | } |
| 505 | |
| 506 | // SetSelectValue set a value for a bazel select for the given axis, config and value. |
| 507 | func (lla *LabelListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list LabelList) { |
| 508 | axis.validateConfig(config) |
| 509 | switch axis.configurationType { |
| 510 | case noConfig: |
| 511 | lla.Value = list |
| 512 | case arch, os, osArch, productVariables: |
| 513 | if lla.ConfigurableValues == nil { |
| 514 | lla.ConfigurableValues = make(configurableLabelLists) |
| 515 | } |
| 516 | lla.ConfigurableValues.setValueForAxis(axis, config, list) |
| 517 | default: |
| 518 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // SelectValue gets a value for a bazel select for the given axis and config. |
| 523 | func (lla *LabelListAttribute) SelectValue(axis ConfigurationAxis, config string) LabelList { |
| 524 | axis.validateConfig(config) |
| 525 | switch axis.configurationType { |
| 526 | case noConfig: |
| 527 | return lla.Value |
| 528 | case arch, os, osArch, productVariables: |
| 529 | return lla.ConfigurableValues[axis][config] |
| 530 | default: |
| 531 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order. |
| 536 | func (lla *LabelListAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 537 | keys := make([]ConfigurationAxis, 0, len(lla.ConfigurableValues)) |
| 538 | for k := range lla.ConfigurableValues { |
| 539 | keys = append(keys, k) |
| 540 | } |
| 541 | |
| 542 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 543 | return keys |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 546 | // Append all values, including os and arch specific ones, from another |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 547 | // LabelListAttribute to this LabelListAttribute. |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 548 | func (lla *LabelListAttribute) Append(other LabelListAttribute) { |
| 549 | lla.Value.Append(other.Value) |
| 550 | if lla.ConfigurableValues == nil { |
| 551 | lla.ConfigurableValues = make(configurableLabelLists) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 552 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 553 | lla.ConfigurableValues.Append(other.ConfigurableValues) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 554 | } |
| 555 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 556 | // HasConfigurableValues returns true if the attribute contains axis-specific label list values. |
| 557 | func (lla LabelListAttribute) HasConfigurableValues() bool { |
| 558 | return len(lla.ConfigurableValues) > 0 |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 559 | } |
| 560 | |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 561 | // IsEmpty returns true if the attribute has no values under any configuration. |
| 562 | func (lla LabelListAttribute) IsEmpty() bool { |
| 563 | if len(lla.Value.Includes) > 0 { |
| 564 | return false |
| 565 | } |
| 566 | for axis, _ := range lla.ConfigurableValues { |
| 567 | if lla.ConfigurableValues[axis].HasConfigurableValues() { |
| 568 | return false |
| 569 | } |
| 570 | } |
| 571 | return true |
| 572 | } |
| 573 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 574 | // ResolveExcludes handles excludes across the various axes, ensuring that items are removed from |
| 575 | // the base value and included in default values as appropriate. |
| 576 | func (lla *LabelListAttribute) ResolveExcludes() { |
| 577 | for axis, configToLabels := range lla.ConfigurableValues { |
| 578 | baseLabels := lla.Value.deepCopy() |
| 579 | for config, val := range configToLabels { |
| 580 | // Exclude config-specific excludes from base value |
| 581 | lla.Value = SubtractBazelLabelList(lla.Value, LabelList{Includes: val.Excludes}) |
| 582 | |
| 583 | // add base values to config specific to add labels excluded by others in this axis |
| 584 | // then remove all config-specific excludes |
| 585 | allLabels := baseLabels.deepCopy() |
| 586 | allLabels.Append(val) |
| 587 | lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(allLabels, LabelList{Includes: val.Excludes}) |
| 588 | } |
| 589 | |
| 590 | // After going through all configs, delete the duplicates in the config |
| 591 | // values that are already in the base Value. |
| 592 | for config, val := range configToLabels { |
| 593 | lla.ConfigurableValues[axis][config] = SubtractBazelLabelList(val, lla.Value) |
| 594 | } |
| 595 | |
| 596 | // Now that the Value list is finalized for this axis, compare it with the original |
| 597 | // list, and put the difference into the default condition for the axis. |
| 598 | lla.ConfigurableValues[axis][conditionsDefault] = SubtractBazelLabelList(baseLabels, lla.Value) |
| 599 | |
| 600 | // if everything ends up without includes, just delete the axis |
| 601 | if !lla.ConfigurableValues[axis].HasConfigurableValues() { |
| 602 | delete(lla.ConfigurableValues, axis) |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 607 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 608 | // support for additional metadata, like configurations. |
| 609 | type StringListAttribute struct { |
| 610 | // The base value of the string list attribute. |
| 611 | Value []string |
| 612 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 613 | // The configured attribute label list Values. Optional |
| 614 | // a map of independent configurability axes |
| 615 | ConfigurableValues configurableStringLists |
| 616 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 617 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 618 | type configurableStringLists map[ConfigurationAxis]stringListSelectValues |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 619 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 620 | func (csl configurableStringLists) Append(other configurableStringLists) { |
| 621 | for axis, otherSelects := range other { |
| 622 | selects := csl[axis] |
| 623 | if selects == nil { |
| 624 | selects = make(stringListSelectValues, len(otherSelects)) |
| 625 | } |
| 626 | selects.appendSelects(otherSelects) |
| 627 | csl[axis] = selects |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | func (csl configurableStringLists) setValueForAxis(axis ConfigurationAxis, config string, list []string) { |
| 632 | if csl[axis] == nil { |
| 633 | csl[axis] = make(stringListSelectValues) |
| 634 | } |
| 635 | csl[axis][config] = list |
| 636 | } |
| 637 | |
| 638 | type stringListSelectValues map[string][]string |
| 639 | |
| 640 | func (sl stringListSelectValues) appendSelects(other stringListSelectValues) { |
| 641 | for k, v := range other { |
| 642 | sl[k] = append(sl[k], v...) |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | func (sl stringListSelectValues) hasConfigurableValues(other stringListSelectValues) bool { |
| 647 | for _, val := range sl { |
| 648 | if len(val) > 0 { |
| 649 | return true |
| 650 | } |
| 651 | } |
| 652 | return false |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 653 | } |
| 654 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 655 | // MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value. |
| 656 | func MakeStringListAttribute(value []string) StringListAttribute { |
| 657 | // NOTE: These strings are not necessarily unique or sorted. |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 658 | return StringListAttribute{ |
| 659 | Value: value, |
| 660 | ConfigurableValues: make(configurableStringLists), |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 664 | // HasConfigurableValues returns true if the attribute contains axis-specific string_list values. |
| 665 | func (sla StringListAttribute) HasConfigurableValues() bool { |
| 666 | return len(sla.ConfigurableValues) > 0 |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 667 | } |
| 668 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 669 | // Append appends all values, including os and arch specific ones, from another |
| 670 | // StringListAttribute to this StringListAttribute |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 671 | func (sla *StringListAttribute) Append(other StringListAttribute) { |
| 672 | sla.Value = append(sla.Value, other.Value...) |
| 673 | if sla.ConfigurableValues == nil { |
| 674 | sla.ConfigurableValues = make(configurableStringLists) |
| 675 | } |
| 676 | sla.ConfigurableValues.Append(other.ConfigurableValues) |
| 677 | } |
| 678 | |
| 679 | // SetSelectValue set a value for a bazel select for the given axis, config and value. |
| 680 | func (sla *StringListAttribute) SetSelectValue(axis ConfigurationAxis, config string, list []string) { |
| 681 | axis.validateConfig(config) |
| 682 | switch axis.configurationType { |
| 683 | case noConfig: |
| 684 | sla.Value = list |
| 685 | case arch, os, osArch, productVariables: |
| 686 | if sla.ConfigurableValues == nil { |
| 687 | sla.ConfigurableValues = make(configurableStringLists) |
| 688 | } |
| 689 | sla.ConfigurableValues.setValueForAxis(axis, config, list) |
| 690 | default: |
| 691 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | // SelectValue gets a value for a bazel select for the given axis and config. |
| 696 | func (sla *StringListAttribute) SelectValue(axis ConfigurationAxis, config string) []string { |
| 697 | axis.validateConfig(config) |
| 698 | switch axis.configurationType { |
| 699 | case noConfig: |
| 700 | return sla.Value |
| 701 | case arch, os, osArch, productVariables: |
| 702 | return sla.ConfigurableValues[axis][config] |
| 703 | default: |
| 704 | panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis)) |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | // SortedConfigurationAxes returns all the used ConfigurationAxis in sorted order. |
| 709 | func (sla *StringListAttribute) SortedConfigurationAxes() []ConfigurationAxis { |
| 710 | keys := make([]ConfigurationAxis, 0, len(sla.ConfigurableValues)) |
| 711 | for k := range sla.ConfigurableValues { |
| 712 | keys = append(keys, k) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 715 | sort.Slice(keys, func(i, j int) bool { return keys[i].less(keys[j]) }) |
| 716 | return keys |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 719 | // TryVariableSubstitution, replace string substitution formatting within each string in slice with |
| 720 | // Starlark string.format compatible tag for productVariable. |
| 721 | func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) { |
| 722 | ret := make([]string, 0, len(slice)) |
| 723 | changesMade := false |
| 724 | for _, s := range slice { |
| 725 | newS, changed := TryVariableSubstitution(s, productVariable) |
| 726 | ret = append(ret, newS) |
| 727 | changesMade = changesMade || changed |
| 728 | } |
| 729 | return ret, changesMade |
| 730 | } |
| 731 | |
| 732 | // TryVariableSubstitution, replace string substitution formatting within s with Starlark |
| 733 | // string.format compatible tag for productVariable. |
| 734 | func TryVariableSubstitution(s string, productVariable string) (string, bool) { |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 735 | sub := productVariableSubstitutionPattern.ReplaceAllString(s, "$("+productVariable+")") |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 736 | return sub, s != sub |
| 737 | } |