Support aconfig_declarations, aconfig_values and aconfig_value_set

Bug: 297356603
Test: Unit tests
Change-Id: I2f797578a35322440db0f281b4d46b6652512e00
This commit is contained in:
Yu Liu 2023-09-05 17:19:45 -07:00
parent 3f0aa4d618
commit 2cc802a442
10 changed files with 202 additions and 16 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)

View file

@ -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).

View file

@ -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

View file

@ -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

View file

@ -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"`

View file

@ -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,
})
}

View file

@ -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))