Merge "Support enabled flag in product variable config"

This commit is contained in:
Sam Delmerico
2022-01-12 20:34:14 +00:00
committed by Gerrit Code Review
3 changed files with 227 additions and 21 deletions

View File

@@ -1140,20 +1140,71 @@ func (attrs *CommonAttributes) fillCommonBp2BuildModuleAttrs(ctx *topDownMutator
}
}
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