blob: df30ff2614caddb44cd6f7929604a74d764954fa [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"
Spandan Das64852422023-08-02 21:58:41 +000025 "github.com/google/blueprint/bootstrap"
Liz Kammerba3ea162021-02-17 13:22:03 -050026 "github.com/google/blueprint/proptools"
Sam Delmerico24c56032022-03-28 19:53:03 +000027
28 "android/soong/android/allowlists"
29)
30
31const (
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 Kammerba3ea162021-02-17 13:22:03 -050036)
37
MarkDacekf47e1422023-04-19 16:47:36 +000038type MixedBuildEnabledStatus int
39
40const (
41 // This module can be mixed_built.
42 MixedBuildEnabled = iota
43
44 // There is a technical incompatibility preventing this module from being
45 // bazel-analyzed. Note: the module might also be incompatible.
46 TechnicalIncompatibility
47
48 // This module cannot be mixed_built due to some incompatibility with it
49 // that is not a platform incompatibility. Example: the module-type is not
50 // enabled, or is not bp2build-converted.
51 ModuleIncompatibility
Liz Kammerc13f7852023-05-17 13:01:48 -040052
53 // Missing dependencies. We can't query Bazel for modules if it has missing dependencies, there
54 // will be failures.
55 ModuleMissingDeps
MarkDacekf47e1422023-04-19 16:47:36 +000056)
57
Yu Liu2aa806b2022-09-01 11:54:47 -070058// FileGroupAsLibrary describes a filegroup module that is converted to some library
59// such as aidl_library or proto_library.
60type FileGroupAsLibrary interface {
Vinh Tran444154d2022-08-16 13:10:31 -040061 ShouldConvertToAidlLibrary(ctx BazelConversionPathContext) bool
Yu Liu2aa806b2022-09-01 11:54:47 -070062 ShouldConvertToProtoLibrary(ctx BazelConversionPathContext) bool
Vinh Tran444154d2022-08-16 13:10:31 -040063 GetAidlLibraryLabel(ctx BazelConversionPathContext) string
Yu Liu2aa806b2022-09-01 11:54:47 -070064 GetProtoLibraryLabel(ctx BazelConversionPathContext) string
Vinh Tran444154d2022-08-16 13:10:31 -040065}
66
Sasha Smundaka0954062022-08-02 18:23:58 -070067type BazelConversionStatus struct {
68 // Information about _all_ bp2build targets generated by this module. Multiple targets are
69 // supported as Soong handles some things within a single target that we may choose to split into
70 // multiple targets, e.g. renderscript, protos, yacc within a cc module.
71 Bp2buildInfo []bp2buildInfo `blueprint:"mutated"`
72
73 // UnconvertedBp2buildDep stores the module names of direct dependency that were not converted to
74 // Bazel
75 UnconvertedDeps []string `blueprint:"mutated"`
76
77 // MissingBp2buildDep stores the module names of direct dependency that were not found
78 MissingDeps []string `blueprint:"mutated"`
Chris Parsons39a16972023-06-08 14:28:51 +000079
80 // If non-nil, indicates that the module could not be converted successfully
81 // with bp2build. This will describe the reason the module could not be converted.
82 UnconvertedReason *UnconvertedReason
83}
84
85// The reason a module could not be converted to a BUILD target via bp2build.
86// This should match bp2build_metrics_proto.UnconvertedReason, but omits private
87// proto-related fields that prevent copying this struct.
88type UnconvertedReason struct {
89 // Should correspond to a valid value in bp2build_metrics_proto.UnconvertedReasonType.
90 // A raw int is used here instead, because blueprint logic requires that all transitive
91 // fields of module definitions be primitives.
92 ReasonType int
93 Detail string
Sasha Smundaka0954062022-08-02 18:23:58 -070094}
95
Romain Jobredeaux8242b432023-05-04 10:16:26 -040096type BazelModuleProperties struct {
Jingwen Chen01812022021-11-19 14:29:43 +000097 // The label of the Bazel target replacing this Soong module. When run in conversion mode, this
98 // will import the handcrafted build target into the autogenerated file. Note: this may result in
99 // a conflict due to duplicate targets if bp2build_available is also set.
100 Label *string
101
102 // If true, bp2build will generate the converted Bazel target for this module. Note: this may
103 // cause a conflict due to the duplicate targets if label is also set.
104 //
105 // This is a bool pointer to support tristates: true, false, not set.
106 //
Sasha Smundak39a301c2022-12-29 17:11:49 -0800107 // To opt in a module, set bazel_module: { bp2build_available: true }
108 // To opt out a module, set bazel_module: { bp2build_available: false }
Jingwen Chen01812022021-11-19 14:29:43 +0000109 // To defer the default setting for the directory, do not set the value.
110 Bp2build_available *bool
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400111
112 // CanConvertToBazel is set via InitBazelModule to indicate that a module type can be converted to
113 // Bazel with Bp2build.
114 CanConvertToBazel bool `blueprint:"mutated"`
Jingwen Chen01812022021-11-19 14:29:43 +0000115}
116
Liz Kammerba3ea162021-02-17 13:22:03 -0500117// Properties contains common module properties for Bazel migration purposes.
118type properties struct {
Chris Parsons1bb58da2022-08-30 13:37:57 -0400119 // In "Bazel mixed build" mode, this represents the Bazel target replacing
Liz Kammerba3ea162021-02-17 13:22:03 -0500120 // this Soong module.
Romain Jobredeaux8242b432023-05-04 10:16:26 -0400121 Bazel_module BazelModuleProperties
Liz Kammerba3ea162021-02-17 13:22:03 -0500122}
Liz Kammerea6666f2021-02-17 10:17:28 -0500123
Jingwen Chen25825ca2021-11-15 12:28:43 +0000124// namespacedVariableProperties is a map from a string representing a Soong
Jingwen Chen84817de2021-11-17 10:57:35 +0000125// config variable namespace, like "android" or "vendor_name" to a slice of
126// pointer to a struct containing a single field called Soong_config_variables
127// whose value mirrors the structure in the Blueprint file.
128type namespacedVariableProperties map[string][]interface{}
Jingwen Chena47f28d2021-11-02 16:43:57 +0000129
Liz Kammerea6666f2021-02-17 10:17:28 -0500130// BazelModuleBase contains the property structs with metadata for modules which can be converted to
131// Bazel.
132type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -0500133 bazelProperties properties
Jingwen Chena47f28d2021-11-02 16:43:57 +0000134
135 // namespacedVariableProperties is used for soong_config_module_type support
136 // in bp2build. Soong config modules allow users to set module properties
137 // based on custom product variables defined in Android.bp files. These
138 // variables are namespaced to prevent clobbering, especially when set from
139 // Makefiles.
140 namespacedVariableProperties namespacedVariableProperties
141
142 // baseModuleType is set when this module was created from a module type
143 // defined by a soong_config_module_type. Every soong_config_module_type
144 // "wraps" another module type, e.g. a soong_config_module_type can wrap a
145 // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
146 // This baseModuleType is set to the wrapped module type.
147 baseModuleType string
Liz Kammerea6666f2021-02-17 10:17:28 -0500148}
149
150// Bazelable is specifies the interface for modules that can be converted to Bazel.
151type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -0500152 bazelProps() *properties
153 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -0500154 HandcraftedLabel() string
155 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400156 ShouldConvertWithBp2build(ctx BazelConversionContext) bool
Sam Delmerico24c56032022-03-28 19:53:03 +0000157 shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool
Chris Parsons39a16972023-06-08 14:28:51 +0000158
159 // ConvertWithBp2build either converts the module to a Bazel build target or
160 // declares the module as unconvertible (for logging and metrics).
161 // Modules must implement this function to be bp2build convertible. The function
162 // must either create at least one Bazel target module (using ctx.CreateBazelTargetModule or
163 // its related functions), or declare itself unconvertible using ctx.MarkBp2buildUnconvertible.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400164 ConvertWithBp2build(ctx TopDownMutatorContext)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000165
Jingwen Chen84817de2021-11-17 10:57:35 +0000166 // namespacedVariableProps is a map from a soong config variable namespace
167 // (e.g. acme, android) to a map of interfaces{}, which are really
168 // reflect.Struct pointers, representing the value of the
169 // soong_config_variables property of a module. The struct pointer is the
170 // one with the single member called Soong_config_variables, which itself is
171 // a struct containing fields for each supported feature in that namespace.
172 //
Sasha Smundak39a301c2022-12-29 17:11:49 -0800173 // The reason for using a slice of interface{} is to support defaults
Jingwen Chen84817de2021-11-17 10:57:35 +0000174 // propagation of the struct pointers.
Jingwen Chena47f28d2021-11-02 16:43:57 +0000175 namespacedVariableProps() namespacedVariableProperties
176 setNamespacedVariableProps(props namespacedVariableProperties)
177 BaseModuleType() string
Jingwen Chen84817de2021-11-17 10:57:35 +0000178 SetBaseModuleType(baseModuleType string)
Liz Kammerea6666f2021-02-17 10:17:28 -0500179}
180
Spandan Das5af0bd32022-09-28 20:43:08 +0000181// ApiProvider is implemented by modules that contribute to an API surface
182type ApiProvider interface {
183 ConvertWithApiBp2build(ctx TopDownMutatorContext)
184}
185
Chris Parsonsf874e462022-05-10 13:50:12 -0400186// MixedBuildBuildable is an interface that module types should implement in order
187// to be "handled by Bazel" in a mixed build.
188type MixedBuildBuildable interface {
189 // IsMixedBuildSupported returns true if and only if this module should be
190 // "handled by Bazel" in a mixed build.
191 // This "escape hatch" allows modules with corner-case scenarios to opt out
192 // of being built with Bazel.
193 IsMixedBuildSupported(ctx BaseModuleContext) bool
194
195 // QueueBazelCall invokes request-queueing functions on the BazelContext
196 // so that these requests are handled when Bazel's cquery is invoked.
197 QueueBazelCall(ctx BaseModuleContext)
198
199 // ProcessBazelQueryResponse uses Bazel information (obtained from the BazelContext)
200 // to set module fields and providers to propagate this module's metadata upstream.
201 // This effectively "bridges the gap" between Bazel and Soong in a mixed build.
202 // Soong modules depending on this module should be oblivious to the fact that
203 // this module was handled by Bazel.
204 ProcessBazelQueryResponse(ctx ModuleContext)
205}
206
Liz Kammerea6666f2021-02-17 10:17:28 -0500207// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
208type BazelModule interface {
209 Module
210 Bazelable
211}
212
213// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
214// properties.
215func InitBazelModule(module BazelModule) {
216 module.AddProperties(module.bazelProps())
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400217 module.bazelProps().Bazel_module.CanConvertToBazel = true
Liz Kammerea6666f2021-02-17 10:17:28 -0500218}
219
220// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -0500221func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -0500222 return &b.bazelProperties
223}
224
Jingwen Chena47f28d2021-11-02 16:43:57 +0000225func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
226 return b.namespacedVariableProperties
227}
228
229func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
230 b.namespacedVariableProperties = props
231}
232
233func (b *BazelModuleBase) BaseModuleType() string {
234 return b.baseModuleType
235}
236
237func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
238 b.baseModuleType = baseModuleType
239}
240
Liz Kammerba3ea162021-02-17 13:22:03 -0500241// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
242func (b *BazelModuleBase) HasHandcraftedLabel() bool {
243 return b.bazelProperties.Bazel_module.Label != nil
244}
245
246// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
247func (b *BazelModuleBase) HandcraftedLabel() string {
248 return proptools.String(b.bazelProperties.Bazel_module.Label)
249}
250
Liz Kammerea6666f2021-02-17 10:17:28 -0500251// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -0500252func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
253 if b.HasHandcraftedLabel() {
254 return b.HandcraftedLabel()
255 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400256 if b.ShouldConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500257 return bp2buildModuleLabel(ctx, module)
258 }
Spandan Das5b18c0c2023-07-14 00:23:29 +0000259 panic(fmt.Errorf("requested non-existent label for module %s", module.Name()))
Liz Kammerea6666f2021-02-17 10:17:28 -0500260}
261
Cole Faust324a92e2022-08-23 15:29:05 -0700262type Bp2BuildConversionAllowlist struct {
Sam Delmerico24c56032022-03-28 19:53:03 +0000263 // Configure modules in these directories to enable bp2build_available: true or false by default.
264 defaultConfig allowlists.Bp2BuildConfig
Jingwen Chen12b4c272021-03-10 02:05:59 -0500265
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400266 // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
Jingwen Chenb643c7a2021-07-26 04:45:48 +0000267 // in the synthetic Bazel workspace.
Sam Delmerico24c56032022-03-28 19:53:03 +0000268 keepExistingBuildFile map[string]bool
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000269
Chris Parsonsef615e52022-08-18 22:04:11 -0400270 // Per-module allowlist to always opt modules into both bp2build and Bazel Dev Mode mixed
271 // builds. These modules are usually in directories with many other modules that are not ready
272 // for conversion.
Jingwen Chen7edadab2022-03-04 07:01:29 +0000273 //
274 // A module can either be in this list or its directory allowlisted entirely
275 // in bp2buildDefaultConfig, but not both at the same time.
Sam Delmerico24c56032022-03-28 19:53:03 +0000276 moduleAlwaysConvert map[string]bool
Sam Delmericofa1831c2022-02-22 18:07:55 +0000277
Chris Parsonsef615e52022-08-18 22:04:11 -0400278 // Per-module-type allowlist to always opt modules in to both bp2build and
279 // Bazel Dev Mode mixed builds when they have the same type as one listed.
Sam Delmerico24c56032022-03-28 19:53:03 +0000280 moduleTypeAlwaysConvert map[string]bool
Sam Delmerico85d831a2022-03-07 19:12:42 +0000281
Chris Parsonsad876012022-08-20 14:48:32 -0400282 // Per-module denylist to always opt modules out of bp2build conversion.
Sam Delmerico24c56032022-03-28 19:53:03 +0000283 moduleDoNotConvert map[string]bool
Sam Delmerico24c56032022-03-28 19:53:03 +0000284}
Liz Kammer5c313582021-12-03 15:23:26 -0500285
Cole Faust324a92e2022-08-23 15:29:05 -0700286// NewBp2BuildAllowlist creates a new, empty Bp2BuildConversionAllowlist
Sam Delmerico24c56032022-03-28 19:53:03 +0000287// which can be populated using builder pattern Set* methods
Cole Faust324a92e2022-08-23 15:29:05 -0700288func NewBp2BuildAllowlist() Bp2BuildConversionAllowlist {
289 return Bp2BuildConversionAllowlist{
Sam Delmerico24c56032022-03-28 19:53:03 +0000290 allowlists.Bp2BuildConfig{},
291 map[string]bool{},
292 map[string]bool{},
293 map[string]bool{},
294 map[string]bool{},
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400295 }
296}
297
Sam Delmerico24c56032022-03-28 19:53:03 +0000298// SetDefaultConfig copies the entries from defaultConfig into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700299func (a Bp2BuildConversionAllowlist) SetDefaultConfig(defaultConfig allowlists.Bp2BuildConfig) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000300 if a.defaultConfig == nil {
301 a.defaultConfig = allowlists.Bp2BuildConfig{}
302 }
303 for k, v := range defaultConfig {
304 a.defaultConfig[k] = v
305 }
306
307 return a
308}
309
310// SetKeepExistingBuildFile copies the entries from keepExistingBuildFile into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700311func (a Bp2BuildConversionAllowlist) SetKeepExistingBuildFile(keepExistingBuildFile map[string]bool) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000312 if a.keepExistingBuildFile == nil {
313 a.keepExistingBuildFile = map[string]bool{}
314 }
315 for k, v := range keepExistingBuildFile {
316 a.keepExistingBuildFile[k] = v
317 }
318
319 return a
320}
321
322// SetModuleAlwaysConvertList copies the entries from moduleAlwaysConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700323func (a Bp2BuildConversionAllowlist) SetModuleAlwaysConvertList(moduleAlwaysConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000324 if a.moduleAlwaysConvert == nil {
325 a.moduleAlwaysConvert = map[string]bool{}
326 }
327 for _, m := range moduleAlwaysConvert {
328 a.moduleAlwaysConvert[m] = true
329 }
330
331 return a
332}
333
334// SetModuleTypeAlwaysConvertList copies the entries from moduleTypeAlwaysConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700335func (a Bp2BuildConversionAllowlist) SetModuleTypeAlwaysConvertList(moduleTypeAlwaysConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000336 if a.moduleTypeAlwaysConvert == nil {
337 a.moduleTypeAlwaysConvert = map[string]bool{}
338 }
339 for _, m := range moduleTypeAlwaysConvert {
340 a.moduleTypeAlwaysConvert[m] = true
341 }
342
343 return a
344}
345
346// SetModuleDoNotConvertList copies the entries from moduleDoNotConvert into the allowlist
Cole Faust324a92e2022-08-23 15:29:05 -0700347func (a Bp2BuildConversionAllowlist) SetModuleDoNotConvertList(moduleDoNotConvert []string) Bp2BuildConversionAllowlist {
Sam Delmerico24c56032022-03-28 19:53:03 +0000348 if a.moduleDoNotConvert == nil {
349 a.moduleDoNotConvert = map[string]bool{}
350 }
351 for _, m := range moduleDoNotConvert {
352 a.moduleDoNotConvert[m] = true
353 }
354
355 return a
356}
357
Sam Delmerico24c56032022-03-28 19:53:03 +0000358// ShouldKeepExistingBuildFileForDir returns whether an existing BUILD file should be
359// added to the build symlink forest based on the current global configuration.
Cole Faust324a92e2022-08-23 15:29:05 -0700360func (a Bp2BuildConversionAllowlist) ShouldKeepExistingBuildFileForDir(dir string) bool {
361 if _, ok := a.keepExistingBuildFile[dir]; ok {
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400362 // Exact dir match
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400363 return true
364 }
Usta Shresthaea999642022-11-02 01:03:07 -0400365 var i int
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400366 // Check if subtree match
Usta Shresthaea999642022-11-02 01:03:07 -0400367 for {
368 j := strings.Index(dir[i:], "/")
369 if j == -1 {
370 return false //default
371 }
372 prefix := dir[0 : i+j]
373 i = i + j + 1 // skip the "/"
374 if recursive, ok := a.keepExistingBuildFile[prefix]; ok && recursive {
375 return true
Rupert Shuttleworth00960792021-05-12 21:20:13 -0400376 }
377 }
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400378}
379
Cole Faust324a92e2022-08-23 15:29:05 -0700380var bp2BuildAllowListKey = NewOnceKey("Bp2BuildAllowlist")
381var bp2buildAllowlist OncePer
382
383func GetBp2BuildAllowList() Bp2BuildConversionAllowlist {
384 return bp2buildAllowlist.Once(bp2BuildAllowListKey, func() interface{} {
385 return NewBp2BuildAllowlist().SetDefaultConfig(allowlists.Bp2buildDefaultConfig).
386 SetKeepExistingBuildFile(allowlists.Bp2buildKeepExistingBuildFile).
387 SetModuleAlwaysConvertList(allowlists.Bp2buildModuleAlwaysConvertList).
388 SetModuleTypeAlwaysConvertList(allowlists.Bp2buildModuleTypeAlwaysConvertList).
Sasha Smundak39a301c2022-12-29 17:11:49 -0800389 SetModuleDoNotConvertList(allowlists.Bp2buildModuleDoNotConvertList)
Cole Faust324a92e2022-08-23 15:29:05 -0700390 }).(Bp2BuildConversionAllowlist)
391}
392
MarkDacekf47e1422023-04-19 16:47:36 +0000393// MixedBuildsEnabled returns a MixedBuildEnabledStatus regarding whether
394// a module is ready to be replaced by a converted or handcrafted Bazel target.
395// As a side effect, calling this method will also log whether this module is
396// mixed build enabled for metrics reporting.
397func MixedBuildsEnabled(ctx BaseModuleContext) MixedBuildEnabledStatus {
MarkDacekf47e1422023-04-19 16:47:36 +0000398 platformIncompatible := isPlatformIncompatible(ctx.Os(), ctx.Arch().ArchType)
399 if platformIncompatible {
400 ctx.Config().LogMixedBuild(ctx, false)
401 return TechnicalIncompatibility
402 }
403
Liz Kammerc13f7852023-05-17 13:01:48 -0400404 if ctx.Config().AllowMissingDependencies() {
405 missingDeps := ctx.getMissingDependencies()
406 // If there are missing dependencies, querying Bazel will fail. Soong instead fails at execution
407 // time, not loading/analysis. disable mixed builds and fall back to Soong to maintain that
408 // behavior.
409 if len(missingDeps) > 0 {
410 ctx.Config().LogMixedBuild(ctx, false)
411 return ModuleMissingDeps
412 }
413 }
414
415 module := ctx.Module()
416 apexInfo := ctx.Provider(ApexInfoProvider).(ApexInfo)
417 withinApex := !apexInfo.IsForPlatform()
Sasha Smundak39a301c2022-12-29 17:11:49 -0800418 mixedBuildEnabled := ctx.Config().IsMixedBuildsEnabled() &&
Sasha Smundak39a301c2022-12-29 17:11:49 -0800419 module.Enabled() &&
420 convertedToBazel(ctx, module) &&
Yu Liue4312402023-01-18 09:15:31 -0800421 ctx.Config().BazelContext.IsModuleNameAllowed(module.Name(), withinApex)
MarkDacekff851b82022-04-21 18:33:17 +0000422 ctx.Config().LogMixedBuild(ctx, mixedBuildEnabled)
MarkDacekf47e1422023-04-19 16:47:36 +0000423
424 if mixedBuildEnabled {
425 return MixedBuildEnabled
426 }
427 return ModuleIncompatibility
MarkDacekff851b82022-04-21 18:33:17 +0000428}
429
Spandan Das64852422023-08-02 21:58:41 +0000430func isGoModule(module blueprint.Module) bool {
431 if _, ok := module.(*bootstrap.GoPackage); ok {
432 return true
433 }
434 if _, ok := module.(*bootstrap.GoBinary); ok {
435 return true
436 }
437 return false
438}
439
Liz Kammer6eff3232021-08-26 08:37:59 -0400440// ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000441func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
Spandan Das64852422023-08-02 21:58:41 +0000442 // Special-case bootstrap_go_package and bootstrap_go_binary
443 // These do not implement Bazelable, but have been converted
444 if isGoModule(module) {
445 return true
446 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400447 b, ok := module.(Bazelable)
448 if !ok {
449 return false
450 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400451 return b.shouldConvertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
Liz Kammer6eff3232021-08-26 08:37:59 -0400452}
453
Sam Delmerico24c56032022-03-28 19:53:03 +0000454// ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400455func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx BazelConversionContext) bool {
456 return b.shouldConvertWithBp2build(ctx, ctx.Module())
Liz Kammer6eff3232021-08-26 08:37:59 -0400457}
458
Sam Delmerico24c56032022-03-28 19:53:03 +0000459type bazelOtherModuleContext interface {
460 ModuleErrorf(format string, args ...interface{})
461 Config() Config
462 OtherModuleType(m blueprint.Module) string
463 OtherModuleName(m blueprint.Module) string
464 OtherModuleDir(m blueprint.Module) string
465}
Sam Delmericofa1831c2022-02-22 18:07:55 +0000466
MarkDacekf47e1422023-04-19 16:47:36 +0000467func isPlatformIncompatible(osType OsType, arch ArchType) bool {
468 return osType == Windows || // Windows toolchains are not currently supported.
469 osType == LinuxBionic || // Linux Bionic toolchains are not currently supported.
470 osType == LinuxMusl || // Linux musl toolchains are not currently supported (b/259266326).
471 arch == Riscv64 // TODO(b/262192655) Riscv64 toolchains are not currently supported.
472}
473
Sam Delmerico24c56032022-03-28 19:53:03 +0000474func (b *BazelModuleBase) shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400475 if !b.bazelProps().Bazel_module.CanConvertToBazel {
476 return false
Jingwen Chen12b4c272021-03-10 02:05:59 -0500477 }
478
Spandan Das5af0bd32022-09-28 20:43:08 +0000479 // In api_bp2build mode, all soong modules that can provide API contributions should be converted
480 // This is irrespective of its presence/absence in bp2build allowlists
481 if ctx.Config().BuildMode == ApiBp2build {
482 _, providesApis := module.(ApiProvider)
483 return providesApis
484 }
485
Sam Delmerico85d831a2022-03-07 19:12:42 +0000486 propValue := b.bazelProperties.Bazel_module.Bp2build_available
Liz Kammer20f0f782023-05-01 13:46:33 -0400487 packagePath := moduleDirWithPossibleOverride(ctx, module)
Sam Delmerico24c56032022-03-28 19:53:03 +0000488
Sam Delmerico85d831a2022-03-07 19:12:42 +0000489 // Modules in unit tests which are enabled in the allowlist by type or name
490 // trigger this conditional because unit tests run under the "." package path
Sam Delmerico24c56032022-03-28 19:53:03 +0000491 isTestModule := packagePath == Bp2BuildTopLevel && proptools.BoolDefault(propValue, false)
492 if isTestModule {
493 return true
494 }
495
Liz Kammer20f0f782023-05-01 13:46:33 -0400496 moduleName := moduleNameWithPossibleOverride(ctx, module)
Cole Faust324a92e2022-08-23 15:29:05 -0700497 allowlist := ctx.Config().Bp2buildPackageConfig
Sam Delmerico24c56032022-03-28 19:53:03 +0000498 moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName]
499 moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[ctx.OtherModuleType(module)]
500 allowlistConvert := moduleNameAllowed || moduleTypeAllowed
501 if moduleNameAllowed && moduleTypeAllowed {
502 ctx.ModuleErrorf("A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert")
503 return false
504 }
505
506 if allowlist.moduleDoNotConvert[moduleName] {
Sam Delmerico85d831a2022-03-07 19:12:42 +0000507 if moduleNameAllowed {
Sam Delmerico24c56032022-03-28 19:53:03 +0000508 ctx.ModuleErrorf("a module cannot be in moduleDoNotConvert and also be in moduleAlwaysConvert")
Sam Delmerico85d831a2022-03-07 19:12:42 +0000509 }
Sam Delmerico94d26c22022-02-25 21:34:51 +0000510 return false
511 }
512
Sam Delmerico24c56032022-03-28 19:53:03 +0000513 // This is a tristate value: true, false, or unset.
514 if ok, directoryPath := bp2buildDefaultTrueRecursively(packagePath, allowlist.defaultConfig); ok {
515 if moduleNameAllowed {
516 ctx.ModuleErrorf("A module cannot be in a directory marked Bp2BuildDefaultTrue"+
Yu Liu10853f92022-09-14 16:05:22 -0700517 " or Bp2BuildDefaultTrueRecursively and also be in moduleAlwaysConvert. Directory: '%s'"+
518 " Module: '%s'", directoryPath, moduleName)
Sam Delmerico24c56032022-03-28 19:53:03 +0000519 return false
Sam Delmericofa1831c2022-02-22 18:07:55 +0000520 }
521
Jingwen Chen12b4c272021-03-10 02:05:59 -0500522 // Allow modules to explicitly opt-out.
523 return proptools.BoolDefault(propValue, true)
524 }
525
526 // Allow modules to explicitly opt-in.
Sam Delmerico85d831a2022-03-07 19:12:42 +0000527 return proptools.BoolDefault(propValue, allowlistConvert)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500528}
529
530// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
531// set of package prefixes where all modules must be converted. That is, if the
532// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
533// return true.
534//
535// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
536// exactly, this module will return false early.
537//
538// This function will also return false if the package doesn't match anything in
539// the config.
Sam Delmerico24c56032022-03-28 19:53:03 +0000540//
541// This function will also return the allowlist entry which caused a particular
542// package to be enabled. Since packages can be enabled via a recursive declaration,
543// the path returned will not always be the same as the one provided.
544func bp2buildDefaultTrueRecursively(packagePath string, config allowlists.Bp2BuildConfig) (bool, string) {
Jingwen Chen294e7742021-08-31 05:58:01 +0000545 // Check if the package path has an exact match in the config.
Sam Delmerico24c56032022-03-28 19:53:03 +0000546 if config[packagePath] == allowlists.Bp2BuildDefaultTrue || config[packagePath] == allowlists.Bp2BuildDefaultTrueRecursively {
547 return true, packagePath
MarkDacek756b2962022-10-13 17:50:17 +0000548 } else if config[packagePath] == allowlists.Bp2BuildDefaultFalse || config[packagePath] == allowlists.Bp2BuildDefaultFalseRecursively {
Sam Delmerico24c56032022-03-28 19:53:03 +0000549 return false, packagePath
Jingwen Chen12b4c272021-03-10 02:05:59 -0500550 }
551
Jingwen Chen91220d72021-03-24 02:18:33 -0400552 // If not, check for the config recursively.
MarkDacek756b2962022-10-13 17:50:17 +0000553 packagePrefix := packagePath
554
555 // e.g. for x/y/z, iterate over x/y, then x, taking the most-specific value from the allowlist.
556 for strings.Contains(packagePrefix, "/") {
557 dirIndex := strings.LastIndex(packagePrefix, "/")
558 packagePrefix = packagePrefix[:dirIndex]
559 switch value := config[packagePrefix]; value {
560 case allowlists.Bp2BuildDefaultTrueRecursively:
Jingwen Chen12b4c272021-03-10 02:05:59 -0500561 // package contains this prefix and this prefix should convert all modules
Sam Delmerico24c56032022-03-28 19:53:03 +0000562 return true, packagePrefix
MarkDacek756b2962022-10-13 17:50:17 +0000563 case allowlists.Bp2BuildDefaultFalseRecursively:
564 //package contains this prefix and this prefix should NOT convert any modules
565 return false, packagePrefix
Jingwen Chen12b4c272021-03-10 02:05:59 -0500566 }
567 // Continue to the next part of the package dir.
MarkDacek756b2962022-10-13 17:50:17 +0000568
Jingwen Chen12b4c272021-03-10 02:05:59 -0500569 }
570
Sam Delmerico24c56032022-03-28 19:53:03 +0000571 return false, packagePath
Liz Kammerea6666f2021-02-17 10:17:28 -0500572}
Liz Kammerba3ea162021-02-17 13:22:03 -0500573
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400574func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) {
Chris Parsons39a16972023-06-08 14:28:51 +0000575 ctx.TopDown("bp2build_conversion", bp2buildConversionMutator).Parallel()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400576}
577
Chris Parsons39a16972023-06-08 14:28:51 +0000578func bp2buildConversionMutator(ctx TopDownMutatorContext) {
Chris Parsons8152a942023-06-06 16:17:50 +0000579 if ctx.Config().HasBazelBuildTargetInSource(ctx) {
580 // Defer to the BUILD target. Generating an additional target would
581 // cause a BUILD file conflict.
Chris Parsons39a16972023-06-08 14:28:51 +0000582 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, "")
Chris Parsons8152a942023-06-06 16:17:50 +0000583 return
584 }
585
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400586 bModule, ok := ctx.Module().(Bazelable)
Chris Parsons39a16972023-06-08 14:28:51 +0000587 if !ok {
588 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400589 return
590 }
Chris Parsons39a16972023-06-08 14:28:51 +0000591 // TODO: b/285631638 - Differentiate between denylisted modules and missing bp2build capabilities.
592 if !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) {
593 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "")
594 return
595 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400596 bModule.ConvertWithBp2build(ctx)
Chris Parsons39a16972023-06-08 14:28:51 +0000597
598 if !ctx.Module().base().IsConvertedByBp2build() && ctx.Module().base().GetUnconvertedReason() == nil {
599 panic(fmt.Errorf("illegal bp2build invariant: module '%s' was neither converted nor marked unconvertible", ctx.ModuleName()))
600 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400601}
Wei Libafb6d62021-12-10 03:14:59 -0800602
Spandan Das5af0bd32022-09-28 20:43:08 +0000603func registerApiBp2buildConversionMutator(ctx RegisterMutatorsContext) {
604 ctx.TopDown("apiBp2build_conversion", convertWithApiBp2build).Parallel()
605}
606
607// Generate API contribution targets if the Soong module provides APIs
608func convertWithApiBp2build(ctx TopDownMutatorContext) {
609 if m, ok := ctx.Module().(ApiProvider); ok {
610 m.ConvertWithApiBp2build(ctx)
611 }
612}
613
Wei Libafb6d62021-12-10 03:14:59 -0800614// GetMainClassInManifest scans the manifest file specified in filepath and returns
615// the value of attribute Main-Class in the manifest file if it exists, or returns error.
616// WARNING: this is for bp2build converters of java_* modules only.
617func GetMainClassInManifest(c Config, filepath string) (string, error) {
618 file, err := c.fs.Open(filepath)
619 if err != nil {
620 return "", err
621 }
Liz Kammer0fe123d2022-02-07 10:17:35 -0500622 defer file.Close()
Wei Libafb6d62021-12-10 03:14:59 -0800623 scanner := bufio.NewScanner(file)
624 for scanner.Scan() {
625 line := scanner.Text()
626 if strings.HasPrefix(line, "Main-Class:") {
627 return strings.TrimSpace(line[len("Main-Class:"):]), nil
628 }
629 }
630
631 return "", errors.New("Main-Class is not found.")
632}
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500633
634func AttachValidationActions(ctx ModuleContext, outputFilePath Path, validations Paths) ModuleOutPath {
635 validatedOutputFilePath := PathForModuleOut(ctx, "validated", outputFilePath.Base())
636 ctx.Build(pctx, BuildParams{
637 Rule: CpNoPreserveSymlink,
638 Description: "run validations " + outputFilePath.Base(),
639 Output: validatedOutputFilePath,
640 Input: outputFilePath,
641 Validations: validations,
642 })
643 return validatedOutputFilePath
644}