Jingwen Chen | 30f5aaa | 2020-11-19 05:38:02 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package bazel |
| 16 | |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 17 | import ( |
| 18 | "fmt" |
| 19 | "sort" |
| 20 | ) |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 21 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 22 | // BazelTargetModuleProperties contain properties and metadata used for |
| 23 | // Blueprint to BUILD file conversion. |
| 24 | type BazelTargetModuleProperties struct { |
| 25 | // The Bazel rule class for this target. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 26 | Rule_class string `blueprint:"mutated"` |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 27 | |
| 28 | // The target label for the bzl file containing the definition of the rule class. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 29 | Bzl_load_location string `blueprint:"mutated"` |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 30 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 31 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 32 | const BazelTargetModuleNamePrefix = "__bp2build__" |
| 33 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 34 | // Label is used to represent a Bazel compatible Label. Also stores the original bp text to support |
| 35 | // string replacement. |
| 36 | type Label struct { |
| 37 | Bp_text string |
| 38 | Label string |
| 39 | } |
| 40 | |
| 41 | // LabelList is used to represent a list of Bazel labels. |
| 42 | type LabelList struct { |
| 43 | Includes []Label |
| 44 | Excludes []Label |
| 45 | } |
| 46 | |
| 47 | // Append appends the fields of other labelList to the corresponding fields of ll. |
| 48 | func (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 Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 56 | |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 57 | func 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 | |
| 72 | func 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 Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 79 | const ( |
| 80 | ARCH_X86 = "x86" |
| 81 | ARCH_X86_64 = "x86_64" |
| 82 | ARCH_ARM = "arm" |
| 83 | ARCH_ARM64 = "arm64" |
| 84 | ) |
| 85 | |
| 86 | var ( |
| 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. |
| 96 | type 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. |
| 107 | type 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. |
| 118 | func 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. |
| 124 | func (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. |
| 134 | func (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. |
| 150 | func (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 Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 165 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 166 | // support for additional metadata, like configurations. |
| 167 | type 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. |
| 177 | type stringListArchValues struct { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 178 | X86 []string |
| 179 | X86_64 []string |
| 180 | Arm []string |
| 181 | Arm64 []string |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 182 | // 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. |
| 188 | func (attrs *StringListAttribute) HasArchSpecificValues() bool { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 189 | for _, arch := range selectableArchs { |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 190 | 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. |
| 198 | func (attrs *StringListAttribute) GetValueForArch(arch string) []string { |
| 199 | switch arch { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 200 | case ARCH_X86: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 201 | return attrs.ArchValues.X86 |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 202 | case ARCH_X86_64: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 203 | return attrs.ArchValues.X86_64 |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 204 | case ARCH_ARM: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 205 | return attrs.ArchValues.Arm |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 206 | case ARCH_ARM64: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 207 | return attrs.ArchValues.Arm64 |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 208 | default: |
| 209 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // SetValueForArch sets the string_list attribute value for an architecture. |
| 214 | func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) { |
| 215 | switch arch { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 216 | case ARCH_X86: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 217 | attrs.ArchValues.X86 = value |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 218 | case ARCH_X86_64: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 219 | attrs.ArchValues.X86_64 = value |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 220 | case ARCH_ARM: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 221 | attrs.ArchValues.Arm = value |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 222 | case ARCH_ARM64: |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 223 | attrs.ArchValues.Arm64 = value |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 224 | default: |
| 225 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 226 | } |
| 227 | } |