Merge "Make bp2build-generated selects() based on product config build settings" into main

This commit is contained in:
Cole Faust 2023-08-03 20:09:30 +00:00 committed by Gerrit Code Review
commit fc47771bdd
14 changed files with 272 additions and 109 deletions

View file

@ -414,6 +414,12 @@ func saveToConfigFile(config *ProductVariables, filename string) error {
return nil
}
type productVariableStarlarkRepresentation struct {
soongType string
selectable bool
archVariant bool
}
func saveToBazelConfigFile(config *ProductVariables, outDir string) error {
dir := filepath.Join(outDir, bazel.SoongInjectionDirName, "product_config")
err := createDirIfNonexistent(dir, os.ModePerm)
@ -421,32 +427,43 @@ func saveToBazelConfigFile(config *ProductVariables, outDir string) error {
return fmt.Errorf("Could not create dir %s: %s", dir, err)
}
nonArchVariantProductVariables := []string{}
archVariantProductVariables := []string{}
allProductVariablesType := reflect.TypeOf((*ProductVariables)(nil)).Elem()
productVariablesInfo := make(map[string]productVariableStarlarkRepresentation)
p := variableProperties{}
t := reflect.TypeOf(p.Product_variables)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
nonArchVariantProductVariables = append(nonArchVariantProductVariables, strings.ToLower(f.Name))
if proptools.HasTag(f, "android", "arch_variant") {
archVariantProductVariables = append(archVariantProductVariables, strings.ToLower(f.Name))
if f.Name == "Pdk" {
// Pdk is deprecated and has no effect as of aosp/1319667
continue
}
archVariant := proptools.HasTag(f, "android", "arch_variant")
if mainProductVariablesStructField, ok := allProductVariablesType.FieldByName(f.Name); ok {
productVariablesInfo[f.Name] = productVariableStarlarkRepresentation{
soongType: stringRepresentationOfSimpleType(mainProductVariablesStructField.Type),
selectable: true,
archVariant: archVariant,
}
} else {
panic("Unknown variable " + f.Name)
}
}
nonArchVariantProductVariablesJson := starlark_fmt.PrintStringList(nonArchVariantProductVariables, 0)
if err != nil {
return fmt.Errorf("cannot marshal product variable data: %s", err.Error())
}
archVariantProductVariablesJson := starlark_fmt.PrintStringList(archVariantProductVariables, 0)
if err != nil {
return fmt.Errorf("cannot marshal arch variant product variable data: %s", err.Error())
}
err = pathtools.WriteFileIfChanged(filepath.Join(dir, "product_variable_constants.bzl"), []byte(fmt.Sprintf(`
product_var_constraints = %s
arch_variant_product_var_constraints = %s
`, nonArchVariantProductVariablesJson, archVariantProductVariablesJson)), 0644)
# product_var_constant_info is a map of product variables to information about them. The fields are:
# - soongType: The type of the product variable as it appears in soong's ProductVariables struct.
# examples are string, bool, int, *bool, *string, []string, etc. This may be an overly
# conservative estimation of the type, for example a *bool could oftentimes just be a
# bool that defaults to false.
# - selectable: if this product variable can be selected on in Android.bp/build files. This means
# it's listed in the "variableProperties" soong struct. Currently all variables in
# this list are selectable because we only need the selectable ones at the moment,
# but the list may be expanded later.
# - archVariant: If the variable is tagged as arch variant in the "variableProperties" struct.
product_var_constant_info = %s
product_var_constraints = [k for k, v in product_var_constant_info.items() if v.selectable]
arch_variant_product_var_constraints = [k for k, v in product_var_constant_info.items() if v.selectable and v.archVariant]
`, starlark_fmt.PrintAny(productVariablesInfo, 0))), 0644)
if err != nil {
return fmt.Errorf("Could not write .bzl config file %s", err)
}
@ -459,6 +476,23 @@ arch_variant_product_var_constraints = %s
return nil
}
func stringRepresentationOfSimpleType(ty reflect.Type) string {
switch ty.Kind() {
case reflect.String:
return "string"
case reflect.Bool:
return "bool"
case reflect.Int:
return "int"
case reflect.Slice:
return "[]" + stringRepresentationOfSimpleType(ty.Elem())
case reflect.Pointer:
return "*" + stringRepresentationOfSimpleType(ty.Elem())
default:
panic("unimplemented type: " + ty.Kind().String())
}
}
// NullConfig returns a mostly empty Config for use by standalone tools like dexpreopt_gen that
// use the android package.
func NullConfig(outDir, soongOutDir string) Config {

View file

@ -1373,15 +1373,15 @@ func (attrs *CommonAttributes) fillCommonBp2BuildModuleAttrs(ctx *topDownMutator
}
}
productConfigEnabledLabels := []bazel.Label{}
productConfigEnabledAttribute := bazel.LabelListAttribute{}
// TODO(b/234497586): Soong config variables and product variables have different overriding behavior, we
// should handle it correctly
if !proptools.BoolDefault(enabledProperty.Value, true) && !neitherHostNorDevice {
// If the module is not enabled by default, then we can check if a
// product variable enables it
productConfigEnabledLabels = productVariableConfigEnableLabels(ctx)
productConfigEnabledAttribute = productVariableConfigEnableAttribute(ctx)
if len(productConfigEnabledLabels) > 0 {
if len(productConfigEnabledAttribute.ConfigurableValues) > 0 {
// In this case, an existing product variable configuration overrides any
// module-level `enable: false` definition
newValue := true
@ -1389,10 +1389,6 @@ func (attrs *CommonAttributes) fillCommonBp2BuildModuleAttrs(ctx *topDownMutator
}
}
productConfigEnabledAttribute := bazel.MakeLabelListAttribute(bazel.LabelList{
productConfigEnabledLabels, nil,
})
platformEnabledAttribute, err := enabledProperty.ToLabelListAttribute(
bazel.LabelList{[]bazel.Label{{Label: "@platforms//:incompatible"}}, nil},
bazel.LabelList{[]bazel.Label{}, nil})
@ -1423,31 +1419,28 @@ func (attrs *CommonAttributes) fillCommonBp2BuildModuleAttrs(ctx *topDownMutator
// 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 {
func productVariableConfigEnableAttribute(ctx *topDownMutatorContext) bazel.LabelListAttribute {
result := bazel.LabelListAttribute{}
productVariableProps := ProductVariableProperties(ctx, ctx.Module())
productConfigEnablingTargets := []bazel.Label{}
const propName = "Enabled"
if productConfigProps, exists := productVariableProps[propName]; exists {
if productConfigProps, exists := productVariableProps["Enabled"]; exists {
for productConfigProp, prop := range productConfigProps {
flag, ok := prop.(*bool)
if !ok {
ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
ctx.ModuleErrorf("Could not convert product variable enabled property")
}
if *flag {
axis := productConfigProp.ConfigurationAxis()
targetLabel := axis.SelectKey(productConfigProp.SelectKey())
productConfigEnablingTargets = append(productConfigEnablingTargets, bazel.Label{
Label: targetLabel,
})
result.SetSelectValue(axis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{{Label: "@platforms//:incompatible"}}))
result.SetSelectValue(axis, productConfigProp.SelectKey(), bazel.LabelList{Includes: []bazel.Label{}})
} 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))
ctx.ModuleErrorf("`enabled: false` is not currently supported for configuration variables. See b/210546943")
}
}
}
return productConfigEnablingTargets
return result
}
// A ModuleBase object contains the properties that are common to all Android

View file

@ -160,6 +160,7 @@ type variableProperties struct {
}
}
// Deprecated, has no effect as of aosp/1319667
Pdk struct {
Enabled *bool `android:"arch_variant"`
} `android:"arch_variant"`

View file

@ -67,7 +67,7 @@ const (
ConditionsDefaultSelectKey = "//conditions:default"
productVariableBazelPackage = "//build/bazel/product_variables"
productVariableBazelPackage = "//build/bazel/product_config/config_settings"
AndroidAndInApex = "android-in_apex"
AndroidPlatform = "system"

View file

@ -1555,7 +1555,7 @@ override_apex {
"file_contexts": `":foo-file_contexts"`,
"manifest": `"apex_manifest.json"`,
"min_sdk_version": `select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": "30",
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": "30",
"//conditions:default": "31",
})`,
"package_name": `"pkg_name"`,
@ -1564,7 +1564,7 @@ override_apex {
"file_contexts": `":foo-file_contexts"`,
"manifest": `"apex_manifest.json"`,
"min_sdk_version": `select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": "30",
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": "30",
"//conditions:default": "31",
})`,
"package_name": `"override_pkg_name"`,

View file

@ -2,6 +2,7 @@ package bp2build
import (
"android/soong/android"
"android/soong/android/soongconfig"
"android/soong/starlark_import"
"encoding/json"
"fmt"
@ -51,7 +52,7 @@ func CreateProductConfigFiles(
"{VARIANT}", targetBuildVariant,
"{PRODUCT_FOLDER}", currentProductFolder)
platformMappingContent, err := platformMappingContent(productReplacer.Replace("@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"), &productVariables)
platformMappingContent, err := platformMappingContent(productReplacer.Replace("@soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}"), &productVariables, ctx.Config().Bp2buildSoongConfigDefinitions)
if err != nil {
return nil, nil, err
}
@ -148,20 +149,20 @@ build --host_platform @soong_injection//{PRODUCT_FOLDER}:{PRODUCT}-{VARIANT}_dar
return injectionDirFiles, bp2buildDirFiles, nil
}
func platformMappingContent(mainProductLabel string, mainProductVariables *android.ProductVariables) (string, error) {
func platformMappingContent(mainProductLabel string, mainProductVariables *android.ProductVariables, soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions) (string, error) {
productsForTesting, err := starlark_import.GetStarlarkValue[map[string]map[string]starlark.Value]("products_for_testing")
if err != nil {
return "", err
}
var result strings.Builder
result.WriteString("platforms:\n")
platformMappingSingleProduct(mainProductLabel, mainProductVariables, &result)
platformMappingSingleProduct(mainProductLabel, mainProductVariables, soongConfigDefinitions, &result)
for product, productVariablesStarlark := range productsForTesting {
productVariables, err := starlarkMapToProductVariables(productVariablesStarlark)
if err != nil {
return "", err
}
platformMappingSingleProduct("@//build/bazel/tests/products:"+product, &productVariables, &result)
platformMappingSingleProduct("@//build/bazel/tests/products:"+product, &productVariables, soongConfigDefinitions, &result)
}
return result.String(), nil
}
@ -180,7 +181,7 @@ var bazelPlatformSuffixes = []string{
"_windows_x86_64",
}
func platformMappingSingleProduct(label string, productVariables *android.ProductVariables, result *strings.Builder) {
func platformMappingSingleProduct(label string, productVariables *android.ProductVariables, soongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions, result *strings.Builder) {
targetBuildVariant := "user"
if proptools.Bool(productVariables.Eng) {
targetBuildVariant = "eng"
@ -194,27 +195,72 @@ func platformMappingSingleProduct(label string, productVariables *android.Produc
result.WriteString(suffix)
result.WriteString("\n")
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:always_use_prebuilt_sdks=%t\n", proptools.Bool(productVariables.Always_use_prebuilt_sdks)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:arc=%t\n", proptools.Bool(productVariables.Arc)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:apex_global_min_sdk_version_override=%s\n", proptools.String(productVariables.ApexGlobalMinSdkVersionOverride)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:binder32bit=%t\n", proptools.Bool(productVariables.Binder32bit)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_from_text_stub=%t\n", proptools.Bool(productVariables.Build_from_text_stub)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_id=%s\n", proptools.String(productVariables.BuildId)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:build_version_tags=%s\n", strings.Join(productVariables.BuildVersionTags, ",")))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:certificate_overrides=%s\n", strings.Join(productVariables.CertificateOverrides, ",")))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_exclude_paths=%s\n", strings.Join(productVariables.CFIExcludePaths, ",")))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:cfi_include_paths=%s\n", strings.Join(productVariables.CFIIncludePaths, ",")))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:compressed_apex=%t\n", proptools.Bool(productVariables.CompressedApex)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:debuggable=%t\n", proptools.Bool(productVariables.Debuggable)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:default_app_certificate=%s\n", proptools.String(productVariables.DefaultAppCertificate)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_abi=%s\n", strings.Join(productVariables.DeviceAbi, ",")))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_max_page_size_supported=%s\n", proptools.String(productVariables.DeviceMaxPageSizeSupported)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_name=%s\n", proptools.String(productVariables.DeviceName)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_page_size_agnostic=%t\n", proptools.Bool(productVariables.Device_page_size_agnostic)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:device_product=%s\n", proptools.String(productVariables.DeviceProduct)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enable_cfi=%t\n", proptools.BoolDefault(productVariables.EnableCFI, true)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:enforce_vintf_manifest=%t\n", proptools.Bool(productVariables.Enforce_vintf_manifest)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:eng=%t\n", proptools.Bool(productVariables.Eng)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_not_svelte=%t\n", proptools.Bool(productVariables.Malloc_not_svelte)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_pattern_fill_contents=%t\n", proptools.Bool(productVariables.Malloc_pattern_fill_contents)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:malloc_zero_contents=%t\n", proptools.Bool(productVariables.Malloc_zero_contents)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:manifest_package_name_overrides=%s\n", strings.Join(productVariables.ManifestPackageNameOverrides, ",")))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:native_coverage=%t\n", proptools.Bool(productVariables.Native_coverage)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_version_name=%s\n", proptools.String(productVariables.Platform_version_name)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_brand=%s\n", productVariables.ProductBrand))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:product_manufacturer=%s\n", productVariables.ProductManufacturer))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", *productVariables.Platform_sdk_version))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:safestack=%t\n", proptools.Bool(productVariables.Safestack)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:target_build_variant=%s\n", targetBuildVariant))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:treble_linker_namespaces=%t\n", proptools.Bool(productVariables.Treble_linker_namespaces)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:tidy_checks=%s\n", proptools.String(productVariables.TidyChecks)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:uml=%t\n", proptools.Bool(productVariables.Uml)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build=%t\n", proptools.Bool(productVariables.Unbundled_build)))
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:unbundled_build_apps=%s\n", strings.Join(productVariables.Unbundled_build_apps, ",")))
for namespace, namespaceContents := range productVariables.VendorVars {
for variable, value := range namespaceContents {
key := namespace + "__" + variable
_, hasBool := soongConfigDefinitions.BoolVars[key]
_, hasString := soongConfigDefinitions.StringVars[key]
_, hasValue := soongConfigDefinitions.ValueVars[key]
if !hasBool && !hasString && !hasValue {
// Not all soong config variables are defined in Android.bp files. For example,
// prebuilt_bootclasspath_fragment uses soong config variables in a nonstandard
// way, that causes them to be present in the soong.variables file but not
// defined in an Android.bp file. There's also nothing stopping you from setting
// a variable in make that doesn't exist in soong. We only generate build
// settings for the ones that exist in soong, so skip all others.
continue
}
if hasBool && hasString || hasBool && hasValue || hasString && hasValue {
panic(fmt.Sprintf("Soong config variable %s:%s appears to be of multiple types. bool? %t, string? %t, value? %t", namespace, variable, hasBool, hasString, hasValue))
}
if hasBool {
// Logic copied from soongConfig.Bool()
value = strings.ToLower(value)
if value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" {
value = "true"
} else {
value = "false"
}
}
result.WriteString(fmt.Sprintf(" --//build/bazel/product_config/soong_config_variables:%s=%s\n", strings.ToLower(key), value))
}
}
}
}
@ -225,12 +271,20 @@ func starlarkMapToProductVariables(in map[string]starlark.Value) (android.Produc
field := productVarsReflect.Field(i)
fieldType := productVarsReflect.Type().Field(i)
name := fieldType.Name
if name == "BootJars" || name == "ApexBootJars" || name == "VendorVars" ||
name == "VendorSnapshotModules" || name == "RecoverySnapshotModules" {
if name == "BootJars" || name == "ApexBootJars" || name == "VendorSnapshotModules" ||
name == "RecoverySnapshotModules" {
// These variables have more complicated types, and we don't need them right now
continue
}
if _, ok := in[name]; ok {
if name == "VendorVars" {
vendorVars, err := starlark_import.Unmarshal[map[string]map[string]string](in[name])
if err != nil {
return result, err
}
field.Set(reflect.ValueOf(vendorVars))
continue
}
switch field.Type().Kind() {
case reflect.Bool:
val, err := starlark_import.Unmarshal[bool](in[name])
@ -282,5 +336,9 @@ func starlarkMapToProductVariables(in map[string]starlark.Value) (android.Produc
}
}
result.Native_coverage = proptools.BoolPtr(
proptools.Bool(result.GcovCoverage) ||
proptools.Bool(result.ClangCoverage))
return result, nil
}

View file

@ -69,6 +69,7 @@ func TestStarlarkMapToProductVariables(t *testing.T) {
t.Error(err)
continue
}
testCase.result.Native_coverage = proptools.BoolPtr(false)
if !reflect.DeepEqual(testCase.result, productVariables) {
expected, err := json.Marshal(testCase.result)
if err != nil {

View file

@ -640,7 +640,10 @@ func TestBp2buildHostAndDevice(t *testing.T) {
}`,
ExpectedBazelTargets: []string{
MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
"target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`,
"target_compatible_with": `select({
"//build/bazel/product_config/config_settings:unbundled_build": [],
"//conditions:default": ["@platforms//:incompatible"],
})`,
}),
},
},

