From 921af32310e740ea0c4f0f83b7349771b3378916 Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Wed, 26 Apr 2023 02:56:37 +0000 Subject: [PATCH] Print default val if all vals in axis match default val To avoid verbosity, we currently dedupe keys in axis if its value matches the value of //conditions:default. For axes where all values might match the default value, we would effectively drop the common value. To fix this, we are now dropping the select statement and not the common value. Test: go test ./bp2build Change-Id: Ic377b93ee2aba971753f6a5e7a62e15d1fcfa2bc --- bp2build/build_conversion_test.go | 15 +++++++++++++++ bp2build/configurability.go | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go index 1b64055f7..e198a11b8 100644 --- a/bp2build/build_conversion_test.go +++ b/bp2build/build_conversion_test.go @@ -21,6 +21,7 @@ import ( "android/soong/android" "android/soong/android/allowlists" + "android/soong/bazel" "android/soong/python" ) @@ -1931,3 +1932,17 @@ func TestGenerateConfigSetting(t *testing.T) { Description: "Generating API contribution Bazel targets for custom module", }) } + +// If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value +func TestPrettyPrintSelectMapEqualValues(t *testing.T) { + lla := bazel.LabelListAttribute{ + Value: bazel.LabelList{}, + } + libFooImplLabel := bazel.Label{ + Label: ":libfoo.impl", + } + lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) + lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) + actual, _ := prettyPrintAttribute(lla, 0) + android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual) +} diff --git a/bp2build/configurability.go b/bp2build/configurability.go index 8e171031c..3d9f0a274 100644 --- a/bp2build/configurability.go +++ b/bp2build/configurability.go @@ -279,6 +279,10 @@ func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue *stri } if len(selects) == 0 { + // If there is a default value, and there are no selects for this axis, print that without any selects. + if val, exists := selectMap[bazel.ConditionsDefaultSelectKey]; exists { + return prettyPrint(val, indent, emitZeroValues) + } // No conditions (or all values are empty lists), so no need for a map. return "", nil }