blob: b4e7ae55835ec7f7682b0bffb2f379abbe71d8f7 [file] [log] [blame]
Liz Kammerea6666f2021-02-17 10:17:28 -05001// 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
15package android
16
Liz Kammerba3ea162021-02-17 13:22:03 -050017import (
Wei Libafb6d62021-12-10 03:14:59 -080018 "bufio"
19 "errors"
Chris Parsons39a16972023-06-08 14:28:51 +000020 "fmt"
Liz Kammerba3ea162021-02-17 13:22:03 -050021 "strings"
22
Chris Parsons39a16972023-06-08 14:28:51 +000023 "android/soong/ui/metrics/bp2build_metrics_proto"
Liz Kammerc86e0942023-08-11 16:15:12 -040024
Liz Kammerbdc60992021-02-24 16:55:11 -050025 "github.com/google/blueprint"
Spandan Das64852422023-08-02 21:58:41 +000026 "github.com/google/blueprint/bootstrap"
Liz Kammerba3ea162021-02-17 13:22:03 -050027 "github.com/google/blueprint/proptools"
Sam Delmerico24c56032022-03-28 19:53:03 +000028
29 "android/soong/android/allowlists"
30)
31
32const (
33 // A sentinel value to be used as a key in Bp2BuildConfig for modules with
34 // no package path. This is also the module dir for top level Android.bp
35 // modules.
36 Bp2BuildTopLevel = "."
Liz Kammerba3ea162021-02-17 13:22:03 -050037)
38
MarkDacekf47e1422023-04-19 16:47:36 +000039type MixedBuildEnabledStatus int
40
41const (
42 // This module can be mixed_built.
43 MixedBuildEnabled = iota
44
45 // There is a technical incompatibility preventing this module from being
46 // bazel-analyzed. Note: the module might also be incompatible.
47 TechnicalIncompatibility
48
49 // This module cannot be mixed_built due to some incompatibility with it
50 // that is not a platform incompatibility. Example: the module-type is not
51 // enabled, or is not bp2build-converted.
52 ModuleIncompatibility
Liz Kammerc13f7852023-05-17 13:01:48 -040053
54 // Missing dependencies. We can't query Bazel for modules if it has missing dependencies, there
55 // will be failures.
56 ModuleMissingDeps
MarkDacekf47e1422023-04-19 16:47:36 +000057)
58
Yu Liu2aa806b2022-09-01 11:54:47 -070059// FileGroupAsLibrary describes a filegroup module that is converted to some library
60// such as aidl_library or proto_library.
61type FileGroupAsLibrary interface {
Vinh Tran444154d2022-08-16 13:10:31 -040062 ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool
Yu Liu2aa806b2022-09-01 11:54:47 -070063 ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool
Vinh Tran444154d2022-08-16 13:10:31 -040064 GetAidlLibraryLabel(ctx BazelConversionPathContext) string
Yu Liu2aa806b2022-09-01 11:54:47 -070065 GetProtoLibraryLabel(ctx BazelConversionPathContext) string
Vinh Tran444154d2022-08-16 13:10:31 -040066}
67
Sasha Smundaka0954062022-08-02 18:23:58 -070068type BazelConversionStatus struct {
69 // Information about _all_ bp2build targets generated by this module. Multiple targets are
70 // supported as Soong handles some things within a single target that we may choose to split into
71 // multiple targets, e.g. renderscript, protos, yacc within a cc module.
72 Bp2buildInfo []bp2buildInfo `blueprint:"mutated"`
73
74 // UnconvertedBp2buildDep stores the module names of direct dependency that were not converted to
75 // Bazel
76 UnconvertedDeps []string `blueprint:"mutated"`
77
78 // MissingBp2buildDep stores the module names of direct dependency that were not found
79 MissingDeps []string `blueprint:"mutated"`
Chris Parsons39a16972023-06-08 14:28:51 +000080
81 // If non-nil, indicates that the module could not be converted successfully
82 // with bp2build. This will describe the reason the module could not be converted.
83 UnconvertedReason *UnconvertedReason
84}
85
86// The reason a module could not be converted to a BUILD target via bp2build.
87// This should match bp2build_metrics_proto.UnconvertedReason, but omits private
88// proto-related fields that prevent copying this struct.
89type UnconvertedReason struct {
90 // Should correspond to a valid value in bp2build_metrics_proto.UnconvertedReasonType.
91 // A raw int is used here instead, because blueprint logic requires that all transitive
92 // fields of module definitions be primitives.
93 ReasonType int
94 Detail string
Sasha Smundaka0954062022-08-02 18:23:58 -070095}
96
Romain Jobredeaux8242b432023-05-04 10:16:26 -040097type BazelModuleProperties struct {
Jingwen Chen01812022021-11-19 14:29:43 +000098 // The label of the Bazel target replacing this Soong module. When run in conversion mode, this
99 // will import the handcrafted build target into the autogenerated file. Note: this may result in
100 // a conflict due to duplicate targets if bp2build_available is also set.
101 Label *string
102
103 // If true, bp2build will generate the converted Bazel target for this module. Note: this may
104 // cause a conflict due to the duplicate targets if label is also set.
105 //
106 // This is a bool pointer to support tristates: true, false, not set.
107 //
Sasha Smundak39a301c2022-12-29 17:11:49 -0800108 // To opt in a module, set bazel_module: { bp2build_available: true }
109 // To opt out a module, set bazel_module: { bp2build_available: false }
Jingwen Chen01812022021-11-19 14:29:43 +0000110 // To defer the default setting for the directory, do not set the value.
111 Bp2build_available *bool
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400112
113 // CanConvertToBazel is set via InitBazelModule to indicate that a module type can be converted to
114 // Bazel with Bp2build.
115 CanConvertToBazel bool `blueprint:"mutated"`
Jingwen Chen01812022021-11-19 14:29:43 +0000116}
117
Liz Kammerba3ea162021-02-17 13:22:03 -0500118// Properties contains common module properties for Bazel migration purposes.
119type properties struct {
Chris Parsons1bb58da2022-08-30 13:37:57 -0400120 // In "Bazel mixed build" mode, this represents the Bazel target replacing
Liz Kammerba3ea162021-02-17 13:22:03 -0500121 // this Soong module.
Romain Jobredeaux8242b432023-05-04 10:16:26 -0400122 Bazel_module BazelModuleProperties
Liz Kammerba3ea162021-02-17 13:22:03 -0500123}
Liz Kammerea6666f2021-02-17 10:17:28 -0500124
Jingwen Chen25825ca2021-11-15 12:28:43 +0000125// namespacedVariableProperties is a map from a string representing a Soong
Jingwen Chen84817de2021-11-17 10:57:35 +0000126// config variable namespace, like "android" or "vendor_name" to a slice of
127// pointer to a struct containing a single field called Soong_config_variables
128// whose value mirrors the structure in the Blueprint file.
129type namespacedVariableProperties map[string][]interface{}
Jingwen Chena47f28d2021-11-02 16:43:57 +0000130
Liz Kammerea6666f2021-02-17 10:17:28 -0500131// BazelModuleBase contains the property structs with metadata for modules which can be converted to
132// Bazel.
133type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -0500134 bazelProperties properties
Jingwen Chena47f28d2021-11-02 16:43:57 +0000135
136 // namespacedVariableProperties is used for soong_config_module_type support
137 // in bp2build. Soong config modules allow users to set module properties
138 // based on custom product variables defined in Android.bp files. These
139 // variables are namespaced to prevent clobbering, especially when set from
140 // Makefiles.
141 namespacedVariableProperties namespacedVariableProperties
142
143 // baseModuleType is set when this module was created from a module type
144 // defined by a soong_config_module_type. Every soong_config_module_type
145 // "wraps" another module type, e.g. a soong_config_module_type can wrap a
146 // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
147 // This baseModuleType is set to the wrapped module type.
148 baseModuleType string
Liz Kammerea6666f2021-02-17 10:17:28 -0500149}
150
151// Bazelable is specifies the interface for modules that can be converted to Bazel.
152type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -0500153 bazelProps() *properties
154 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -0500155 HandcraftedLabel() string
156 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Liz Kammerc86e0942023-08-11 16:15:12 -0400157 ShouldConvertWithBp2build(ctx ShouldConvertWithBazelContext) bool
158 shouldConvertWithBp2build(shouldConvertModuleContext, shouldConvertParams) bool
Chris Parsons39a16972023-06-08 14:28:51 +0000159
160 // ConvertWithBp2build either converts the module to a Bazel build target or
161 // declares the module as unconvertible (for logging and metrics).
162 // Modules must implement this function to be bp2build convertible. The function
163 // must either create at least one Bazel target module (using ctx.CreateBazelTargetModule or
164 // its related functions), or declare itself unconvertible using ctx.MarkBp2buildUnconvertible.
Chris Parsons637458d2023-09-19 20:09:00 +0000165 ConvertWithBp2build(ctx Bp2buildMutatorContext)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000166
Jingwen Chen84817de2021-11-17 10:57:35 +0000167 // namespacedVariableProps is a map from a soong config variable namespace
168 // (e.g. acme, android) to a map of interfaces{}, which are really
169 // reflect.Struct pointers, representing the value of the
170 // soong_config_variables property of a module. The struct pointer is the
171 // one with the single member called Soong_config_variables, which itself is
172 // a struct containing fields for each supported feature in that namespace.
173 //
Sasha Smundak39a301c2022-12-29 17:11:49 -0800174 // The reason for using a slice of interface{} is to support defaults
Jingwen Chen84817de2021-11-17 10:57:35 +0000175 // propagation of the struct pointers.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000176 namespacedVariableProps() namespacedVariableProperties
177 setNamespacedVariableProps(props namespacedVariableProperties)
178 BaseModuleType() string
Jingwen Chen84817de2021-11-17 10:57:35 +0000179 SetBaseModuleType(baseModuleType string)
Liz Kammerea6666f2021-02-17 10:17:28 -0500180}
181
Spandan Das5af0bd32022-09-28 20:43:08 +0000182// ApiProvider is implemented by modules that contribute to an API surface
183type ApiProvider interface {
184 ConvertWithApiBp2build(ctx TopDownMutatorContext)
185}
186
Chris Parsonsf874e462022-05-10 13:50:12 -0400187// MixedBuildBuildable is an interface that module types should implement in order
188// to be "handled by Bazel" in a mixed build.
189type MixedBuildBuildable interface {
190 // IsMixedBuildSupported returns true if and only if this module should be
191 // "handled by Bazel" in a mixed build.
192 // This "escape hatch" allows modules with corner-case scenarios to opt out
193 // of being built with Bazel.
194 IsMixedBuildSupported(ctx BaseModuleContext) bool
195
196 // QueueBazelCall invokes request-queueing functions on the BazelContext
197 // so that these requests are handled when Bazel's cquery is invoked.
198 QueueBazelCall(ctx BaseModuleContext)
199
200 // ProcessBazelQueryResponse uses Bazel information (obtained from the BazelContext)
201 // to set module fields and providers to propagate this module's metadata upstream.
202 // This effectively "bridges the gap" between Bazel and Soong in a mixed build.
203 // Soong modules depending on this module should be oblivious to the fact that
204 // this module was handled by Bazel.
205 ProcessBazelQueryResponse(ctx ModuleContext)
206}
207
Liz Kammerea6666f2021-02-17 10:17:28 -0500208// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
209type BazelModule interface {
210 Module
211 Bazelable
212}
213
214// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
215// properties.
216func InitBazelModule(module BazelModule) {
217 module.AddProperties(module.bazelProps())
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400218 module.bazelProps().Bazel_module.CanConvertToBazel = true
Liz Kammerea6666f2021-02-17 10:17:28 -0500219}
220
Chris Parsons2173b5f2023-09-25 19:01:40 +0000221// BazelHandcraftedHook is a load hook to possibly register the current module as
222// a "handcrafted" Bazel target of a given name. If the current module should be
223// registered in this way, the hook function should return the target name. If
224// it should not be registered in this way, this function should return the empty string.
225type BazelHandcraftedHook func(ctx LoadHookContext) string
226
227// AddBazelHandcraftedHook adds a load hook to (maybe) mark the given module so that
228// it is treated by bp2build as if it has a handcrafted Bazel target.
229func AddBazelHandcraftedHook(module BazelModule, hook BazelHandcraftedHook) {
230 AddLoadHook(module, func(ctx LoadHookContext) {
231 var targetName string = hook(ctx)
232 if len(targetName) > 0 {
233 moduleDir := ctx.ModuleDir()
234 if moduleDir == Bp2BuildTopLevel {
235 moduleDir = ""
236 }
237 label := fmt.Sprintf("//%s:%s", moduleDir, targetName)
238 module.bazelProps().Bazel_module.Label = &label
239 }
240 })
241}
242
Liz Kammerea6666f2021-02-17 10:17:28 -0500243// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -0500244func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -0500245 return &b.bazelProperties
246}
247
Jingwen Chena47f28d2021-11-02 16:43:57 +0000248func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
249 return b.namespacedVariableProperties
250}
251
252func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
253 b.namespacedVariableProperties = props
254}
255
256func (b *BazelModuleBase) BaseModuleType() string {
257 return b.baseModuleType
258}
259
260func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
261 b.baseModuleType = baseModuleType
262}
263
Liz Kammerba3ea162021-02-17 13:22:03 -0500264// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
265func (b *BazelModuleBase) HasHandcraftedLabel() bool {
266 return b.bazelProperties.Bazel_module.Label != nil
267}
268
269// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
270func (b *BazelModuleBase) HandcraftedLabel() string {
271 return proptools.String(b.bazelProperties.Bazel_module.Label)
272}
273
Liz Kammerea6666f2021-02-17 10:17:28 -0500274// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -0500275func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
276 if b.HasHandcraftedLabel() {
277 return b.HandcraftedLabel()
278 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400279 if b.ShouldConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500280 return bp2buildModuleLabel(ctx, module)
281 }
Spandan Das5b18c0c2023-07-14 00:23:29 +0000282 panic(fmt.Errorf("requested non-existent label for module %s", module.Name()))
Liz Kammerea6666f2021-02-17 10:17:28 -0500283}
284
Cole Faust324a92e2022-08-23 15:29:05 -0700285type Bp2BuildConversionAllowlist struct {
Sam Delmerico24c56032022-03-28 19:53:03 +0000286 // Configure modules in these directories to enable bp2build_available: true or false by default.
287 defaultConfig allowlists.Bp2BuildConfig
Jingwen Chen12b4c272021-03-10 02:05:59 -0500288
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400289 // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000290 // in the synthetic Bazel workspace.
Sam Delmerico24c56032022-03-28 19:53:03 +0000291 keepExistingBuildFile map[string]bool
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000292
Chris Parsonsef615e52022-08-18 22:04:11 -0400293 // Per-module allowlist to always opt modules into both bp2build and Bazel Dev Mode mixed
294 // builds. These modules are usually in directories with many other modules that are not ready
295 // for conversion.
Jingwen Chen7edadab2022-03-04 07:01:29 +0000296 //
297 // A module can either be in this list or its directory allowlisted entirely
298 // in bp2buildDefaultConfig, but not both at the same time.
Sam Delmerico24c56032022-03-28 19:53:03 +0000299 moduleAlwaysConvert map[string]bool
Sam Delmericofa1831c2022-02-22 18:07:55 +0000300
Chris Parsonsef615e52022-08-18 22:04:11 -0400301 // Per-module-type allowlist to always opt modules in to both bp2build and
302 // Bazel Dev Mode mixed builds when they have the same type as one listed.
Sam Delmerico24c56032022-03-28 19:53:03 +0000303 moduleTypeAlwaysConvert map[string]bool
Sam Delmerico85d831a2022-03-07 19:12:42 +0000304
Chris Parsonsad876012022-08-20 14:48:32 -0400305 // Per-module denylist to always opt modules out of bp2build conversion.
Sam Delmerico24c56032022-03-28 19:53:03 +0000306 moduleDoNotConvert map[string]bool
Sam Delmerico24c56032022-03-28 19:53:03 +0000307}
Liz Kammer5c313582021-12-03 15:23:26 -0500308
Cole Faust324a92e2022-08-23 15:29:05 -0700309// NewBp2BuildAllowlist creates a new, empty Bp2BuildConversionAllowlist
Sam Delmerico24c56032022-03-28 19:53:03 +0000310// which can be populated using builder pattern Set* methods
Cole Faust324a92e2022-08-23 15:29:05 -0700311func NewBp2BuildAllowlist() Bp2BuildConversionAllowlist {
312 return Bp2BuildConversionAllowlist{
Sam Delmerico24c56032022-03-28 19:53:03 +0000313 allowlists.Bp2BuildConfig{},
314 map[string]bool{},
315 map[string]bool{},
316 map[string]bool{},
317 map[string]bool{},
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400318 }
319}
320
Sam Delmerico24c56032022-03-28 19:53:03 +0000321// SetDefaultConfig copies the entries from defaultConfig into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700322func (a Bp2BuildConversionAllowlist) SetDefaultConfig(defaultConfig allowlists.Bp2BuildConfig) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000323 if a.defaultConfig == nil {
324 a.defaultConfig = allowlists.Bp2BuildConfig{}
325 }
326 for k, v := range defaultConfig {
327 a.defaultConfig[k] = v
328 }
329
330 return a
331}
332
333// SetKeepExistingBuildFile copies the entries from keepExistingBuildFile into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700334func (a Bp2BuildConversionAllowlist) SetKeepExistingBuildFile(keepExistingBuildFile map[string]bool) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000335 if a.keepExistingBuildFile == nil {
336 a.keepExistingBuildFile = map[string]bool{}
337 }
338 for k, v := range keepExistingBuildFile {
339 a.keepExistingBuildFile[k] = v
340 }
341
342 return a
343}
344
345// SetModuleAlwaysConvertList copies the entries from moduleAlwaysConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700346func (a Bp2BuildConversionAllowlist) SetModuleAlwaysConvertList(moduleAlwaysConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000347 if a.moduleAlwaysConvert == nil {
348 a.moduleAlwaysConvert = map[string]bool{}
349 }
350 for _, m := range moduleAlwaysConvert {
351 a.moduleAlwaysConvert[m] = true
352 }
353
354 return a
355}
356
357// SetModuleTypeAlwaysConvertList copies the entries from moduleTypeAlwaysConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700358func (a Bp2BuildConversionAllowlist) SetModuleTypeAlwaysConvertList(moduleTypeAlwaysConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000359 if a.moduleTypeAlwaysConvert == nil {
360 a.moduleTypeAlwaysConvert = map[string]bool{}
361 }
362 for _, m := range moduleTypeAlwaysConvert {
363 a.moduleTypeAlwaysConvert[m] = true
364 }
365
366 return a
367}
368
369// SetModuleDoNotConvertList copies the entries from moduleDoNotConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700370func (a Bp2BuildConversionAllowlist) SetModuleDoNotConvertList(moduleDoNotConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000371 if a.moduleDoNotConvert == nil {
372 a.moduleDoNotConvert = map[string]bool{}
373 }
374 for _, m := range moduleDoNotConvert {
375 a.moduleDoNotConvert[m] = true
376 }
377
378 return a
379}
380
Sam Delmerico24c56032022-03-28 19:53:03 +0000381// ShouldKeepExistingBuildFileForDir returns whether an existing BUILD file should be
382// added to the build symlink forest based on the current global configuration.
Cole Faust324a92e2022-08-23 15:29:05 -0700383func (a Bp2BuildConversionAllowlist) ShouldKeepExistingBuildFileForDir(dir string) bool {
384 if _, ok := a.keepExistingBuildFile[dir]; ok {
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400385 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400386 return true
387 }
Usta Shresthaea999642022-11-02 01:03:07 -0400388 var i int
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400389 // Check if subtree match
Usta Shresthaea999642022-11-02 01:03:07 -0400390 for {
391 j := strings.Index(dir[i:], "/")
392 if j == -1 {
393 return false //default
394 }
395 prefix := dir[0 : i+j]
396 i = i + j + 1 // skip the "/"
397 if recursive, ok := a.keepExistingBuildFile[prefix]; ok && recursive {
398 return true
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400399 }
400 }
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400401}
402
Cole Faust324a92e2022-08-23 15:29:05 -0700403var bp2BuildAllowListKey = NewOnceKey("Bp2BuildAllowlist")
404var bp2buildAllowlist OncePer
405
406func GetBp2BuildAllowList() Bp2BuildConversionAllowlist {
407 return bp2buildAllowlist.Once(bp2BuildAllowListKey, func() interface{} {
408 return NewBp2BuildAllowlist().SetDefaultConfig(allowlists.Bp2buildDefaultConfig).
409 SetKeepExistingBuildFile(allowlists.Bp2buildKeepExistingBuildFile).
410 SetModuleAlwaysConvertList(allowlists.Bp2buildModuleAlwaysConvertList).
411 SetModuleTypeAlwaysConvertList(allowlists.Bp2buildModuleTypeAlwaysConvertList).
Sasha Smundak39a301c2022-12-29 17:11:49 -0800412 SetModuleDoNotConvertList(allowlists.Bp2buildModuleDoNotConvertList)
Cole Faust324a92e2022-08-23 15:29:05 -0700413 }).(Bp2BuildConversionAllowlist)
414}
415
MarkDacekf47e1422023-04-19 16:47:36 +0000416// MixedBuildsEnabled returns a MixedBuildEnabledStatus regarding whether
417// a module is ready to be replaced by a converted or handcrafted Bazel target.
418// As a side effect, calling this method will also log whether this module is
419// mixed build enabled for metrics reporting.
420func MixedBuildsEnabled(ctx BaseModuleContext) MixedBuildEnabledStatus {
MarkDacekf47e1422023-04-19 16:47:36 +0000421 platformIncompatible := isPlatformIncompatible(ctx.Os(), ctx.Arch().ArchType)
422 if platformIncompatible {
423 ctx.Config().LogMixedBuild(ctx, false)
424 return TechnicalIncompatibility
425 }
426
Liz Kammerc13f7852023-05-17 13:01:48 -0400427 if ctx.Config().AllowMissingDependencies() {
428 missingDeps := ctx.getMissingDependencies()
429 // If there are missing dependencies, querying Bazel will fail. Soong instead fails at execution
430 // time, not loading/analysis. disable mixed builds and fall back to Soong to maintain that
431 // behavior.
432 if len(missingDeps) > 0 {
433 ctx.Config().LogMixedBuild(ctx, false)
434 return ModuleMissingDeps
435 }
436 }
437
438 module := ctx.Module()
439 apexInfo := ctx.Provider(ApexInfoProvider).(ApexInfo)
440 withinApex := !apexInfo.IsForPlatform()
Sasha Smundak39a301c2022-12-29 17:11:49 -0800441 mixedBuildEnabled := ctx.Config().IsMixedBuildsEnabled() &&
Sasha Smundak39a301c2022-12-29 17:11:49 -0800442 module.Enabled() &&
443 convertedToBazel(ctx, module) &&
Yu Liue4312402023-01-18 09:15:31 -0800444 ctx.Config().BazelContext.IsModuleNameAllowed(module.Name(), withinApex)
MarkDacekff851b82022-04-21 18:33:17 +0000445 ctx.Config().LogMixedBuild(ctx, mixedBuildEnabled)
MarkDacekf47e1422023-04-19 16:47:36 +0000446
447 if mixedBuildEnabled {
448 return MixedBuildEnabled
449 }
450 return ModuleIncompatibility
MarkDacekff851b82022-04-21 18:33:17 +0000451}
452
Spandan Das64852422023-08-02 21:58:41 +0000453func isGoModule(module blueprint.Module) bool {
454 if _, ok := module.(*bootstrap.GoPackage); ok {
455 return true
456 }
457 if _, ok := module.(*bootstrap.GoBinary); ok {
458 return true
459 }
460 return false
461}
462
Liz Kammer6eff3232021-08-26 08:37:59 -0400463// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000464func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
Spandan Das64852422023-08-02 21:58:41 +0000465 // Special-case bootstrap_go_package and bootstrap_go_binary
466 // These do not implement Bazelable, but have been converted
467 if isGoModule(module) {
468 return true
469 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400470 b, ok := module.(Bazelable)
471 if !ok {
472 return false
473 }
Liz Kammerc86e0942023-08-11 16:15:12 -0400474
475 return b.HasHandcraftedLabel() || b.shouldConvertWithBp2build(ctx, shouldConvertParams{
476 module: module,
477 moduleDir: ctx.OtherModuleDir(module),
478 moduleName: ctx.OtherModuleName(module),
479 moduleType: ctx.OtherModuleType(module),
480 })
481}
482
483type ShouldConvertWithBazelContext interface {
484 ModuleErrorf(format string, args ...interface{})
485 Module() Module
486 Config() Config
487 ModuleType() string
488 ModuleName() string
489 ModuleDir() string
Liz Kammer6eff3232021-08-26 08:37:59 -0400490}
491
Sam Delmerico24c56032022-03-28 19:53:03 +0000492// ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build
Liz Kammerc86e0942023-08-11 16:15:12 -0400493func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx ShouldConvertWithBazelContext) bool {
494 return b.shouldConvertWithBp2build(ctx, shouldConvertParams{
495 module: ctx.Module(),
496 moduleDir: ctx.ModuleDir(),
497 moduleName: ctx.ModuleName(),
498 moduleType: ctx.ModuleType(),
499 })
Liz Kammer6eff3232021-08-26 08:37:59 -0400500}
501
Sam Delmerico24c56032022-03-28 19:53:03 +0000502type bazelOtherModuleContext interface {
503 ModuleErrorf(format string, args ...interface{})
504 Config() Config
505 OtherModuleType(m blueprint.Module) string
506 OtherModuleName(m blueprint.Module) string
507 OtherModuleDir(m blueprint.Module) string
508}
Sam Delmericofa1831c2022-02-22 18:07:55 +0000509
MarkDacekf47e1422023-04-19 16:47:36 +0000510func isPlatformIncompatible(osType OsType, arch ArchType) bool {
511 return osType == Windows || // Windows toolchains are not currently supported.
512 osType == LinuxBionic || // Linux Bionic toolchains are not currently supported.
513 osType == LinuxMusl || // Linux musl toolchains are not currently supported (b/259266326).
514 arch == Riscv64 // TODO(b/262192655) Riscv64 toolchains are not currently supported.
515}
516
Liz Kammerc86e0942023-08-11 16:15:12 -0400517type shouldConvertModuleContext interface {
518 ModuleErrorf(format string, args ...interface{})
519 Config() Config
520}
521
522type shouldConvertParams struct {
523 module blueprint.Module
524 moduleType string
525 moduleDir string
526 moduleName string
527}
528
529func (b *BazelModuleBase) shouldConvertWithBp2build(ctx shouldConvertModuleContext, p shouldConvertParams) bool {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400530 if !b.bazelProps().Bazel_module.CanConvertToBazel {
531 return false
Jingwen Chen12b4c272021-03-10 02:05:59 -0500532 }
533
Liz Kammerc86e0942023-08-11 16:15:12 -0400534 module := p.module
Spandan Das5af0bd32022-09-28 20:43:08 +0000535
Sam Delmerico85d831a2022-03-07 19:12:42 +0000536 propValue := b.bazelProperties.Bazel_module.Bp2build_available
Liz Kammerc86e0942023-08-11 16:15:12 -0400537 packagePath := moduleDirWithPossibleOverride(ctx, module, p.moduleDir)
Sam Delmerico24c56032022-03-28 19:53:03 +0000538
Sam Delmerico85d831a2022-03-07 19:12:42 +0000539 // Modules in unit tests which are enabled in the allowlist by type or name
540 // trigger this conditional because unit tests run under the "." package path
Sam Delmerico24c56032022-03-28 19:53:03 +0000541 isTestModule := packagePath == Bp2BuildTopLevel && proptools.BoolDefault(propValue, false)
542 if isTestModule {
543 return true
544 }
545
Liz Kammerc86e0942023-08-11 16:15:12 -0400546 moduleName := moduleNameWithPossibleOverride(ctx, module, p.moduleName)
Cole Faust324a92e2022-08-23 15:29:05 -0700547 allowlist := ctx.Config().Bp2buildPackageConfig
Sam Delmerico24c56032022-03-28 19:53:03 +0000548 moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName]
Liz Kammerc86e0942023-08-11 16:15:12 -0400549 moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[p.moduleType]
Sam Delmerico24c56032022-03-28 19:53:03 +0000550 allowlistConvert := moduleNameAllowed || moduleTypeAllowed
551 if moduleNameAllowed && moduleTypeAllowed {
Sam Delmericoe42edc32023-09-05 19:00:38 +0000552 ctx.ModuleErrorf("A module %q of type %q cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert", moduleName, p.moduleType)
Sam Delmerico24c56032022-03-28 19:53:03 +0000553 return false
554 }
555
556 if allowlist.moduleDoNotConvert[moduleName] {
Sam Delmerico85d831a2022-03-07 19:12:42 +0000557 if moduleNameAllowed {
Sam Delmericoe42edc32023-09-05 19:00:38 +0000558 ctx.ModuleErrorf("a module %q cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert", moduleName)
Sam Delmerico85d831a2022-03-07 19:12:42 +0000559 }
Sam Delmerico94d26c22022-02-25 21:34:51 +0000560 return false
561 }
562
Sam Delmerico24c56032022-03-28 19:53:03 +0000563 // This is a tristate value: true, false, or unset.
564 if ok, directoryPath := bp2buildDefaultTrueRecursively(packagePath, allowlist.defaultConfig); ok {
565 if moduleNameAllowed {
566 ctx.ModuleErrorf("A module cannot be in a directory marked Bp2BuildDefaultTrue"+
Yu Liu10853f92022-09-14 16:05:22 -0700567 " or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: '%s'"+
568 " Module: '%s'", directoryPath, moduleName)
Sam Delmerico24c56032022-03-28 19:53:03 +0000569 return false
Sam Delmericofa1831c2022-02-22 18:07:55 +0000570 }
571
Jingwen Chen12b4c272021-03-10 02:05:59 -0500572 // Allow modules to explicitly opt-out.
573 return proptools.BoolDefault(propValue, true)
574 }
575
576 // Allow modules to explicitly opt-in.
Sam Delmerico85d831a2022-03-07 19:12:42 +0000577 return proptools.BoolDefault(propValue, allowlistConvert)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500578}
579
580// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
581// set of package prefixes where all modules must be converted. That is, if the
582// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
583// return true.
584//
585// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
586// exactly, this module will return false early.
587//
588// This function will also return false if the package doesn't match anything in
589// the config.
Sam Delmerico24c56032022-03-28 19:53:03 +0000590//
591// This function will also return the allowlist entry which caused a particular
592// package to be enabled. Since packages can be enabled via a recursive declaration,
593// the path returned will not always be the same as the one provided.
594func bp2buildDefaultTrueRecursively(packagePath string, config allowlists.Bp2BuildConfig) (bool, string) {
Jingwen Chen294e7742021-08-31 05:58:01 +0000595 // Check if the package path has an exact match in the config.
Sam Delmerico24c56032022-03-28 19:53:03 +0000596 if config[packagePath] == allowlists.Bp2BuildDefaultTrue || config[packagePath] == allowlists.Bp2BuildDefaultTrueRecursively {
597 return true, packagePath
MarkDacek756b2962022-10-13 17:50:17 +0000598 } else if config[packagePath] == allowlists.Bp2BuildDefaultFalse || config[packagePath] == allowlists.Bp2BuildDefaultFalseRecursively {
Sam Delmerico24c56032022-03-28 19:53:03 +0000599 return false, packagePath
Jingwen Chen12b4c272021-03-10 02:05:59 -0500600 }
601
Jingwen Chen91220d72021-03-24 02:18:33 -0400602 // If not, check for the config recursively.
MarkDacek756b2962022-10-13 17:50:17 +0000603 packagePrefix := packagePath
604
605 // e.g. for x/y/z, iterate over x/y, then x, taking the most-specific value from the allowlist.
606 for strings.Contains(packagePrefix, "/") {
607 dirIndex := strings.LastIndex(packagePrefix, "/")
608 packagePrefix = packagePrefix[:dirIndex]
609 switch value := config[packagePrefix]; value {
610 case allowlists.Bp2BuildDefaultTrueRecursively:
Jingwen Chen12b4c272021-03-10 02:05:59 -0500611 // package contains this prefix and this prefix should convert all modules
Sam Delmerico24c56032022-03-28 19:53:03 +0000612 return true, packagePrefix
MarkDacek756b2962022-10-13 17:50:17 +0000613 case allowlists.Bp2BuildDefaultFalseRecursively:
614 //package contains this prefix and this prefix should NOT convert any modules
615 return false, packagePrefix
Jingwen Chen12b4c272021-03-10 02:05:59 -0500616 }
617 // Continue to the next part of the package dir.
MarkDacek756b2962022-10-13 17:50:17 +0000618
Jingwen Chen12b4c272021-03-10 02:05:59 -0500619 }
620
Sam Delmerico24c56032022-03-28 19:53:03 +0000621 return false, packagePath
Liz Kammerea6666f2021-02-17 10:17:28 -0500622}
Liz Kammerba3ea162021-02-17 13:22:03 -0500623
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400624func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) {
Chris Parsons6666d0f2023-09-22 16:21:53 +0000625 ctx.BottomUp("bp2build_conversion", bp2buildConversionMutator).Parallel()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400626}
627
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000628func registerBp2buildDepsMutator(ctx RegisterMutatorsContext) {
629 ctx.BottomUp("bp2build_deps", bp2buildDepsMutator).Parallel()
630}
631
Chris Parsons6666d0f2023-09-22 16:21:53 +0000632func bp2buildConversionMutator(ctx BottomUpMutatorContext) {
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000633 // If an existing BUILD file in the module directory has a target defined
634 // with this same name as this module, assume that this is an existing
635 // definition for this target.
636 if ctx.Config().HasBazelBuildTargetInSource(ctx.ModuleDir(), ctx.ModuleName()) {
637 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, ctx.ModuleName())
638 return
639 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400640 bModule, ok := ctx.Module().(Bazelable)
Chris Parsons39a16972023-06-08 14:28:51 +0000641 if !ok {
642 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400643 return
644 }
Liz Kammerc86e0942023-08-11 16:15:12 -0400645 // There may be cases where the target is created by a macro rather than in a BUILD file, those
646 // should be captured as well.
647 if bModule.HasHandcraftedLabel() {
648 // Defer to the BUILD target. Generating an additional target would
649 // cause a BUILD file conflict.
650 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, "")
651 return
652 }
Chris Parsons39a16972023-06-08 14:28:51 +0000653 // TODO: b/285631638 - Differentiate between denylisted modules and missing bp2build capabilities.
Liz Kammerc86e0942023-08-11 16:15:12 -0400654 if !bModule.shouldConvertWithBp2build(ctx, shouldConvertParams{
655 module: ctx.Module(),
656 moduleDir: ctx.ModuleDir(),
657 moduleName: ctx.ModuleName(),
658 moduleType: ctx.ModuleType(),
659 }) {
Chris Parsons39a16972023-06-08 14:28:51 +0000660 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "")
661 return
662 }
Chris Parsons0c4de1f2023-09-21 20:36:35 +0000663 if ctx.Module().base().GetUnconvertedReason() != nil {
664 return
665 }
666
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400667 bModule.ConvertWithBp2build(ctx)
Chris Parsons39a16972023-06-08 14:28:51 +0000668
Chris Parsons0c4de1f2023-09-21 20:36:35 +0000669 if len(ctx.Module().base().Bp2buildTargets()) == 0 && ctx.Module().base().GetUnconvertedReason() == nil {
Chris Parsons39a16972023-06-08 14:28:51 +0000670 panic(fmt.Errorf("illegal bp2build invariant: module '%s' was neither converted nor marked unconvertible", ctx.ModuleName()))
671 }
Chris Parsons0c4de1f2023-09-21 20:36:35 +0000672
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000673 // If an existing BUILD file in the module directory has a target defined
674 // with the same name as any target generated by this module, assume that this
675 // is an existing definition for this target. (These generated target names
676 // may be different than the module name, as checked at the beginning of this function!)
Chris Parsons0c4de1f2023-09-21 20:36:35 +0000677 for _, targetInfo := range ctx.Module().base().Bp2buildTargets() {
678 if ctx.Config().HasBazelBuildTargetInSource(targetInfo.TargetPackage(), targetInfo.TargetName()) {
679 // Defer to the BUILD target. Generating an additional target would
680 // cause a BUILD file conflict.
681 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, targetInfo.TargetName())
682 return
683 }
684 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400685}
Wei Libafb6d62021-12-10 03:14:59 -0800686
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000687// TODO: b/285631638 - Add this as a new mutator to the bp2build conversion mutators.
688// Currently, this only exists to prepare test coverage for the launch of this feature.
689func bp2buildDepsMutator(ctx BottomUpMutatorContext) {
690 if ctx.Module().base().GetUnconvertedReason() != nil {
691 return
692 }
693
694 if len(ctx.Module().GetMissingBp2buildDeps()) > 0 {
695 exampleDep := ctx.Module().GetMissingBp2buildDeps()[0]
Jingwen Chen464e6a02023-10-05 09:58:00 +0000696 ctx.MarkBp2buildUnconvertible(
697 bp2build_metrics_proto.UnconvertedReasonType_UNCONVERTED_DEP, exampleDep)
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000698 }
699
Jingwen Chen464e6a02023-10-05 09:58:00 +0000700 // Transitively mark modules unconvertible with the following set of conditions.
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000701 ctx.VisitDirectDeps(func(dep Module) {
Jingwen Chen464e6a02023-10-05 09:58:00 +0000702 if dep.base().GetUnconvertedReason() == nil {
703 return
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000704 }
Jingwen Chen464e6a02023-10-05 09:58:00 +0000705
706 if dep.base().GetUnconvertedReason().ReasonType ==
707 int(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE) {
708 return
709 }
710
711 if ctx.OtherModuleDependencyTag(dep) != Bp2buildDepTag {
712 return
713 }
714
715 ctx.MarkBp2buildUnconvertible(
716 bp2build_metrics_proto.UnconvertedReasonType_UNCONVERTED_DEP, dep.Name())
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000717 })
718}
719
Wei Libafb6d62021-12-10 03:14:59 -0800720// GetMainClassInManifest scans the manifest file specified in filepath and returns
721// the value of attribute Main-Class in the manifest file if it exists, or returns error.
722// WARNING: this is for bp2build converters of java_* modules only.
723func GetMainClassInManifest(c Config, filepath string) (string, error) {
724 file, err := c.fs.Open(filepath)
725 if err != nil {
726 return "", err
727 }
Liz Kammer0fe123d2022-02-07 10:17:35 -0500728 defer file.Close()
Wei Libafb6d62021-12-10 03:14:59 -0800729 scanner := bufio.NewScanner(file)
730 for scanner.Scan() {
731 line := scanner.Text()
732 if strings.HasPrefix(line, "Main-Class:") {
733 return strings.TrimSpace(line[len("Main-Class:"):]), nil
734 }
735 }
736
737 return "", errors.New("Main-Class is not found.")
738}
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500739
740func AttachValidationActions(ctx ModuleContext, outputFilePath Path, validations Paths) ModuleOutPath {
741 validatedOutputFilePath := PathForModuleOut(ctx, "validated", outputFilePath.Base())
742 ctx.Build(pctx, BuildParams{
743 Rule: CpNoPreserveSymlink,
744 Description: "run validations " + outputFilePath.Base(),
745 Output: validatedOutputFilePath,
746 Input: outputFilePath,
747 Validations: validations,
748 })
749 return validatedOutputFilePath
750}
yikefdca7fe2023-08-17 01:03:37 +0000751
752func RunsOn(hostSupported bool, deviceSupported bool, unitTest bool) []string {
753 var runsOn []string
754
755 if hostSupported && deviceSupported {
756 runsOn = []string{"host_without_device", "device"}
757 } else if hostSupported {
758 if unitTest {
759 runsOn = []string{"host_without_device"}
760 } else {
761 runsOn = []string{"host_with_device"}
762 }
763 } else if deviceSupported {
764 runsOn = []string{"device"}
765 }
766
767 return runsOn
768}