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