blob: 1bfbdec00c4ef7557d172cdb2af17e2fa619c070 [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 Kammerbdc60992021-02-24 16:55:11 -050024 "github.com/google/blueprint"
Liz Kammerba3ea162021-02-17 13:22:03 -050025 "github.com/google/blueprint/proptools"
Sam Delmerico24c56032022-03-28 19:53:03 +000026
27 "android/soong/android/allowlists"
28)
29
30const (
31 // A sentinel value to be used as a key in Bp2BuildConfig for modules with
32 // no package path. This is also the module dir for top level Android.bp
33 // modules.
34 Bp2BuildTopLevel = "."
Liz Kammerba3ea162021-02-17 13:22:03 -050035)
36
MarkDacekf47e1422023-04-19 16:47:36 +000037type MixedBuildEnabledStatus int
38
39const (
40 // This module can be mixed_built.
41 MixedBuildEnabled = iota
42
43 // There is a technical incompatibility preventing this module from being
44 // bazel-analyzed. Note: the module might also be incompatible.
45 TechnicalIncompatibility
46
47 // This module cannot be mixed_built due to some incompatibility with it
48 // that is not a platform incompatibility. Example: the module-type is not
49 // enabled, or is not bp2build-converted.
50 ModuleIncompatibility
Liz Kammerc13f7852023-05-17 13:01:48 -040051
52 // Missing dependencies. We can't query Bazel for modules if it has missing dependencies, there
53 // will be failures.
54 ModuleMissingDeps
MarkDacekf47e1422023-04-19 16:47:36 +000055)
56
Yu Liu2aa806b2022-09-01 11:54:47 -070057// FileGroupAsLibrary describes a filegroup module that is converted to some library
58// such as aidl_library or proto_library.
59type FileGroupAsLibrary interface {
Vinh Tran444154d2022-08-16 13:10:31 -040060 ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool
Yu Liu2aa806b2022-09-01 11:54:47 -070061 ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool
Vinh Tran444154d2022-08-16 13:10:31 -040062 GetAidlLibraryLabel(ctx BazelConversionPathContext) string
Yu Liu2aa806b2022-09-01 11:54:47 -070063 GetProtoLibraryLabel(ctx BazelConversionPathContext) string
Vinh Tran444154d2022-08-16 13:10:31 -040064}
65
Sasha Smundaka0954062022-08-02 18:23:58 -070066type BazelConversionStatus struct {
67 // Information about _all_ bp2build targets generated by this module. Multiple targets are
68 // supported as Soong handles some things within a single target that we may choose to split into
69 // multiple targets, e.g. renderscript, protos, yacc within a cc module.
70 Bp2buildInfo []bp2buildInfo `blueprint:"mutated"`
71
72 // UnconvertedBp2buildDep stores the module names of direct dependency that were not converted to
73 // Bazel
74 UnconvertedDeps []string `blueprint:"mutated"`
75
76 // MissingBp2buildDep stores the module names of direct dependency that were not found
77 MissingDeps []string `blueprint:"mutated"`
Chris Parsons39a16972023-06-08 14:28:51 +000078
79 // If non-nil, indicates that the module could not be converted successfully
80 // with bp2build. This will describe the reason the module could not be converted.
81 UnconvertedReason *UnconvertedReason
82}
83
84// The reason a module could not be converted to a BUILD target via bp2build.
85// This should match bp2build_metrics_proto.UnconvertedReason, but omits private
86// proto-related fields that prevent copying this struct.
87type UnconvertedReason struct {
88 // Should correspond to a valid value in bp2build_metrics_proto.UnconvertedReasonType.
89 // A raw int is used here instead, because blueprint logic requires that all transitive
90 // fields of module definitions be primitives.
91 ReasonType int
92 Detail string
Sasha Smundaka0954062022-08-02 18:23:58 -070093}
94
Romain Jobredeaux8242b432023-05-04 10:16:26 -040095type BazelModuleProperties struct {
Jingwen Chen01812022021-11-19 14:29:43 +000096 // The label of the Bazel target replacing this Soong module. When run in conversion mode, this
97 // will import the handcrafted build target into the autogenerated file. Note: this may result in
98 // a conflict due to duplicate targets if bp2build_available is also set.
99 Label *string
100
101 // If true, bp2build will generate the converted Bazel target for this module. Note: this may
102 // cause a conflict due to the duplicate targets if label is also set.
103 //
104 // This is a bool pointer to support tristates: true, false, not set.
105 //
Sasha Smundak39a301c2022-12-29 17:11:49 -0800106 // To opt in a module, set bazel_module: { bp2build_available: true }
107 // To opt out a module, set bazel_module: { bp2build_available: false }
Jingwen Chen01812022021-11-19 14:29:43 +0000108 // To defer the default setting for the directory, do not set the value.
109 Bp2build_available *bool
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400110
111 // CanConvertToBazel is set via InitBazelModule to indicate that a module type can be converted to
112 // Bazel with Bp2build.
113 CanConvertToBazel bool `blueprint:"mutated"`
Jingwen Chen01812022021-11-19 14:29:43 +0000114}
115
Liz Kammerba3ea162021-02-17 13:22:03 -0500116// Properties contains common module properties for Bazel migration purposes.
117type properties struct {
Chris Parsons1bb58da2022-08-30 13:37:57 -0400118 // In "Bazel mixed build" mode, this represents the Bazel target replacing
Liz Kammerba3ea162021-02-17 13:22:03 -0500119 // this Soong module.
Romain Jobredeaux8242b432023-05-04 10:16:26 -0400120 Bazel_module BazelModuleProperties
Liz Kammerba3ea162021-02-17 13:22:03 -0500121}
Liz Kammerea6666f2021-02-17 10:17:28 -0500122
Jingwen Chen25825ca2021-11-15 12:28:43 +0000123// namespacedVariableProperties is a map from a string representing a Soong
Jingwen Chen84817de2021-11-17 10:57:35 +0000124// config variable namespace, like "android" or "vendor_name" to a slice of
125// pointer to a struct containing a single field called Soong_config_variables
126// whose value mirrors the structure in the Blueprint file.
127type namespacedVariableProperties map[string][]interface{}
Jingwen Chena47f28d2021-11-02 16:43:57 +0000128
Liz Kammerea6666f2021-02-17 10:17:28 -0500129// BazelModuleBase contains the property structs with metadata for modules which can be converted to
130// Bazel.
131type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -0500132 bazelProperties properties
Jingwen Chena47f28d2021-11-02 16:43:57 +0000133
134 // namespacedVariableProperties is used for soong_config_module_type support
135 // in bp2build. Soong config modules allow users to set module properties
136 // based on custom product variables defined in Android.bp files. These
137 // variables are namespaced to prevent clobbering, especially when set from
138 // Makefiles.
139 namespacedVariableProperties namespacedVariableProperties
140
141 // baseModuleType is set when this module was created from a module type
142 // defined by a soong_config_module_type. Every soong_config_module_type
143 // "wraps" another module type, e.g. a soong_config_module_type can wrap a
144 // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
145 // This baseModuleType is set to the wrapped module type.
146 baseModuleType string
Liz Kammerea6666f2021-02-17 10:17:28 -0500147}
148
149// Bazelable is specifies the interface for modules that can be converted to Bazel.
150type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -0500151 bazelProps() *properties
152 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -0500153 HandcraftedLabel() string
154 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400155 ShouldConvertWithBp2build(ctx BazelConversionContext) bool
Sam Delmerico24c56032022-03-28 19:53:03 +0000156 shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool
Chris Parsons39a16972023-06-08 14:28:51 +0000157
158 // ConvertWithBp2build either converts the module to a Bazel build target or
159 // declares the module as unconvertible (for logging and metrics).
160 // Modules must implement this function to be bp2build convertible. The function
161 // must either create at least one Bazel target module (using ctx.CreateBazelTargetModule or
162 // its related functions), or declare itself unconvertible using ctx.MarkBp2buildUnconvertible.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400163 ConvertWithBp2build(ctx TopDownMutatorContext)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000164
Jingwen Chen84817de2021-11-17 10:57:35 +0000165 // namespacedVariableProps is a map from a soong config variable namespace
166 // (e.g. acme, android) to a map of interfaces{}, which are really
167 // reflect.Struct pointers, representing the value of the
168 // soong_config_variables property of a module. The struct pointer is the
169 // one with the single member called Soong_config_variables, which itself is
170 // a struct containing fields for each supported feature in that namespace.
171 //
Sasha Smundak39a301c2022-12-29 17:11:49 -0800172 // The reason for using a slice of interface{} is to support defaults
Jingwen Chen84817de2021-11-17 10:57:35 +0000173 // propagation of the struct pointers.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000174 namespacedVariableProps() namespacedVariableProperties
175 setNamespacedVariableProps(props namespacedVariableProperties)
176 BaseModuleType() string
Jingwen Chen84817de2021-11-17 10:57:35 +0000177 SetBaseModuleType(baseModuleType string)
Liz Kammerea6666f2021-02-17 10:17:28 -0500178}
179
Spandan Das5af0bd32022-09-28 20:43:08 +0000180// ApiProvider is implemented by modules that contribute to an API surface
181type ApiProvider interface {
182 ConvertWithApiBp2build(ctx TopDownMutatorContext)
183}
184
Chris Parsonsf874e462022-05-10 13:50:12 -0400185// MixedBuildBuildable is an interface that module types should implement in order
186// to be "handled by Bazel" in a mixed build.
187type MixedBuildBuildable interface {
188 // IsMixedBuildSupported returns true if and only if this module should be
189 // "handled by Bazel" in a mixed build.
190 // This "escape hatch" allows modules with corner-case scenarios to opt out
191 // of being built with Bazel.
192 IsMixedBuildSupported(ctx BaseModuleContext) bool
193
194 // QueueBazelCall invokes request-queueing functions on the BazelContext
195 // so that these requests are handled when Bazel's cquery is invoked.
196 QueueBazelCall(ctx BaseModuleContext)
197
198 // ProcessBazelQueryResponse uses Bazel information (obtained from the BazelContext)
199 // to set module fields and providers to propagate this module's metadata upstream.
200 // This effectively "bridges the gap" between Bazel and Soong in a mixed build.
201 // Soong modules depending on this module should be oblivious to the fact that
202 // this module was handled by Bazel.
203 ProcessBazelQueryResponse(ctx ModuleContext)
204}
205
Liz Kammerea6666f2021-02-17 10:17:28 -0500206// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
207type BazelModule interface {
208 Module
209 Bazelable
210}
211
212// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
213// properties.
214func InitBazelModule(module BazelModule) {
215 module.AddProperties(module.bazelProps())
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400216 module.bazelProps().Bazel_module.CanConvertToBazel = true
Liz Kammerea6666f2021-02-17 10:17:28 -0500217}
218
219// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -0500220func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -0500221 return &b.bazelProperties
222}
223
Jingwen Chena47f28d2021-11-02 16:43:57 +0000224func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
225 return b.namespacedVariableProperties
226}
227
228func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
229 b.namespacedVariableProperties = props
230}
231
232func (b *BazelModuleBase) BaseModuleType() string {
233 return b.baseModuleType
234}
235
236func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
237 b.baseModuleType = baseModuleType
238}
239
Liz Kammerba3ea162021-02-17 13:22:03 -0500240// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
241func (b *BazelModuleBase) HasHandcraftedLabel() bool {
242 return b.bazelProperties.Bazel_module.Label != nil
243}
244
245// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
246func (b *BazelModuleBase) HandcraftedLabel() string {
247 return proptools.String(b.bazelProperties.Bazel_module.Label)
248}
249
Liz Kammerea6666f2021-02-17 10:17:28 -0500250// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -0500251func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
252 if b.HasHandcraftedLabel() {
253 return b.HandcraftedLabel()
254 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400255 if b.ShouldConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500256 return bp2buildModuleLabel(ctx, module)
257 }
Chris Parsons39a16972023-06-08 14:28:51 +0000258 panic(fmt.Errorf("requested non-existent label for module ", module.Name()))
Liz Kammerea6666f2021-02-17 10:17:28 -0500259}
260
Cole Faust324a92e2022-08-23 15:29:05 -0700261type Bp2BuildConversionAllowlist struct {
Sam Delmerico24c56032022-03-28 19:53:03 +0000262 // Configure modules in these directories to enable bp2build_available: true or false by default.
263 defaultConfig allowlists.Bp2BuildConfig
Jingwen Chen12b4c272021-03-10 02:05:59 -0500264
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400265 // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000266 // in the synthetic Bazel workspace.
Sam Delmerico24c56032022-03-28 19:53:03 +0000267 keepExistingBuildFile map[string]bool
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000268
Chris Parsonsef615e52022-08-18 22:04:11 -0400269 // Per-module allowlist to always opt modules into both bp2build and Bazel Dev Mode mixed
270 // builds. These modules are usually in directories with many other modules that are not ready
271 // for conversion.
Jingwen Chen7edadab2022-03-04 07:01:29 +0000272 //
273 // A module can either be in this list or its directory allowlisted entirely
274 // in bp2buildDefaultConfig, but not both at the same time.
Sam Delmerico24c56032022-03-28 19:53:03 +0000275 moduleAlwaysConvert map[string]bool
Sam Delmericofa1831c2022-02-22 18:07:55 +0000276
Chris Parsonsef615e52022-08-18 22:04:11 -0400277 // Per-module-type allowlist to always opt modules in to both bp2build and
278 // Bazel Dev Mode mixed builds when they have the same type as one listed.
Sam Delmerico24c56032022-03-28 19:53:03 +0000279 moduleTypeAlwaysConvert map[string]bool
Sam Delmerico85d831a2022-03-07 19:12:42 +0000280
Chris Parsonsad876012022-08-20 14:48:32 -0400281 // Per-module denylist to always opt modules out of bp2build conversion.
Sam Delmerico24c56032022-03-28 19:53:03 +0000282 moduleDoNotConvert map[string]bool
Sam Delmerico24c56032022-03-28 19:53:03 +0000283}
Liz Kammer5c313582021-12-03 15:23:26 -0500284
Cole Faust324a92e2022-08-23 15:29:05 -0700285// NewBp2BuildAllowlist creates a new, empty Bp2BuildConversionAllowlist
Sam Delmerico24c56032022-03-28 19:53:03 +0000286// which can be populated using builder pattern Set* methods
Cole Faust324a92e2022-08-23 15:29:05 -0700287func NewBp2BuildAllowlist() Bp2BuildConversionAllowlist {
288 return Bp2BuildConversionAllowlist{
Sam Delmerico24c56032022-03-28 19:53:03 +0000289 allowlists.Bp2BuildConfig{},
290 map[string]bool{},
291 map[string]bool{},
292 map[string]bool{},
293 map[string]bool{},
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400294 }
295}
296
Sam Delmerico24c56032022-03-28 19:53:03 +0000297// SetDefaultConfig copies the entries from defaultConfig into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700298func (a Bp2BuildConversionAllowlist) SetDefaultConfig(defaultConfig allowlists.Bp2BuildConfig) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000299 if a.defaultConfig == nil {
300 a.defaultConfig = allowlists.Bp2BuildConfig{}
301 }
302 for k, v := range defaultConfig {
303 a.defaultConfig[k] = v
304 }
305
306 return a
307}
308
309// SetKeepExistingBuildFile copies the entries from keepExistingBuildFile into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700310func (a Bp2BuildConversionAllowlist) SetKeepExistingBuildFile(keepExistingBuildFile map[string]bool) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000311 if a.keepExistingBuildFile == nil {
312 a.keepExistingBuildFile = map[string]bool{}
313 }
314 for k, v := range keepExistingBuildFile {
315 a.keepExistingBuildFile[k] = v
316 }
317
318 return a
319}
320
321// SetModuleAlwaysConvertList copies the entries from moduleAlwaysConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700322func (a Bp2BuildConversionAllowlist) SetModuleAlwaysConvertList(moduleAlwaysConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000323 if a.moduleAlwaysConvert == nil {
324 a.moduleAlwaysConvert = map[string]bool{}
325 }
326 for _, m := range moduleAlwaysConvert {
327 a.moduleAlwaysConvert[m] = true
328 }
329
330 return a
331}
332
333// SetModuleTypeAlwaysConvertList copies the entries from moduleTypeAlwaysConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700334func (a Bp2BuildConversionAllowlist) SetModuleTypeAlwaysConvertList(moduleTypeAlwaysConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000335 if a.moduleTypeAlwaysConvert == nil {
336 a.moduleTypeAlwaysConvert = map[string]bool{}
337 }
338 for _, m := range moduleTypeAlwaysConvert {
339 a.moduleTypeAlwaysConvert[m] = true
340 }
341
342 return a
343}
344
345// SetModuleDoNotConvertList copies the entries from moduleDoNotConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700346func (a Bp2BuildConversionAllowlist) SetModuleDoNotConvertList(moduleDoNotConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000347 if a.moduleDoNotConvert == nil {
348 a.moduleDoNotConvert = map[string]bool{}
349 }
350 for _, m := range moduleDoNotConvert {
351 a.moduleDoNotConvert[m] = true
352 }
353
354 return a
355}
356
Sam Delmerico24c56032022-03-28 19:53:03 +0000357// ShouldKeepExistingBuildFileForDir returns whether an existing BUILD file should be
358// added to the build symlink forest based on the current global configuration.
Cole Faust324a92e2022-08-23 15:29:05 -0700359func (a Bp2BuildConversionAllowlist) ShouldKeepExistingBuildFileForDir(dir string) bool {
360 if _, ok := a.keepExistingBuildFile[dir]; ok {
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400361 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400362 return true
363 }
Usta Shresthaea999642022-11-02 01:03:07 -0400364 var i int
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400365 // Check if subtree match
Usta Shresthaea999642022-11-02 01:03:07 -0400366 for {
367 j := strings.Index(dir[i:], "/")
368 if j == -1 {
369 return false //default
370 }
371 prefix := dir[0 : i+j]
372 i = i + j + 1 // skip the "/"
373 if recursive, ok := a.keepExistingBuildFile[prefix]; ok && recursive {
374 return true
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400375 }
376 }
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400377}
378
Cole Faust324a92e2022-08-23 15:29:05 -0700379var bp2BuildAllowListKey = NewOnceKey("Bp2BuildAllowlist")
380var bp2buildAllowlist OncePer
381
382func GetBp2BuildAllowList() Bp2BuildConversionAllowlist {
383 return bp2buildAllowlist.Once(bp2BuildAllowListKey, func() interface{} {
384 return NewBp2BuildAllowlist().SetDefaultConfig(allowlists.Bp2buildDefaultConfig).
385 SetKeepExistingBuildFile(allowlists.Bp2buildKeepExistingBuildFile).
386 SetModuleAlwaysConvertList(allowlists.Bp2buildModuleAlwaysConvertList).
387 SetModuleTypeAlwaysConvertList(allowlists.Bp2buildModuleTypeAlwaysConvertList).
Sasha Smundak39a301c2022-12-29 17:11:49 -0800388 SetModuleDoNotConvertList(allowlists.Bp2buildModuleDoNotConvertList)
Cole Faust324a92e2022-08-23 15:29:05 -0700389 }).(Bp2BuildConversionAllowlist)
390}
391
MarkDacekf47e1422023-04-19 16:47:36 +0000392// MixedBuildsEnabled returns a MixedBuildEnabledStatus regarding whether
393// a module is ready to be replaced by a converted or handcrafted Bazel target.
394// As a side effect, calling this method will also log whether this module is
395// mixed build enabled for metrics reporting.
396func MixedBuildsEnabled(ctx BaseModuleContext) MixedBuildEnabledStatus {
MarkDacekf47e1422023-04-19 16:47:36 +0000397 platformIncompatible := isPlatformIncompatible(ctx.Os(), ctx.Arch().ArchType)
398 if platformIncompatible {
399 ctx.Config().LogMixedBuild(ctx, false)
400 return TechnicalIncompatibility
401 }
402
Liz Kammerc13f7852023-05-17 13:01:48 -0400403 if ctx.Config().AllowMissingDependencies() {
404 missingDeps := ctx.getMissingDependencies()
405 // If there are missing dependencies, querying Bazel will fail. Soong instead fails at execution
406 // time, not loading/analysis. disable mixed builds and fall back to Soong to maintain that
407 // behavior.
408 if len(missingDeps) > 0 {
409 ctx.Config().LogMixedBuild(ctx, false)
410 return ModuleMissingDeps
411 }
412 }
413
414 module := ctx.Module()
415 apexInfo := ctx.Provider(ApexInfoProvider).(ApexInfo)
416 withinApex := !apexInfo.IsForPlatform()
Sasha Smundak39a301c2022-12-29 17:11:49 -0800417 mixedBuildEnabled := ctx.Config().IsMixedBuildsEnabled() &&
Sasha Smundak39a301c2022-12-29 17:11:49 -0800418 module.Enabled() &&
419 convertedToBazel(ctx, module) &&
Yu Liue4312402023-01-18 09:15:31 -0800420 ctx.Config().BazelContext.IsModuleNameAllowed(module.Name(), withinApex)
MarkDacekff851b82022-04-21 18:33:17 +0000421 ctx.Config().LogMixedBuild(ctx, mixedBuildEnabled)
MarkDacekf47e1422023-04-19 16:47:36 +0000422
423 if mixedBuildEnabled {
424 return MixedBuildEnabled
425 }
426 return ModuleIncompatibility
MarkDacekff851b82022-04-21 18:33:17 +0000427}
428
Liz Kammer6eff3232021-08-26 08:37:59 -0400429// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000430func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
Liz Kammer6eff3232021-08-26 08:37:59 -0400431 b, ok := module.(Bazelable)
432 if !ok {
433 return false
434 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400435 return b.shouldConvertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
Liz Kammer6eff3232021-08-26 08:37:59 -0400436}
437
Sam Delmerico24c56032022-03-28 19:53:03 +0000438// ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400439func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx BazelConversionContext) bool {
440 return b.shouldConvertWithBp2build(ctx, ctx.Module())
Liz Kammer6eff3232021-08-26 08:37:59 -0400441}
442
Sam Delmerico24c56032022-03-28 19:53:03 +0000443type bazelOtherModuleContext interface {
444 ModuleErrorf(format string, args ...interface{})
445 Config() Config
446 OtherModuleType(m blueprint.Module) string
447 OtherModuleName(m blueprint.Module) string
448 OtherModuleDir(m blueprint.Module) string
449}
Sam Delmericofa1831c2022-02-22 18:07:55 +0000450
MarkDacekf47e1422023-04-19 16:47:36 +0000451func isPlatformIncompatible(osType OsType, arch ArchType) bool {
452 return osType == Windows || // Windows toolchains are not currently supported.
453 osType == LinuxBionic || // Linux Bionic toolchains are not currently supported.
454 osType == LinuxMusl || // Linux musl toolchains are not currently supported (b/259266326).
455 arch == Riscv64 // TODO(b/262192655) Riscv64 toolchains are not currently supported.
456}
457
Sam Delmerico24c56032022-03-28 19:53:03 +0000458func (b *BazelModuleBase) shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400459 if !b.bazelProps().Bazel_module.CanConvertToBazel {
460 return false
Jingwen Chen12b4c272021-03-10 02:05:59 -0500461 }
462
Spandan Das5af0bd32022-09-28 20:43:08 +0000463 // In api_bp2build mode, all soong modules that can provide API contributions should be converted
464 // This is irrespective of its presence/absence in bp2build allowlists
465 if ctx.Config().BuildMode == ApiBp2build {
466 _, providesApis := module.(ApiProvider)
467 return providesApis
468 }
469
Sam Delmerico85d831a2022-03-07 19:12:42 +0000470 propValue := b.bazelProperties.Bazel_module.Bp2build_available
Liz Kammer20f0f782023-05-01 13:46:33 -0400471 packagePath := moduleDirWithPossibleOverride(ctx, module)
Sam Delmerico24c56032022-03-28 19:53:03 +0000472
Sam Delmerico85d831a2022-03-07 19:12:42 +0000473 // Modules in unit tests which are enabled in the allowlist by type or name
474 // trigger this conditional because unit tests run under the "." package path
Sam Delmerico24c56032022-03-28 19:53:03 +0000475 isTestModule := packagePath == Bp2BuildTopLevel && proptools.BoolDefault(propValue, false)
476 if isTestModule {
477 return true
478 }
479
Liz Kammer20f0f782023-05-01 13:46:33 -0400480 moduleName := moduleNameWithPossibleOverride(ctx, module)
Cole Faust324a92e2022-08-23 15:29:05 -0700481 allowlist := ctx.Config().Bp2buildPackageConfig
Sam Delmerico24c56032022-03-28 19:53:03 +0000482 moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName]
483 moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[ctx.OtherModuleType(module)]
484 allowlistConvert := moduleNameAllowed || moduleTypeAllowed
485 if moduleNameAllowed && moduleTypeAllowed {
486 ctx.ModuleErrorf("A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert")
487 return false
488 }
489
490 if allowlist.moduleDoNotConvert[moduleName] {
Sam Delmerico85d831a2022-03-07 19:12:42 +0000491 if moduleNameAllowed {
Sam Delmerico24c56032022-03-28 19:53:03 +0000492 ctx.ModuleErrorf("a module cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert")
Sam Delmerico85d831a2022-03-07 19:12:42 +0000493 }
Sam Delmerico94d26c22022-02-25 21:34:51 +0000494 return false
495 }
496
Sam Delmerico24c56032022-03-28 19:53:03 +0000497 // This is a tristate value: true, false, or unset.
498 if ok, directoryPath := bp2buildDefaultTrueRecursively(packagePath, allowlist.defaultConfig); ok {
499 if moduleNameAllowed {
500 ctx.ModuleErrorf("A module cannot be in a directory marked Bp2BuildDefaultTrue"+
Yu Liu10853f92022-09-14 16:05:22 -0700501 " or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: '%s'"+
502 " Module: '%s'", directoryPath, moduleName)
Sam Delmerico24c56032022-03-28 19:53:03 +0000503 return false
Sam Delmericofa1831c2022-02-22 18:07:55 +0000504 }
505
Jingwen Chen12b4c272021-03-10 02:05:59 -0500506 // Allow modules to explicitly opt-out.
507 return proptools.BoolDefault(propValue, true)
508 }
509
510 // Allow modules to explicitly opt-in.
Sam Delmerico85d831a2022-03-07 19:12:42 +0000511 return proptools.BoolDefault(propValue, allowlistConvert)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500512}
513
514// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
515// set of package prefixes where all modules must be converted. That is, if the
516// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
517// return true.
518//
519// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
520// exactly, this module will return false early.
521//
522// This function will also return false if the package doesn't match anything in
523// the config.
Sam Delmerico24c56032022-03-28 19:53:03 +0000524//
525// This function will also return the allowlist entry which caused a particular
526// package to be enabled. Since packages can be enabled via a recursive declaration,
527// the path returned will not always be the same as the one provided.
528func bp2buildDefaultTrueRecursively(packagePath string, config allowlists.Bp2BuildConfig) (bool, string) {
Jingwen Chen294e7742021-08-31 05:58:01 +0000529 // Check if the package path has an exact match in the config.
Sam Delmerico24c56032022-03-28 19:53:03 +0000530 if config[packagePath] == allowlists.Bp2BuildDefaultTrue || config[packagePath] == allowlists.Bp2BuildDefaultTrueRecursively {
531 return true, packagePath
MarkDacek756b2962022-10-13 17:50:17 +0000532 } else if config[packagePath] == allowlists.Bp2BuildDefaultFalse || config[packagePath] == allowlists.Bp2BuildDefaultFalseRecursively {
Sam Delmerico24c56032022-03-28 19:53:03 +0000533 return false, packagePath
Jingwen Chen12b4c272021-03-10 02:05:59 -0500534 }
535
Jingwen Chen91220d72021-03-24 02:18:33 -0400536 // If not, check for the config recursively.
MarkDacek756b2962022-10-13 17:50:17 +0000537 packagePrefix := packagePath
538
539 // e.g. for x/y/z, iterate over x/y, then x, taking the most-specific value from the allowlist.
540 for strings.Contains(packagePrefix, "/") {
541 dirIndex := strings.LastIndex(packagePrefix, "/")
542 packagePrefix = packagePrefix[:dirIndex]
543 switch value := config[packagePrefix]; value {
544 case allowlists.Bp2BuildDefaultTrueRecursively:
Jingwen Chen12b4c272021-03-10 02:05:59 -0500545 // package contains this prefix and this prefix should convert all modules
Sam Delmerico24c56032022-03-28 19:53:03 +0000546 return true, packagePrefix
MarkDacek756b2962022-10-13 17:50:17 +0000547 case allowlists.Bp2BuildDefaultFalseRecursively:
548 //package contains this prefix and this prefix should NOT convert any modules
549 return false, packagePrefix
Jingwen Chen12b4c272021-03-10 02:05:59 -0500550 }
551 // Continue to the next part of the package dir.
MarkDacek756b2962022-10-13 17:50:17 +0000552
Jingwen Chen12b4c272021-03-10 02:05:59 -0500553 }
554
Sam Delmerico24c56032022-03-28 19:53:03 +0000555 return false, packagePath
Liz Kammerea6666f2021-02-17 10:17:28 -0500556}
Liz Kammerba3ea162021-02-17 13:22:03 -0500557
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400558func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) {
Chris Parsons39a16972023-06-08 14:28:51 +0000559 ctx.TopDown("bp2build_conversion", bp2buildConversionMutator).Parallel()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400560}
561
Chris Parsons39a16972023-06-08 14:28:51 +0000562func bp2buildConversionMutator(ctx TopDownMutatorContext) {
Chris Parsons8152a942023-06-06 16:17:50 +0000563 if ctx.Config().HasBazelBuildTargetInSource(ctx) {
564 // Defer to the BUILD target. Generating an additional target would
565 // cause a BUILD file conflict.
Chris Parsons39a16972023-06-08 14:28:51 +0000566 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, "")
Chris Parsons8152a942023-06-06 16:17:50 +0000567 return
568 }
569
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400570 bModule, ok := ctx.Module().(Bazelable)
Chris Parsons39a16972023-06-08 14:28:51 +0000571 if !ok {
572 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400573 return
574 }
Chris Parsons39a16972023-06-08 14:28:51 +0000575 // TODO: b/285631638 - Differentiate between denylisted modules and missing bp2build capabilities.
576 if !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) {
577 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "")
578 return
579 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400580 bModule.ConvertWithBp2build(ctx)
Chris Parsons39a16972023-06-08 14:28:51 +0000581
582 if !ctx.Module().base().IsConvertedByBp2build() && ctx.Module().base().GetUnconvertedReason() == nil {
583 panic(fmt.Errorf("illegal bp2build invariant: module '%s' was neither converted nor marked unconvertible", ctx.ModuleName()))
584 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400585}
Wei Libafb6d62021-12-10 03:14:59 -0800586
Spandan Das5af0bd32022-09-28 20:43:08 +0000587func registerApiBp2buildConversionMutator(ctx RegisterMutatorsContext) {
588 ctx.TopDown("apiBp2build_conversion", convertWithApiBp2build).Parallel()
589}
590
591// Generate API contribution targets if the Soong module provides APIs
592func convertWithApiBp2build(ctx TopDownMutatorContext) {
593 if m, ok := ctx.Module().(ApiProvider); ok {
594 m.ConvertWithApiBp2build(ctx)
595 }
596}
597
Wei Libafb6d62021-12-10 03:14:59 -0800598// GetMainClassInManifest scans the manifest file specified in filepath and returns
599// the value of attribute Main-Class in the manifest file if it exists, or returns error.
600// WARNING: this is for bp2build converters of java_* modules only.
601func GetMainClassInManifest(c Config, filepath string) (string, error) {
602 file, err := c.fs.Open(filepath)
603 if err != nil {
604 return "", err
605 }
Liz Kammer0fe123d2022-02-07 10:17:35 -0500606 defer file.Close()
Wei Libafb6d62021-12-10 03:14:59 -0800607 scanner := bufio.NewScanner(file)
608 for scanner.Scan() {
609 line := scanner.Text()
610 if strings.HasPrefix(line, "Main-Class:") {
611 return strings.TrimSpace(line[len("Main-Class:"):]), nil
612 }
613 }
614
615 return "", errors.New("Main-Class is not found.")
616}
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500617
618func AttachValidationActions(ctx ModuleContext, outputFilePath Path, validations Paths) ModuleOutPath {
619 validatedOutputFilePath := PathForModuleOut(ctx, "validated", outputFilePath.Base())
620 ctx.Build(pctx, BuildParams{
621 Rule: CpNoPreserveSymlink,
622 Description: "run validations " + outputFilePath.Base(),
623 Output: validatedOutputFilePath,
624 Input: outputFilePath,
625 Validations: validations,
626 })
627 return validatedOutputFilePath
628}