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" |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 19 | "regexp" |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 20 | "sort" |
| 21 | ) |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 22 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 23 | // BazelTargetModuleProperties contain properties and metadata used for |
| 24 | // Blueprint to BUILD file conversion. |
| 25 | type BazelTargetModuleProperties struct { |
| 26 | // The Bazel rule class for this target. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 27 | Rule_class string `blueprint:"mutated"` |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 28 | |
| 29 | // 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] | 30 | Bzl_load_location string `blueprint:"mutated"` |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 31 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 32 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 33 | const BazelTargetModuleNamePrefix = "__bp2build__" |
| 34 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 35 | var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)") |
| 36 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 37 | // Label is used to represent a Bazel compatible Label. Also stores the original bp text to support |
| 38 | // string replacement. |
| 39 | type Label struct { |
| 40 | Bp_text string |
| 41 | Label string |
| 42 | } |
| 43 | |
| 44 | // LabelList is used to represent a list of Bazel labels. |
| 45 | type LabelList struct { |
| 46 | Includes []Label |
| 47 | Excludes []Label |
| 48 | } |
| 49 | |
| 50 | // Append appends the fields of other labelList to the corresponding fields of ll. |
| 51 | func (ll *LabelList) Append(other LabelList) { |
| 52 | if len(ll.Includes) > 0 || len(other.Includes) > 0 { |
| 53 | ll.Includes = append(ll.Includes, other.Includes...) |
| 54 | } |
| 55 | if len(ll.Excludes) > 0 || len(other.Excludes) > 0 { |
| 56 | ll.Excludes = append(other.Excludes, other.Excludes...) |
| 57 | } |
| 58 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 59 | |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 60 | func UniqueBazelLabels(originalLabels []Label) []Label { |
| 61 | uniqueLabelsSet := make(map[Label]bool) |
| 62 | for _, l := range originalLabels { |
| 63 | uniqueLabelsSet[l] = true |
| 64 | } |
| 65 | var uniqueLabels []Label |
| 66 | for l, _ := range uniqueLabelsSet { |
| 67 | uniqueLabels = append(uniqueLabels, l) |
| 68 | } |
| 69 | sort.SliceStable(uniqueLabels, func(i, j int) bool { |
| 70 | return uniqueLabels[i].Label < uniqueLabels[j].Label |
| 71 | }) |
| 72 | return uniqueLabels |
| 73 | } |
| 74 | |
| 75 | func UniqueBazelLabelList(originalLabelList LabelList) LabelList { |
| 76 | var uniqueLabelList LabelList |
| 77 | uniqueLabelList.Includes = UniqueBazelLabels(originalLabelList.Includes) |
| 78 | uniqueLabelList.Excludes = UniqueBazelLabels(originalLabelList.Excludes) |
| 79 | return uniqueLabelList |
| 80 | } |
| 81 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame^] | 82 | // Subtract needle from haystack |
| 83 | func SubtractStrings(haystack []string, needle []string) []string { |
| 84 | // This is really a set |
| 85 | remainder := make(map[string]bool) |
| 86 | |
| 87 | for _, s := range haystack { |
| 88 | remainder[s] = true |
| 89 | } |
| 90 | for _, s := range needle { |
| 91 | delete(remainder, s) |
| 92 | } |
| 93 | |
| 94 | var strings []string |
| 95 | for s, _ := range remainder { |
| 96 | strings = append(strings, s) |
| 97 | } |
| 98 | |
| 99 | sort.SliceStable(strings, func(i, j int) bool { |
| 100 | return strings[i] < strings[j] |
| 101 | }) |
| 102 | |
| 103 | return strings |
| 104 | } |
| 105 | |
| 106 | // Subtract needle from haystack |
| 107 | func SubtractBazelLabels(haystack []Label, needle []Label) []Label { |
| 108 | // This is really a set |
| 109 | remainder := make(map[Label]bool) |
| 110 | |
| 111 | for _, label := range haystack { |
| 112 | remainder[label] = true |
| 113 | } |
| 114 | for _, label := range needle { |
| 115 | delete(remainder, label) |
| 116 | } |
| 117 | |
| 118 | var labels []Label |
| 119 | for label, _ := range remainder { |
| 120 | labels = append(labels, label) |
| 121 | } |
| 122 | |
| 123 | sort.SliceStable(labels, func(i, j int) bool { |
| 124 | return labels[i].Label < labels[j].Label |
| 125 | }) |
| 126 | |
| 127 | return labels |
| 128 | } |
| 129 | |
| 130 | // Subtract needle from haystack |
| 131 | func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList { |
| 132 | var result LabelList |
| 133 | result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes) |
| 134 | // NOTE: Excludes are intentionally not subtracted |
| 135 | result.Excludes = haystack.Excludes |
| 136 | return result |
| 137 | } |
| 138 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 139 | const ( |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 140 | // ArchType names in arch.go |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 141 | ARCH_ARM = "arm" |
| 142 | ARCH_ARM64 = "arm64" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 143 | ARCH_X86 = "x86" |
| 144 | ARCH_X86_64 = "x86_64" |
| 145 | |
| 146 | // OsType names in arch.go |
| 147 | OS_ANDROID = "android" |
| 148 | OS_DARWIN = "darwin" |
| 149 | OS_FUCHSIA = "fuchsia" |
| 150 | OS_LINUX = "linux_glibc" |
| 151 | OS_LINUX_BIONIC = "linux_bionic" |
| 152 | OS_WINDOWS = "windows" |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 153 | ) |
| 154 | |
| 155 | var ( |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 156 | // These are the list of OSes and architectures with a Bazel config_setting |
| 157 | // and constraint value equivalent. These exist in arch.go, but the android |
| 158 | // package depends on the bazel package, so a cyclic dependency prevents |
| 159 | // using those variables here. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 160 | |
| 161 | // A map of architectures to the Bazel label of the constraint_value |
| 162 | // for the @platforms//cpu:cpu constraint_setting |
| 163 | PlatformArchMap = map[string]string{ |
| 164 | ARCH_ARM: "//build/bazel/platforms/arch:arm", |
| 165 | ARCH_ARM64: "//build/bazel/platforms/arch:arm64", |
| 166 | ARCH_X86: "//build/bazel/platforms/arch:x86", |
| 167 | ARCH_X86_64: "//build/bazel/platforms/arch:x86_64", |
| 168 | } |
| 169 | |
| 170 | // A map of target operating systems to the Bazel label of the |
| 171 | // constraint_value for the @platforms//os:os constraint_setting |
| 172 | PlatformOsMap = map[string]string{ |
| 173 | OS_ANDROID: "//build/bazel/platforms/os:android", |
| 174 | OS_DARWIN: "//build/bazel/platforms/os:darwin", |
| 175 | OS_FUCHSIA: "//build/bazel/platforms/os:fuchsia", |
| 176 | OS_LINUX: "//build/bazel/platforms/os:linux", |
| 177 | OS_LINUX_BIONIC: "//build/bazel/platforms/os:linux_bionic", |
| 178 | OS_WINDOWS: "//build/bazel/platforms/os:windows", |
| 179 | } |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 180 | ) |
| 181 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 182 | type Attribute interface { |
| 183 | HasConfigurableValues() bool |
| 184 | } |
| 185 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 186 | // Arch-specific label_list typed Bazel attribute values. This should correspond |
| 187 | // to the types of architectures supported for compilation in arch.go. |
| 188 | type labelListArchValues struct { |
| 189 | X86 LabelList |
| 190 | X86_64 LabelList |
| 191 | Arm LabelList |
| 192 | Arm64 LabelList |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 193 | Common LabelList |
| 194 | } |
| 195 | |
| 196 | type labelListOsValues struct { |
| 197 | Android LabelList |
| 198 | Darwin LabelList |
| 199 | Fuchsia LabelList |
| 200 | Linux LabelList |
| 201 | LinuxBionic LabelList |
| 202 | Windows LabelList |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // LabelListAttribute is used to represent a list of Bazel labels as an |
| 206 | // attribute. |
| 207 | type LabelListAttribute struct { |
| 208 | // The non-arch specific attribute label list Value. Required. |
| 209 | Value LabelList |
| 210 | |
| 211 | // The arch-specific attribute label list values. Optional. If used, these |
| 212 | // are generated in a select statement and appended to the non-arch specific |
| 213 | // label list Value. |
| 214 | ArchValues labelListArchValues |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 215 | |
| 216 | // The os-specific attribute label list values. Optional. If used, these |
| 217 | // are generated in a select statement and appended to the non-os specific |
| 218 | // label list Value. |
| 219 | OsValues labelListOsValues |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value. |
| 223 | func MakeLabelListAttribute(value LabelList) LabelListAttribute { |
| 224 | return LabelListAttribute{Value: UniqueBazelLabelList(value)} |
| 225 | } |
| 226 | |
| 227 | // HasArchSpecificValues returns true if the attribute contains |
| 228 | // architecture-specific label_list values. |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 229 | func (attrs LabelListAttribute) HasConfigurableValues() bool { |
| 230 | for arch := range PlatformArchMap { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 231 | if len(attrs.GetValueForArch(arch).Includes) > 0 { |
| 232 | return true |
| 233 | } |
| 234 | } |
| 235 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 236 | for os := range PlatformOsMap { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 237 | if len(attrs.GetValueForOS(os).Includes) > 0 { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 238 | return true |
| 239 | } |
| 240 | } |
| 241 | return false |
| 242 | } |
| 243 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 244 | func (attrs *LabelListAttribute) archValuePtrs() map[string]*LabelList { |
| 245 | return map[string]*LabelList{ |
| 246 | ARCH_X86: &attrs.ArchValues.X86, |
| 247 | ARCH_X86_64: &attrs.ArchValues.X86_64, |
| 248 | ARCH_ARM: &attrs.ArchValues.Arm, |
| 249 | ARCH_ARM64: &attrs.ArchValues.Arm64, |
| 250 | } |
| 251 | } |
| 252 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 253 | // GetValueForArch returns the label_list attribute value for an architecture. |
| 254 | func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 255 | var v *LabelList |
| 256 | if v = attrs.archValuePtrs()[arch]; v == nil { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 257 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 258 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 259 | return *v |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // SetValueForArch sets the label_list attribute value for an architecture. |
| 263 | func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 264 | var v *LabelList |
| 265 | if v = attrs.archValuePtrs()[arch]; v == nil { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 266 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 267 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 268 | *v = value |
| 269 | } |
| 270 | |
| 271 | func (attrs *LabelListAttribute) osValuePtrs() map[string]*LabelList { |
| 272 | return map[string]*LabelList{ |
| 273 | OS_ANDROID: &attrs.OsValues.Android, |
| 274 | OS_DARWIN: &attrs.OsValues.Darwin, |
| 275 | OS_FUCHSIA: &attrs.OsValues.Fuchsia, |
| 276 | OS_LINUX: &attrs.OsValues.Linux, |
| 277 | OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic, |
| 278 | OS_WINDOWS: &attrs.OsValues.Windows, |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // GetValueForOS returns the label_list attribute value for an OS target. |
| 283 | func (attrs *LabelListAttribute) GetValueForOS(os string) LabelList { |
| 284 | var v *LabelList |
| 285 | if v = attrs.osValuePtrs()[os]; v == nil { |
| 286 | panic(fmt.Errorf("Unknown os: %s", os)) |
| 287 | } |
| 288 | return *v |
| 289 | } |
| 290 | |
| 291 | // SetValueForArch sets the label_list attribute value for an OS target. |
| 292 | func (attrs *LabelListAttribute) SetValueForOS(os string, value LabelList) { |
| 293 | var v *LabelList |
| 294 | if v = attrs.osValuePtrs()[os]; v == nil { |
| 295 | panic(fmt.Errorf("Unknown os: %s", os)) |
| 296 | } |
| 297 | *v = value |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 300 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 301 | // support for additional metadata, like configurations. |
| 302 | type StringListAttribute struct { |
| 303 | // The base value of the string list attribute. |
| 304 | Value []string |
| 305 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 306 | // The arch-specific attribute string list values. Optional. If used, these |
| 307 | // are generated in a select statement and appended to the non-arch specific |
| 308 | // label list Value. |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 309 | ArchValues stringListArchValues |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 310 | |
| 311 | // The os-specific attribute string list values. Optional. If used, these |
| 312 | // are generated in a select statement and appended to the non-os specific |
| 313 | // label list Value. |
| 314 | OsValues stringListOsValues |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame^] | 317 | // MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value. |
| 318 | func MakeStringListAttribute(value []string) StringListAttribute { |
| 319 | // NOTE: These strings are not necessarily unique or sorted. |
| 320 | return StringListAttribute{Value: value} |
| 321 | } |
| 322 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 323 | // Arch-specific string_list typed Bazel attribute values. This should correspond |
| 324 | // to the types of architectures supported for compilation in arch.go. |
| 325 | type stringListArchValues struct { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 326 | X86 []string |
| 327 | X86_64 []string |
| 328 | Arm []string |
| 329 | Arm64 []string |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 330 | Common []string |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 333 | type stringListOsValues struct { |
| 334 | Android []string |
| 335 | Darwin []string |
| 336 | Fuchsia []string |
| 337 | Linux []string |
| 338 | LinuxBionic []string |
| 339 | Windows []string |
| 340 | } |
| 341 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 342 | // HasConfigurableValues returns true if the attribute contains |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 343 | // architecture-specific string_list values. |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 344 | func (attrs StringListAttribute) HasConfigurableValues() bool { |
| 345 | for arch := range PlatformArchMap { |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 346 | if len(attrs.GetValueForArch(arch)) > 0 { |
| 347 | return true |
| 348 | } |
| 349 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 350 | |
| 351 | for os := range PlatformOsMap { |
| 352 | if len(attrs.GetValueForOS(os)) > 0 { |
| 353 | return true |
| 354 | } |
| 355 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 356 | return false |
| 357 | } |
| 358 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 359 | func (attrs *StringListAttribute) archValuePtrs() map[string]*[]string { |
| 360 | return map[string]*[]string{ |
| 361 | ARCH_X86: &attrs.ArchValues.X86, |
| 362 | ARCH_X86_64: &attrs.ArchValues.X86_64, |
| 363 | ARCH_ARM: &attrs.ArchValues.Arm, |
| 364 | ARCH_ARM64: &attrs.ArchValues.Arm64, |
| 365 | } |
| 366 | } |
| 367 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 368 | // GetValueForArch returns the string_list attribute value for an architecture. |
| 369 | func (attrs *StringListAttribute) GetValueForArch(arch string) []string { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 370 | var v *[]string |
| 371 | if v = attrs.archValuePtrs()[arch]; v == nil { |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 372 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 373 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 374 | return *v |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // SetValueForArch sets the string_list attribute value for an architecture. |
| 378 | func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 379 | var v *[]string |
| 380 | if v = attrs.archValuePtrs()[arch]; v == nil { |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 381 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 382 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 383 | *v = value |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 384 | } |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 385 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 386 | func (attrs *StringListAttribute) osValuePtrs() map[string]*[]string { |
| 387 | return map[string]*[]string{ |
| 388 | OS_ANDROID: &attrs.OsValues.Android, |
| 389 | OS_DARWIN: &attrs.OsValues.Darwin, |
| 390 | OS_FUCHSIA: &attrs.OsValues.Fuchsia, |
| 391 | OS_LINUX: &attrs.OsValues.Linux, |
| 392 | OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic, |
| 393 | OS_WINDOWS: &attrs.OsValues.Windows, |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // GetValueForOS returns the string_list attribute value for an OS target. |
| 398 | func (attrs *StringListAttribute) GetValueForOS(os string) []string { |
| 399 | var v *[]string |
| 400 | if v = attrs.osValuePtrs()[os]; v == nil { |
| 401 | panic(fmt.Errorf("Unknown os: %s", os)) |
| 402 | } |
| 403 | return *v |
| 404 | } |
| 405 | |
| 406 | // SetValueForArch sets the string_list attribute value for an OS target. |
| 407 | func (attrs *StringListAttribute) SetValueForOS(os string, value []string) { |
| 408 | var v *[]string |
| 409 | if v = attrs.osValuePtrs()[os]; v == nil { |
| 410 | panic(fmt.Errorf("Unknown os: %s", os)) |
| 411 | } |
| 412 | *v = value |
| 413 | } |
| 414 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 415 | // TryVariableSubstitution, replace string substitution formatting within each string in slice with |
| 416 | // Starlark string.format compatible tag for productVariable. |
| 417 | func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) { |
| 418 | ret := make([]string, 0, len(slice)) |
| 419 | changesMade := false |
| 420 | for _, s := range slice { |
| 421 | newS, changed := TryVariableSubstitution(s, productVariable) |
| 422 | ret = append(ret, newS) |
| 423 | changesMade = changesMade || changed |
| 424 | } |
| 425 | return ret, changesMade |
| 426 | } |
| 427 | |
| 428 | // TryVariableSubstitution, replace string substitution formatting within s with Starlark |
| 429 | // string.format compatible tag for productVariable. |
| 430 | func TryVariableSubstitution(s string, productVariable string) (string, bool) { |
| 431 | sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}") |
| 432 | return sub, s != sub |
| 433 | } |