blob: 5d3299b765624480b50830a93e13648014967ca6 [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// GlobsInDir returns a list of glob expressions for a list of extensions
68// (optionally recursive) within a directory.
69func 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.
95func (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.
106func (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 Kammer356f7d42021-01-26 09:18:53 -0500118// Append appends the fields of other labelList to the corresponding fields of ll.
119func (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 Chen5d864492021-02-24 07:20:12 -0500127
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +0000128func 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
143func 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 Shuttleworthb8151682021-04-06 20:06:21 +0000150// Subtract needle from haystack
151func 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
175func 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
199func 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 Chen07027912021-03-15 06:02:43 -0400207const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400208 // ArchType names in arch.go
Jingwen Chen07027912021-03-15 06:02:43 -0400209 ARCH_ARM = "arm"
210 ARCH_ARM64 = "arm64"
Jingwen Chen91220d72021-03-24 02:18:33 -0400211 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 Chen07027912021-03-15 06:02:43 -0400221)
222
223var (
Jingwen Chenc1c26502021-04-05 10:35:13 +0000224 // 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 Chen91220d72021-03-24 02:18:33 -0400228
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 Chen07027912021-03-15 06:02:43 -0400248)
249
Jingwen Chenc1c26502021-04-05 10:35:13 +0000250type Attribute interface {
251 HasConfigurableValues() bool
252}
253
Jingwen Chen07027912021-03-15 06:02:43 -0400254// Arch-specific label_list typed Bazel attribute values. This should correspond
255// to the types of architectures supported for compilation in arch.go.
256type labelListArchValues struct {
257 X86 LabelList
258 X86_64 LabelList
259 Arm LabelList
260 Arm64 LabelList
Jingwen Chen91220d72021-03-24 02:18:33 -0400261 Common LabelList
262}
263
264type labelListOsValues struct {
265 Android LabelList
266 Darwin LabelList
267 Fuchsia LabelList
268 Linux LabelList
269 LinuxBionic LabelList
270 Windows LabelList
Jingwen Chen07027912021-03-15 06:02:43 -0400271}
272
273// LabelListAttribute is used to represent a list of Bazel labels as an
274// attribute.
275type 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 Chen91220d72021-03-24 02:18:33 -0400283
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 Chen07027912021-03-15 06:02:43 -0400288}
289
290// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
291func MakeLabelListAttribute(value LabelList) LabelListAttribute {
292 return LabelListAttribute{Value: UniqueBazelLabelList(value)}
293}
294
Jingwen Chen63930982021-03-24 10:04:33 -0400295// Append appends all values, including os and arch specific ones, from another
296// LabelListAttribute to this LabelListAttribute.
297func (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 Chen07027912021-03-15 06:02:43 -0400315// HasArchSpecificValues returns true if the attribute contains
316// architecture-specific label_list values.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000317func (attrs LabelListAttribute) HasConfigurableValues() bool {
318 for arch := range PlatformArchMap {
Jingwen Chen91220d72021-03-24 02:18:33 -0400319 if len(attrs.GetValueForArch(arch).Includes) > 0 {
320 return true
321 }
322 }
323
Jingwen Chenc1c26502021-04-05 10:35:13 +0000324 for os := range PlatformOsMap {
Jingwen Chen91220d72021-03-24 02:18:33 -0400325 if len(attrs.GetValueForOS(os).Includes) > 0 {
Jingwen Chen07027912021-03-15 06:02:43 -0400326 return true
327 }
328 }
329 return false
330}
331
Jingwen Chen91220d72021-03-24 02:18:33 -0400332func (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 Chen07027912021-03-15 06:02:43 -0400341// GetValueForArch returns the label_list attribute value for an architecture.
342func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList {
Jingwen Chen91220d72021-03-24 02:18:33 -0400343 var v *LabelList
344 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400345 panic(fmt.Errorf("Unknown arch: %s", arch))
346 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400347 return *v
Jingwen Chen07027912021-03-15 06:02:43 -0400348}
349
350// SetValueForArch sets the label_list attribute value for an architecture.
351func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400352 var v *LabelList
353 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400354 panic(fmt.Errorf("Unknown arch: %s", arch))
355 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400356 *v = value
357}
358
359func (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.
371func (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.
380func (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 Chen07027912021-03-15 06:02:43 -0400386}
387
Jingwen Chen5d864492021-02-24 07:20:12 -0500388// StringListAttribute corresponds to the string_list Bazel attribute type with
389// support for additional metadata, like configurations.
390type StringListAttribute struct {
391 // The base value of the string list attribute.
392 Value []string
393
Jingwen Chenc1c26502021-04-05 10:35:13 +0000394 // 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 Chen5d864492021-02-24 07:20:12 -0500397 ArchValues stringListArchValues
Jingwen Chenc1c26502021-04-05 10:35:13 +0000398
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 Chen5d864492021-02-24 07:20:12 -0500403}
404
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000405// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value.
406func MakeStringListAttribute(value []string) StringListAttribute {
407 // NOTE: These strings are not necessarily unique or sorted.
408 return StringListAttribute{Value: value}
409}
410
Jingwen Chen5d864492021-02-24 07:20:12 -0500411// Arch-specific string_list typed Bazel attribute values. This should correspond
412// to the types of architectures supported for compilation in arch.go.
413type stringListArchValues struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400414 X86 []string
415 X86_64 []string
416 Arm []string
417 Arm64 []string
Jingwen Chen91220d72021-03-24 02:18:33 -0400418 Common []string
Jingwen Chen5d864492021-02-24 07:20:12 -0500419}
420
Jingwen Chenc1c26502021-04-05 10:35:13 +0000421type stringListOsValues struct {
422 Android []string
423 Darwin []string
424 Fuchsia []string
425 Linux []string
426 LinuxBionic []string
427 Windows []string
428}
429
Jingwen Chen91220d72021-03-24 02:18:33 -0400430// HasConfigurableValues returns true if the attribute contains
Jingwen Chen5d864492021-02-24 07:20:12 -0500431// architecture-specific string_list values.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000432func (attrs StringListAttribute) HasConfigurableValues() bool {
433 for arch := range PlatformArchMap {
Jingwen Chen5d864492021-02-24 07:20:12 -0500434 if len(attrs.GetValueForArch(arch)) > 0 {
435 return true
436 }
437 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000438
439 for os := range PlatformOsMap {
440 if len(attrs.GetValueForOS(os)) > 0 {
441 return true
442 }
443 }
Jingwen Chen5d864492021-02-24 07:20:12 -0500444 return false
445}
446
Jingwen Chen91220d72021-03-24 02:18:33 -0400447func (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 Chen5d864492021-02-24 07:20:12 -0500456// GetValueForArch returns the string_list attribute value for an architecture.
457func (attrs *StringListAttribute) GetValueForArch(arch string) []string {
Jingwen Chen91220d72021-03-24 02:18:33 -0400458 var v *[]string
459 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500460 panic(fmt.Errorf("Unknown arch: %s", arch))
461 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400462 return *v
Jingwen Chen5d864492021-02-24 07:20:12 -0500463}
464
465// SetValueForArch sets the string_list attribute value for an architecture.
466func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400467 var v *[]string
468 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500469 panic(fmt.Errorf("Unknown arch: %s", arch))
470 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400471 *v = value
Jingwen Chen5d864492021-02-24 07:20:12 -0500472}
Liz Kammera060c452021-03-24 10:14:47 -0400473
Jingwen Chenc1c26502021-04-05 10:35:13 +0000474func (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.
486func (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.
495func (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 Kammera060c452021-03-24 10:14:47 -0400503// TryVariableSubstitution, replace string substitution formatting within each string in slice with
504// Starlark string.format compatible tag for productVariable.
505func 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.
518func TryVariableSubstitution(s string, productVariable string) (string, bool) {
519 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}")
520 return sub, s != sub
521}