blob: a03b0270f062ec1951401556230a59e3d984d600 [file] [log] [blame]
Jingwen Chen30f5aaa2020-11-19 05:38:02 -05001// 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
15package bazel
16
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000017import (
18 "fmt"
Jingwen Chen63930982021-03-24 10:04:33 -040019 "path/filepath"
Liz Kammera060c452021-03-24 10:14:47 -040020 "regexp"
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000021 "sort"
22)
Jingwen Chen5d864492021-02-24 07:20:12 -050023
Jingwen Chen73850672020-12-14 08:25:34 -050024// BazelTargetModuleProperties contain properties and metadata used for
25// Blueprint to BUILD file conversion.
26type BazelTargetModuleProperties struct {
27 // The Bazel rule class for this target.
Liz Kammerfc46bc12021-02-19 11:06:17 -050028 Rule_class string `blueprint:"mutated"`
Jingwen Chen40067de2021-01-26 21:58:43 -050029
30 // The target label for the bzl file containing the definition of the rule class.
Liz Kammerfc46bc12021-02-19 11:06:17 -050031 Bzl_load_location string `blueprint:"mutated"`
Jingwen Chen73850672020-12-14 08:25:34 -050032}
Liz Kammer356f7d42021-01-26 09:18:53 -050033
Jingwen Chenfb4692a2021-02-07 10:05:16 -050034const BazelTargetModuleNamePrefix = "__bp2build__"
35
Liz Kammera060c452021-03-24 10:14:47 -040036var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)")
37
Jingwen Chen38e62642021-04-19 05:00:15 +000038// Label is used to represent a Bazel compatible Label. Also stores the original
39// bp text to support string replacement.
Liz Kammer356f7d42021-01-26 09:18:53 -050040type Label struct {
Jingwen Chen38e62642021-04-19 05:00:15 +000041 // 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 Kammer356f7d42021-01-26 09:18:53 -050059}
60
61// LabelList is used to represent a list of Bazel labels.
62type LabelList struct {
63 Includes []Label
64 Excludes []Label
65}
66
Jingwen Chen63930982021-03-24 10:04:33 -040067// uniqueParentDirectories returns a list of the unique parent directories for
68// all files in ll.Includes.
69func (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 Kammer356f7d42021-01-26 09:18:53 -050081// Append appends the fields of other labelList to the corresponding fields of ll.
82func (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 Chen5d864492021-02-24 07:20:12 -050090
Jingwen Chened9c17d2021-04-13 07:14:55 +000091// UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns
92// the slice in a sorted order.
93func UniqueSortedBazelLabels(originalLabels []Label) []Label {
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000094 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
108func UniqueBazelLabelList(originalLabelList LabelList) LabelList {
109 var uniqueLabelList LabelList
Jingwen Chened9c17d2021-04-13 07:14:55 +0000110 uniqueLabelList.Includes = UniqueSortedBazelLabels(originalLabelList.Includes)
111 uniqueLabelList.Excludes = UniqueSortedBazelLabels(originalLabelList.Excludes)
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000112 return uniqueLabelList
113}
114
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000115// Subtract needle from haystack
116func 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
140func 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
164func 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 Chen07027912021-03-15 06:02:43 -0400172const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400173 // ArchType names in arch.go
Jingwen Chen07027912021-03-15 06:02:43 -0400174 ARCH_ARM = "arm"
175 ARCH_ARM64 = "arm64"
Jingwen Chen91220d72021-03-24 02:18:33 -0400176 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 Chen07027912021-03-15 06:02:43 -0400186)
187
188var (
Jingwen Chenc1c26502021-04-05 10:35:13 +0000189 // These are the list of OSes and architectures with a Bazel config_setting
190 // and constraint value equivalent. These exist in arch.go, but the android
191 // package depends on the bazel package, so a cyclic dependency prevents
192 // using those variables here.
Jingwen Chen91220d72021-03-24 02:18:33 -0400193
194 // A map of architectures to the Bazel label of the constraint_value
195 // for the @platforms//cpu:cpu constraint_setting
196 PlatformArchMap = map[string]string{
197 ARCH_ARM: "//build/bazel/platforms/arch:arm",
198 ARCH_ARM64: "//build/bazel/platforms/arch:arm64",
199 ARCH_X86: "//build/bazel/platforms/arch:x86",
200 ARCH_X86_64: "//build/bazel/platforms/arch:x86_64",
201 }
202
203 // A map of target operating systems to the Bazel label of the
204 // constraint_value for the @platforms//os:os constraint_setting
205 PlatformOsMap = map[string]string{
206 OS_ANDROID: "//build/bazel/platforms/os:android",
207 OS_DARWIN: "//build/bazel/platforms/os:darwin",
208 OS_FUCHSIA: "//build/bazel/platforms/os:fuchsia",
209 OS_LINUX: "//build/bazel/platforms/os:linux",
210 OS_LINUX_BIONIC: "//build/bazel/platforms/os:linux_bionic",
211 OS_WINDOWS: "//build/bazel/platforms/os:windows",
212 }
Jingwen Chen07027912021-03-15 06:02:43 -0400213)
214
Jingwen Chenc1c26502021-04-05 10:35:13 +0000215type Attribute interface {
216 HasConfigurableValues() bool
217}
218
Jingwen Chen07027912021-03-15 06:02:43 -0400219// Arch-specific label_list typed Bazel attribute values. This should correspond
220// to the types of architectures supported for compilation in arch.go.
221type labelListArchValues struct {
222 X86 LabelList
223 X86_64 LabelList
224 Arm LabelList
225 Arm64 LabelList
Jingwen Chen91220d72021-03-24 02:18:33 -0400226 Common LabelList
227}
228
229type labelListOsValues struct {
230 Android LabelList
231 Darwin LabelList
232 Fuchsia LabelList
233 Linux LabelList
234 LinuxBionic LabelList
235 Windows LabelList
Jingwen Chen07027912021-03-15 06:02:43 -0400236}
237
238// LabelListAttribute is used to represent a list of Bazel labels as an
239// attribute.
240type LabelListAttribute struct {
241 // The non-arch specific attribute label list Value. Required.
242 Value LabelList
243
244 // The arch-specific attribute label list values. Optional. If used, these
245 // are generated in a select statement and appended to the non-arch specific
246 // label list Value.
247 ArchValues labelListArchValues
Jingwen Chen91220d72021-03-24 02:18:33 -0400248
249 // The os-specific attribute label list values. Optional. If used, these
250 // are generated in a select statement and appended to the non-os specific
251 // label list Value.
252 OsValues labelListOsValues
Jingwen Chen07027912021-03-15 06:02:43 -0400253}
254
255// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
256func MakeLabelListAttribute(value LabelList) LabelListAttribute {
257 return LabelListAttribute{Value: UniqueBazelLabelList(value)}
258}
259
Jingwen Chened9c17d2021-04-13 07:14:55 +0000260// Append all values, including os and arch specific ones, from another
Jingwen Chen63930982021-03-24 10:04:33 -0400261// LabelListAttribute to this LabelListAttribute.
262func (attrs *LabelListAttribute) Append(other LabelListAttribute) {
263 for arch := range PlatformArchMap {
264 this := attrs.GetValueForArch(arch)
265 that := other.GetValueForArch(arch)
266 this.Append(that)
267 attrs.SetValueForArch(arch, this)
268 }
269
270 for os := range PlatformOsMap {
271 this := attrs.GetValueForOS(os)
272 that := other.GetValueForOS(os)
273 this.Append(that)
274 attrs.SetValueForOS(os, this)
275 }
276
277 attrs.Value.Append(other.Value)
278}
279
Jingwen Chen07027912021-03-15 06:02:43 -0400280// HasArchSpecificValues returns true if the attribute contains
281// architecture-specific label_list values.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000282func (attrs LabelListAttribute) HasConfigurableValues() bool {
283 for arch := range PlatformArchMap {
Jingwen Chen91220d72021-03-24 02:18:33 -0400284 if len(attrs.GetValueForArch(arch).Includes) > 0 {
285 return true
286 }
287 }
288
Jingwen Chenc1c26502021-04-05 10:35:13 +0000289 for os := range PlatformOsMap {
Jingwen Chen91220d72021-03-24 02:18:33 -0400290 if len(attrs.GetValueForOS(os).Includes) > 0 {
Jingwen Chen07027912021-03-15 06:02:43 -0400291 return true
292 }
293 }
294 return false
295}
296
Jingwen Chen91220d72021-03-24 02:18:33 -0400297func (attrs *LabelListAttribute) archValuePtrs() map[string]*LabelList {
298 return map[string]*LabelList{
299 ARCH_X86: &attrs.ArchValues.X86,
300 ARCH_X86_64: &attrs.ArchValues.X86_64,
301 ARCH_ARM: &attrs.ArchValues.Arm,
302 ARCH_ARM64: &attrs.ArchValues.Arm64,
303 }
304}
305
Jingwen Chen07027912021-03-15 06:02:43 -0400306// GetValueForArch returns the label_list attribute value for an architecture.
307func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList {
Jingwen Chen91220d72021-03-24 02:18:33 -0400308 var v *LabelList
309 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400310 panic(fmt.Errorf("Unknown arch: %s", arch))
311 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400312 return *v
Jingwen Chen07027912021-03-15 06:02:43 -0400313}
314
315// SetValueForArch sets the label_list attribute value for an architecture.
316func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400317 var v *LabelList
318 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400319 panic(fmt.Errorf("Unknown arch: %s", arch))
320 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400321 *v = value
322}
323
324func (attrs *LabelListAttribute) osValuePtrs() map[string]*LabelList {
325 return map[string]*LabelList{
326 OS_ANDROID: &attrs.OsValues.Android,
327 OS_DARWIN: &attrs.OsValues.Darwin,
328 OS_FUCHSIA: &attrs.OsValues.Fuchsia,
329 OS_LINUX: &attrs.OsValues.Linux,
330 OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic,
331 OS_WINDOWS: &attrs.OsValues.Windows,
332 }
333}
334
335// GetValueForOS returns the label_list attribute value for an OS target.
336func (attrs *LabelListAttribute) GetValueForOS(os string) LabelList {
337 var v *LabelList
338 if v = attrs.osValuePtrs()[os]; v == nil {
339 panic(fmt.Errorf("Unknown os: %s", os))
340 }
341 return *v
342}
343
344// SetValueForArch sets the label_list attribute value for an OS target.
345func (attrs *LabelListAttribute) SetValueForOS(os string, value LabelList) {
346 var v *LabelList
347 if v = attrs.osValuePtrs()[os]; v == nil {
348 panic(fmt.Errorf("Unknown os: %s", os))
349 }
350 *v = value
Jingwen Chen07027912021-03-15 06:02:43 -0400351}
352
Jingwen Chen5d864492021-02-24 07:20:12 -0500353// StringListAttribute corresponds to the string_list Bazel attribute type with
354// support for additional metadata, like configurations.
355type StringListAttribute struct {
356 // The base value of the string list attribute.
357 Value []string
358
Jingwen Chenc1c26502021-04-05 10:35:13 +0000359 // The arch-specific attribute string list values. Optional. If used, these
360 // are generated in a select statement and appended to the non-arch specific
361 // label list Value.
Jingwen Chen5d864492021-02-24 07:20:12 -0500362 ArchValues stringListArchValues
Jingwen Chenc1c26502021-04-05 10:35:13 +0000363
364 // The os-specific attribute string list values. Optional. If used, these
365 // are generated in a select statement and appended to the non-os specific
366 // label list Value.
367 OsValues stringListOsValues
Jingwen Chen5d864492021-02-24 07:20:12 -0500368}
369
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000370// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value.
371func MakeStringListAttribute(value []string) StringListAttribute {
372 // NOTE: These strings are not necessarily unique or sorted.
373 return StringListAttribute{Value: value}
374}
375
Jingwen Chen5d864492021-02-24 07:20:12 -0500376// Arch-specific string_list typed Bazel attribute values. This should correspond
377// to the types of architectures supported for compilation in arch.go.
378type stringListArchValues struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400379 X86 []string
380 X86_64 []string
381 Arm []string
382 Arm64 []string
Jingwen Chen91220d72021-03-24 02:18:33 -0400383 Common []string
Jingwen Chen5d864492021-02-24 07:20:12 -0500384}
385
Jingwen Chenc1c26502021-04-05 10:35:13 +0000386type stringListOsValues struct {
387 Android []string
388 Darwin []string
389 Fuchsia []string
390 Linux []string
391 LinuxBionic []string
392 Windows []string
393}
394
Jingwen Chen91220d72021-03-24 02:18:33 -0400395// HasConfigurableValues returns true if the attribute contains
Jingwen Chen5d864492021-02-24 07:20:12 -0500396// architecture-specific string_list values.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000397func (attrs StringListAttribute) HasConfigurableValues() bool {
398 for arch := range PlatformArchMap {
Jingwen Chen5d864492021-02-24 07:20:12 -0500399 if len(attrs.GetValueForArch(arch)) > 0 {
400 return true
401 }
402 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000403
404 for os := range PlatformOsMap {
405 if len(attrs.GetValueForOS(os)) > 0 {
406 return true
407 }
408 }
Jingwen Chen5d864492021-02-24 07:20:12 -0500409 return false
410}
411
Jingwen Chen91220d72021-03-24 02:18:33 -0400412func (attrs *StringListAttribute) archValuePtrs() map[string]*[]string {
413 return map[string]*[]string{
414 ARCH_X86: &attrs.ArchValues.X86,
415 ARCH_X86_64: &attrs.ArchValues.X86_64,
416 ARCH_ARM: &attrs.ArchValues.Arm,
417 ARCH_ARM64: &attrs.ArchValues.Arm64,
418 }
419}
420
Jingwen Chen5d864492021-02-24 07:20:12 -0500421// GetValueForArch returns the string_list attribute value for an architecture.
422func (attrs *StringListAttribute) GetValueForArch(arch string) []string {
Jingwen Chen91220d72021-03-24 02:18:33 -0400423 var v *[]string
424 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500425 panic(fmt.Errorf("Unknown arch: %s", arch))
426 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400427 return *v
Jingwen Chen5d864492021-02-24 07:20:12 -0500428}
429
430// SetValueForArch sets the string_list attribute value for an architecture.
431func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400432 var v *[]string
433 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500434 panic(fmt.Errorf("Unknown arch: %s", arch))
435 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400436 *v = value
Jingwen Chen5d864492021-02-24 07:20:12 -0500437}
Liz Kammera060c452021-03-24 10:14:47 -0400438
Jingwen Chenc1c26502021-04-05 10:35:13 +0000439func (attrs *StringListAttribute) osValuePtrs() map[string]*[]string {
440 return map[string]*[]string{
441 OS_ANDROID: &attrs.OsValues.Android,
442 OS_DARWIN: &attrs.OsValues.Darwin,
443 OS_FUCHSIA: &attrs.OsValues.Fuchsia,
444 OS_LINUX: &attrs.OsValues.Linux,
445 OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic,
446 OS_WINDOWS: &attrs.OsValues.Windows,
447 }
448}
449
450// GetValueForOS returns the string_list attribute value for an OS target.
451func (attrs *StringListAttribute) GetValueForOS(os string) []string {
452 var v *[]string
453 if v = attrs.osValuePtrs()[os]; v == nil {
454 panic(fmt.Errorf("Unknown os: %s", os))
455 }
456 return *v
457}
458
459// SetValueForArch sets the string_list attribute value for an OS target.
460func (attrs *StringListAttribute) SetValueForOS(os string, value []string) {
461 var v *[]string
462 if v = attrs.osValuePtrs()[os]; v == nil {
463 panic(fmt.Errorf("Unknown os: %s", os))
464 }
465 *v = value
466}
467
Jingwen Chened9c17d2021-04-13 07:14:55 +0000468// Append appends all values, including os and arch specific ones, from another
469// StringListAttribute to this StringListAttribute
470func (attrs *StringListAttribute) Append(other StringListAttribute) {
471 for arch := range PlatformArchMap {
472 this := attrs.GetValueForArch(arch)
473 that := other.GetValueForArch(arch)
474 this = append(this, that...)
475 attrs.SetValueForArch(arch, this)
476 }
477
478 for os := range PlatformOsMap {
479 this := attrs.GetValueForOS(os)
480 that := other.GetValueForOS(os)
481 this = append(this, that...)
482 attrs.SetValueForOS(os, this)
483 }
484
485 attrs.Value = append(attrs.Value, other.Value...)
486}
487
Liz Kammera060c452021-03-24 10:14:47 -0400488// TryVariableSubstitution, replace string substitution formatting within each string in slice with
489// Starlark string.format compatible tag for productVariable.
490func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
491 ret := make([]string, 0, len(slice))
492 changesMade := false
493 for _, s := range slice {
494 newS, changed := TryVariableSubstitution(s, productVariable)
495 ret = append(ret, newS)
496 changesMade = changesMade || changed
497 }
498 return ret, changesMade
499}
500
501// TryVariableSubstitution, replace string substitution formatting within s with Starlark
502// string.format compatible tag for productVariable.
503func TryVariableSubstitution(s string, productVariable string) (string, bool) {
504 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}")
505 return sub, s != sub
506}