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" |
| 22 | ) |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 23 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 24 | // BazelTargetModuleProperties contain properties and metadata used for |
| 25 | // Blueprint to BUILD file conversion. |
| 26 | type BazelTargetModuleProperties struct { |
| 27 | // The Bazel rule class for this target. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 28 | Rule_class string `blueprint:"mutated"` |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 29 | |
| 30 | // 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] | 31 | Bzl_load_location string `blueprint:"mutated"` |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 32 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 33 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 34 | const BazelTargetModuleNamePrefix = "__bp2build__" |
| 35 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 36 | var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)") |
| 37 | |
Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame^] | 38 | // Label is used to represent a Bazel compatible Label. Also stores the original |
| 39 | // bp text to support string replacement. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 40 | type Label struct { |
Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame^] | 41 | // The string representation of a Bazel target label. This can be a relative |
| 42 | // or fully qualified label. These labels are used for generating BUILD |
| 43 | // files with bp2build. |
| 44 | Label string |
| 45 | |
| 46 | // The original Soong/Blueprint module name that the label was derived from. |
| 47 | // This is used for replacing references to the original name with the new |
| 48 | // label, for example in genrule cmds. |
| 49 | // |
| 50 | // While there is a reversible 1:1 mapping from the module name to Bazel |
| 51 | // label with bp2build that could make computing the original module name |
| 52 | // from the label automatic, it is not the case for handcrafted targets, |
| 53 | // where modules can have a custom label mapping through the { bazel_module: |
| 54 | // { label: <label> } } property. |
| 55 | // |
| 56 | // With handcrafted labels, those modules don't go through bp2build |
| 57 | // conversion, but relies on handcrafted targets in the source tree. |
| 58 | OriginalModuleName string |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // LabelList is used to represent a list of Bazel labels. |
| 62 | type LabelList struct { |
| 63 | Includes []Label |
| 64 | Excludes []Label |
| 65 | } |
| 66 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 67 | // GlobsInDir returns a list of glob expressions for a list of extensions |
| 68 | // (optionally recursive) within a directory. |
| 69 | func GlobsInDir(dir string, recursive bool, extensions []string) []string { |
| 70 | globs := []string{} |
| 71 | |
| 72 | globInfix := "" |
| 73 | if dir == "." { |
| 74 | if recursive { |
| 75 | // e.g "**/*.h" |
| 76 | globInfix = "**/" |
| 77 | } // else e.g. "*.h" |
| 78 | for _, ext := range extensions { |
| 79 | globs = append(globs, globInfix+"*"+ext) |
| 80 | } |
| 81 | } else { |
| 82 | if recursive { |
| 83 | // e.g. "foo/bar/**/*.h" |
| 84 | dir += "/**" |
| 85 | } // else e.g. "foo/bar/*.h" |
| 86 | for _, ext := range extensions { |
| 87 | globs = append(globs, dir+"/*"+ext) |
| 88 | } |
| 89 | } |
| 90 | return globs |
| 91 | } |
| 92 | |
| 93 | // LooseHdrsGlobs returns the list of non-recursive header globs for each parent directory of |
| 94 | // each source file in this LabelList's Includes. |
| 95 | func (ll *LabelList) LooseHdrsGlobs(exts []string) []string { |
| 96 | var globs []string |
| 97 | for _, parentDir := range ll.uniqueParentDirectories() { |
| 98 | globs = append(globs, |
| 99 | GlobsInDir(parentDir, false, exts)...) |
| 100 | } |
| 101 | return globs |
| 102 | } |
| 103 | |
| 104 | // uniqueParentDirectories returns a list of the unique parent directories for |
| 105 | // all files in ll.Includes. |
| 106 | func (ll *LabelList) uniqueParentDirectories() []string { |
| 107 | dirMap := map[string]bool{} |
| 108 | for _, label := range ll.Includes { |
| 109 | dirMap[filepath.Dir(label.Label)] = true |
| 110 | } |
| 111 | dirs := []string{} |
| 112 | for dir := range dirMap { |
| 113 | dirs = append(dirs, dir) |
| 114 | } |
| 115 | return dirs |
| 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 | |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 128 | func UniqueBazelLabels(originalLabels []Label) []Label { |
| 129 | uniqueLabelsSet := make(map[Label]bool) |
| 130 | for _, l := range originalLabels { |
| 131 | uniqueLabelsSet[l] = true |
| 132 | } |
| 133 | var uniqueLabels []Label |
| 134 | for l, _ := range uniqueLabelsSet { |
| 135 | uniqueLabels = append(uniqueLabels, l) |
| 136 | } |
| 137 | sort.SliceStable(uniqueLabels, func(i, j int) bool { |
| 138 | return uniqueLabels[i].Label < uniqueLabels[j].Label |
| 139 | }) |
| 140 | return uniqueLabels |
| 141 | } |
| 142 | |
| 143 | func UniqueBazelLabelList(originalLabelList LabelList) LabelList { |
| 144 | var uniqueLabelList LabelList |
| 145 | uniqueLabelList.Includes = UniqueBazelLabels(originalLabelList.Includes) |
| 146 | uniqueLabelList.Excludes = UniqueBazelLabels(originalLabelList.Excludes) |
| 147 | return uniqueLabelList |
| 148 | } |
| 149 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 150 | // Subtract needle from haystack |
| 151 | func SubtractStrings(haystack []string, needle []string) []string { |
| 152 | // This is really a set |
| 153 | remainder := make(map[string]bool) |
| 154 | |
| 155 | for _, s := range haystack { |
| 156 | remainder[s] = true |
| 157 | } |
| 158 | for _, s := range needle { |
| 159 | delete(remainder, s) |
| 160 | } |
| 161 | |
| 162 | var strings []string |
| 163 | for s, _ := range remainder { |
| 164 | strings = append(strings, s) |
| 165 | } |
| 166 | |
| 167 | sort.SliceStable(strings, func(i, j int) bool { |
| 168 | return strings[i] < strings[j] |
| 169 | }) |
| 170 | |
| 171 | return strings |
| 172 | } |
| 173 | |
| 174 | // Subtract needle from haystack |
| 175 | func SubtractBazelLabels(haystack []Label, needle []Label) []Label { |
| 176 | // This is really a set |
| 177 | remainder := make(map[Label]bool) |
| 178 | |
| 179 | for _, label := range haystack { |
| 180 | remainder[label] = true |
| 181 | } |
| 182 | for _, label := range needle { |
| 183 | delete(remainder, label) |
| 184 | } |
| 185 | |
| 186 | var labels []Label |
| 187 | for label, _ := range remainder { |
| 188 | labels = append(labels, label) |
| 189 | } |
| 190 | |
| 191 | sort.SliceStable(labels, func(i, j int) bool { |
| 192 | return labels[i].Label < labels[j].Label |
| 193 | }) |
| 194 | |
| 195 | return labels |
| 196 | } |
| 197 | |
| 198 | // Subtract needle from haystack |
| 199 | func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList { |
| 200 | var result LabelList |
| 201 | result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes) |
| 202 | // NOTE: Excludes are intentionally not subtracted |
| 203 | result.Excludes = haystack.Excludes |
| 204 | return result |
| 205 | } |
| 206 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 207 | const ( |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 208 | // ArchType names in arch.go |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 209 | ARCH_ARM = "arm" |
| 210 | ARCH_ARM64 = "arm64" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 211 | ARCH_X86 = "x86" |
| 212 | ARCH_X86_64 = "x86_64" |
| 213 | |
| 214 | // OsType names in arch.go |
| 215 | OS_ANDROID = "android" |
| 216 | OS_DARWIN = "darwin" |
| 217 | OS_FUCHSIA = "fuchsia" |
| 218 | OS_LINUX = "linux_glibc" |
| 219 | OS_LINUX_BIONIC = "linux_bionic" |
| 220 | OS_WINDOWS = "windows" |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 221 | ) |
| 222 | |
| 223 | var ( |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 224 | // These are the list of OSes and architectures with a Bazel config_setting |
| 225 | // and constraint value equivalent. These exist in arch.go, but the android |
| 226 | // package depends on the bazel package, so a cyclic dependency prevents |
| 227 | // using those variables here. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 228 | |
| 229 | // A map of architectures to the Bazel label of the constraint_value |
| 230 | // for the @platforms//cpu:cpu constraint_setting |
| 231 | PlatformArchMap = map[string]string{ |
| 232 | ARCH_ARM: "//build/bazel/platforms/arch:arm", |
| 233 | ARCH_ARM64: "//build/bazel/platforms/arch:arm64", |
| 234 | ARCH_X86: "//build/bazel/platforms/arch:x86", |
| 235 | ARCH_X86_64: "//build/bazel/platforms/arch:x86_64", |
| 236 | } |
| 237 | |
| 238 | // A map of target operating systems to the Bazel label of the |
| 239 | // constraint_value for the @platforms//os:os constraint_setting |
| 240 | PlatformOsMap = map[string]string{ |
| 241 | OS_ANDROID: "//build/bazel/platforms/os:android", |
| 242 | OS_DARWIN: "//build/bazel/platforms/os:darwin", |
| 243 | OS_FUCHSIA: "//build/bazel/platforms/os:fuchsia", |
| 244 | OS_LINUX: "//build/bazel/platforms/os:linux", |
| 245 | OS_LINUX_BIONIC: "//build/bazel/platforms/os:linux_bionic", |
| 246 | OS_WINDOWS: "//build/bazel/platforms/os:windows", |
| 247 | } |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 248 | ) |
| 249 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 250 | type Attribute interface { |
| 251 | HasConfigurableValues() bool |
| 252 | } |
| 253 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 254 | // Arch-specific label_list typed Bazel attribute values. This should correspond |
| 255 | // to the types of architectures supported for compilation in arch.go. |
| 256 | type labelListArchValues struct { |
| 257 | X86 LabelList |
| 258 | X86_64 LabelList |
| 259 | Arm LabelList |
| 260 | Arm64 LabelList |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 261 | Common LabelList |
| 262 | } |
| 263 | |
| 264 | type labelListOsValues struct { |
| 265 | Android LabelList |
| 266 | Darwin LabelList |
| 267 | Fuchsia LabelList |
| 268 | Linux LabelList |
| 269 | LinuxBionic LabelList |
| 270 | Windows LabelList |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // LabelListAttribute is used to represent a list of Bazel labels as an |
| 274 | // attribute. |
| 275 | type LabelListAttribute struct { |
| 276 | // The non-arch specific attribute label list Value. Required. |
| 277 | Value LabelList |
| 278 | |
| 279 | // The arch-specific attribute label list values. Optional. If used, these |
| 280 | // are generated in a select statement and appended to the non-arch specific |
| 281 | // label list Value. |
| 282 | ArchValues labelListArchValues |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 283 | |
| 284 | // The os-specific attribute label list values. Optional. If used, these |
| 285 | // are generated in a select statement and appended to the non-os specific |
| 286 | // label list Value. |
| 287 | OsValues labelListOsValues |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value. |
| 291 | func MakeLabelListAttribute(value LabelList) LabelListAttribute { |
| 292 | return LabelListAttribute{Value: UniqueBazelLabelList(value)} |
| 293 | } |
| 294 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 295 | // Append appends all values, including os and arch specific ones, from another |
| 296 | // LabelListAttribute to this LabelListAttribute. |
| 297 | func (attrs *LabelListAttribute) Append(other LabelListAttribute) { |
| 298 | for arch := range PlatformArchMap { |
| 299 | this := attrs.GetValueForArch(arch) |
| 300 | that := other.GetValueForArch(arch) |
| 301 | this.Append(that) |
| 302 | attrs.SetValueForArch(arch, this) |
| 303 | } |
| 304 | |
| 305 | for os := range PlatformOsMap { |
| 306 | this := attrs.GetValueForOS(os) |
| 307 | that := other.GetValueForOS(os) |
| 308 | this.Append(that) |
| 309 | attrs.SetValueForOS(os, this) |
| 310 | } |
| 311 | |
| 312 | attrs.Value.Append(other.Value) |
| 313 | } |
| 314 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 315 | // HasArchSpecificValues returns true if the attribute contains |
| 316 | // architecture-specific label_list values. |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 317 | func (attrs LabelListAttribute) HasConfigurableValues() bool { |
| 318 | for arch := range PlatformArchMap { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 319 | if len(attrs.GetValueForArch(arch).Includes) > 0 { |
| 320 | return true |
| 321 | } |
| 322 | } |
| 323 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 324 | for os := range PlatformOsMap { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 325 | if len(attrs.GetValueForOS(os).Includes) > 0 { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 326 | return true |
| 327 | } |
| 328 | } |
| 329 | return false |
| 330 | } |
| 331 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 332 | func (attrs *LabelListAttribute) archValuePtrs() map[string]*LabelList { |
| 333 | return map[string]*LabelList{ |
| 334 | ARCH_X86: &attrs.ArchValues.X86, |
| 335 | ARCH_X86_64: &attrs.ArchValues.X86_64, |
| 336 | ARCH_ARM: &attrs.ArchValues.Arm, |
| 337 | ARCH_ARM64: &attrs.ArchValues.Arm64, |
| 338 | } |
| 339 | } |
| 340 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 341 | // GetValueForArch returns the label_list attribute value for an architecture. |
| 342 | func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 343 | var v *LabelList |
| 344 | if v = attrs.archValuePtrs()[arch]; v == nil { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 345 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 346 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 347 | return *v |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // SetValueForArch sets the label_list attribute value for an architecture. |
| 351 | func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 352 | var v *LabelList |
| 353 | if v = attrs.archValuePtrs()[arch]; v == nil { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 354 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 355 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 356 | *v = value |
| 357 | } |
| 358 | |
| 359 | func (attrs *LabelListAttribute) osValuePtrs() map[string]*LabelList { |
| 360 | return map[string]*LabelList{ |
| 361 | OS_ANDROID: &attrs.OsValues.Android, |
| 362 | OS_DARWIN: &attrs.OsValues.Darwin, |
| 363 | OS_FUCHSIA: &attrs.OsValues.Fuchsia, |
| 364 | OS_LINUX: &attrs.OsValues.Linux, |
| 365 | OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic, |
| 366 | OS_WINDOWS: &attrs.OsValues.Windows, |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // GetValueForOS returns the label_list attribute value for an OS target. |
| 371 | func (attrs *LabelListAttribute) GetValueForOS(os string) LabelList { |
| 372 | var v *LabelList |
| 373 | if v = attrs.osValuePtrs()[os]; v == nil { |
| 374 | panic(fmt.Errorf("Unknown os: %s", os)) |
| 375 | } |
| 376 | return *v |
| 377 | } |
| 378 | |
| 379 | // SetValueForArch sets the label_list attribute value for an OS target. |
| 380 | func (attrs *LabelListAttribute) SetValueForOS(os string, value LabelList) { |
| 381 | var v *LabelList |
| 382 | if v = attrs.osValuePtrs()[os]; v == nil { |
| 383 | panic(fmt.Errorf("Unknown os: %s", os)) |
| 384 | } |
| 385 | *v = value |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 388 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 389 | // support for additional metadata, like configurations. |
| 390 | type StringListAttribute struct { |
| 391 | // The base value of the string list attribute. |
| 392 | Value []string |
| 393 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 394 | // The arch-specific attribute string list values. Optional. If used, these |
| 395 | // are generated in a select statement and appended to the non-arch specific |
| 396 | // label list Value. |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 397 | ArchValues stringListArchValues |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 398 | |
| 399 | // The os-specific attribute string list values. Optional. If used, these |
| 400 | // are generated in a select statement and appended to the non-os specific |
| 401 | // label list Value. |
| 402 | OsValues stringListOsValues |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 403 | } |
| 404 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 405 | // MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value. |
| 406 | func MakeStringListAttribute(value []string) StringListAttribute { |
| 407 | // NOTE: These strings are not necessarily unique or sorted. |
| 408 | return StringListAttribute{Value: value} |
| 409 | } |
| 410 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 411 | // Arch-specific string_list typed Bazel attribute values. This should correspond |
| 412 | // to the types of architectures supported for compilation in arch.go. |
| 413 | type stringListArchValues struct { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 414 | X86 []string |
| 415 | X86_64 []string |
| 416 | Arm []string |
| 417 | Arm64 []string |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 418 | Common []string |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 419 | } |
| 420 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 421 | type stringListOsValues struct { |
| 422 | Android []string |
| 423 | Darwin []string |
| 424 | Fuchsia []string |
| 425 | Linux []string |
| 426 | LinuxBionic []string |
| 427 | Windows []string |
| 428 | } |
| 429 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 430 | // HasConfigurableValues returns true if the attribute contains |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 431 | // architecture-specific string_list values. |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 432 | func (attrs StringListAttribute) HasConfigurableValues() bool { |
| 433 | for arch := range PlatformArchMap { |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 434 | if len(attrs.GetValueForArch(arch)) > 0 { |
| 435 | return true |
| 436 | } |
| 437 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 438 | |
| 439 | for os := range PlatformOsMap { |
| 440 | if len(attrs.GetValueForOS(os)) > 0 { |
| 441 | return true |
| 442 | } |
| 443 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 444 | return false |
| 445 | } |
| 446 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 447 | func (attrs *StringListAttribute) archValuePtrs() map[string]*[]string { |
| 448 | return map[string]*[]string{ |
| 449 | ARCH_X86: &attrs.ArchValues.X86, |
| 450 | ARCH_X86_64: &attrs.ArchValues.X86_64, |
| 451 | ARCH_ARM: &attrs.ArchValues.Arm, |
| 452 | ARCH_ARM64: &attrs.ArchValues.Arm64, |
| 453 | } |
| 454 | } |
| 455 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 456 | // GetValueForArch returns the string_list attribute value for an architecture. |
| 457 | func (attrs *StringListAttribute) GetValueForArch(arch string) []string { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 458 | var v *[]string |
| 459 | if v = attrs.archValuePtrs()[arch]; v == nil { |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 460 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 461 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 462 | return *v |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // SetValueForArch sets the string_list attribute value for an architecture. |
| 466 | func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 467 | var v *[]string |
| 468 | if v = attrs.archValuePtrs()[arch]; v == nil { |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 469 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 470 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 471 | *v = value |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 472 | } |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 473 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 474 | func (attrs *StringListAttribute) osValuePtrs() map[string]*[]string { |
| 475 | return map[string]*[]string{ |
| 476 | OS_ANDROID: &attrs.OsValues.Android, |
| 477 | OS_DARWIN: &attrs.OsValues.Darwin, |
| 478 | OS_FUCHSIA: &attrs.OsValues.Fuchsia, |
| 479 | OS_LINUX: &attrs.OsValues.Linux, |
| 480 | OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic, |
| 481 | OS_WINDOWS: &attrs.OsValues.Windows, |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // GetValueForOS returns the string_list attribute value for an OS target. |
| 486 | func (attrs *StringListAttribute) GetValueForOS(os string) []string { |
| 487 | var v *[]string |
| 488 | if v = attrs.osValuePtrs()[os]; v == nil { |
| 489 | panic(fmt.Errorf("Unknown os: %s", os)) |
| 490 | } |
| 491 | return *v |
| 492 | } |
| 493 | |
| 494 | // SetValueForArch sets the string_list attribute value for an OS target. |
| 495 | func (attrs *StringListAttribute) SetValueForOS(os string, value []string) { |
| 496 | var v *[]string |
| 497 | if v = attrs.osValuePtrs()[os]; v == nil { |
| 498 | panic(fmt.Errorf("Unknown os: %s", os)) |
| 499 | } |
| 500 | *v = value |
| 501 | } |
| 502 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 503 | // TryVariableSubstitution, replace string substitution formatting within each string in slice with |
| 504 | // Starlark string.format compatible tag for productVariable. |
| 505 | func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) { |
| 506 | ret := make([]string, 0, len(slice)) |
| 507 | changesMade := false |
| 508 | for _, s := range slice { |
| 509 | newS, changed := TryVariableSubstitution(s, productVariable) |
| 510 | ret = append(ret, newS) |
| 511 | changesMade = changesMade || changed |
| 512 | } |
| 513 | return ret, changesMade |
| 514 | } |
| 515 | |
| 516 | // TryVariableSubstitution, replace string substitution formatting within s with Starlark |
| 517 | // string.format compatible tag for productVariable. |
| 518 | func TryVariableSubstitution(s string, productVariable string) (string, bool) { |
| 519 | sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}") |
| 520 | return sub, s != sub |
| 521 | } |