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 | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 79 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 80 | // support for additional metadata, like configurations. |
| 81 | type 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. |
| 91 | type 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. |
| 103 | func (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. |
| 113 | func (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. |
| 131 | func (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 | } |