Provide reason for unconverted bp2build modules
This also changes the expectation of ConvertWithBp2build. Each
implementation must either create one or more Bazel target modules, or
mark the module as unconvertible (with a specific reason).
Manually verified no runtime hit in AOSP
In AOSP, the metrics file size increases from 252K to 1.6M
This changes some effective module counts in bp2build metrics:
- Removes "package" modules from the module count list in
metrics, as these will not be converted like regular modules.
- Counts Handcrafted modules as being "unconverted", as bp2build is not
responsible for them.
Bug: 285631638
Test: Verified generated BUILD.bazel files are bit-for-bit identical
with this change
Test: Manually verified one case of each implemented reasonType
Change-Id: I308dd451d8f28379b15671dae9f931bd0446f5c1
diff --git a/android/bazel.go b/android/bazel.go
index e631ed4..1bfbdec 100644
--- a/android/bazel.go
+++ b/android/bazel.go
@@ -17,8 +17,10 @@
import (
"bufio"
"errors"
+ "fmt"
"strings"
+ "android/soong/ui/metrics/bp2build_metrics_proto"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
@@ -73,6 +75,21 @@
// MissingBp2buildDep stores the module names of direct dependency that were not found
MissingDeps []string `blueprint:"mutated"`
+
+ // If non-nil, indicates that the module could not be converted successfully
+ // with bp2build. This will describe the reason the module could not be converted.
+ UnconvertedReason *UnconvertedReason
+}
+
+// The reason a module could not be converted to a BUILD target via bp2build.
+// This should match bp2build_metrics_proto.UnconvertedReason, but omits private
+// proto-related fields that prevent copying this struct.
+type UnconvertedReason struct {
+ // Should correspond to a valid value in bp2build_metrics_proto.UnconvertedReasonType.
+ // A raw int is used here instead, because blueprint logic requires that all transitive
+ // fields of module definitions be primitives.
+ ReasonType int
+ Detail string
}
type BazelModuleProperties struct {
@@ -137,6 +154,12 @@
GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
ShouldConvertWithBp2build(ctx BazelConversionContext) bool
shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool
+
+ // ConvertWithBp2build either converts the module to a Bazel build target or
+ // declares the module as unconvertible (for logging and metrics).
+ // Modules must implement this function to be bp2build convertible. The function
+ // must either create at least one Bazel target module (using ctx.CreateBazelTargetModule or
+ // its related functions), or declare itself unconvertible using ctx.MarkBp2buildUnconvertible.
ConvertWithBp2build(ctx TopDownMutatorContext)
// namespacedVariableProps is a map from a soong config variable namespace
@@ -232,7 +255,7 @@
if b.ShouldConvertWithBp2build(ctx) {
return bp2buildModuleLabel(ctx, module)
}
- return "" // no label for unconverted module
+ panic(fmt.Errorf("requested non-existent label for module ", module.Name()))
}
type Bp2BuildConversionAllowlist struct {
@@ -533,22 +556,32 @@
}
func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) {
- ctx.TopDown("bp2build_conversion", convertWithBp2build).Parallel()
+ ctx.TopDown("bp2build_conversion", bp2buildConversionMutator).Parallel()
}
-func convertWithBp2build(ctx TopDownMutatorContext) {
+func bp2buildConversionMutator(ctx TopDownMutatorContext) {
if ctx.Config().HasBazelBuildTargetInSource(ctx) {
// Defer to the BUILD target. Generating an additional target would
// cause a BUILD file conflict.
+ ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, "")
return
}
bModule, ok := ctx.Module().(Bazelable)
- if !ok || !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) {
+ if !ok {
+ ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
return
}
-
+ // TODO: b/285631638 - Differentiate between denylisted modules and missing bp2build capabilities.
+ if !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) {
+ ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "")
+ return
+ }
bModule.ConvertWithBp2build(ctx)
+
+ if !ctx.Module().base().IsConvertedByBp2build() && ctx.Module().base().GetUnconvertedReason() == nil {
+ panic(fmt.Errorf("illegal bp2build invariant: module '%s' was neither converted nor marked unconvertible", ctx.ModuleName()))
+ }
}
func registerApiBp2buildConversionMutator(ctx RegisterMutatorsContext) {