View file

@ -1260,14 +1260,14 @@ cc_library {
"//build/bazel/platforms/arch:arm": [],
"//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte": [],
"//build/bazel/product_config/config_settings:malloc_not_svelte": [],
"//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
})`,
"implementation_dynamic_deps": `select({
"//build/bazel/platforms/arch:arm": [],
"//conditions:default": [":arm_shared_lib_excludes"],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
"//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
"//conditions:default": [],
})`,
"srcs_c": `["common.c"]`,
@ -1275,7 +1275,7 @@ cc_library {
"//build/bazel/platforms/arch:arm": [],
"//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
"//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
"//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
})`,
}),
@ -1307,7 +1307,7 @@ cc_library {
`,
ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
"implementation_deps": `select({
"//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
"//build/bazel/product_config/config_settings:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
"//conditions:default": [],
})`,
"srcs_c": `["common.c"]`,
@ -4631,7 +4631,7 @@ func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
"-Wextra",
"-DDEBUG_ONLY_CODE=0",
] + select({
"//build/bazel/product_variables:eng": [
"//build/bazel/product_config/config_settings:eng": [
"-UDEBUG_ONLY_CODE",
"-DDEBUG_ONLY_CODE=1",
],

View file

@ -1188,13 +1188,13 @@ cc_library_static {
ExpectedBazelTargets: []string{
MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
"copts": `select({
"//build/bazel/product_variables:binder32bit": ["-Wbinder32bit"],
"//build/bazel/product_config/config_settings:binder32bit": ["-Wbinder32bit"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
"//build/bazel/product_config/config_settings:malloc_not_svelte": ["-Wmalloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_zero_contents": ["-Wmalloc_zero_contents"],
"//build/bazel/product_config/config_settings:malloc_zero_contents": ["-Wmalloc_zero_contents"],
"//conditions:default": [],
})`,
"srcs_c": `["common.c"]`,
@ -1248,19 +1248,19 @@ cc_library_static {
ExpectedBazelTargets: []string{
MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
"copts": `select({
"//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
"//build/bazel/product_config/config_settings:malloc_not_svelte": ["-Wmalloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte-android": ["-Wandroid_malloc_not_svelte"],
"//build/bazel/product_config/config_settings:malloc_not_svelte-android": ["-Wandroid_malloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte-arm": ["-Wlib32_malloc_not_svelte"],
"//build/bazel/product_config/config_settings:malloc_not_svelte-arm": ["-Wlib32_malloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte-arm64": ["-Warm64_malloc_not_svelte"],
"//build/bazel/product_config/config_settings:malloc_not_svelte-arm64": ["-Warm64_malloc_not_svelte"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:malloc_not_svelte-x86": ["-Wlib32_malloc_not_svelte"],
"//build/bazel/product_config/config_settings:malloc_not_svelte-x86": ["-Wlib32_malloc_not_svelte"],
"//conditions:default": [],
})`,
"srcs_c": `["common.c"]`,
@ -1287,7 +1287,7 @@ cc_library_static {
ExpectedBazelTargets: []string{
MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
"asflags": `select({
"//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
"//build/bazel/product_config/config_settings:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
"//conditions:default": [],
})`,
"srcs_as": `["common.S"]`,

View file

@ -200,7 +200,7 @@ func TestCcObjectProductVariable(t *testing.T) {
ExpectedBazelTargets: []string{
MakeBazelTarget("cc_object", "foo", AttrNameToString{
"asflags": `select({
"//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
"//build/bazel/product_config/config_settings:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
"//conditions:default": [],
})`,
"copts": `["-fno-addrsig"]`,

View file

@ -149,7 +149,7 @@ prebuilt_etc {
MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
"filename": `"tz_version"`,
"src": `select({
"//build/bazel/product_variables:native_coverage": "src1",
"//build/bazel/product_config/config_settings:native_coverage": "src1",
"//conditions:default": "version/tz_version",
})`,
"dir": `"etc"`,
@ -318,7 +318,7 @@ prebuilt_etc {
"dir": `"etc"`,
"src": `select({
"//build/bazel/platforms/arch:arm": "armSrc",
"//build/bazel/product_variables:native_coverage-arm": "nativeCoverageArmSrc",
"//build/bazel/product_config/config_settings:native_coverage-arm": "nativeCoverageArmSrc",
"//conditions:default": None,
})`,
})}})

View file

@ -91,7 +91,7 @@ custom_cc_library_static {
ExpectedBazelTargets: []string{`cc_library_static(
name = "foo",
copts = select({
"//build/bazel/product_variables:acme__feature1": ["-DFEATURE1"],
"//build/bazel/product_config/config_settings:acme__feature1": ["-DFEATURE1"],
"//conditions:default": ["-DDEFAULT1"],
}),
local_includes = ["."],
@ -140,7 +140,7 @@ custom_cc_library_static {
ExpectedBazelTargets: []string{`cc_library_static(
name = "foo",
copts = select({
"//build/bazel/product_variables:acme__feature1": ["-DFEATURE1"],
"//build/bazel/product_config/config_settings:acme__feature1": ["-DFEATURE1"],
"//conditions:default": ["-DDEFAULT1"],
}),
local_includes = ["."],
@ -191,9 +191,9 @@ custom_cc_library_static {
ExpectedBazelTargets: []string{`cc_library_static(
name = "foo",
copts = select({
"//build/bazel/product_variables:acme__board__soc_a": ["-DSOC_A"],
"//build/bazel/product_variables:acme__board__soc_b": ["-DSOC_B"],
"//build/bazel/product_variables:acme__board__soc_c": [],
"//build/bazel/product_config/config_settings:acme__board__soc_a": ["-DSOC_A"],
"//build/bazel/product_config/config_settings:acme__board__soc_b": ["-DSOC_B"],
"//build/bazel/product_config/config_settings:acme__board__soc_c": [],
"//conditions:default": ["-DSOC_DEFAULT"],
}),
local_includes = ["."],
@ -240,7 +240,7 @@ custom_cc_library_static {
ExpectedBazelTargets: []string{`cc_library_static(
name = "foo",
copts = select({
"//build/bazel/product_variables:acme__feature1": ["-DFEATURE1"],
"//build/bazel/product_config/config_settings:acme__feature1": ["-DFEATURE1"],
"//conditions:default": ["-DDEFAULT1"],
}),
local_includes = ["."],
@ -310,15 +310,15 @@ custom_cc_library_static {
ExpectedBazelTargets: []string{`cc_library_static(
name = "foo",
copts = select({
"//build/bazel/product_variables:acme__board__soc_a": ["-DSOC_A"],
"//build/bazel/product_variables:acme__board__soc_b": ["-DSOC_B"],
"//build/bazel/product_variables:acme__board__soc_c": [],
"//build/bazel/product_config/config_settings:acme__board__soc_a": ["-DSOC_A"],
"//build/bazel/product_config/config_settings:acme__board__soc_b": ["-DSOC_B"],
"//build/bazel/product_config/config_settings:acme__board__soc_c": [],
"//conditions:default": ["-DSOC_DEFAULT"],
}) + select({
"//build/bazel/product_variables:acme__feature1": ["-DFEATURE1"],
"//build/bazel/product_config/config_settings:acme__feature1": ["-DFEATURE1"],
"//conditions:default": ["-DDEFAULT1"],
}) + select({
"//build/bazel/product_variables:acme__feature2": ["-DFEATURE2"],
"//build/bazel/product_config/config_settings:acme__feature2": ["-DFEATURE2"],
"//conditions:default": ["-DDEFAULT2"],
}),
local_includes = ["."],
@ -380,15 +380,15 @@ cc_library_static { name: "soc_default_static_dep", bazel_module: { bp2build_ava
ExpectedBazelTargets: []string{`cc_library_static(
name = "foo",
copts = select({
"//build/bazel/product_variables:acme__board__soc_a": ["-DSOC_A"],
"//build/bazel/product_variables:acme__board__soc_b": ["-DSOC_B"],
"//build/bazel/product_variables:acme__board__soc_c": [],
"//build/bazel/product_config/config_settings:acme__board__soc_a": ["-DSOC_A"],
"//build/bazel/product_config/config_settings:acme__board__soc_b": ["-DSOC_B"],
"//build/bazel/product_config/config_settings:acme__board__soc_c": [],
"//conditions:default": ["-DSOC_DEFAULT"],
}),
implementation_deps = select({
"//build/bazel/product_variables:acme__board__soc_a": ["//foo/bar:soc_a_dep"],
"//build/bazel/product_variables:acme__board__soc_b": ["//foo/bar:soc_b_dep"],
"//build/bazel/product_variables:acme__board__soc_c": [],
"//build/bazel/product_config/config_settings:acme__board__soc_a": ["//foo/bar:soc_a_dep"],
"//build/bazel/product_config/config_settings:acme__board__soc_b": ["//foo/bar:soc_b_dep"],
"//build/bazel/product_config/config_settings:acme__board__soc_c": [],
"//conditions:default": ["//foo/bar:soc_default_static_dep"],
}),
local_includes = ["."],
@ -446,7 +446,7 @@ cc_library_static {
ExpectedBazelTargets: []string{`cc_library_static(
name = "lib",
copts = select({
"//build/bazel/product_variables:vendor_foo__feature": [
"//build/bazel/product_config/config_settings:vendor_foo__feature": [
"-cflag_feature_2",
"-cflag_feature_1",
],
@ -527,11 +527,11 @@ cc_library_static {
ExpectedBazelTargets: []string{`cc_library_static(
name = "lib",
asflags = select({
"//build/bazel/product_variables:acme__feature": ["-asflag_bar"],
"//build/bazel/product_config/config_settings:acme__feature": ["-asflag_bar"],
"//conditions:default": ["-asflag_default_bar"],
}),
copts = select({
"//build/bazel/product_variables:acme__feature": [
"//build/bazel/product_config/config_settings:acme__feature": [
"-cflag_foo",
"-cflag_bar",
],
@ -546,11 +546,11 @@ cc_library_static {
`cc_library_static(
name = "lib2",
asflags = select({
"//build/bazel/product_variables:acme__feature": ["-asflag_bar"],
"//build/bazel/product_config/config_settings:acme__feature": ["-asflag_bar"],
"//conditions:default": ["-asflag_default_bar"],
}),
copts = select({
"//build/bazel/product_variables:acme__feature": [
"//build/bazel/product_config/config_settings:acme__feature": [
"-cflag_bar",
"-cflag_foo",
],
@ -643,13 +643,13 @@ cc_library_static {
ExpectedBazelTargets: []string{`cc_library_static(
name = "lib",
copts = select({
"//build/bazel/product_variables:vendor_bar__feature": ["-DVENDOR_BAR_FEATURE"],
"//build/bazel/product_config/config_settings:vendor_bar__feature": ["-DVENDOR_BAR_FEATURE"],
"//conditions:default": ["-DVENDOR_BAR_DEFAULT"],
}) + select({
"//build/bazel/product_variables:vendor_foo__feature": ["-DVENDOR_FOO_FEATURE"],
"//build/bazel/product_config/config_settings:vendor_foo__feature": ["-DVENDOR_FOO_FEATURE"],
"//conditions:default": ["-DVENDOR_FOO_DEFAULT"],
}) + select({
"//build/bazel/product_variables:vendor_qux__feature": ["-DVENDOR_QUX_FEATURE"],
"//build/bazel/product_config/config_settings:vendor_qux__feature": ["-DVENDOR_QUX_FEATURE"],
"//conditions:default": ["-DVENDOR_QUX_DEFAULT"],
}),
local_includes = ["."],
@ -697,7 +697,7 @@ library_linking_strategy_custom {
ExpectedBazelTargets: []string{
MakeBazelTarget("custom", "foo", AttrNameToString{
"string_literal_prop": `select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": "29",
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": "29",
"//conditions:default": "30",
})`,
}),
@ -779,7 +779,7 @@ cc_library { name: "lib_default", bazel_module: { bp2build_available: false } }
ExpectedBazelTargets: []string{`cc_binary(
name = "library_linking_strategy_sample_binary",
dynamic_deps = select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [],
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": [],
"//conditions:default": [
"//foo/bar:lib_b",
"//foo/bar:lib_a",
@ -868,7 +868,7 @@ cc_library { name: "lib_c", bazel_module: { bp2build_available: false } }
ExpectedBazelTargets: []string{
MakeBazelTargetNoRestrictions("cc_binary", "library_linking_strategy_sample_binary", AttrNameToString{
"dynamic_deps": `select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [],
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": [],
"//conditions:default": [
"//foo/bar:lib_b",
"//foo/bar:lib_c",
@ -877,7 +877,7 @@ cc_library { name: "lib_c", bazel_module: { bp2build_available: false } }
}),
MakeBazelTargetNoRestrictions("cc_binary", "library_linking_strategy_sample_binary_with_excludes", AttrNameToString{
"dynamic_deps": `select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [],
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": [],
"//conditions:default": ["//foo/bar:lib_b"],
})`,
}),
@ -965,14 +965,14 @@ cc_library { name: "lib_default", bazel_module: { bp2build_available: false } }
ExpectedBazelTargets: []string{`cc_binary(
name = "library_linking_strategy_sample_binary",
deps = select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": [
"//foo/bar:lib_b_bp2build_cc_library_static",
"//foo/bar:lib_a_bp2build_cc_library_static",
],
"//conditions:default": [],
}),
dynamic_deps = select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [],
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": [],
"//conditions:default": [
"//foo/bar:lib_b",
"//foo/bar:lib_a",
@ -1046,14 +1046,14 @@ cc_library { name: "lib_b", bazel_module: { bp2build_available: false } }
ExpectedBazelTargets: []string{`cc_binary(
name = "library_linking_strategy_sample_binary",
deps = select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": [
"//foo/bar:lib_a_bp2build_cc_library_static",
"//foo/bar:lib_b_bp2build_cc_library_static",
],
"//conditions:default": [],
}),
dynamic_deps = select({
"//build/bazel/product_variables:android__library_linking_strategy__prefer_static": [],
"//build/bazel/product_config/config_settings:android__library_linking_strategy__prefer_static": [],
"//conditions:default": [
"//foo/bar:lib_a",
"//foo/bar:lib_b",
@ -1134,13 +1134,13 @@ cc_library { name: "lib_default", bazel_module: { bp2build_available: false } }
ExpectedBazelTargets: []string{`cc_binary(
name = "alphabet_binary",
deps = select({
"//build/bazel/product_variables:android__alphabet__a": [],
"//build/bazel/product_variables:android__alphabet__b": [],
"//build/bazel/product_config/config_settings:android__alphabet__a": [],
"//build/bazel/product_config/config_settings:android__alphabet__b": [],
"//conditions:default": ["//foo/bar:lib_default_bp2build_cc_library_static"],
}),
dynamic_deps = select({
"//build/bazel/product_variables:android__alphabet__a": ["//foo/bar:lib_a"],
"//build/bazel/product_variables:android__alphabet__b": ["//foo/bar:lib_b"],
"//build/bazel/product_config/config_settings:android__alphabet__a": ["//foo/bar:lib_a"],
"//build/bazel/product_config/config_settings:android__alphabet__b": ["//foo/bar:lib_b"],
"//conditions:default": [],
}),
local_includes = ["."],
@ -1199,7 +1199,7 @@ cc_binary {
name = "alphabet_binary",
local_includes = ["."],
srcs = ["main.cc"],
target_compatible_with = ["//build/bazel/product_variables:alphabet_module__special_build"] + select({
target_compatible_with = select({
"//build/bazel/platforms/os_arch:android_x86_64": ["@platforms//:incompatible"],
"//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
"//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
@ -1208,6 +1208,9 @@ cc_binary {
"//build/bazel/platforms/os_arch:linux_musl_x86_64": ["@platforms//:incompatible"],
"//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_config/config_settings:alphabet_module__special_build": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
)`}})
}
@ -1252,7 +1255,10 @@ cc_binary {
name = "alphabet_binary",
local_includes = ["."],
srcs = ["main.cc"],
target_compatible_with = ["//build/bazel/product_variables:alphabet_module__special_build"],
target_compatible_with = select({
"//build/bazel/product_config/config_settings:alphabet_module__special_build": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
)`}})
}
@ -1389,16 +1395,16 @@ cc_binary {
"//build/bazel/platforms/os:android": ["-DFOO"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:my_namespace__my_bool_variable__android": ["-DBAR"],
"//build/bazel/product_variables:my_namespace__my_bool_variable__conditions_default__android": ["-DBAZ"],
"//build/bazel/product_config/config_settings:my_namespace__my_bool_variable__android": ["-DBAR"],
"//build/bazel/product_config/config_settings:my_namespace__my_bool_variable__conditions_default__android": ["-DBAZ"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:my_namespace__my_string_variable__value1": ["-DVALUE1_NOT_ANDROID"],
"//build/bazel/product_config/config_settings:my_namespace__my_string_variable__value1": ["-DVALUE1_NOT_ANDROID"],
"//conditions:default": [],
}) + select({
"//build/bazel/product_variables:my_namespace__my_string_variable__conditions_default__android": ["-DSTRING_VAR_CONDITIONS_DEFAULT"],
"//build/bazel/product_variables:my_namespace__my_string_variable__value1__android": ["-DVALUE1"],
"//build/bazel/product_variables:my_namespace__my_string_variable__value2__android": ["-DVALUE2"],
"//build/bazel/product_config/config_settings:my_namespace__my_string_variable__conditions_default__android": ["-DSTRING_VAR_CONDITIONS_DEFAULT"],
"//build/bazel/product_config/config_settings:my_namespace__my_string_variable__value1__android": ["-DVALUE1"],
"//build/bazel/product_config/config_settings:my_namespace__my_string_variable__value2__android": ["-DVALUE2"],
"//conditions:default": [],
}),
local_includes = ["."],

View file

@ -16,6 +16,7 @@ package starlark_fmt
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
@ -33,6 +34,72 @@ func Indention(level int) string {
return strings.Repeat(" ", level*indent)
}
func PrintAny(value any, indentLevel int) string {
return printAnyRecursive(reflect.ValueOf(value), indentLevel)
}
func printAnyRecursive(value reflect.Value, indentLevel int) string {
switch value.Type().Kind() {
case reflect.String:
val := value.String()
if strings.Contains(val, "\"") || strings.Contains(val, "\n") {
return `'''` + val + `'''`
}
return `"` + val + `"`
case reflect.Bool:
if value.Bool() {
return "True"
} else {
return "False"
}
case reflect.Int:
return fmt.Sprintf("%d", value.Int())
case reflect.Slice:
if value.Len() == 0 {
return "[]"
} else if value.Len() == 1 {
return "[" + printAnyRecursive(value.Index(0), indentLevel) + "]"
}
list := make([]string, 0, value.Len()+2)
list = append(list, "[")
innerIndent := Indention(indentLevel + 1)
for i := 0; i < value.Len(); i++ {
list = append(list, innerIndent+printAnyRecursive(value.Index(i), indentLevel+1)+`,`)
}
list = append(list, Indention(indentLevel)+"]")
return strings.Join(list, "\n")
case reflect.Map:
if value.Len() == 0 {
return "{}"
}
items := make([]string, 0, value.Len())
for _, key := range value.MapKeys() {
items = append(items, fmt.Sprintf(`%s%s: %s,`, Indention(indentLevel+1), printAnyRecursive(key, indentLevel+1), printAnyRecursive(value.MapIndex(key), indentLevel+1)))
}
sort.Strings(items)
return fmt.Sprintf(`{
%s
%s}`, strings.Join(items, "\n"), Indention(indentLevel))
case reflect.Struct:
if value.NumField() == 0 {
return "struct()"
}
items := make([]string, 0, value.NumField()+2)
items = append(items, "struct(")
for i := 0; i < value.NumField(); i++ {
if value.Type().Field(i).Anonymous {
panic("anonymous fields aren't supported")
}
name := value.Type().Field(i).Name
items = append(items, fmt.Sprintf(`%s%s = %s,`, Indention(indentLevel+1), name, printAnyRecursive(value.Field(i), indentLevel+1)))
}
items = append(items, Indention(indentLevel)+")")
return strings.Join(items, "\n")
default:
panic("Unhandled kind: " + value.Kind().String())
}
}
// PrintBool returns a Starlark compatible bool string.
func PrintBool(item bool) string {
if item {