blob: 148386f6a3dcf0ffba97553006cdbe8bbd35bab8 [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"
Liz Kammera060c452021-03-24 10:14:47 -040019 "regexp"
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000020 "sort"
21)
Jingwen Chen5d864492021-02-24 07:20:12 -050022
Jingwen Chen73850672020-12-14 08:25:34 -050023// BazelTargetModuleProperties contain properties and metadata used for
24// Blueprint to BUILD file conversion.
25type BazelTargetModuleProperties struct {
26 // The Bazel rule class for this target.
Liz Kammerfc46bc12021-02-19 11:06:17 -050027 Rule_class string `blueprint:"mutated"`
Jingwen Chen40067de2021-01-26 21:58:43 -050028
29 // The target label for the bzl file containing the definition of the rule class.
Liz Kammerfc46bc12021-02-19 11:06:17 -050030 Bzl_load_location string `blueprint:"mutated"`
Jingwen Chen73850672020-12-14 08:25:34 -050031}
Liz Kammer356f7d42021-01-26 09:18:53 -050032
Jingwen Chenfb4692a2021-02-07 10:05:16 -050033const BazelTargetModuleNamePrefix = "__bp2build__"
34
Liz Kammera060c452021-03-24 10:14:47 -040035var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)")
36
Liz Kammer356f7d42021-01-26 09:18:53 -050037// Label is used to represent a Bazel compatible Label. Also stores the original bp text to support
38// string replacement.
39type Label struct {
40 Bp_text string
41 Label string
42}
43
44// LabelList is used to represent a list of Bazel labels.
45type LabelList struct {
46 Includes []Label
47 Excludes []Label
48}
49
50// Append appends the fields of other labelList to the corresponding fields of ll.
51func (ll *LabelList) Append(other LabelList) {
52 if len(ll.Includes) > 0 || len(other.Includes) > 0 {
53 ll.Includes = append(ll.Includes, other.Includes...)
54 }
55 if len(ll.Excludes) > 0 || len(other.Excludes) > 0 {
56 ll.Excludes = append(other.Excludes, other.Excludes...)
57 }
58}
Jingwen Chen5d864492021-02-24 07:20:12 -050059
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000060func UniqueBazelLabels(originalLabels []Label) []Label {
61 uniqueLabelsSet := make(map[Label]bool)
62 for _, l := range originalLabels {
63 uniqueLabelsSet[l] = true
64 }
65 var uniqueLabels []Label
66 for l, _ := range uniqueLabelsSet {
67 uniqueLabels = append(uniqueLabels, l)
68 }
69 sort.SliceStable(uniqueLabels, func(i, j int) bool {
70 return uniqueLabels[i].Label < uniqueLabels[j].Label
71 })
72 return uniqueLabels
73}
74
75func UniqueBazelLabelList(originalLabelList LabelList) LabelList {
76 var uniqueLabelList LabelList
77 uniqueLabelList.Includes = UniqueBazelLabels(originalLabelList.Includes)
78 uniqueLabelList.Excludes = UniqueBazelLabels(originalLabelList.Excludes)
79 return uniqueLabelList
80}
81
Rupert Shuttleworthb8151682021-04-06 20:06:21 +000082// Subtract needle from haystack
83func SubtractStrings(haystack []string, needle []string) []string {
84 // This is really a set
85 remainder := make(map[string]bool)
86
87 for _, s := range haystack {
88 remainder[s] = true
89 }
90 for _, s := range needle {
91 delete(remainder, s)
92 }
93
94 var strings []string
95 for s, _ := range remainder {
96 strings = append(strings, s)
97 }
98
99 sort.SliceStable(strings, func(i, j int) bool {
100 return strings[i] < strings[j]
101 })
102
103 return strings
104}
105
106// Subtract needle from haystack
107func SubtractBazelLabels(haystack []Label, needle []Label) []Label {
108 // This is really a set
109 remainder := make(map[Label]bool)
110
111 for _, label := range haystack {
112 remainder[label] = true
113 }
114 for _, label := range needle {
115 delete(remainder, label)
116 }
117
118 var labels []Label
119 for label, _ := range remainder {
120 labels = append(labels, label)
121 }
122
123 sort.SliceStable(labels, func(i, j int) bool {
124 return labels[i].Label < labels[j].Label
125 })
126
127 return labels
128}
129
130// Subtract needle from haystack
131func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList {
132 var result LabelList
133 result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes)
134 // NOTE: Excludes are intentionally not subtracted
135 result.Excludes = haystack.Excludes
136 return result
137}
138
Jingwen Chen07027912021-03-15 06:02:43 -0400139const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400140 // ArchType names in arch.go
Jingwen Chen07027912021-03-15 06:02:43 -0400141 ARCH_ARM = "arm"
142 ARCH_ARM64 = "arm64"
Jingwen Chen91220d72021-03-24 02:18:33 -0400143 ARCH_X86 = "x86"
144 ARCH_X86_64 = "x86_64"
145
146 // OsType names in arch.go
147 OS_ANDROID = "android"
148 OS_DARWIN = "darwin"
149 OS_FUCHSIA = "fuchsia"
150 OS_LINUX = "linux_glibc"
151 OS_LINUX_BIONIC = "linux_bionic"
152 OS_WINDOWS = "windows"
Jingwen Chen07027912021-03-15 06:02:43 -0400153)
154
155var (
Jingwen Chenc1c26502021-04-05 10:35:13 +0000156 // These are the list of OSes and architectures with a Bazel config_setting
157 // and constraint value equivalent. These exist in arch.go, but the android
158 // package depends on the bazel package, so a cyclic dependency prevents
159 // using those variables here.
Jingwen Chen91220d72021-03-24 02:18:33 -0400160
161 // A map of architectures to the Bazel label of the constraint_value
162 // for the @platforms//cpu:cpu constraint_setting
163 PlatformArchMap = map[string]string{
164 ARCH_ARM: "//build/bazel/platforms/arch:arm",
165 ARCH_ARM64: "//build/bazel/platforms/arch:arm64",
166 ARCH_X86: "//build/bazel/platforms/arch:x86",
167 ARCH_X86_64: "//build/bazel/platforms/arch:x86_64",
168 }
169
170 // A map of target operating systems to the Bazel label of the
171 // constraint_value for the @platforms//os:os constraint_setting
172 PlatformOsMap = map[string]string{
173 OS_ANDROID: "//build/bazel/platforms/os:android",
174 OS_DARWIN: "//build/bazel/platforms/os:darwin",
175 OS_FUCHSIA: "//build/bazel/platforms/os:fuchsia",
176 OS_LINUX: "//build/bazel/platforms/os:linux",
177 OS_LINUX_BIONIC: "//build/bazel/platforms/os:linux_bionic",
178 OS_WINDOWS: "//build/bazel/platforms/os:windows",
179 }
Jingwen Chen07027912021-03-15 06:02:43 -0400180)
181
Jingwen Chenc1c26502021-04-05 10:35:13 +0000182type Attribute interface {
183 HasConfigurableValues() bool
184}
185
Jingwen Chen07027912021-03-15 06:02:43 -0400186// Arch-specific label_list typed Bazel attribute values. This should correspond
187// to the types of architectures supported for compilation in arch.go.
188type labelListArchValues struct {
189 X86 LabelList
190 X86_64 LabelList
191 Arm LabelList
192 Arm64 LabelList
Jingwen Chen91220d72021-03-24 02:18:33 -0400193 Common LabelList
194}
195
196type labelListOsValues struct {
197 Android LabelList
198 Darwin LabelList
199 Fuchsia LabelList
200 Linux LabelList
201 LinuxBionic LabelList
202 Windows LabelList
Jingwen Chen07027912021-03-15 06:02:43 -0400203}
204
205// LabelListAttribute is used to represent a list of Bazel labels as an
206// attribute.
207type LabelListAttribute struct {
208 // The non-arch specific attribute label list Value. Required.
209 Value LabelList
210
211 // The arch-specific attribute label list values. Optional. If used, these
212 // are generated in a select statement and appended to the non-arch specific
213 // label list Value.
214 ArchValues labelListArchValues
Jingwen Chen91220d72021-03-24 02:18:33 -0400215
216 // The os-specific attribute label list values. Optional. If used, these
217 // are generated in a select statement and appended to the non-os specific
218 // label list Value.
219 OsValues labelListOsValues
Jingwen Chen07027912021-03-15 06:02:43 -0400220}
221
222// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
223func MakeLabelListAttribute(value LabelList) LabelListAttribute {
224 return LabelListAttribute{Value: UniqueBazelLabelList(value)}
225}
226
227// HasArchSpecificValues returns true if the attribute contains
228// architecture-specific label_list values.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000229func (attrs LabelListAttribute) HasConfigurableValues() bool {
230 for arch := range PlatformArchMap {
Jingwen Chen91220d72021-03-24 02:18:33 -0400231 if len(attrs.GetValueForArch(arch).Includes) > 0 {
232 return true
233 }
234 }
235
Jingwen Chenc1c26502021-04-05 10:35:13 +0000236 for os := range PlatformOsMap {
Jingwen Chen91220d72021-03-24 02:18:33 -0400237 if len(attrs.GetValueForOS(os).Includes) > 0 {
Jingwen Chen07027912021-03-15 06:02:43 -0400238 return true
239 }
240 }
241 return false
242}
243
Jingwen Chen91220d72021-03-24 02:18:33 -0400244func (attrs *LabelListAttribute) archValuePtrs() map[string]*LabelList {
245 return map[string]*LabelList{
246 ARCH_X86: &attrs.ArchValues.X86,
247 ARCH_X86_64: &attrs.ArchValues.X86_64,
248 ARCH_ARM: &attrs.ArchValues.Arm,
249 ARCH_ARM64: &attrs.ArchValues.Arm64,
250 }
251}
252
Jingwen Chen07027912021-03-15 06:02:43 -0400253// GetValueForArch returns the label_list attribute value for an architecture.
254func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList {
Jingwen Chen91220d72021-03-24 02:18:33 -0400255 var v *LabelList
256 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400257 panic(fmt.Errorf("Unknown arch: %s", arch))
258 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400259 return *v
Jingwen Chen07027912021-03-15 06:02:43 -0400260}
261
262// SetValueForArch sets the label_list attribute value for an architecture.
263func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400264 var v *LabelList
265 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400266 panic(fmt.Errorf("Unknown arch: %s", arch))
267 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400268 *v = value
269}
270
271func (attrs *LabelListAttribute) osValuePtrs() map[string]*LabelList {
272 return map[string]*LabelList{
273 OS_ANDROID: &attrs.OsValues.Android,
274 OS_DARWIN: &attrs.OsValues.Darwin,
275 OS_FUCHSIA: &attrs.OsValues.Fuchsia,
276 OS_LINUX: &attrs.OsValues.Linux,
277 OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic,
278 OS_WINDOWS: &attrs.OsValues.Windows,
279 }
280}
281
282// GetValueForOS returns the label_list attribute value for an OS target.
283func (attrs *LabelListAttribute) GetValueForOS(os string) LabelList {
284 var v *LabelList
285 if v = attrs.osValuePtrs()[os]; v == nil {
286 panic(fmt.Errorf("Unknown os: %s", os))
287 }
288 return *v
289}
290
291// SetValueForArch sets the label_list attribute value for an OS target.
292func (attrs *LabelListAttribute) SetValueForOS(os string, value LabelList) {
293 var v *LabelList
294 if v = attrs.osValuePtrs()[os]; v == nil {
295 panic(fmt.Errorf("Unknown os: %s", os))
296 }
297 *v = value
Jingwen Chen07027912021-03-15 06:02:43 -0400298}
299
Jingwen Chen5d864492021-02-24 07:20:12 -0500300// StringListAttribute corresponds to the string_list Bazel attribute type with
301// support for additional metadata, like configurations.
302type StringListAttribute struct {
303 // The base value of the string list attribute.
304 Value []string
305
Jingwen Chenc1c26502021-04-05 10:35:13 +0000306 // The arch-specific attribute string list values. Optional. If used, these
307 // are generated in a select statement and appended to the non-arch specific
308 // label list Value.
Jingwen Chen5d864492021-02-24 07:20:12 -0500309 ArchValues stringListArchValues
Jingwen Chenc1c26502021-04-05 10:35:13 +0000310
311 // The os-specific attribute string list values. Optional. If used, these
312 // are generated in a select statement and appended to the non-os specific
313 // label list Value.
314 OsValues stringListOsValues
Jingwen Chen5d864492021-02-24 07:20:12 -0500315}
316
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000317// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value.
318func MakeStringListAttribute(value []string) StringListAttribute {
319 // NOTE: These strings are not necessarily unique or sorted.
320 return StringListAttribute{Value: value}
321}
322
Jingwen Chen5d864492021-02-24 07:20:12 -0500323// Arch-specific string_list typed Bazel attribute values. This should correspond
324// to the types of architectures supported for compilation in arch.go.
325type stringListArchValues struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400326 X86 []string
327 X86_64 []string
328 Arm []string
329 Arm64 []string
Jingwen Chen91220d72021-03-24 02:18:33 -0400330 Common []string
Jingwen Chen5d864492021-02-24 07:20:12 -0500331}
332
Jingwen Chenc1c26502021-04-05 10:35:13 +0000333type stringListOsValues struct {
334 Android []string
335 Darwin []string
336 Fuchsia []string
337 Linux []string
338 LinuxBionic []string
339 Windows []string
340}
341
Jingwen Chen91220d72021-03-24 02:18:33 -0400342// HasConfigurableValues returns true if the attribute contains
Jingwen Chen5d864492021-02-24 07:20:12 -0500343// architecture-specific string_list values.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000344func (attrs StringListAttribute) HasConfigurableValues() bool {
345 for arch := range PlatformArchMap {
Jingwen Chen5d864492021-02-24 07:20:12 -0500346 if len(attrs.GetValueForArch(arch)) > 0 {
347 return true
348 }
349 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000350
351 for os := range PlatformOsMap {
352 if len(attrs.GetValueForOS(os)) > 0 {
353 return true
354 }
355 }
Jingwen Chen5d864492021-02-24 07:20:12 -0500356 return false
357}
358
Jingwen Chen91220d72021-03-24 02:18:33 -0400359func (attrs *StringListAttribute) archValuePtrs() map[string]*[]string {
360 return map[string]*[]string{
361 ARCH_X86: &attrs.ArchValues.X86,
362 ARCH_X86_64: &attrs.ArchValues.X86_64,
363 ARCH_ARM: &attrs.ArchValues.Arm,
364 ARCH_ARM64: &attrs.ArchValues.Arm64,
365 }
366}
367
Jingwen Chen5d864492021-02-24 07:20:12 -0500368// GetValueForArch returns the string_list attribute value for an architecture.
369func (attrs *StringListAttribute) GetValueForArch(arch string) []string {
Jingwen Chen91220d72021-03-24 02:18:33 -0400370 var v *[]string
371 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500372 panic(fmt.Errorf("Unknown arch: %s", arch))
373 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400374 return *v
Jingwen Chen5d864492021-02-24 07:20:12 -0500375}
376
377// SetValueForArch sets the string_list attribute value for an architecture.
378func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400379 var v *[]string
380 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500381 panic(fmt.Errorf("Unknown arch: %s", arch))
382 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400383 *v = value
Jingwen Chen5d864492021-02-24 07:20:12 -0500384}
Liz Kammera060c452021-03-24 10:14:47 -0400385
Jingwen Chenc1c26502021-04-05 10:35:13 +0000386func (attrs *StringListAttribute) osValuePtrs() map[string]*[]string {
387 return map[string]*[]string{
388 OS_ANDROID: &attrs.OsValues.Android,
389 OS_DARWIN: &attrs.OsValues.Darwin,
390 OS_FUCHSIA: &attrs.OsValues.Fuchsia,
391 OS_LINUX: &attrs.OsValues.Linux,
392 OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic,
393 OS_WINDOWS: &attrs.OsValues.Windows,
394 }
395}
396
397// GetValueForOS returns the string_list attribute value for an OS target.
398func (attrs *StringListAttribute) GetValueForOS(os string) []string {
399 var v *[]string
400 if v = attrs.osValuePtrs()[os]; v == nil {
401 panic(fmt.Errorf("Unknown os: %s", os))
402 }
403 return *v
404}
405
406// SetValueForArch sets the string_list attribute value for an OS target.
407func (attrs *StringListAttribute) SetValueForOS(os string, value []string) {
408 var v *[]string
409 if v = attrs.osValuePtrs()[os]; v == nil {
410 panic(fmt.Errorf("Unknown os: %s", os))
411 }
412 *v = value
413}
414
Liz Kammera060c452021-03-24 10:14:47 -0400415// TryVariableSubstitution, replace string substitution formatting within each string in slice with
416// Starlark string.format compatible tag for productVariable.
417func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
418 ret := make([]string, 0, len(slice))
419 changesMade := false
420 for _, s := range slice {
421 newS, changed := TryVariableSubstitution(s, productVariable)
422 ret = append(ret, newS)
423 changesMade = changesMade || changed
424 }
425 return ret, changesMade
426}
427
428// TryVariableSubstitution, replace string substitution formatting within s with Starlark
429// string.format compatible tag for productVariable.
430func TryVariableSubstitution(s string, productVariable string) (string, bool) {
431 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}")
432 return sub, s != sub
433}