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 | 30f5aaa | 2020-11-19 05:38:02 -0500 | [diff] [blame] | 22 | type bazelModuleProperties struct { |
| 23 | // The label of the Bazel target replacing this Soong module. |
| 24 | Label string |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 25 | |
| 26 | // If true, bp2build will generate the converted Bazel target for this module. |
| 27 | Bp2build_available bool |
Jingwen Chen | 30f5aaa | 2020-11-19 05:38:02 -0500 | [diff] [blame] | 28 | } |
| 29 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 30 | // Properties contains common module properties for Bazel migration purposes. |
Jingwen Chen | 30f5aaa | 2020-11-19 05:38:02 -0500 | [diff] [blame] | 31 | type Properties struct { |
| 32 | // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing |
| 33 | // this Soong module. |
| 34 | Bazel_module bazelModuleProperties |
| 35 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 36 | |
| 37 | // BazelTargetModuleProperties contain properties and metadata used for |
| 38 | // Blueprint to BUILD file conversion. |
| 39 | type BazelTargetModuleProperties struct { |
| 40 | // The Bazel rule class for this target. |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 41 | Rule_class string `blueprint:"mutated"` |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 42 | |
| 43 | // 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] | 44 | Bzl_load_location string `blueprint:"mutated"` |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 45 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 46 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 47 | const BazelTargetModuleNamePrefix = "__bp2build__" |
| 48 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 49 | // Label is used to represent a Bazel compatible Label. Also stores the original bp text to support |
| 50 | // string replacement. |
| 51 | type Label struct { |
| 52 | Bp_text string |
| 53 | Label string |
| 54 | } |
| 55 | |
| 56 | // LabelList is used to represent a list of Bazel labels. |
| 57 | type LabelList struct { |
| 58 | Includes []Label |
| 59 | Excludes []Label |
| 60 | } |
| 61 | |
| 62 | // Append appends the fields of other labelList to the corresponding fields of ll. |
| 63 | func (ll *LabelList) Append(other LabelList) { |
| 64 | if len(ll.Includes) > 0 || len(other.Includes) > 0 { |
| 65 | ll.Includes = append(ll.Includes, other.Includes...) |
| 66 | } |
| 67 | if len(ll.Excludes) > 0 || len(other.Excludes) > 0 { |
| 68 | ll.Excludes = append(other.Excludes, other.Excludes...) |
| 69 | } |
| 70 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 71 | |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame^] | 72 | func UniqueBazelLabels(originalLabels []Label) []Label { |
| 73 | uniqueLabelsSet := make(map[Label]bool) |
| 74 | for _, l := range originalLabels { |
| 75 | uniqueLabelsSet[l] = true |
| 76 | } |
| 77 | var uniqueLabels []Label |
| 78 | for l, _ := range uniqueLabelsSet { |
| 79 | uniqueLabels = append(uniqueLabels, l) |
| 80 | } |
| 81 | sort.SliceStable(uniqueLabels, func(i, j int) bool { |
| 82 | return uniqueLabels[i].Label < uniqueLabels[j].Label |
| 83 | }) |
| 84 | return uniqueLabels |
| 85 | } |
| 86 | |
| 87 | func UniqueBazelLabelList(originalLabelList LabelList) LabelList { |
| 88 | var uniqueLabelList LabelList |
| 89 | uniqueLabelList.Includes = UniqueBazelLabels(originalLabelList.Includes) |
| 90 | uniqueLabelList.Excludes = UniqueBazelLabels(originalLabelList.Excludes) |
| 91 | return uniqueLabelList |
| 92 | } |
| 93 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 94 | // StringListAttribute corresponds to the string_list Bazel attribute type with |
| 95 | // support for additional metadata, like configurations. |
| 96 | type StringListAttribute struct { |
| 97 | // The base value of the string list attribute. |
| 98 | Value []string |
| 99 | |
| 100 | // Optional additive set of list values to the base value. |
| 101 | ArchValues stringListArchValues |
| 102 | } |
| 103 | |
| 104 | // Arch-specific string_list typed Bazel attribute values. This should correspond |
| 105 | // to the types of architectures supported for compilation in arch.go. |
| 106 | type stringListArchValues struct { |
| 107 | X86 []string |
| 108 | X86_64 []string |
| 109 | Arm []string |
| 110 | Arm64 []string |
| 111 | Default []string |
| 112 | // TODO(b/181299724): this is currently missing the "common" arch, which |
| 113 | // doesn't have an equivalent platform() definition yet. |
| 114 | } |
| 115 | |
| 116 | // HasArchSpecificValues returns true if the attribute contains |
| 117 | // architecture-specific string_list values. |
| 118 | func (attrs *StringListAttribute) HasArchSpecificValues() bool { |
| 119 | for _, arch := range []string{"x86", "x86_64", "arm", "arm64", "default"} { |
| 120 | if len(attrs.GetValueForArch(arch)) > 0 { |
| 121 | return true |
| 122 | } |
| 123 | } |
| 124 | return false |
| 125 | } |
| 126 | |
| 127 | // GetValueForArch returns the string_list attribute value for an architecture. |
| 128 | func (attrs *StringListAttribute) GetValueForArch(arch string) []string { |
| 129 | switch arch { |
| 130 | case "x86": |
| 131 | return attrs.ArchValues.X86 |
| 132 | case "x86_64": |
| 133 | return attrs.ArchValues.X86_64 |
| 134 | case "arm": |
| 135 | return attrs.ArchValues.Arm |
| 136 | case "arm64": |
| 137 | return attrs.ArchValues.Arm64 |
| 138 | case "default": |
| 139 | return attrs.ArchValues.Default |
| 140 | default: |
| 141 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // SetValueForArch sets the string_list attribute value for an architecture. |
| 146 | func (attrs *StringListAttribute) SetValueForArch(arch string, value []string) { |
| 147 | switch arch { |
| 148 | case "x86": |
| 149 | attrs.ArchValues.X86 = value |
| 150 | case "x86_64": |
| 151 | attrs.ArchValues.X86_64 = value |
| 152 | case "arm": |
| 153 | attrs.ArchValues.Arm = value |
| 154 | case "arm64": |
| 155 | attrs.ArchValues.Arm64 = value |
| 156 | case "default": |
| 157 | attrs.ArchValues.Default = value |
| 158 | default: |
| 159 | panic(fmt.Errorf("Unknown arch: %s", arch)) |
| 160 | } |
| 161 | } |