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