blob: abdc1077de41333acc31f91cb4000a265bf9dab0 [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 Chen5d864492021-02-24 07:20:12 -050079// StringListAttribute corresponds to the string_list Bazel attribute type with
80// support for additional metadata, like configurations.
81type StringListAttribute struct {
82 // The base value of the string list attribute.
83 Value []string
84
85 // Optional additive set of list values to the base value.
86 ArchValues stringListArchValues
87}
88
89// Arch-specific string_list typed Bazel attribute values. This should correspond
90// to the types of architectures supported for compilation in arch.go.
91type stringListArchValues struct {
92 X86 []string
93 X86_64 []string
94 Arm []string
95 Arm64 []string
96 Default []string
97 // TODO(b/181299724): this is currently missing the "common" arch, which
98 // doesn't have an equivalent platform() definition yet.
99}
100
101// HasArchSpecificValues returns true if the attribute contains
102// architecture-specific string_list values.
103func (attrs *StringListAttribute) HasArchSpecificValues() bool {
104 for _, arch := range []string{"x86", "x86_64", "arm", "arm64", "default"} {
105 if len(attrs.GetValueForArch(arch)) > 0 {
106 return true
107 }
108 }
109 return false
110}
111
112// GetValueForArch returns the string_list attribute value for an architecture.
113func (attrs *StringListAttribute) GetValueForArch(arch string) []string {
114 switch arch {
115 case "x86":
116 return attrs.ArchValues.X86
117 case "x86_64":
118 return attrs.ArchValues.X86_64
119 case "arm":
120 return attrs.ArchValues.Arm
121 case "arm64":
122 return attrs.ArchValues.Arm64
123 case "default":
124 return attrs.ArchValues.Default
125 default:
126 panic(fmt.Errorf("Unknown arch: %s", arch))
127 }
128}
129
130// SetValueForArch sets the string_list attribute value for an architecture.
131func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) {
132 switch arch {
133 case "x86":
134 attrs.ArchValues.X86 = value
135 case "x86_64":
136 attrs.ArchValues.X86_64 = value
137 case "arm":
138 attrs.ArchValues.Arm = value
139 case "arm64":
140 attrs.ArchValues.Arm64 = value
141 case "default":
142 attrs.ArchValues.Default = value
143 default:
144 panic(fmt.Errorf("Unknown arch: %s", arch))
145 }
146}