Handle arch/os-specific product variables

Bug: 183595873
Test: go test bp2build tests
Change-Id: I36e93ae1eb2943555dd304d5bdf62d995e77b437
This commit is contained in:
Liz Kammer
2021-05-10 11:39:53 -04:00
parent 6fd7b3fee9
commit e3e4a5f2d8
4 changed files with 107 additions and 10 deletions

View File

@@ -467,7 +467,7 @@ type ProductConfigProperties map[string][]ProductConfigProperty
// ProductVariableProperties returns a ProductConfigProperties containing only the properties which
// have been set for the module in the given context.
func ProductVariableProperties(ctx ProductConfigContext) ProductConfigProperties {
func ProductVariableProperties(ctx BaseMutatorContext) ProductConfigProperties {
module := ctx.Module()
moduleBase := module.base()
@@ -477,7 +477,28 @@ func ProductVariableProperties(ctx ProductConfigContext) ProductConfigProperties
return productConfigProperties
}
variableValues := reflect.ValueOf(moduleBase.variableProperties).Elem().FieldByName("Product_variables")
productVariableValues(moduleBase.variableProperties, "", &productConfigProperties)
for arch, targetProps := range moduleBase.GetArchProperties(ctx, moduleBase.variableProperties) {
// GetArchProperties is creating an instance of the requested type
// and productVariablesValues expects an interface, so no need to cast
productVariableValues(targetProps, arch.Name, &productConfigProperties)
}
for os, targetProps := range moduleBase.GetTargetProperties(ctx, moduleBase.variableProperties) {
// GetTargetProperties is creating an instance of the requested type
// and productVariablesValues expects an interface, so no need to cast
productVariableValues(targetProps, os.Name, &productConfigProperties)
}
return productConfigProperties
}
func productVariableValues(variableProps interface{}, suffix string, productConfigProperties *ProductConfigProperties) {
if suffix != "" {
suffix = "-" + suffix
}
variableValues := reflect.ValueOf(variableProps).Elem().FieldByName("Product_variables")
for i := 0; i < variableValues.NumField(); i++ {
variableValue := variableValues.Field(i)
// Check if any properties were set for the module
@@ -495,15 +516,13 @@ func ProductVariableProperties(ctx ProductConfigContext) ProductConfigProperties
// e.g. Asflags, Cflags, Enabled, etc.
propertyName := variableValue.Type().Field(j).Name
productConfigProperties[propertyName] = append(productConfigProperties[propertyName],
(*productConfigProperties)[propertyName] = append((*productConfigProperties)[propertyName],
ProductConfigProperty{
ProductConfigVariable: productVariableName,
ProductConfigVariable: productVariableName + suffix,
Property: property.Interface(),
})
}
}
return productConfigProperties
}
func VariableMutator(mctx BottomUpMutatorContext) {

View File

@@ -563,6 +563,12 @@ func (attrs *StringListAttribute) SetValueForOS(os string, value []string) {
*v = value
}
func (attrs *StringListAttribute) SortedProductVariables() []ProductVariableValues {
vals := attrs.ProductValues[:]
sort.Slice(vals, func(i, j int) bool { return vals[i].ProductVariable < vals[j].ProductVariable })
return vals
}
// Append appends all values, including os and arch specific ones, from another
// StringListAttribute to this StringListAttribute
func (attrs *StringListAttribute) Append(other StringListAttribute) {

View File

@@ -1187,14 +1187,86 @@ cc_library_static {
"-I.",
"-I$(BINDIR)/.",
] + select({
"//build/bazel/product_variables:binder32bit": ["-Wbinder32bit"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_zero_contents": ["-Wmalloc_zero_contents"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:binder32bit": ["-Wbinder32bit"],
"//conditions:default": [],
}),
linkstatic = True,
srcs = ["common.c"],
)`},
})
}
func TestCcLibraryStaticProductVariableArchSpecificSelects(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static arch-specific product variable selects",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
srcs: ["common.c"],
product_variables: {
malloc_not_svelte: {
cflags: ["-Wmalloc_not_svelte"],
},
},
arch: {
arm64: {
product_variables: {
malloc_not_svelte: {
cflags: ["-Warm64_malloc_not_svelte"],
},
},
},
},
multilib: {
lib32: {
product_variables: {
malloc_not_svelte: {
cflags: ["-Wlib32_malloc_not_svelte"],
},
},
},
},
target: {
android: {
product_variables: {
malloc_not_svelte: {
cflags: ["-Wandroid_malloc_not_svelte"],
},
},
}
},
} `,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = [
"-I.",
"-I$(BINDIR)/.",
] + select({
"//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte-android": ["-Wandroid_malloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte-arm": ["-Wlib32_malloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte-arm64": ["-Warm64_malloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte-x86": ["-Wlib32_malloc_not_svelte"],
"//conditions:default": [],
}),
linkstatic = True,
srcs = ["common.c"],

View File

@@ -34,7 +34,7 @@ func getStringListValues(list bazel.StringListAttribute) (reflect.Value, []selec
selectValues = append(selectValues, osSelects)
}
for _, pv := range list.ProductValues {
for _, pv := range list.SortedProductVariables() {
s := make(selects)
if len(pv.Values) > 0 {
s[pv.SelectKey()] = reflect.ValueOf(pv.Values)