Support `enabled` flag in product variable config
Some Android.bp modules have `enabled: false` but only use a product
variable such as `source_build` to enable the module. Currently b2build
does not handle this case at all. This commit adds the functionality
to support this use case.
Also, remove `__enabled` suffix in ProductVariable SelectKey.
Bug: 210546943
Test: go test ./bp2build
Topic: use_enabled_flag_product_variable_config
Change-Id: I459c17a84c172df010666391066bf4d11d19253e
diff --git a/android/module.go b/android/module.go
index c2fa848..5053720 100644
--- a/android/module.go
+++ b/android/module.go
@@ -1139,20 +1139,71 @@
}
}
- data.Append(required)
+ productConfigEnabledLabels := []bazel.Label{}
+ if !proptools.BoolDefault(enabledProperty.Value, true) {
+ // If the module is not enabled by default, then we can check if a
+ // product variable enables it
+ productConfigEnabledLabels = productVariableConfigEnableLabels(ctx)
- var err error
- constraints := constraintAttributes{}
- constraints.Target_compatible_with, err = enabledProperty.ToLabelListAttribute(
+ if len(productConfigEnabledLabels) > 0 {
+ // In this case, an existing product variable configuration overrides any
+ // module-level `enable: false` definition
+ newValue := true
+ enabledProperty.Value = &newValue
+ }
+ }
+
+ productConfigEnabledAttribute := bazel.MakeLabelListAttribute(bazel.LabelList{
+ productConfigEnabledLabels, nil,
+ })
+
+ platformEnabledAttribute, err := enabledProperty.ToLabelListAttribute(
bazel.LabelList{[]bazel.Label{bazel.Label{Label: "@platforms//:incompatible"}}, nil},
bazel.LabelList{[]bazel.Label{}, nil})
-
if err != nil {
- ctx.ModuleErrorf("Error processing enabled attribute: %s", err)
+ ctx.ModuleErrorf("Error processing platform enabled attribute: %s", err)
}
+
+ data.Append(required)
+
+ constraints := constraintAttributes{}
+ moduleEnableConstraints := bazel.LabelListAttribute{}
+ moduleEnableConstraints.Append(platformEnabledAttribute)
+ moduleEnableConstraints.Append(productConfigEnabledAttribute)
+ constraints.Target_compatible_with = moduleEnableConstraints
+
return constraints
}
+// Check product variables for `enabled: true` flag override.
+// Returns a list of the constraint_value targets who enable this override.
+func productVariableConfigEnableLabels(ctx *topDownMutatorContext) []bazel.Label {
+ productVariableProps := ProductVariableProperties(ctx)
+ productConfigEnablingTargets := []bazel.Label{}
+ const propName = "Enabled"
+ if productConfigProps, exists := productVariableProps[propName]; exists {
+ for productConfigProp, prop := range productConfigProps {
+ flag, ok := prop.(*bool)
+ if !ok {
+ ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
+ }
+
+ if *flag {
+ axis := productConfigProp.ConfigurationAxis()
+ targetLabel := axis.SelectKey(productConfigProp.SelectKey())
+ productConfigEnablingTargets = append(productConfigEnablingTargets, bazel.Label{
+ Label: targetLabel,
+ })
+ } else {
+ // TODO(b/210546943): handle negative case where `enabled: false`
+ ctx.ModuleErrorf("`enabled: false` is not currently supported for configuration variables. See b/210546943", proptools.PropertyNameForField(propName))
+ }
+ }
+ }
+
+ return productConfigEnablingTargets
+}
+
// A ModuleBase object contains the properties that are common to all Android
// modules. It should be included as an anonymous field in every module
// struct definition. InitAndroidModule should then be called from the module's