blob: 1763f2dc05400f37a947018e82ae55fd8583ad16 [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 (
83 ARCH_X86 = "x86"
84 ARCH_X86_64 = "x86_64"
85 ARCH_ARM = "arm"
86 ARCH_ARM64 = "arm64"
87)
88
89var (
90 // This is the list of architectures with a Bazel config_setting and
91 // constraint value equivalent. is actually android.ArchTypeList, but the
92 // android package depends on the bazel package, so a cyclic dependency
93 // prevents using that here.
94 selectableArchs = []string{ARCH_X86, ARCH_X86_64, ARCH_ARM, ARCH_ARM64}
95)
96
97// Arch-specific label_list typed Bazel attribute values. This should correspond
98// to the types of architectures supported for compilation in arch.go.
99type labelListArchValues struct {
100 X86 LabelList
101 X86_64 LabelList
102 Arm LabelList
103 Arm64 LabelList
104 // TODO(b/181299724): this is currently missing the "common" arch, which
105 // doesn't have an equivalent platform() definition yet.
106}
107
108// LabelListAttribute is used to represent a list of Bazel labels as an
109// attribute.
110type LabelListAttribute struct {
111 // The non-arch specific attribute label list Value. Required.
112 Value LabelList
113
114 // The arch-specific attribute label list values. Optional. If used, these
115 // are generated in a select statement and appended to the non-arch specific
116 // label list Value.
117 ArchValues labelListArchValues
118}
119
120// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
121func MakeLabelListAttribute(value LabelList) LabelListAttribute {
122 return LabelListAttribute{Value: UniqueBazelLabelList(value)}
123}
124
125// HasArchSpecificValues returns true if the attribute contains
126// architecture-specific label_list values.
127func (attrs *LabelListAttribute) HasArchSpecificValues() bool {
128 for _, arch := range selectableArchs {
129 if len(attrs.GetValueForArch(arch).Includes) > 0 || len(attrs.GetValueForArch(arch).Excludes) > 0 {
130 return true
131 }
132 }
133 return false
134}
135
136// GetValueForArch returns the label_list attribute value for an architecture.
137func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList {
138 switch arch {
139 case ARCH_X86:
140 return attrs.ArchValues.X86
141 case ARCH_X86_64:
142 return attrs.ArchValues.X86_64
143 case ARCH_ARM:
144 return attrs.ArchValues.Arm
145 case ARCH_ARM64:
146 return attrs.ArchValues.Arm64
147 default:
148 panic(fmt.Errorf("Unknown arch: %s", arch))
149 }
150}
151
152// SetValueForArch sets the label_list attribute value for an architecture.
153func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) {
154 switch arch {
155 case "x86":
156 attrs.ArchValues.X86 = value
157 case "x86_64":
158 attrs.ArchValues.X86_64 = value
159 case "arm":
160 attrs.ArchValues.Arm = value
161 case "arm64":
162 attrs.ArchValues.Arm64 = value
163 default:
164 panic(fmt.Errorf("Unknown arch: %s", arch))
165 }
166}
167
Jingwen Chen5d864492021-02-24 07:20:12 -0500168// StringListAttribute corresponds to the string_list Bazel attribute type with
169// support for additional metadata, like configurations.
170type StringListAttribute struct {
171 // The base value of the string list attribute.
172 Value []string
173
174 // Optional additive set of list values to the base value.
175 ArchValues stringListArchValues
176}
177
178// Arch-specific string_list typed Bazel attribute values. This should correspond
179// to the types of architectures supported for compilation in arch.go.
180type stringListArchValues struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400181 X86 []string
182 X86_64 []string
183 Arm []string
184 Arm64 []string
Jingwen Chen5d864492021-02-24 07:20:12 -0500185 // TODO(b/181299724): this is currently missing the "common" arch, which
186 // doesn't have an equivalent platform() definition yet.
187}
188
189// HasArchSpecificValues returns true if the attribute contains
190// architecture-specific string_list values.
191func (attrs *StringListAttribute) HasArchSpecificValues() bool {
Jingwen Chen07027912021-03-15 06:02:43 -0400192 for _, arch := range selectableArchs {
Jingwen Chen5d864492021-02-24 07:20:12 -0500193 if len(attrs.GetValueForArch(arch)) > 0 {
194 return true
195 }
196 }
197 return false
198}
199
200// GetValueForArch returns the string_list attribute value for an architecture.
201func (attrs *StringListAttribute) GetValueForArch(arch string) []string {
202 switch arch {
Jingwen Chen07027912021-03-15 06:02:43 -0400203 case ARCH_X86:
Jingwen Chen5d864492021-02-24 07:20:12 -0500204 return attrs.ArchValues.X86
Jingwen Chen07027912021-03-15 06:02:43 -0400205 case ARCH_X86_64:
Jingwen Chen5d864492021-02-24 07:20:12 -0500206 return attrs.ArchValues.X86_64
Jingwen Chen07027912021-03-15 06:02:43 -0400207 case ARCH_ARM:
Jingwen Chen5d864492021-02-24 07:20:12 -0500208 return attrs.ArchValues.Arm
Jingwen Chen07027912021-03-15 06:02:43 -0400209 case ARCH_ARM64:
Jingwen Chen5d864492021-02-24 07:20:12 -0500210 return attrs.ArchValues.Arm64
Jingwen Chen5d864492021-02-24 07:20:12 -0500211 default:
212 panic(fmt.Errorf("Unknown arch: %s", arch))
213 }
214}
215
216// SetValueForArch sets the string_list attribute value for an architecture.
217func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) {
218 switch arch {
Jingwen Chen07027912021-03-15 06:02:43 -0400219 case ARCH_X86:
Jingwen Chen5d864492021-02-24 07:20:12 -0500220 attrs.ArchValues.X86 = value
Jingwen Chen07027912021-03-15 06:02:43 -0400221 case ARCH_X86_64:
Jingwen Chen5d864492021-02-24 07:20:12 -0500222 attrs.ArchValues.X86_64 = value
Jingwen Chen07027912021-03-15 06:02:43 -0400223 case ARCH_ARM:
Jingwen Chen5d864492021-02-24 07:20:12 -0500224 attrs.ArchValues.Arm = value
Jingwen Chen07027912021-03-15 06:02:43 -0400225 case ARCH_ARM64:
Jingwen Chen5d864492021-02-24 07:20:12 -0500226 attrs.ArchValues.Arm64 = value
Jingwen Chen5d864492021-02-24 07:20:12 -0500227 default:
228 panic(fmt.Errorf("Unknown arch: %s", arch))
229 }
230}
Liz Kammera060c452021-03-24 10:14:47 -0400231
232// TryVariableSubstitution, replace string substitution formatting within each string in slice with
233// Starlark string.format compatible tag for productVariable.
234func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {
235 ret := make([]string, 0, len(slice))
236 changesMade := false
237 for _, s := range slice {
238 newS, changed := TryVariableSubstitution(s, productVariable)
239 ret = append(ret, newS)
240 changesMade = changesMade || changed
241 }
242 return ret, changesMade
243}
244
245// TryVariableSubstitution, replace string substitution formatting within s with Starlark
246// string.format compatible tag for productVariable.
247func TryVariableSubstitution(s string, productVariable string) (string, bool) {
248 sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}")
249 return sub, s != sub
250}