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