blob: 250fea4cd89284317f9009bff15c68af64a28a1e [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
Jingwen Chen07027912021-03-15 06:02:43 -040082const (
Jingwen Chen91220d72021-03-24 02:18:33 -040083 // ArchType names in arch.go
Jingwen Chen07027912021-03-15 06:02:43 -040084 ARCH_ARM = "arm"
85 ARCH_ARM64 = "arm64"
Jingwen Chen91220d72021-03-24 02:18:33 -040086 ARCH_X86 = "x86"
87 ARCH_X86_64 = "x86_64"
88
89 // OsType names in arch.go
90 OS_ANDROID = "android"
91 OS_DARWIN = "darwin"
92 OS_FUCHSIA = "fuchsia"
93 OS_LINUX = "linux_glibc"
94 OS_LINUX_BIONIC = "linux_bionic"
95 OS_WINDOWS = "windows"
Jingwen Chen07027912021-03-15 06:02:43 -040096)
97
98var (
99 // This is the list of architectures with a Bazel config_setting and
100 // constraint value equivalent. is actually android.ArchTypeList, but the
101 // android package depends on the bazel package, so a cyclic dependency
102 // prevents using that here.
103 selectableArchs = []string{ARCH_X86, ARCH_X86_64, ARCH_ARM, ARCH_ARM64}
Jingwen Chen91220d72021-03-24 02:18:33 -0400104
105 // Likewise, this is the list of target operating systems.
106 selectableTargetOs = []string{
107 OS_ANDROID,
108 OS_DARWIN,
109 OS_FUCHSIA,
110 OS_LINUX,
111 OS_LINUX_BIONIC,
112 OS_WINDOWS,
113 }
114
115 // A map of architectures to the Bazel label of the constraint_value
116 // for the @platforms//cpu:cpu constraint_setting
117 PlatformArchMap = map[string]string{
118 ARCH_ARM: "//build/bazel/platforms/arch:arm",
119 ARCH_ARM64: "//build/bazel/platforms/arch:arm64",
120 ARCH_X86: "//build/bazel/platforms/arch:x86",
121 ARCH_X86_64: "//build/bazel/platforms/arch:x86_64",
122 }
123
124 // A map of target operating systems to the Bazel label of the
125 // constraint_value for the @platforms//os:os constraint_setting
126 PlatformOsMap = map[string]string{
127 OS_ANDROID: "//build/bazel/platforms/os:android",
128 OS_DARWIN: "//build/bazel/platforms/os:darwin",
129 OS_FUCHSIA: "//build/bazel/platforms/os:fuchsia",
130 OS_LINUX: "//build/bazel/platforms/os:linux",
131 OS_LINUX_BIONIC: "//build/bazel/platforms/os:linux_bionic",
132 OS_WINDOWS: "//build/bazel/platforms/os:windows",
133 }
Jingwen Chen07027912021-03-15 06:02:43 -0400134)
135
136// Arch-specific label_list typed Bazel attribute values. This should correspond
137// to the types of architectures supported for compilation in arch.go.
138type labelListArchValues struct {
139 X86 LabelList
140 X86_64 LabelList
141 Arm LabelList
142 Arm64 LabelList
Jingwen Chen91220d72021-03-24 02:18:33 -0400143 Common LabelList
144}
145
146type labelListOsValues struct {
147 Android LabelList
148 Darwin LabelList
149 Fuchsia LabelList
150 Linux LabelList
151 LinuxBionic LabelList
152 Windows LabelList
Jingwen Chen07027912021-03-15 06:02:43 -0400153}
154
155// LabelListAttribute is used to represent a list of Bazel labels as an
156// attribute.
157type LabelListAttribute struct {
158 // The non-arch specific attribute label list Value. Required.
159 Value LabelList
160
161 // The arch-specific attribute label list values. Optional. If used, these
162 // are generated in a select statement and appended to the non-arch specific
163 // label list Value.
164 ArchValues labelListArchValues
Jingwen Chen91220d72021-03-24 02:18:33 -0400165
166 // The os-specific attribute label list values. Optional. If used, these
167 // are generated in a select statement and appended to the non-os specific
168 // label list Value.
169 OsValues labelListOsValues
Jingwen Chen07027912021-03-15 06:02:43 -0400170}
171
172// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
173func MakeLabelListAttribute(value LabelList) LabelListAttribute {
174 return LabelListAttribute{Value: UniqueBazelLabelList(value)}
175}
176
177// HasArchSpecificValues returns true if the attribute contains
178// architecture-specific label_list values.
Jingwen Chen91220d72021-03-24 02:18:33 -0400179func (attrs *LabelListAttribute) HasConfigurableValues() bool {
Jingwen Chen07027912021-03-15 06:02:43 -0400180 for _, arch := range selectableArchs {
Jingwen Chen91220d72021-03-24 02:18:33 -0400181 if len(attrs.GetValueForArch(arch).Includes) > 0 {
182 return true
183 }
184 }
185
186 for _, os := range selectableTargetOs {
187 if len(attrs.GetValueForOS(os).Includes) > 0 {
Jingwen Chen07027912021-03-15 06:02:43 -0400188 return true
189 }
190 }
191 return false
192}
193
Jingwen Chen91220d72021-03-24 02:18:33 -0400194func (attrs *LabelListAttribute) archValuePtrs() map[string]*LabelList {
195 return map[string]*LabelList{
196 ARCH_X86: &attrs.ArchValues.X86,
197 ARCH_X86_64: &attrs.ArchValues.X86_64,
198 ARCH_ARM: &attrs.ArchValues.Arm,
199 ARCH_ARM64: &attrs.ArchValues.Arm64,
200 }
201}
202
Jingwen Chen07027912021-03-15 06:02:43 -0400203// GetValueForArch returns the label_list attribute value for an architecture.
204func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList {
Jingwen Chen91220d72021-03-24 02:18:33 -0400205 var v *LabelList
206 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400207 panic(fmt.Errorf("Unknown arch: %s", arch))
208 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400209 return *v
Jingwen Chen07027912021-03-15 06:02:43 -0400210}
211
212// SetValueForArch sets the label_list attribute value for an architecture.
213func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400214 var v *LabelList
215 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen07027912021-03-15 06:02:43 -0400216 panic(fmt.Errorf("Unknown arch: %s", arch))
217 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400218 *v = value
219}
220
221func (attrs *LabelListAttribute) osValuePtrs() map[string]*LabelList {
222 return map[string]*LabelList{
223 OS_ANDROID: &attrs.OsValues.Android,
224 OS_DARWIN: &attrs.OsValues.Darwin,
225 OS_FUCHSIA: &attrs.OsValues.Fuchsia,
226 OS_LINUX: &attrs.OsValues.Linux,
227 OS_LINUX_BIONIC: &attrs.OsValues.LinuxBionic,
228 OS_WINDOWS: &attrs.OsValues.Windows,
229 }
230}
231
232// GetValueForOS returns the label_list attribute value for an OS target.
233func (attrs *LabelListAttribute) GetValueForOS(os string) LabelList {
234 var v *LabelList
235 if v = attrs.osValuePtrs()[os]; v == nil {
236 panic(fmt.Errorf("Unknown os: %s", os))
237 }
238 return *v
239}
240
241// SetValueForArch sets the label_list attribute value for an OS target.
242func (attrs *LabelListAttribute) SetValueForOS(os string, value LabelList) {
243 var v *LabelList
244 if v = attrs.osValuePtrs()[os]; v == nil {
245 panic(fmt.Errorf("Unknown os: %s", os))
246 }
247 *v = value
Jingwen Chen07027912021-03-15 06:02:43 -0400248}
249
Jingwen Chen5d864492021-02-24 07:20:12 -0500250// StringListAttribute corresponds to the string_list Bazel attribute type with
251// support for additional metadata, like configurations.
252type StringListAttribute struct {
253 // The base value of the string list attribute.
254 Value []string
255
256 // Optional additive set of list values to the base value.
257 ArchValues stringListArchValues
258}
259
260// Arch-specific string_list typed Bazel attribute values. This should correspond
261// to the types of architectures supported for compilation in arch.go.
262type stringListArchValues struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400263 X86 []string
264 X86_64 []string
265 Arm []string
266 Arm64 []string
Jingwen Chen91220d72021-03-24 02:18:33 -0400267 Common []string
Jingwen Chen5d864492021-02-24 07:20:12 -0500268}
269
Jingwen Chen91220d72021-03-24 02:18:33 -0400270// HasConfigurableValues returns true if the attribute contains
Jingwen Chen5d864492021-02-24 07:20:12 -0500271// architecture-specific string_list values.
Jingwen Chen91220d72021-03-24 02:18:33 -0400272func (attrs *StringListAttribute) HasConfigurableValues() bool {
Jingwen Chen07027912021-03-15 06:02:43 -0400273 for _, arch := range selectableArchs {
Jingwen Chen5d864492021-02-24 07:20:12 -0500274 if len(attrs.GetValueForArch(arch)) > 0 {
275 return true
276 }
277 }
278 return false
279}
280
Jingwen Chen91220d72021-03-24 02:18:33 -0400281func (attrs *StringListAttribute) archValuePtrs() map[string]*[]string {
282 return map[string]*[]string{
283 ARCH_X86: &attrs.ArchValues.X86,
284 ARCH_X86_64: &attrs.ArchValues.X86_64,
285 ARCH_ARM: &attrs.ArchValues.Arm,
286 ARCH_ARM64: &attrs.ArchValues.Arm64,
287 }
288}
289
Jingwen Chen5d864492021-02-24 07:20:12 -0500290// GetValueForArch returns the string_list attribute value for an architecture.
291func (attrs *StringListAttribute) GetValueForArch(arch string) []string {
Jingwen Chen91220d72021-03-24 02:18:33 -0400292 var v *[]string
293 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500294 panic(fmt.Errorf("Unknown arch: %s", arch))
295 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400296 return *v
Jingwen Chen5d864492021-02-24 07:20:12 -0500297}
298
299// SetValueForArch sets the string_list attribute value for an architecture.
300func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400301 var v *[]string
302 if v = attrs.archValuePtrs()[arch]; v == nil {
Jingwen Chen5d864492021-02-24 07:20:12 -0500303 panic(fmt.Errorf("Unknown arch: %s", arch))
304 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400305 *v = value
Jingwen Chen5d864492021-02-24 07:20:12 -0500306}
Liz Kammera060c452021-03-24 10:14:47 -0400307
308// TryVariableSubstitution, replace string substitution formatting within each string in slice with
309// Starlark string.format compatible tag for productVariable.
310func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
311 ret := make([]string, 0, len(slice))
312 changesMade := false
313 for _, s := range slice {
314 newS, changed := TryVariableSubstitution(s, productVariable)
315 ret = append(ret, newS)
316 changesMade = changesMade || changed
317 }
318 return ret, changesMade
319}
320
321// TryVariableSubstitution, replace string substitution formatting within s with Starlark
322// string.format compatible tag for productVariable.
323func TryVariableSubstitution(s string, productVariable string) (string, bool) {
324 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}")
325 return sub, s != sub
326}