blob: 25e110a67437601005fc3528c418d354554d855b [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"
19 "sort"
20)
Jingwen Chen5d864492021-02-24 07:20:12 -050021
Jingwen Chen73850672020-12-14 08:25:34 -050022// BazelTargetModuleProperties contain properties and metadata used for
23// Blueprint to BUILD file conversion.
24type BazelTargetModuleProperties struct {
25 // The Bazel rule class for this target.
Liz Kammerfc46bc12021-02-19 11:06:17 -050026 Rule_class string `blueprint:"mutated"`
Jingwen Chen40067de2021-01-26 21:58:43 -050027
28 // The target label for the bzl file containing the definition of the rule class.
Liz Kammerfc46bc12021-02-19 11:06:17 -050029 Bzl_load_location string `blueprint:"mutated"`
Jingwen Chen73850672020-12-14 08:25:34 -050030}
Liz Kammer356f7d42021-01-26 09:18:53 -050031
Jingwen Chenfb4692a2021-02-07 10:05:16 -050032const BazelTargetModuleNamePrefix = "__bp2build__"
33
Liz Kammer356f7d42021-01-26 09:18:53 -050034// Label is used to represent a Bazel compatible Label. Also stores the original bp text to support
35// string replacement.
36type Label struct {
37 Bp_text string
38 Label string
39}
40
41// LabelList is used to represent a list of Bazel labels.
42type LabelList struct {
43 Includes []Label
44 Excludes []Label
45}
46
47// Append appends the fields of other labelList to the corresponding fields of ll.
48func (ll *LabelList) Append(other LabelList) {
49 if len(ll.Includes) > 0 || len(other.Includes) > 0 {
50 ll.Includes = append(ll.Includes, other.Includes...)
51 }
52 if len(ll.Excludes) > 0 || len(other.Excludes) > 0 {
53 ll.Excludes = append(other.Excludes, other.Excludes...)
54 }
55}
Jingwen Chen5d864492021-02-24 07:20:12 -050056
Rupert Shuttleworth2e4219b2021-03-12 11:04:21 +000057func UniqueBazelLabels(originalLabels []Label) []Label {
58 uniqueLabelsSet := make(map[Label]bool)
59 for _, l := range originalLabels {
60 uniqueLabelsSet[l] = true
61 }
62 var uniqueLabels []Label
63 for l, _ := range uniqueLabelsSet {
64 uniqueLabels = append(uniqueLabels, l)
65 }
66 sort.SliceStable(uniqueLabels, func(i, j int) bool {
67 return uniqueLabels[i].Label < uniqueLabels[j].Label
68 })
69 return uniqueLabels
70}
71
72func UniqueBazelLabelList(originalLabelList LabelList) LabelList {
73 var uniqueLabelList LabelList
74 uniqueLabelList.Includes = UniqueBazelLabels(originalLabelList.Includes)
75 uniqueLabelList.Excludes = UniqueBazelLabels(originalLabelList.Excludes)
76 return uniqueLabelList
77}
78
Jingwen Chen07027912021-03-15 06:02:43 -040079const (
80 ARCH_X86 = "x86"
81 ARCH_X86_64 = "x86_64"
82 ARCH_ARM = "arm"
83 ARCH_ARM64 = "arm64"
84)
85
86var (
87 // This is the list of architectures with a Bazel config_setting and
88 // constraint value equivalent. is actually android.ArchTypeList, but the
89 // android package depends on the bazel package, so a cyclic dependency
90 // prevents using that here.
91 selectableArchs = []string{ARCH_X86, ARCH_X86_64, ARCH_ARM, ARCH_ARM64}
92)
93
94// Arch-specific label_list typed Bazel attribute values. This should correspond
95// to the types of architectures supported for compilation in arch.go.
96type labelListArchValues struct {
97 X86 LabelList
98 X86_64 LabelList
99 Arm LabelList
100 Arm64 LabelList
101 // TODO(b/181299724): this is currently missing the "common" arch, which
102 // doesn't have an equivalent platform() definition yet.
103}
104
105// LabelListAttribute is used to represent a list of Bazel labels as an
106// attribute.
107type LabelListAttribute struct {
108 // The non-arch specific attribute label list Value. Required.
109 Value LabelList
110
111 // The arch-specific attribute label list values. Optional. If used, these
112 // are generated in a select statement and appended to the non-arch specific
113 // label list Value.
114 ArchValues labelListArchValues
115}
116
117// MakeLabelListAttribute initializes a LabelListAttribute with the non-arch specific value.
118func MakeLabelListAttribute(value LabelList) LabelListAttribute {
119 return LabelListAttribute{Value: UniqueBazelLabelList(value)}
120}
121
122// HasArchSpecificValues returns true if the attribute contains
123// architecture-specific label_list values.
124func (attrs *LabelListAttribute) HasArchSpecificValues() bool {
125 for _, arch := range selectableArchs {
126 if len(attrs.GetValueForArch(arch).Includes) > 0 || len(attrs.GetValueForArch(arch).Excludes) > 0 {
127 return true
128 }
129 }
130 return false
131}
132
133// GetValueForArch returns the label_list attribute value for an architecture.
134func (attrs *LabelListAttribute) GetValueForArch(arch string) LabelList {
135 switch arch {
136 case ARCH_X86:
137 return attrs.ArchValues.X86
138 case ARCH_X86_64:
139 return attrs.ArchValues.X86_64
140 case ARCH_ARM:
141 return attrs.ArchValues.Arm
142 case ARCH_ARM64:
143 return attrs.ArchValues.Arm64
144 default:
145 panic(fmt.Errorf("Unknown arch: %s", arch))
146 }
147}
148
149// SetValueForArch sets the label_list attribute value for an architecture.
150func (attrs *LabelListAttribute) SetValueForArch(arch string, value LabelList) {
151 switch arch {
152 case "x86":
153 attrs.ArchValues.X86 = value
154 case "x86_64":
155 attrs.ArchValues.X86_64 = value
156 case "arm":
157 attrs.ArchValues.Arm = value
158 case "arm64":
159 attrs.ArchValues.Arm64 = value
160 default:
161 panic(fmt.Errorf("Unknown arch: %s", arch))
162 }
163}
164
Jingwen Chen5d864492021-02-24 07:20:12 -0500165// StringListAttribute corresponds to the string_list Bazel attribute type with
166// support for additional metadata, like configurations.
167type StringListAttribute struct {
168 // The base value of the string list attribute.
169 Value []string
170
171 // Optional additive set of list values to the base value.
172 ArchValues stringListArchValues
173}
174
175// Arch-specific string_list typed Bazel attribute values. This should correspond
176// to the types of architectures supported for compilation in arch.go.
177type stringListArchValues struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400178 X86 []string
179 X86_64 []string
180 Arm []string
181 Arm64 []string
Jingwen Chen5d864492021-02-24 07:20:12 -0500182 // TODO(b/181299724): this is currently missing the "common" arch, which
183 // doesn't have an equivalent platform() definition yet.
184}
185
186// HasArchSpecificValues returns true if the attribute contains
187// architecture-specific string_list values.
188func (attrs *StringListAttribute) HasArchSpecificValues() bool {
Jingwen Chen07027912021-03-15 06:02:43 -0400189 for _, arch := range selectableArchs {
Jingwen Chen5d864492021-02-24 07:20:12 -0500190 if len(attrs.GetValueForArch(arch)) > 0 {
191 return true
192 }
193 }
194 return false
195}
196
197// GetValueForArch returns the string_list attribute value for an architecture.
198func (attrs *StringListAttribute) GetValueForArch(arch string) []string {
199 switch arch {
Jingwen Chen07027912021-03-15 06:02:43 -0400200 case ARCH_X86:
Jingwen Chen5d864492021-02-24 07:20:12 -0500201 return attrs.ArchValues.X86
Jingwen Chen07027912021-03-15 06:02:43 -0400202 case ARCH_X86_64:
Jingwen Chen5d864492021-02-24 07:20:12 -0500203 return attrs.ArchValues.X86_64
Jingwen Chen07027912021-03-15 06:02:43 -0400204 case ARCH_ARM:
Jingwen Chen5d864492021-02-24 07:20:12 -0500205 return attrs.ArchValues.Arm
Jingwen Chen07027912021-03-15 06:02:43 -0400206 case ARCH_ARM64:
Jingwen Chen5d864492021-02-24 07:20:12 -0500207 return attrs.ArchValues.Arm64
Jingwen Chen5d864492021-02-24 07:20:12 -0500208 default:
209 panic(fmt.Errorf("Unknown arch: %s", arch))
210 }
211}
212
213// SetValueForArch sets the string_list attribute value for an architecture.
214func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) {
215 switch arch {
Jingwen Chen07027912021-03-15 06:02:43 -0400216 case ARCH_X86:
Jingwen Chen5d864492021-02-24 07:20:12 -0500217 attrs.ArchValues.X86 = value
Jingwen Chen07027912021-03-15 06:02:43 -0400218 case ARCH_X86_64:
Jingwen Chen5d864492021-02-24 07:20:12 -0500219 attrs.ArchValues.X86_64 = value
Jingwen Chen07027912021-03-15 06:02:43 -0400220 case ARCH_ARM:
Jingwen Chen5d864492021-02-24 07:20:12 -0500221 attrs.ArchValues.Arm = value
Jingwen Chen07027912021-03-15 06:02:43 -0400222 case ARCH_ARM64:
Jingwen Chen5d864492021-02-24 07:20:12 -0500223 attrs.ArchValues.Arm64 = value
Jingwen Chen5d864492021-02-24 07:20:12 -0500224 default:
225 panic(fmt.Errorf("Unknown arch: %s", arch))
226 }
227}