diff --git a/android/variable.go b/android/variable.go index 672576a9f..cf7493369 100644 --- a/android/variable.go +++ b/android/variable.go @@ -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) { diff --git a/bazel/properties.go b/bazel/properties.go index 640275f6f..84dca7e26 100644 --- a/bazel/properties.go +++ b/bazel/properties.go @@ -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) { diff --git a/bp2build/cc_library_static_conversion_test.go b/bp2build/cc_library_static_conversion_test.go index 44c6ad4d1..229b1c25e 100644 --- a/bp2build/cc_library_static_conversion_test.go +++ b/bp2build/cc_library_static_conversion_test.go @@ -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"], diff --git a/bp2build/configurability.go b/bp2build/configurability.go index 3cdc99437..9869c5d37 100644 --- a/bp2build/configurability.go +++ b/bp2build/configurability.go @@ -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)