Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 1 | // Copyright 2021 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 android |
| 16 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 17 | import ( |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 18 | "bufio" |
| 19 | "errors" |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 20 | "fmt" |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 21 | "strings" |
| 22 | |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 23 | "android/soong/ui/metrics/bp2build_metrics_proto" |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 26 | |
| 27 | "android/soong/android/allowlists" |
| 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | // A sentinel value to be used as a key in Bp2BuildConfig for modules with |
| 32 | // no package path. This is also the module dir for top level Android.bp |
| 33 | // modules. |
| 34 | Bp2BuildTopLevel = "." |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 35 | ) |
| 36 | |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 37 | type MixedBuildEnabledStatus int |
| 38 | |
| 39 | const ( |
| 40 | // This module can be mixed_built. |
| 41 | MixedBuildEnabled = iota |
| 42 | |
| 43 | // There is a technical incompatibility preventing this module from being |
| 44 | // bazel-analyzed. Note: the module might also be incompatible. |
| 45 | TechnicalIncompatibility |
| 46 | |
| 47 | // This module cannot be mixed_built due to some incompatibility with it |
| 48 | // that is not a platform incompatibility. Example: the module-type is not |
| 49 | // enabled, or is not bp2build-converted. |
| 50 | ModuleIncompatibility |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 51 | |
| 52 | // Missing dependencies. We can't query Bazel for modules if it has missing dependencies, there |
| 53 | // will be failures. |
| 54 | ModuleMissingDeps |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 55 | ) |
| 56 | |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 57 | // FileGroupAsLibrary describes a filegroup module that is converted to some library |
| 58 | // such as aidl_library or proto_library. |
| 59 | type FileGroupAsLibrary interface { |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 60 | ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 61 | ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 62 | GetAidlLibraryLabel(ctx BazelConversionPathContext) string |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 63 | GetProtoLibraryLabel(ctx BazelConversionPathContext) string |
Vinh Tran | 444154d | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Sasha Smundak | a095406 | 2022-08-02 18:23:58 -0700 | [diff] [blame] | 66 | type BazelConversionStatus struct { |
| 67 | // Information about _all_ bp2build targets generated by this module. Multiple targets are |
| 68 | // supported as Soong handles some things within a single target that we may choose to split into |
| 69 | // multiple targets, e.g. renderscript, protos, yacc within a cc module. |
| 70 | Bp2buildInfo []bp2buildInfo `blueprint:"mutated"` |
| 71 | |
| 72 | // UnconvertedBp2buildDep stores the module names of direct dependency that were not converted to |
| 73 | // Bazel |
| 74 | UnconvertedDeps []string `blueprint:"mutated"` |
| 75 | |
| 76 | // MissingBp2buildDep stores the module names of direct dependency that were not found |
| 77 | MissingDeps []string `blueprint:"mutated"` |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 78 | |
| 79 | // If non-nil, indicates that the module could not be converted successfully |
| 80 | // with bp2build. This will describe the reason the module could not be converted. |
| 81 | UnconvertedReason *UnconvertedReason |
| 82 | } |
| 83 | |
| 84 | // The reason a module could not be converted to a BUILD target via bp2build. |
| 85 | // This should match bp2build_metrics_proto.UnconvertedReason, but omits private |
| 86 | // proto-related fields that prevent copying this struct. |
| 87 | type UnconvertedReason struct { |
| 88 | // Should correspond to a valid value in bp2build_metrics_proto.UnconvertedReasonType. |
| 89 | // A raw int is used here instead, because blueprint logic requires that all transitive |
| 90 | // fields of module definitions be primitives. |
| 91 | ReasonType int |
| 92 | Detail string |
Sasha Smundak | a095406 | 2022-08-02 18:23:58 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Romain Jobredeaux | 8242b43 | 2023-05-04 10:16:26 -0400 | [diff] [blame] | 95 | type BazelModuleProperties struct { |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 96 | // The label of the Bazel target replacing this Soong module. When run in conversion mode, this |
| 97 | // will import the handcrafted build target into the autogenerated file. Note: this may result in |
| 98 | // a conflict due to duplicate targets if bp2build_available is also set. |
| 99 | Label *string |
| 100 | |
| 101 | // If true, bp2build will generate the converted Bazel target for this module. Note: this may |
| 102 | // cause a conflict due to the duplicate targets if label is also set. |
| 103 | // |
| 104 | // This is a bool pointer to support tristates: true, false, not set. |
| 105 | // |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 106 | // To opt in a module, set bazel_module: { bp2build_available: true } |
| 107 | // To opt out a module, set bazel_module: { bp2build_available: false } |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 108 | // To defer the default setting for the directory, do not set the value. |
| 109 | Bp2build_available *bool |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 110 | |
| 111 | // CanConvertToBazel is set via InitBazelModule to indicate that a module type can be converted to |
| 112 | // Bazel with Bp2build. |
| 113 | CanConvertToBazel bool `blueprint:"mutated"` |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 116 | // Properties contains common module properties for Bazel migration purposes. |
| 117 | type properties struct { |
Chris Parsons | 1bb58da | 2022-08-30 13:37:57 -0400 | [diff] [blame] | 118 | // In "Bazel mixed build" mode, this represents the Bazel target replacing |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 119 | // this Soong module. |
Romain Jobredeaux | 8242b43 | 2023-05-04 10:16:26 -0400 | [diff] [blame] | 120 | Bazel_module BazelModuleProperties |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 121 | } |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 122 | |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 123 | // namespacedVariableProperties is a map from a string representing a Soong |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 124 | // config variable namespace, like "android" or "vendor_name" to a slice of |
| 125 | // pointer to a struct containing a single field called Soong_config_variables |
| 126 | // whose value mirrors the structure in the Blueprint file. |
| 127 | type namespacedVariableProperties map[string][]interface{} |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 128 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 129 | // BazelModuleBase contains the property structs with metadata for modules which can be converted to |
| 130 | // Bazel. |
| 131 | type BazelModuleBase struct { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 132 | bazelProperties properties |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 133 | |
| 134 | // namespacedVariableProperties is used for soong_config_module_type support |
| 135 | // in bp2build. Soong config modules allow users to set module properties |
| 136 | // based on custom product variables defined in Android.bp files. These |
| 137 | // variables are namespaced to prevent clobbering, especially when set from |
| 138 | // Makefiles. |
| 139 | namespacedVariableProperties namespacedVariableProperties |
| 140 | |
| 141 | // baseModuleType is set when this module was created from a module type |
| 142 | // defined by a soong_config_module_type. Every soong_config_module_type |
| 143 | // "wraps" another module type, e.g. a soong_config_module_type can wrap a |
| 144 | // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary. |
| 145 | // This baseModuleType is set to the wrapped module type. |
| 146 | baseModuleType string |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Bazelable is specifies the interface for modules that can be converted to Bazel. |
| 150 | type Bazelable interface { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 151 | bazelProps() *properties |
| 152 | HasHandcraftedLabel() bool |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 153 | HandcraftedLabel() string |
| 154 | GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 155 | ShouldConvertWithBp2build(ctx BazelConversionContext) bool |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 156 | shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 157 | |
| 158 | // ConvertWithBp2build either converts the module to a Bazel build target or |
| 159 | // declares the module as unconvertible (for logging and metrics). |
| 160 | // Modules must implement this function to be bp2build convertible. The function |
| 161 | // must either create at least one Bazel target module (using ctx.CreateBazelTargetModule or |
| 162 | // its related functions), or declare itself unconvertible using ctx.MarkBp2buildUnconvertible. |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 163 | ConvertWithBp2build(ctx TopDownMutatorContext) |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 164 | |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 165 | // namespacedVariableProps is a map from a soong config variable namespace |
| 166 | // (e.g. acme, android) to a map of interfaces{}, which are really |
| 167 | // reflect.Struct pointers, representing the value of the |
| 168 | // soong_config_variables property of a module. The struct pointer is the |
| 169 | // one with the single member called Soong_config_variables, which itself is |
| 170 | // a struct containing fields for each supported feature in that namespace. |
| 171 | // |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 172 | // The reason for using a slice of interface{} is to support defaults |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 173 | // propagation of the struct pointers. |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 174 | namespacedVariableProps() namespacedVariableProperties |
| 175 | setNamespacedVariableProps(props namespacedVariableProperties) |
| 176 | BaseModuleType() string |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 177 | SetBaseModuleType(baseModuleType string) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 180 | // ApiProvider is implemented by modules that contribute to an API surface |
| 181 | type ApiProvider interface { |
| 182 | ConvertWithApiBp2build(ctx TopDownMutatorContext) |
| 183 | } |
| 184 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 185 | // MixedBuildBuildable is an interface that module types should implement in order |
| 186 | // to be "handled by Bazel" in a mixed build. |
| 187 | type MixedBuildBuildable interface { |
| 188 | // IsMixedBuildSupported returns true if and only if this module should be |
| 189 | // "handled by Bazel" in a mixed build. |
| 190 | // This "escape hatch" allows modules with corner-case scenarios to opt out |
| 191 | // of being built with Bazel. |
| 192 | IsMixedBuildSupported(ctx BaseModuleContext) bool |
| 193 | |
| 194 | // QueueBazelCall invokes request-queueing functions on the BazelContext |
| 195 | // so that these requests are handled when Bazel's cquery is invoked. |
| 196 | QueueBazelCall(ctx BaseModuleContext) |
| 197 | |
| 198 | // ProcessBazelQueryResponse uses Bazel information (obtained from the BazelContext) |
| 199 | // to set module fields and providers to propagate this module's metadata upstream. |
| 200 | // This effectively "bridges the gap" between Bazel and Soong in a mixed build. |
| 201 | // Soong modules depending on this module should be oblivious to the fact that |
| 202 | // this module was handled by Bazel. |
| 203 | ProcessBazelQueryResponse(ctx ModuleContext) |
| 204 | } |
| 205 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 206 | // BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules. |
| 207 | type BazelModule interface { |
| 208 | Module |
| 209 | Bazelable |
| 210 | } |
| 211 | |
| 212 | // InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion |
| 213 | // properties. |
| 214 | func InitBazelModule(module BazelModule) { |
| 215 | module.AddProperties(module.bazelProps()) |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 216 | module.bazelProps().Bazel_module.CanConvertToBazel = true |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // bazelProps returns the Bazel properties for the given BazelModuleBase. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 220 | func (b *BazelModuleBase) bazelProps() *properties { |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 221 | return &b.bazelProperties |
| 222 | } |
| 223 | |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 224 | func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties { |
| 225 | return b.namespacedVariableProperties |
| 226 | } |
| 227 | |
| 228 | func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) { |
| 229 | b.namespacedVariableProperties = props |
| 230 | } |
| 231 | |
| 232 | func (b *BazelModuleBase) BaseModuleType() string { |
| 233 | return b.baseModuleType |
| 234 | } |
| 235 | |
| 236 | func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) { |
| 237 | b.baseModuleType = baseModuleType |
| 238 | } |
| 239 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 240 | // HasHandcraftedLabel returns whether this module has a handcrafted Bazel label. |
| 241 | func (b *BazelModuleBase) HasHandcraftedLabel() bool { |
| 242 | return b.bazelProperties.Bazel_module.Label != nil |
| 243 | } |
| 244 | |
| 245 | // HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none |
| 246 | func (b *BazelModuleBase) HandcraftedLabel() string { |
| 247 | return proptools.String(b.bazelProperties.Bazel_module.Label) |
| 248 | } |
| 249 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 250 | // GetBazelLabel returns the Bazel label for the given BazelModuleBase. |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 251 | func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string { |
| 252 | if b.HasHandcraftedLabel() { |
| 253 | return b.HandcraftedLabel() |
| 254 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 255 | if b.ShouldConvertWithBp2build(ctx) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 256 | return bp2buildModuleLabel(ctx, module) |
| 257 | } |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 258 | panic(fmt.Errorf("requested non-existent label for module ", module.Name())) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 259 | } |
| 260 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 261 | type Bp2BuildConversionAllowlist struct { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 262 | // Configure modules in these directories to enable bp2build_available: true or false by default. |
| 263 | defaultConfig allowlists.Bp2BuildConfig |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 264 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 265 | // Keep any existing BUILD files (and do not generate new BUILD files) for these directories |
Jingwen Chen | b643c7a | 2021-07-26 04:45:48 +0000 | [diff] [blame] | 266 | // in the synthetic Bazel workspace. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 267 | keepExistingBuildFile map[string]bool |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 268 | |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 269 | // Per-module allowlist to always opt modules into both bp2build and Bazel Dev Mode mixed |
| 270 | // builds. These modules are usually in directories with many other modules that are not ready |
| 271 | // for conversion. |
Jingwen Chen | 7edadab | 2022-03-04 07:01:29 +0000 | [diff] [blame] | 272 | // |
| 273 | // A module can either be in this list or its directory allowlisted entirely |
| 274 | // in bp2buildDefaultConfig, but not both at the same time. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 275 | moduleAlwaysConvert map[string]bool |
Sam Delmerico | fa1831c | 2022-02-22 18:07:55 +0000 | [diff] [blame] | 276 | |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 277 | // Per-module-type allowlist to always opt modules in to both bp2build and |
| 278 | // Bazel Dev Mode mixed builds when they have the same type as one listed. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 279 | moduleTypeAlwaysConvert map[string]bool |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 280 | |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 281 | // Per-module denylist to always opt modules out of bp2build conversion. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 282 | moduleDoNotConvert map[string]bool |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 283 | } |
Liz Kammer | 5c31358 | 2021-12-03 15:23:26 -0500 | [diff] [blame] | 284 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 285 | // NewBp2BuildAllowlist creates a new, empty Bp2BuildConversionAllowlist |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 286 | // which can be populated using builder pattern Set* methods |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 287 | func NewBp2BuildAllowlist() Bp2BuildConversionAllowlist { |
| 288 | return Bp2BuildConversionAllowlist{ |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 289 | allowlists.Bp2BuildConfig{}, |
| 290 | map[string]bool{}, |
| 291 | map[string]bool{}, |
| 292 | map[string]bool{}, |
| 293 | map[string]bool{}, |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 297 | // SetDefaultConfig copies the entries from defaultConfig into the allowlist |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 298 | func (a Bp2BuildConversionAllowlist) SetDefaultConfig(defaultConfig allowlists.Bp2BuildConfig) Bp2BuildConversionAllowlist { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 299 | if a.defaultConfig == nil { |
| 300 | a.defaultConfig = allowlists.Bp2BuildConfig{} |
| 301 | } |
| 302 | for k, v := range defaultConfig { |
| 303 | a.defaultConfig[k] = v |
| 304 | } |
| 305 | |
| 306 | return a |
| 307 | } |
| 308 | |
| 309 | // SetKeepExistingBuildFile copies the entries from keepExistingBuildFile into the allowlist |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 310 | func (a Bp2BuildConversionAllowlist) SetKeepExistingBuildFile(keepExistingBuildFile map[string]bool) Bp2BuildConversionAllowlist { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 311 | if a.keepExistingBuildFile == nil { |
| 312 | a.keepExistingBuildFile = map[string]bool{} |
| 313 | } |
| 314 | for k, v := range keepExistingBuildFile { |
| 315 | a.keepExistingBuildFile[k] = v |
| 316 | } |
| 317 | |
| 318 | return a |
| 319 | } |
| 320 | |
| 321 | // SetModuleAlwaysConvertList copies the entries from moduleAlwaysConvert into the allowlist |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 322 | func (a Bp2BuildConversionAllowlist) SetModuleAlwaysConvertList(moduleAlwaysConvert []string) Bp2BuildConversionAllowlist { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 323 | if a.moduleAlwaysConvert == nil { |
| 324 | a.moduleAlwaysConvert = map[string]bool{} |
| 325 | } |
| 326 | for _, m := range moduleAlwaysConvert { |
| 327 | a.moduleAlwaysConvert[m] = true |
| 328 | } |
| 329 | |
| 330 | return a |
| 331 | } |
| 332 | |
| 333 | // SetModuleTypeAlwaysConvertList copies the entries from moduleTypeAlwaysConvert into the allowlist |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 334 | func (a Bp2BuildConversionAllowlist) SetModuleTypeAlwaysConvertList(moduleTypeAlwaysConvert []string) Bp2BuildConversionAllowlist { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 335 | if a.moduleTypeAlwaysConvert == nil { |
| 336 | a.moduleTypeAlwaysConvert = map[string]bool{} |
| 337 | } |
| 338 | for _, m := range moduleTypeAlwaysConvert { |
| 339 | a.moduleTypeAlwaysConvert[m] = true |
| 340 | } |
| 341 | |
| 342 | return a |
| 343 | } |
| 344 | |
| 345 | // SetModuleDoNotConvertList copies the entries from moduleDoNotConvert into the allowlist |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 346 | func (a Bp2BuildConversionAllowlist) SetModuleDoNotConvertList(moduleDoNotConvert []string) Bp2BuildConversionAllowlist { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 347 | if a.moduleDoNotConvert == nil { |
| 348 | a.moduleDoNotConvert = map[string]bool{} |
| 349 | } |
| 350 | for _, m := range moduleDoNotConvert { |
| 351 | a.moduleDoNotConvert[m] = true |
| 352 | } |
| 353 | |
| 354 | return a |
| 355 | } |
| 356 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 357 | // ShouldKeepExistingBuildFileForDir returns whether an existing BUILD file should be |
| 358 | // added to the build symlink forest based on the current global configuration. |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 359 | func (a Bp2BuildConversionAllowlist) ShouldKeepExistingBuildFileForDir(dir string) bool { |
| 360 | if _, ok := a.keepExistingBuildFile[dir]; ok { |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 361 | // Exact dir match |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 362 | return true |
| 363 | } |
Usta Shrestha | ea99964 | 2022-11-02 01:03:07 -0400 | [diff] [blame] | 364 | var i int |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 365 | // Check if subtree match |
Usta Shrestha | ea99964 | 2022-11-02 01:03:07 -0400 | [diff] [blame] | 366 | for { |
| 367 | j := strings.Index(dir[i:], "/") |
| 368 | if j == -1 { |
| 369 | return false //default |
| 370 | } |
| 371 | prefix := dir[0 : i+j] |
| 372 | i = i + j + 1 // skip the "/" |
| 373 | if recursive, ok := a.keepExistingBuildFile[prefix]; ok && recursive { |
| 374 | return true |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 375 | } |
| 376 | } |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 377 | } |
| 378 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 379 | var bp2BuildAllowListKey = NewOnceKey("Bp2BuildAllowlist") |
| 380 | var bp2buildAllowlist OncePer |
| 381 | |
| 382 | func GetBp2BuildAllowList() Bp2BuildConversionAllowlist { |
| 383 | return bp2buildAllowlist.Once(bp2BuildAllowListKey, func() interface{} { |
| 384 | return NewBp2BuildAllowlist().SetDefaultConfig(allowlists.Bp2buildDefaultConfig). |
| 385 | SetKeepExistingBuildFile(allowlists.Bp2buildKeepExistingBuildFile). |
| 386 | SetModuleAlwaysConvertList(allowlists.Bp2buildModuleAlwaysConvertList). |
| 387 | SetModuleTypeAlwaysConvertList(allowlists.Bp2buildModuleTypeAlwaysConvertList). |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 388 | SetModuleDoNotConvertList(allowlists.Bp2buildModuleDoNotConvertList) |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 389 | }).(Bp2BuildConversionAllowlist) |
| 390 | } |
| 391 | |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 392 | // MixedBuildsEnabled returns a MixedBuildEnabledStatus regarding whether |
| 393 | // a module is ready to be replaced by a converted or handcrafted Bazel target. |
| 394 | // As a side effect, calling this method will also log whether this module is |
| 395 | // mixed build enabled for metrics reporting. |
| 396 | func MixedBuildsEnabled(ctx BaseModuleContext) MixedBuildEnabledStatus { |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 397 | platformIncompatible := isPlatformIncompatible(ctx.Os(), ctx.Arch().ArchType) |
| 398 | if platformIncompatible { |
| 399 | ctx.Config().LogMixedBuild(ctx, false) |
| 400 | return TechnicalIncompatibility |
| 401 | } |
| 402 | |
Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 403 | if ctx.Config().AllowMissingDependencies() { |
| 404 | missingDeps := ctx.getMissingDependencies() |
| 405 | // If there are missing dependencies, querying Bazel will fail. Soong instead fails at execution |
| 406 | // time, not loading/analysis. disable mixed builds and fall back to Soong to maintain that |
| 407 | // behavior. |
| 408 | if len(missingDeps) > 0 { |
| 409 | ctx.Config().LogMixedBuild(ctx, false) |
| 410 | return ModuleMissingDeps |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | module := ctx.Module() |
| 415 | apexInfo := ctx.Provider(ApexInfoProvider).(ApexInfo) |
| 416 | withinApex := !apexInfo.IsForPlatform() |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 417 | mixedBuildEnabled := ctx.Config().IsMixedBuildsEnabled() && |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 418 | module.Enabled() && |
| 419 | convertedToBazel(ctx, module) && |
Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 420 | ctx.Config().BazelContext.IsModuleNameAllowed(module.Name(), withinApex) |
MarkDacek | ff851b8 | 2022-04-21 18:33:17 +0000 | [diff] [blame] | 421 | ctx.Config().LogMixedBuild(ctx, mixedBuildEnabled) |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 422 | |
| 423 | if mixedBuildEnabled { |
| 424 | return MixedBuildEnabled |
| 425 | } |
| 426 | return ModuleIncompatibility |
MarkDacek | ff851b8 | 2022-04-21 18:33:17 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 429 | // ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 430 | func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 431 | b, ok := module.(Bazelable) |
| 432 | if !ok { |
| 433 | return false |
| 434 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 435 | return b.shouldConvertWithBp2build(ctx, module) || b.HasHandcraftedLabel() |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 438 | // ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 439 | func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx BazelConversionContext) bool { |
| 440 | return b.shouldConvertWithBp2build(ctx, ctx.Module()) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 443 | type bazelOtherModuleContext interface { |
| 444 | ModuleErrorf(format string, args ...interface{}) |
| 445 | Config() Config |
| 446 | OtherModuleType(m blueprint.Module) string |
| 447 | OtherModuleName(m blueprint.Module) string |
| 448 | OtherModuleDir(m blueprint.Module) string |
| 449 | } |
Sam Delmerico | fa1831c | 2022-02-22 18:07:55 +0000 | [diff] [blame] | 450 | |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 451 | func isPlatformIncompatible(osType OsType, arch ArchType) bool { |
| 452 | return osType == Windows || // Windows toolchains are not currently supported. |
| 453 | osType == LinuxBionic || // Linux Bionic toolchains are not currently supported. |
| 454 | osType == LinuxMusl || // Linux musl toolchains are not currently supported (b/259266326). |
| 455 | arch == Riscv64 // TODO(b/262192655) Riscv64 toolchains are not currently supported. |
| 456 | } |
| 457 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 458 | func (b *BazelModuleBase) shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 459 | if !b.bazelProps().Bazel_module.CanConvertToBazel { |
| 460 | return false |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 461 | } |
| 462 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 463 | // In api_bp2build mode, all soong modules that can provide API contributions should be converted |
| 464 | // This is irrespective of its presence/absence in bp2build allowlists |
| 465 | if ctx.Config().BuildMode == ApiBp2build { |
| 466 | _, providesApis := module.(ApiProvider) |
| 467 | return providesApis |
| 468 | } |
| 469 | |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 470 | propValue := b.bazelProperties.Bazel_module.Bp2build_available |
Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 471 | packagePath := moduleDirWithPossibleOverride(ctx, module) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 472 | |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 473 | // Modules in unit tests which are enabled in the allowlist by type or name |
| 474 | // trigger this conditional because unit tests run under the "." package path |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 475 | isTestModule := packagePath == Bp2BuildTopLevel && proptools.BoolDefault(propValue, false) |
| 476 | if isTestModule { |
| 477 | return true |
| 478 | } |
| 479 | |
Liz Kammer | 20f0f78 | 2023-05-01 13:46:33 -0400 | [diff] [blame] | 480 | moduleName := moduleNameWithPossibleOverride(ctx, module) |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 481 | allowlist := ctx.Config().Bp2buildPackageConfig |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 482 | moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName] |
| 483 | moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[ctx.OtherModuleType(module)] |
| 484 | allowlistConvert := moduleNameAllowed || moduleTypeAllowed |
| 485 | if moduleNameAllowed && moduleTypeAllowed { |
| 486 | ctx.ModuleErrorf("A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert") |
| 487 | return false |
| 488 | } |
| 489 | |
| 490 | if allowlist.moduleDoNotConvert[moduleName] { |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 491 | if moduleNameAllowed { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 492 | ctx.ModuleErrorf("a module cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert") |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 493 | } |
Sam Delmerico | 94d26c2 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 494 | return false |
| 495 | } |
| 496 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 497 | // This is a tristate value: true, false, or unset. |
| 498 | if ok, directoryPath := bp2buildDefaultTrueRecursively(packagePath, allowlist.defaultConfig); ok { |
| 499 | if moduleNameAllowed { |
| 500 | ctx.ModuleErrorf("A module cannot be in a directory marked Bp2BuildDefaultTrue"+ |
Yu Liu | 10853f9 | 2022-09-14 16:05:22 -0700 | [diff] [blame] | 501 | " or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: '%s'"+ |
| 502 | " Module: '%s'", directoryPath, moduleName) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 503 | return false |
Sam Delmerico | fa1831c | 2022-02-22 18:07:55 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 506 | // Allow modules to explicitly opt-out. |
| 507 | return proptools.BoolDefault(propValue, true) |
| 508 | } |
| 509 | |
| 510 | // Allow modules to explicitly opt-in. |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 511 | return proptools.BoolDefault(propValue, allowlistConvert) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // bp2buildDefaultTrueRecursively checks that the package contains a prefix from the |
| 515 | // set of package prefixes where all modules must be converted. That is, if the |
| 516 | // package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will |
| 517 | // return true. |
| 518 | // |
| 519 | // However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry |
| 520 | // exactly, this module will return false early. |
| 521 | // |
| 522 | // This function will also return false if the package doesn't match anything in |
| 523 | // the config. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 524 | // |
| 525 | // This function will also return the allowlist entry which caused a particular |
| 526 | // package to be enabled. Since packages can be enabled via a recursive declaration, |
| 527 | // the path returned will not always be the same as the one provided. |
| 528 | func bp2buildDefaultTrueRecursively(packagePath string, config allowlists.Bp2BuildConfig) (bool, string) { |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 529 | // Check if the package path has an exact match in the config. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 530 | if config[packagePath] == allowlists.Bp2BuildDefaultTrue || config[packagePath] == allowlists.Bp2BuildDefaultTrueRecursively { |
| 531 | return true, packagePath |
MarkDacek | 756b296 | 2022-10-13 17:50:17 +0000 | [diff] [blame] | 532 | } else if config[packagePath] == allowlists.Bp2BuildDefaultFalse || config[packagePath] == allowlists.Bp2BuildDefaultFalseRecursively { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 533 | return false, packagePath |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 534 | } |
| 535 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 536 | // If not, check for the config recursively. |
MarkDacek | 756b296 | 2022-10-13 17:50:17 +0000 | [diff] [blame] | 537 | packagePrefix := packagePath |
| 538 | |
| 539 | // e.g. for x/y/z, iterate over x/y, then x, taking the most-specific value from the allowlist. |
| 540 | for strings.Contains(packagePrefix, "/") { |
| 541 | dirIndex := strings.LastIndex(packagePrefix, "/") |
| 542 | packagePrefix = packagePrefix[:dirIndex] |
| 543 | switch value := config[packagePrefix]; value { |
| 544 | case allowlists.Bp2BuildDefaultTrueRecursively: |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 545 | // package contains this prefix and this prefix should convert all modules |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 546 | return true, packagePrefix |
MarkDacek | 756b296 | 2022-10-13 17:50:17 +0000 | [diff] [blame] | 547 | case allowlists.Bp2BuildDefaultFalseRecursively: |
| 548 | //package contains this prefix and this prefix should NOT convert any modules |
| 549 | return false, packagePrefix |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 550 | } |
| 551 | // Continue to the next part of the package dir. |
MarkDacek | 756b296 | 2022-10-13 17:50:17 +0000 | [diff] [blame] | 552 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 553 | } |
| 554 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 555 | return false, packagePath |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 556 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 557 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 558 | func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) { |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 559 | ctx.TopDown("bp2build_conversion", bp2buildConversionMutator).Parallel() |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 562 | func bp2buildConversionMutator(ctx TopDownMutatorContext) { |
Chris Parsons | 8152a94 | 2023-06-06 16:17:50 +0000 | [diff] [blame] | 563 | if ctx.Config().HasBazelBuildTargetInSource(ctx) { |
| 564 | // Defer to the BUILD target. Generating an additional target would |
| 565 | // cause a BUILD file conflict. |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 566 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, "") |
Chris Parsons | 8152a94 | 2023-06-06 16:17:50 +0000 | [diff] [blame] | 567 | return |
| 568 | } |
| 569 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 570 | bModule, ok := ctx.Module().(Bazelable) |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 571 | if !ok { |
| 572 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "") |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 573 | return |
| 574 | } |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 575 | // TODO: b/285631638 - Differentiate between denylisted modules and missing bp2build capabilities. |
| 576 | if !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) { |
| 577 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "") |
| 578 | return |
| 579 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 580 | bModule.ConvertWithBp2build(ctx) |
Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame^] | 581 | |
| 582 | if !ctx.Module().base().IsConvertedByBp2build() && ctx.Module().base().GetUnconvertedReason() == nil { |
| 583 | panic(fmt.Errorf("illegal bp2build invariant: module '%s' was neither converted nor marked unconvertible", ctx.ModuleName())) |
| 584 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 585 | } |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 586 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 587 | func registerApiBp2buildConversionMutator(ctx RegisterMutatorsContext) { |
| 588 | ctx.TopDown("apiBp2build_conversion", convertWithApiBp2build).Parallel() |
| 589 | } |
| 590 | |
| 591 | // Generate API contribution targets if the Soong module provides APIs |
| 592 | func convertWithApiBp2build(ctx TopDownMutatorContext) { |
| 593 | if m, ok := ctx.Module().(ApiProvider); ok { |
| 594 | m.ConvertWithApiBp2build(ctx) |
| 595 | } |
| 596 | } |
| 597 | |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 598 | // GetMainClassInManifest scans the manifest file specified in filepath and returns |
| 599 | // the value of attribute Main-Class in the manifest file if it exists, or returns error. |
| 600 | // WARNING: this is for bp2build converters of java_* modules only. |
| 601 | func GetMainClassInManifest(c Config, filepath string) (string, error) { |
| 602 | file, err := c.fs.Open(filepath) |
| 603 | if err != nil { |
| 604 | return "", err |
| 605 | } |
Liz Kammer | 0fe123d | 2022-02-07 10:17:35 -0500 | [diff] [blame] | 606 | defer file.Close() |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 607 | scanner := bufio.NewScanner(file) |
| 608 | for scanner.Scan() { |
| 609 | line := scanner.Text() |
| 610 | if strings.HasPrefix(line, "Main-Class:") { |
| 611 | return strings.TrimSpace(line[len("Main-Class:"):]), nil |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | return "", errors.New("Main-Class is not found.") |
| 616 | } |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 617 | |
| 618 | func AttachValidationActions(ctx ModuleContext, outputFilePath Path, validations Paths) ModuleOutPath { |
| 619 | validatedOutputFilePath := PathForModuleOut(ctx, "validated", outputFilePath.Base()) |
| 620 | ctx.Build(pctx, BuildParams{ |
| 621 | Rule: CpNoPreserveSymlink, |
| 622 | Description: "run validations " + outputFilePath.Base(), |
| 623 | Output: validatedOutputFilePath, |
| 624 | Input: outputFilePath, |
| 625 | Validations: validations, |
| 626 | }) |
| 627 | return validatedOutputFilePath |
| 628 | } |