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