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