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