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 | "fmt" |
| 21 | "io/ioutil" |
| 22 | "path/filepath" |
| 23 | "strings" |
| 24 | |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 25 | "github.com/google/blueprint" |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 27 | |
| 28 | "android/soong/android/allowlists" |
| 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | // A sentinel value to be used as a key in Bp2BuildConfig for modules with |
| 33 | // no package path. This is also the module dir for top level Android.bp |
| 34 | // modules. |
| 35 | Bp2BuildTopLevel = "." |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 36 | ) |
| 37 | |
Sasha Smundak | a095406 | 2022-08-02 18:23:58 -0700 | [diff] [blame^] | 38 | type BazelConversionStatus struct { |
| 39 | // Information about _all_ bp2build targets generated by this module. Multiple targets are |
| 40 | // supported as Soong handles some things within a single target that we may choose to split into |
| 41 | // multiple targets, e.g. renderscript, protos, yacc within a cc module. |
| 42 | Bp2buildInfo []bp2buildInfo `blueprint:"mutated"` |
| 43 | |
| 44 | // UnconvertedBp2buildDep stores the module names of direct dependency that were not converted to |
| 45 | // Bazel |
| 46 | UnconvertedDeps []string `blueprint:"mutated"` |
| 47 | |
| 48 | // MissingBp2buildDep stores the module names of direct dependency that were not found |
| 49 | MissingDeps []string `blueprint:"mutated"` |
| 50 | } |
| 51 | |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 52 | type bazelModuleProperties struct { |
| 53 | // The label of the Bazel target replacing this Soong module. When run in conversion mode, this |
| 54 | // will import the handcrafted build target into the autogenerated file. Note: this may result in |
| 55 | // a conflict due to duplicate targets if bp2build_available is also set. |
| 56 | Label *string |
| 57 | |
| 58 | // If true, bp2build will generate the converted Bazel target for this module. Note: this may |
| 59 | // cause a conflict due to the duplicate targets if label is also set. |
| 60 | // |
| 61 | // This is a bool pointer to support tristates: true, false, not set. |
| 62 | // |
| 63 | // To opt-in a module, set bazel_module: { bp2build_available: true } |
| 64 | // To opt-out a module, set bazel_module: { bp2build_available: false } |
| 65 | // To defer the default setting for the directory, do not set the value. |
| 66 | Bp2build_available *bool |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 67 | |
| 68 | // CanConvertToBazel is set via InitBazelModule to indicate that a module type can be converted to |
| 69 | // Bazel with Bp2build. |
| 70 | CanConvertToBazel bool `blueprint:"mutated"` |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 73 | // Properties contains common module properties for Bazel migration purposes. |
| 74 | type properties struct { |
| 75 | // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing |
| 76 | // this Soong module. |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 77 | Bazel_module bazelModuleProperties |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 78 | } |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 79 | |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 80 | // namespacedVariableProperties is a map from a string representing a Soong |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 81 | // config variable namespace, like "android" or "vendor_name" to a slice of |
| 82 | // pointer to a struct containing a single field called Soong_config_variables |
| 83 | // whose value mirrors the structure in the Blueprint file. |
| 84 | type namespacedVariableProperties map[string][]interface{} |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 85 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 86 | // BazelModuleBase contains the property structs with metadata for modules which can be converted to |
| 87 | // Bazel. |
| 88 | type BazelModuleBase struct { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 89 | bazelProperties properties |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 90 | |
| 91 | // namespacedVariableProperties is used for soong_config_module_type support |
| 92 | // in bp2build. Soong config modules allow users to set module properties |
| 93 | // based on custom product variables defined in Android.bp files. These |
| 94 | // variables are namespaced to prevent clobbering, especially when set from |
| 95 | // Makefiles. |
| 96 | namespacedVariableProperties namespacedVariableProperties |
| 97 | |
| 98 | // baseModuleType is set when this module was created from a module type |
| 99 | // defined by a soong_config_module_type. Every soong_config_module_type |
| 100 | // "wraps" another module type, e.g. a soong_config_module_type can wrap a |
| 101 | // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary. |
| 102 | // This baseModuleType is set to the wrapped module type. |
| 103 | baseModuleType string |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Bazelable is specifies the interface for modules that can be converted to Bazel. |
| 107 | type Bazelable interface { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 108 | bazelProps() *properties |
| 109 | HasHandcraftedLabel() bool |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 110 | HandcraftedLabel() string |
| 111 | GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 112 | ShouldConvertWithBp2build(ctx BazelConversionContext) bool |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 113 | shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 114 | GetBazelBuildFileContents(c Config, path, name string) (string, error) |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 115 | ConvertWithBp2build(ctx TopDownMutatorContext) |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 116 | |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 117 | // namespacedVariableProps is a map from a soong config variable namespace |
| 118 | // (e.g. acme, android) to a map of interfaces{}, which are really |
| 119 | // reflect.Struct pointers, representing the value of the |
| 120 | // soong_config_variables property of a module. The struct pointer is the |
| 121 | // one with the single member called Soong_config_variables, which itself is |
| 122 | // a struct containing fields for each supported feature in that namespace. |
| 123 | // |
| 124 | // The reason for using an slice of interface{} is to support defaults |
| 125 | // propagation of the struct pointers. |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 126 | namespacedVariableProps() namespacedVariableProperties |
| 127 | setNamespacedVariableProps(props namespacedVariableProperties) |
| 128 | BaseModuleType() string |
Jingwen Chen | 84817de | 2021-11-17 10:57:35 +0000 | [diff] [blame] | 129 | SetBaseModuleType(baseModuleType string) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 132 | // MixedBuildBuildable is an interface that module types should implement in order |
| 133 | // to be "handled by Bazel" in a mixed build. |
| 134 | type MixedBuildBuildable interface { |
| 135 | // IsMixedBuildSupported returns true if and only if this module should be |
| 136 | // "handled by Bazel" in a mixed build. |
| 137 | // This "escape hatch" allows modules with corner-case scenarios to opt out |
| 138 | // of being built with Bazel. |
| 139 | IsMixedBuildSupported(ctx BaseModuleContext) bool |
| 140 | |
| 141 | // QueueBazelCall invokes request-queueing functions on the BazelContext |
| 142 | // so that these requests are handled when Bazel's cquery is invoked. |
| 143 | QueueBazelCall(ctx BaseModuleContext) |
| 144 | |
| 145 | // ProcessBazelQueryResponse uses Bazel information (obtained from the BazelContext) |
| 146 | // to set module fields and providers to propagate this module's metadata upstream. |
| 147 | // This effectively "bridges the gap" between Bazel and Soong in a mixed build. |
| 148 | // Soong modules depending on this module should be oblivious to the fact that |
| 149 | // this module was handled by Bazel. |
| 150 | ProcessBazelQueryResponse(ctx ModuleContext) |
| 151 | } |
| 152 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 153 | // BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules. |
| 154 | type BazelModule interface { |
| 155 | Module |
| 156 | Bazelable |
| 157 | } |
| 158 | |
| 159 | // InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion |
| 160 | // properties. |
| 161 | func InitBazelModule(module BazelModule) { |
| 162 | module.AddProperties(module.bazelProps()) |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 163 | module.bazelProps().Bazel_module.CanConvertToBazel = true |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // bazelProps returns the Bazel properties for the given BazelModuleBase. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 167 | func (b *BazelModuleBase) bazelProps() *properties { |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 168 | return &b.bazelProperties |
| 169 | } |
| 170 | |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 171 | func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties { |
| 172 | return b.namespacedVariableProperties |
| 173 | } |
| 174 | |
| 175 | func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) { |
| 176 | b.namespacedVariableProperties = props |
| 177 | } |
| 178 | |
| 179 | func (b *BazelModuleBase) BaseModuleType() string { |
| 180 | return b.baseModuleType |
| 181 | } |
| 182 | |
| 183 | func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) { |
| 184 | b.baseModuleType = baseModuleType |
| 185 | } |
| 186 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 187 | // HasHandcraftedLabel returns whether this module has a handcrafted Bazel label. |
| 188 | func (b *BazelModuleBase) HasHandcraftedLabel() bool { |
| 189 | return b.bazelProperties.Bazel_module.Label != nil |
| 190 | } |
| 191 | |
| 192 | // HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none |
| 193 | func (b *BazelModuleBase) HandcraftedLabel() string { |
| 194 | return proptools.String(b.bazelProperties.Bazel_module.Label) |
| 195 | } |
| 196 | |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 197 | // GetBazelLabel returns the Bazel label for the given BazelModuleBase. |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 198 | func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string { |
| 199 | if b.HasHandcraftedLabel() { |
| 200 | return b.HandcraftedLabel() |
| 201 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 202 | if b.ShouldConvertWithBp2build(ctx) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 203 | return bp2buildModuleLabel(ctx, module) |
| 204 | } |
| 205 | return "" // no label for unconverted module |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 208 | type bp2BuildConversionAllowlist struct { |
| 209 | // Configure modules in these directories to enable bp2build_available: true or false by default. |
| 210 | defaultConfig allowlists.Bp2BuildConfig |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 211 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 212 | // 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] | 213 | // in the synthetic Bazel workspace. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 214 | keepExistingBuildFile map[string]bool |
Jingwen Chen | 5d72cba | 2021-03-25 09:28:38 +0000 | [diff] [blame] | 215 | |
Sam Delmerico | fa1831c | 2022-02-22 18:07:55 +0000 | [diff] [blame] | 216 | // Per-module allowlist to always opt modules in of both bp2build and mixed builds. |
Jingwen Chen | 7edadab | 2022-03-04 07:01:29 +0000 | [diff] [blame] | 217 | // These modules are usually in directories with many other modules that are not ready for |
| 218 | // conversion. |
| 219 | // |
| 220 | // A module can either be in this list or its directory allowlisted entirely |
| 221 | // in bp2buildDefaultConfig, but not both at the same time. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 222 | moduleAlwaysConvert map[string]bool |
Sam Delmerico | fa1831c | 2022-02-22 18:07:55 +0000 | [diff] [blame] | 223 | |
Sam Delmerico | a9b047a | 2022-02-22 19:21:28 +0000 | [diff] [blame] | 224 | // Per-module-type allowlist to always opt modules in to both bp2build and mixed builds |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 225 | // when they have the same type as one listed. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 226 | moduleTypeAlwaysConvert map[string]bool |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 227 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 228 | // Per-module denylist to always opt modules out of both bp2build and mixed builds. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 229 | moduleDoNotConvert map[string]bool |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame] | 230 | |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 231 | // Per-module denylist of cc_library modules to only generate the static |
| 232 | // variant if their shared variant isn't ready or buildable by Bazel. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 233 | ccLibraryStaticOnly map[string]bool |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 234 | |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 235 | // Per-module denylist to opt modules out of mixed builds. Such modules will |
| 236 | // still be generated via bp2build. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 237 | mixedBuildsDisabled map[string]bool |
| 238 | } |
Liz Kammer | 5c31358 | 2021-12-03 15:23:26 -0500 | [diff] [blame] | 239 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 240 | // NewBp2BuildAllowlist creates a new, empty bp2BuildConversionAllowlist |
| 241 | // which can be populated using builder pattern Set* methods |
| 242 | func NewBp2BuildAllowlist() bp2BuildConversionAllowlist { |
| 243 | return bp2BuildConversionAllowlist{ |
| 244 | allowlists.Bp2BuildConfig{}, |
| 245 | map[string]bool{}, |
| 246 | map[string]bool{}, |
| 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 |
| 255 | func (a bp2BuildConversionAllowlist) SetDefaultConfig(defaultConfig allowlists.Bp2BuildConfig) bp2BuildConversionAllowlist { |
| 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 |
| 267 | func (a bp2BuildConversionAllowlist) SetKeepExistingBuildFile(keepExistingBuildFile map[string]bool) bp2BuildConversionAllowlist { |
| 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 |
| 279 | func (a bp2BuildConversionAllowlist) SetModuleAlwaysConvertList(moduleAlwaysConvert []string) bp2BuildConversionAllowlist { |
| 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 |
| 291 | func (a bp2BuildConversionAllowlist) SetModuleTypeAlwaysConvertList(moduleTypeAlwaysConvert []string) bp2BuildConversionAllowlist { |
| 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 |
| 303 | func (a bp2BuildConversionAllowlist) SetModuleDoNotConvertList(moduleDoNotConvert []string) bp2BuildConversionAllowlist { |
| 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 | |
| 314 | // SetCcLibraryStaticOnlyList copies the entries from ccLibraryStaticOnly into the allowlist |
| 315 | func (a bp2BuildConversionAllowlist) SetCcLibraryStaticOnlyList(ccLibraryStaticOnly []string) bp2BuildConversionAllowlist { |
| 316 | if a.ccLibraryStaticOnly == nil { |
| 317 | a.ccLibraryStaticOnly = map[string]bool{} |
| 318 | } |
| 319 | for _, m := range ccLibraryStaticOnly { |
| 320 | a.ccLibraryStaticOnly[m] = true |
| 321 | } |
| 322 | |
| 323 | return a |
| 324 | } |
| 325 | |
| 326 | // SetMixedBuildsDisabledList copies the entries from mixedBuildsDisabled into the allowlist |
| 327 | func (a bp2BuildConversionAllowlist) SetMixedBuildsDisabledList(mixedBuildsDisabled []string) bp2BuildConversionAllowlist { |
| 328 | if a.mixedBuildsDisabled == nil { |
| 329 | a.mixedBuildsDisabled = map[string]bool{} |
| 330 | } |
| 331 | for _, m := range mixedBuildsDisabled { |
| 332 | a.mixedBuildsDisabled[m] = true |
| 333 | } |
| 334 | |
| 335 | return a |
| 336 | } |
| 337 | |
Wei Li | d7736ec | 2022-05-12 23:37:53 -0700 | [diff] [blame] | 338 | var bp2BuildAllowListKey = NewOnceKey("Bp2BuildAllowlist") |
| 339 | var bp2buildAllowlist OncePer |
| 340 | |
| 341 | func getBp2BuildAllowList() bp2BuildConversionAllowlist { |
| 342 | return bp2buildAllowlist.Once(bp2BuildAllowListKey, func() interface{} { |
| 343 | return NewBp2BuildAllowlist().SetDefaultConfig(allowlists.Bp2buildDefaultConfig). |
| 344 | SetKeepExistingBuildFile(allowlists.Bp2buildKeepExistingBuildFile). |
| 345 | SetModuleAlwaysConvertList(allowlists.Bp2buildModuleAlwaysConvertList). |
| 346 | SetModuleTypeAlwaysConvertList(allowlists.Bp2buildModuleTypeAlwaysConvertList). |
| 347 | SetModuleDoNotConvertList(allowlists.Bp2buildModuleDoNotConvertList). |
| 348 | SetCcLibraryStaticOnlyList(allowlists.Bp2buildCcLibraryStaticOnlyList). |
| 349 | SetMixedBuildsDisabledList(allowlists.MixedBuildsDisabledList) |
| 350 | }).(bp2BuildConversionAllowlist) |
| 351 | } |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 352 | |
| 353 | // GenerateCcLibraryStaticOnly returns whether a cc_library module should only |
| 354 | // generate a static version of itself based on the current global configuration. |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 355 | func GenerateCcLibraryStaticOnly(moduleName string) bool { |
Wei Li | d7736ec | 2022-05-12 23:37:53 -0700 | [diff] [blame] | 356 | return getBp2BuildAllowList().ccLibraryStaticOnly[moduleName] |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 359 | // ShouldKeepExistingBuildFileForDir returns whether an existing BUILD file should be |
| 360 | // added to the build symlink forest based on the current global configuration. |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 361 | func ShouldKeepExistingBuildFileForDir(dir string) bool { |
Wei Li | d7736ec | 2022-05-12 23:37:53 -0700 | [diff] [blame] | 362 | return shouldKeepExistingBuildFileForDir(getBp2BuildAllowList(), dir) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | func shouldKeepExistingBuildFileForDir(allowlist bp2BuildConversionAllowlist, dir string) bool { |
| 366 | if _, ok := allowlist.keepExistingBuildFile[dir]; ok { |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 367 | // Exact dir match |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 368 | return true |
| 369 | } |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 370 | // Check if subtree match |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 371 | for prefix, recursive := range allowlist.keepExistingBuildFile { |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 372 | if recursive { |
| 373 | if strings.HasPrefix(dir, prefix+"/") { |
| 374 | return true |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | // Default |
| 379 | return false |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 380 | } |
| 381 | |
MarkDacek | ff851b8 | 2022-04-21 18:33:17 +0000 | [diff] [blame] | 382 | // MixedBuildsEnabled returns true if a module is ready to be replaced by a |
| 383 | // converted or handcrafted Bazel target. As a side effect, calling this |
| 384 | // method will also log whether this module is mixed build enabled for |
| 385 | // metrics reporting. |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 386 | func MixedBuildsEnabled(ctx BaseModuleContext) bool { |
MarkDacek | ff851b8 | 2022-04-21 18:33:17 +0000 | [diff] [blame] | 387 | mixedBuildEnabled := mixedBuildPossible(ctx) |
| 388 | ctx.Config().LogMixedBuild(ctx, mixedBuildEnabled) |
| 389 | return mixedBuildEnabled |
| 390 | } |
| 391 | |
| 392 | // mixedBuildPossible returns true if a module is ready to be replaced by a |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 393 | // converted or handcrafted Bazel target. |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 394 | func mixedBuildPossible(ctx BaseModuleContext) bool { |
Chris Parsons | 494eef3 | 2021-11-09 10:29:52 -0500 | [diff] [blame] | 395 | if ctx.Os() == Windows { |
| 396 | // Windows toolchains are not currently supported. |
| 397 | return false |
| 398 | } |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 399 | if !ctx.Module().Enabled() { |
| 400 | return false |
| 401 | } |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 402 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 403 | return false |
| 404 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 405 | if !convertedToBazel(ctx, ctx.Module()) { |
Chris Parsons | bab4d7e | 2021-04-15 17:27:08 -0400 | [diff] [blame] | 406 | return false |
| 407 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 408 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 409 | if GenerateCcLibraryStaticOnly(ctx.Module().Name()) { |
Jingwen Chen | 179856a | 2021-05-03 09:15:48 +0000 | [diff] [blame] | 410 | // Don't use partially-converted cc_library targets in mixed builds, |
| 411 | // since mixed builds would generally rely on both static and shared |
| 412 | // variants of a cc_library. |
| 413 | return false |
| 414 | } |
Wei Li | d7736ec | 2022-05-12 23:37:53 -0700 | [diff] [blame] | 415 | return !getBp2BuildAllowList().mixedBuildsDisabled[ctx.Module().Name()] |
Rupert Shuttleworth | 4f43fe9 | 2021-03-30 14:13:16 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 418 | // 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] | 419 | func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 420 | b, ok := module.(Bazelable) |
| 421 | if !ok { |
| 422 | return false |
| 423 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 424 | return b.shouldConvertWithBp2build(ctx, module) || b.HasHandcraftedLabel() |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 427 | // ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 428 | func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx BazelConversionContext) bool { |
| 429 | return b.shouldConvertWithBp2build(ctx, ctx.Module()) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 430 | } |
| 431 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 432 | type bazelOtherModuleContext interface { |
| 433 | ModuleErrorf(format string, args ...interface{}) |
| 434 | Config() Config |
| 435 | OtherModuleType(m blueprint.Module) string |
| 436 | OtherModuleName(m blueprint.Module) string |
| 437 | OtherModuleDir(m blueprint.Module) string |
| 438 | } |
Sam Delmerico | fa1831c | 2022-02-22 18:07:55 +0000 | [diff] [blame] | 439 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 440 | func (b *BazelModuleBase) shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 441 | if !b.bazelProps().Bazel_module.CanConvertToBazel { |
| 442 | return false |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 443 | } |
| 444 | |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 445 | propValue := b.bazelProperties.Bazel_module.Bp2build_available |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 446 | packagePath := ctx.OtherModuleDir(module) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 447 | |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 448 | // Modules in unit tests which are enabled in the allowlist by type or name |
| 449 | // trigger this conditional because unit tests run under the "." package path |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 450 | isTestModule := packagePath == Bp2BuildTopLevel && proptools.BoolDefault(propValue, false) |
| 451 | if isTestModule { |
| 452 | return true |
| 453 | } |
| 454 | |
| 455 | moduleName := module.Name() |
| 456 | allowlist := ctx.Config().bp2buildPackageConfig |
| 457 | moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName] |
| 458 | moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[ctx.OtherModuleType(module)] |
| 459 | allowlistConvert := moduleNameAllowed || moduleTypeAllowed |
| 460 | if moduleNameAllowed && moduleTypeAllowed { |
| 461 | ctx.ModuleErrorf("A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert") |
| 462 | return false |
| 463 | } |
| 464 | |
| 465 | if allowlist.moduleDoNotConvert[moduleName] { |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 466 | if moduleNameAllowed { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 467 | 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] | 468 | } |
Sam Delmerico | 94d26c2 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 469 | return false |
| 470 | } |
| 471 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 472 | if allowlistConvert && shouldKeepExistingBuildFileForDir(allowlist, packagePath) { |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 473 | if moduleNameAllowed { |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 474 | ctx.ModuleErrorf("A module cannot be in a directory listed in keepExistingBuildFile"+ |
| 475 | " and also be in moduleAlwaysConvert. Directory: '%s'", packagePath) |
| 476 | return false |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // This is a tristate value: true, false, or unset. |
| 481 | if ok, directoryPath := bp2buildDefaultTrueRecursively(packagePath, allowlist.defaultConfig); ok { |
| 482 | if moduleNameAllowed { |
| 483 | ctx.ModuleErrorf("A module cannot be in a directory marked Bp2BuildDefaultTrue"+ |
| 484 | " or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: '%s'", |
| 485 | directoryPath) |
| 486 | return false |
Sam Delmerico | fa1831c | 2022-02-22 18:07:55 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 489 | // Allow modules to explicitly opt-out. |
| 490 | return proptools.BoolDefault(propValue, true) |
| 491 | } |
| 492 | |
| 493 | // Allow modules to explicitly opt-in. |
Sam Delmerico | 85d831a | 2022-03-07 19:12:42 +0000 | [diff] [blame] | 494 | return proptools.BoolDefault(propValue, allowlistConvert) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // bp2buildDefaultTrueRecursively checks that the package contains a prefix from the |
| 498 | // set of package prefixes where all modules must be converted. That is, if the |
| 499 | // package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will |
| 500 | // return true. |
| 501 | // |
| 502 | // However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry |
| 503 | // exactly, this module will return false early. |
| 504 | // |
| 505 | // This function will also return false if the package doesn't match anything in |
| 506 | // the config. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 507 | // |
| 508 | // This function will also return the allowlist entry which caused a particular |
| 509 | // package to be enabled. Since packages can be enabled via a recursive declaration, |
| 510 | // the path returned will not always be the same as the one provided. |
| 511 | func bp2buildDefaultTrueRecursively(packagePath string, config allowlists.Bp2BuildConfig) (bool, string) { |
Jingwen Chen | 294e774 | 2021-08-31 05:58:01 +0000 | [diff] [blame] | 512 | // Check if the package path has an exact match in the config. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 513 | if config[packagePath] == allowlists.Bp2BuildDefaultTrue || config[packagePath] == allowlists.Bp2BuildDefaultTrueRecursively { |
| 514 | return true, packagePath |
| 515 | } else if config[packagePath] == allowlists.Bp2BuildDefaultFalse { |
| 516 | return false, packagePath |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 517 | } |
| 518 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 519 | // If not, check for the config recursively. |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 520 | packagePrefix := "" |
| 521 | // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist. |
| 522 | for _, part := range strings.Split(packagePath, "/") { |
| 523 | packagePrefix += part |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 524 | if config[packagePrefix] == allowlists.Bp2BuildDefaultTrueRecursively { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 525 | // package contains this prefix and this prefix should convert all modules |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 526 | return true, packagePrefix |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 527 | } |
| 528 | // Continue to the next part of the package dir. |
| 529 | packagePrefix += "/" |
| 530 | } |
| 531 | |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 532 | return false, packagePath |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 533 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 534 | |
| 535 | // GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or |
| 536 | // an error if there are errors reading the file. |
| 537 | // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do |
| 538 | // something more targeted based on the rule type and target. |
| 539 | func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 540 | if !strings.Contains(b.HandcraftedLabel(), path) { |
| 541 | return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel()) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 542 | } |
| 543 | name = filepath.Join(path, name) |
| 544 | f, err := c.fs.Open(name) |
| 545 | if err != nil { |
| 546 | return "", err |
| 547 | } |
| 548 | defer f.Close() |
| 549 | |
| 550 | data, err := ioutil.ReadAll(f) |
| 551 | if err != nil { |
| 552 | return "", err |
| 553 | } |
| 554 | return string(data[:]), nil |
| 555 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 556 | |
| 557 | func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) { |
| 558 | ctx.TopDown("bp2build_conversion", convertWithBp2build).Parallel() |
| 559 | } |
| 560 | |
| 561 | func convertWithBp2build(ctx TopDownMutatorContext) { |
| 562 | bModule, ok := ctx.Module().(Bazelable) |
| 563 | if !ok || !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) { |
| 564 | return |
| 565 | } |
| 566 | |
| 567 | bModule.ConvertWithBp2build(ctx) |
| 568 | } |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 569 | |
| 570 | // GetMainClassInManifest scans the manifest file specified in filepath and returns |
| 571 | // the value of attribute Main-Class in the manifest file if it exists, or returns error. |
| 572 | // WARNING: this is for bp2build converters of java_* modules only. |
| 573 | func GetMainClassInManifest(c Config, filepath string) (string, error) { |
| 574 | file, err := c.fs.Open(filepath) |
| 575 | if err != nil { |
| 576 | return "", err |
| 577 | } |
Liz Kammer | 0fe123d | 2022-02-07 10:17:35 -0500 | [diff] [blame] | 578 | defer file.Close() |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 579 | scanner := bufio.NewScanner(file) |
| 580 | for scanner.Scan() { |
| 581 | line := scanner.Text() |
| 582 | if strings.HasPrefix(line, "Main-Class:") { |
| 583 | return strings.TrimSpace(line[len("Main-Class:"):]), nil |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return "", errors.New("Main-Class is not found.") |
| 588 | } |