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" |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame^] | 19 | "regexp" |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 20 | "sort" |
| 21 | ) |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 22 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 23 | // BazelTargetModuleProperties contain properties and metadata used for |
| 24 | // Blueprint to BUILD file conversion. |
| 25 | type BazelTargetModuleProperties struct { |
| 26 | // The Bazel rule class for this target. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 27 | Rule_class string `blueprint:"mutated"` |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 28 | |
| 29 | // 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] | 30 | Bzl_load_location string `blueprint:"mutated"` |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 31 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 32 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 33 | const BazelTargetModuleNamePrefix = "__bp2build__" |
| 34 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame^] | 35 | var productVariableSubstitutionPattern = regexp.MustCompile("%(d|s)") |
| 36 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 37 | // Label is used to represent a Bazel compatible Label. Also stores the original bp text to support |
| 38 | // string replacement. |
| 39 | type Label struct { |
| 40 | Bp_text string |
| 41 | Label string |
| 42 | } |
| 43 | |
| 44 | // LabelList is used to represent a list of Bazel labels. |
| 45 | type LabelList struct { |
| 46 | Includes []Label |
| 47 | Excludes []Label |
| 48 | } |
| 49 | |
| 50 | // Append appends the fields of other labelList to the corresponding fields of ll. |
| 51 | func (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 Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 59 | |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 60 | func 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 | |
| 75 | func 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 Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 82 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 83 | // support for additional metadata, like configurations. |
| 84 | type StringListAttribute struct { |
| 85 | // The base value of the string list attribute. |
| 86 | Value []string |
| 87 | |
| 88 | // Optional additive set of list values to the base value. |
| 89 | ArchValues stringListArchValues |
| 90 | } |
| 91 | |
| 92 | // Arch-specific string_list typed Bazel attribute values. This should correspond |
| 93 | // to the types of architectures supported for compilation in arch.go. |
| 94 | type stringListArchValues struct { |
| 95 | X86 []string |
| 96 | X86_64 []string |
| 97 | Arm []string |
| 98 | Arm64 []string |
| 99 | Default []string |
| 100 | // TODO(b/181299724): this is currently missing the "common" arch, which |
| 101 | // doesn't have an equivalent platform() definition yet. |
| 102 | } |
| 103 | |
| 104 | // HasArchSpecificValues returns true if the attribute contains |
| 105 | // architecture-specific string_list values. |
| 106 | func (attrs *StringListAttribute) HasArchSpecificValues() bool { |
| 107 | for _, arch := range []string{"x86", "x86_64", "arm", "arm64", "default"} { |
| 108 | if len(attrs.GetValueForArch(arch)) > 0 { |
| 109 | return true |
| 110 | } |
| 111 | } |
| 112 | return false |
| 113 | } |
| 114 | |
| 115 | // GetValueForArch returns the string_list attribute value for an architecture. |
| 116 | func (attrs *StringListAttribute) GetValueForArch(arch string) []string { |
| 117 | switch arch { |
| 118 | case "x86": |
| 119 | return attrs.ArchValues.X86 |
| 120 | case "x86_64": |
| 121 | return attrs.ArchValues.X86_64 |
| 122 | case "arm": |
| 123 | return attrs.ArchValues.Arm |
| 124 | case "arm64": |
| 125 | return attrs.ArchValues.Arm64 |
| 126 | case "default": |
| 127 | return attrs.ArchValues.Default |
| 128 | default: |
| 129 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // SetValueForArch sets the string_list attribute value for an architecture. |
| 134 | func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) { |
| 135 | switch arch { |
| 136 | case "x86": |
| 137 | attrs.ArchValues.X86 = value |
| 138 | case "x86_64": |
| 139 | attrs.ArchValues.X86_64 = value |
| 140 | case "arm": |
| 141 | attrs.ArchValues.Arm = value |
| 142 | case "arm64": |
| 143 | attrs.ArchValues.Arm64 = value |
| 144 | case "default": |
| 145 | attrs.ArchValues.Default = value |
| 146 | default: |
| 147 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 148 | } |
| 149 | } |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame^] | 150 | |
| 151 | // TryVariableSubstitution, replace string substitution formatting within each string in slice with |
| 152 | // Starlark string.format compatible tag for productVariable. |
| 153 | func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) { |
| 154 | ret := make([]string, 0, len(slice)) |
| 155 | changesMade := false |
| 156 | for _, s := range slice { |
| 157 | newS, changed := TryVariableSubstitution(s, productVariable) |
| 158 | ret = append(ret, newS) |
| 159 | changesMade = changesMade || changed |
| 160 | } |
| 161 | return ret, changesMade |
| 162 | } |
| 163 | |
| 164 | // TryVariableSubstitution, replace string substitution formatting within s with Starlark |
| 165 | // string.format compatible tag for productVariable. |
| 166 | func TryVariableSubstitution(s string, productVariable string) (string, bool) { |
| 167 | sub := productVariableSubstitutionPattern.ReplaceAllString(s, "{"+productVariable+"}") |
| 168 | return sub, s != sub |
| 169 | } |