Add a function to create config_setting(s)
The use case for this is creating config_setting(s) specific to an apex
variant and selecting stub/impl in that config_setting. We likely need
only a handful of such config_setting(s), but determining that list
requires iterating the build graph.
Test: go test ./bp2build
Change-Id: I9aa552e3d0bcf67513023c3a7d4bbf8fae464ee4
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index b7678a4..a860484 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -600,6 +600,11 @@
// TODO(b/164227191): implement pretty print for interfaces.
// Interfaces are used for for arch, multilib and target properties.
return "", nil
+ case reflect.Map:
+ if v, ok := propertyValue.Interface().(bazel.StringMapAttribute); ok {
+ return starlark_fmt.PrintStringStringDict(v, indent), nil
+ }
+ return "", fmt.Errorf("bp2build expects map of type map[string]string for field: %s", propertyValue)
default:
return "", fmt.Errorf(
"unexpected kind for property struct field: %s", propertyValue.Kind())
diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go
index 73ee26b..1b64055 100644
--- a/bp2build/build_conversion_test.go
+++ b/bp2build/build_conversion_test.go
@@ -1898,3 +1898,36 @@
Description: "Generating API contribution Bazel targets for custom module",
})
}
+
+func TestGenerateConfigSetting(t *testing.T) {
+ bp := `
+ custom {
+ name: "foo",
+ test_config_setting: true,
+ }
+ `
+ expectedBazelTargets := []string{
+ MakeBazelTargetNoRestrictions(
+ "config_setting",
+ "foo_config_setting",
+ AttrNameToString{
+ "flag_values": `{
+ "//build/bazel/rules/my_string_setting": "foo",
+ }`,
+ },
+ ),
+ MakeBazelTarget(
+ "custom",
+ "foo",
+ AttrNameToString{},
+ ),
+ }
+ registerCustomModule := func(ctx android.RegistrationContext) {
+ ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
+ }
+ RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
+ Blueprint: bp,
+ ExpectedBazelTargets: expectedBazelTargets,
+ Description: "Generating API contribution Bazel targets for custom module",
+ })
+}
diff --git a/bp2build/bzl_conversion_test.go b/bp2build/bzl_conversion_test.go
index a8e557d..fa1bf8a 100644
--- a/bp2build/bzl_conversion_test.go
+++ b/bp2build/bzl_conversion_test.go
@@ -108,6 +108,7 @@
"string_literal_prop": attr.string(),
"string_prop": attr.string(),
"string_ptr_prop": attr.string(),
+ "test_config_setting": attr.bool(),
},
)
@@ -139,6 +140,7 @@
"string_literal_prop": attr.string(),
"string_prop": attr.string(),
"string_ptr_prop": attr.string(),
+ "test_config_setting": attr.bool(),
},
)
@@ -170,6 +172,7 @@
"string_literal_prop": attr.string(),
"string_prop": attr.string(),
"string_ptr_prop": attr.string(),
+ "test_config_setting": attr.bool(),
# test_prop start
# "test_string_prop": attr.string(),
# test_prop end
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 6e919db..fd99ff0 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -317,6 +317,8 @@
One_to_many_prop *bool
Api *string // File describing the APIs of this module
+
+ Test_config_setting *bool // Used to test generation of config_setting targets
}
type customModule struct {
@@ -490,6 +492,27 @@
}
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
+
+ if proptools.Bool(m.props.Test_config_setting) {
+ m.createConfigSetting(ctx)
+ }
+
+}
+
+func (m *customModule) createConfigSetting(ctx android.TopDownMutatorContext) {
+ csa := bazel.ConfigSettingAttributes{
+ Flag_values: bazel.StringMapAttribute{
+ "//build/bazel/rules/my_string_setting": m.Name(),
+ },
+ }
+ ca := android.CommonAttributes{
+ Name: m.Name() + "_config_setting",
+ }
+ ctx.CreateBazelConfigSetting(
+ csa,
+ ca,
+ ctx.ModuleDir(),
+ )
}
var _ android.ApiProvider = (*customModule)(nil)