From 2cc802a4421313db16a054674a4dc567f400844b Mon Sep 17 00:00:00 2001 From: Yu Liu Date: Tue, 5 Sep 2023 17:19:45 -0700 Subject: [PATCH] Support aconfig_declarations, aconfig_values and aconfig_value_set Bug: 297356603 Test: Unit tests Change-Id: I2f797578a35322440db0f281b4d46b6652512e00 --- aconfig/aconfig_declarations.go | 34 +++++++++-- aconfig/aconfig_value_set.go | 25 +++++++- aconfig/aconfig_values.go | 29 ++++++++- aconfig/init.go | 4 +- aconfig/testing.go | 2 +- android/allowlists/allowlists.go | 3 + android/config.go | 19 +++++- android/variable.go | 4 +- bp2build/aconfig_conversion_test.go | 92 +++++++++++++++++++++++++++++ bp2build/bp2build_product_config.go | 6 ++ 10 files changed, 202 insertions(+), 16 deletions(-) create mode 100644 bp2build/aconfig_conversion_test.go diff --git a/aconfig/aconfig_declarations.go b/aconfig/aconfig_declarations.go index 5cdf5b605..ed0961b25 100644 --- a/aconfig/aconfig_declarations.go +++ b/aconfig/aconfig_declarations.go @@ -15,16 +15,18 @@ package aconfig import ( - "android/soong/android" "fmt" "strings" + "android/soong/android" + "android/soong/bazel" "github.com/google/blueprint" ) type DeclarationsModule struct { android.ModuleBase android.DefaultableModuleBase + android.BazelModuleBase // Properties for "aconfig_declarations" properties struct { @@ -47,8 +49,7 @@ func DeclarationsFactory() android.Module { android.InitAndroidModule(module) android.InitDefaultableModule(module) module.AddProperties(&module.properties) - // TODO: bp2build - //android.InitBazelModule(module) + android.InitBazelModule(module) return module } @@ -73,7 +74,9 @@ func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that // match our package. valuesFromConfig := ctx.Config().ReleaseAconfigValueSets() - ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...) + if valuesFromConfig != "" { + ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig) + } } func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) { @@ -159,3 +162,26 @@ func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.Module }) } + +type bazelAconfigDeclarationsAttributes struct { + Srcs bazel.LabelListAttribute + Package string +} + +func (module *DeclarationsModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { + if ctx.ModuleType() != "aconfig_declarations" { + return + } + srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs)) + + attrs := bazelAconfigDeclarationsAttributes{ + Srcs: srcs, + Package: module.properties.Package, + } + props := bazel.BazelTargetModuleProperties{ + Rule_class: "aconfig_declarations", + Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_declarations.bzl", + } + + ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) +} diff --git a/aconfig/aconfig_value_set.go b/aconfig/aconfig_value_set.go index 252908fa3..af9ddd3dc 100644 --- a/aconfig/aconfig_value_set.go +++ b/aconfig/aconfig_value_set.go @@ -16,6 +16,7 @@ package aconfig import ( "android/soong/android" + "android/soong/bazel" "github.com/google/blueprint" ) @@ -23,6 +24,7 @@ import ( type ValueSetModule struct { android.ModuleBase android.DefaultableModuleBase + android.BazelModuleBase properties struct { // aconfig_values modules @@ -36,8 +38,7 @@ func ValueSetFactory() android.Module { android.InitAndroidModule(module) android.InitDefaultableModule(module) module.AddProperties(&module.properties) - // TODO: bp2build - //android.InitBazelModule(module) + android.InitBazelModule(module) return module } @@ -90,3 +91,23 @@ func (module *ValueSetModule) GenerateAndroidBuildActions(ctx android.ModuleCont AvailablePackages: packages, }) } + +type bazelAconfigValueSetAttributes struct { + Values bazel.LabelListAttribute +} + +func (module *ValueSetModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { + if ctx.ModuleType() != "aconfig_value_set" { + return + } + + attrs := bazelAconfigValueSetAttributes{ + Values: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, module.properties.Values)), + } + props := bazel.BazelTargetModuleProperties{ + Rule_class: "aconfig_value_set", + Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_value_set.bzl", + } + + ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) +} diff --git a/aconfig/aconfig_values.go b/aconfig/aconfig_values.go index 91f1c9098..0aa6a72a4 100644 --- a/aconfig/aconfig_values.go +++ b/aconfig/aconfig_values.go @@ -16,6 +16,7 @@ package aconfig import ( "android/soong/android" + "android/soong/bazel" "github.com/google/blueprint" ) @@ -23,6 +24,7 @@ import ( type ValuesModule struct { android.ModuleBase android.DefaultableModuleBase + android.BazelModuleBase properties struct { // aconfig files, relative to this Android.bp file @@ -39,8 +41,7 @@ func ValuesFactory() android.Module { android.InitAndroidModule(module) android.InitDefaultableModule(module) module.AddProperties(&module.properties) - // TODO: bp2build - //android.InitBazelModule(module) + android.InitBazelModule(module) return module } @@ -68,3 +69,27 @@ func (module *ValuesModule) GenerateAndroidBuildActions(ctx android.ModuleContex } ctx.SetProvider(valuesProviderKey, providerData) } + +type bazelAconfigValuesAttributes struct { + Srcs bazel.LabelListAttribute + Package string +} + +func (module *ValuesModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { + if ctx.ModuleType() != "aconfig_values" { + return + } + + srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs)) + + attrs := bazelAconfigValuesAttributes{ + Srcs: srcs, + Package: module.properties.Package, + } + props := bazel.BazelTargetModuleProperties{ + Rule_class: "aconfig_values", + Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_values.bzl", + } + + ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) +} diff --git a/aconfig/init.go b/aconfig/init.go index 797388d6c..c14f8aea9 100644 --- a/aconfig/init.go +++ b/aconfig/init.go @@ -97,12 +97,12 @@ var ( ) func init() { - registerBuildComponents(android.InitRegistrationContext) + RegisterBuildComponents(android.InitRegistrationContext) pctx.HostBinToolVariable("aconfig", "aconfig") pctx.HostBinToolVariable("soong_zip", "soong_zip") } -func registerBuildComponents(ctx android.RegistrationContext) { +func RegisterBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType("aconfig_declarations", DeclarationsFactory) ctx.RegisterModuleType("aconfig_values", ValuesFactory) ctx.RegisterModuleType("aconfig_value_set", ValueSetFactory) diff --git a/aconfig/testing.go b/aconfig/testing.go index 60cefeb2b..f6489ec3f 100644 --- a/aconfig/testing.go +++ b/aconfig/testing.go @@ -20,7 +20,7 @@ import ( "android/soong/android" ) -var PrepareForTestWithAconfigBuildComponents = android.FixtureRegisterWithContext(registerBuildComponents) +var PrepareForTestWithAconfigBuildComponents = android.FixtureRegisterWithContext(RegisterBuildComponents) func runTest(t *testing.T, errorHandler android.FixtureErrorHandler, bp string) *android.TestResult { return android.GroupFixturePreparers(PrepareForTestWithAconfigBuildComponents). diff --git a/android/allowlists/allowlists.go b/android/allowlists/allowlists.go index ababd4c27..1de14cb66 100644 --- a/android/allowlists/allowlists.go +++ b/android/allowlists/allowlists.go @@ -928,6 +928,9 @@ var ( "java_sdk_library", "sysprop_library", "xsd_config", + "aconfig_declarations", + "aconfig_values", + "aconfig_value_set", } // Add the names of modules that bp2build should never convert, if it is diff --git a/android/config.go b/android/config.go index 645a263cb..445c6cddc 100644 --- a/android/config.go +++ b/android/config.go @@ -197,9 +197,22 @@ func (c Config) ReleaseVersion() string { return c.config.productVariables.ReleaseVersion } -// The flag values files passed to aconfig, derived from RELEASE_VERSION -func (c Config) ReleaseAconfigValueSets() []string { - return c.config.productVariables.ReleaseAconfigValueSets +// The aconfig value set passed to aconfig, derived from RELEASE_VERSION +func (c Config) ReleaseAconfigValueSets() string { + // This logic to handle both Soong module name and bazel target is temporary in order to + // provide backward compatibility where aosp and vendor/google both have the release + // aconfig value set but can't be updated at the same time to use bazel target + value := strings.Split(c.config.productVariables.ReleaseAconfigValueSets, ":") + value_len := len(value) + if value_len > 2 { + // This shouldn't happen as this should be either a module name or a bazel target path. + panic(fmt.Errorf("config file: invalid value for release aconfig value sets: %s", + c.config.productVariables.ReleaseAconfigValueSets)) + } + if value_len > 0 { + return value[value_len-1] + } + return "" } // The flag default permission value passed to aconfig diff --git a/android/variable.go b/android/variable.go index 02eff25f9..3ca7963ab 100644 --- a/android/variable.go +++ b/android/variable.go @@ -475,8 +475,8 @@ type ProductVariables struct { ProductBrand string `json:",omitempty"` BuildVersionTags []string `json:",omitempty"` - ReleaseVersion string `json:",omitempty"` - ReleaseAconfigValueSets []string `json:",omitempty"` + ReleaseVersion string `json:",omitempty"` + ReleaseAconfigValueSets string `json:",omitempty"` ReleaseAconfigFlagDefaultPermission string `json:",omitempty"` diff --git a/bp2build/aconfig_conversion_test.go b/bp2build/aconfig_conversion_test.go new file mode 100644 index 000000000..ddb62f783 --- /dev/null +++ b/bp2build/aconfig_conversion_test.go @@ -0,0 +1,92 @@ +// Copyright 2023 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package bp2build + +import ( + "testing" + + "android/soong/aconfig" + "android/soong/android" +) + +func registerAconfigModuleTypes(ctx android.RegistrationContext) { + aconfig.RegisterBuildComponents(ctx) +} + +func TestAconfigDeclarations(t *testing.T) { + bp := ` + aconfig_declarations { + name: "foo", + srcs: [ + "foo1.aconfig", + "test/foo2.aconfig", + ], + package: "com.android.foo", + } + ` + expectedBazelTarget := MakeBazelTargetNoRestrictions( + "aconfig_declarations", + "foo", + AttrNameToString{ + "srcs": `[ + "foo1.aconfig", + "test/foo2.aconfig", + ]`, + "package": `"com.android.foo"`, + }, + ) + RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ + Blueprint: bp, + ExpectedBazelTargets: []string{expectedBazelTarget}, + }) +} + +func TestAconfigValues(t *testing.T) { + bp := ` + aconfig_values { + name: "foo", + srcs: [ + "foo1.textproto", + ], + package: "com.android.foo", + } + aconfig_value_set { + name: "bar", + values: [ + "foo" + ] + } + ` + expectedBazelTargets := []string{ + MakeBazelTargetNoRestrictions( + "aconfig_values", + "foo", + AttrNameToString{ + "srcs": `["foo1.textproto"]`, + "package": `"com.android.foo"`, + }, + ), + MakeBazelTargetNoRestrictions( + "aconfig_value_set", + "bar", + AttrNameToString{ + "values": `[":foo"]`, + }, + )} + RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ + Blueprint: bp, + ExpectedBazelTargets: expectedBazelTargets, + }) +} diff --git a/bp2build/bp2build_product_config.go b/bp2build/bp2build_product_config.go index 77179934f..5c2d03f19 100644 --- a/bp2build/bp2build_product_config.go +++ b/bp2build/bp2build_product_config.go @@ -274,6 +274,12 @@ func platformMappingSingleProduct( 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:release_aconfig_flag_default_permission=%s\n", productVariables.ReleaseAconfigFlagDefaultPermission)) + // Empty string can't be used as label_flag on the bazel side + if len(productVariables.ReleaseAconfigValueSets) > 0 { + result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_aconfig_value_sets=%s\n", productVariables.ReleaseAconfigValueSets)) + } + result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:release_version=%s\n", productVariables.ReleaseVersion)) result.WriteString(fmt.Sprintf(" --//build/bazel/product_config:platform_sdk_version=%d\n", 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))