Convert java_aconfig_library to bazel.

Bug: 297357579
Test: Unit test and AconfigJavaHostTest
Change-Id: Icf944cc0b2a7382107923d49b2d2ff0eb4113638
This commit is contained in:
Yu Liu 2023-09-19 15:09:10 -07:00
parent dec9ce6693
commit f2b94010c8
7 changed files with 160 additions and 5 deletions

View file

@ -15,10 +15,13 @@
package aconfig package aconfig
import ( import (
"android/soong/android"
"android/soong/java"
"fmt" "fmt"
"android/soong/android"
"android/soong/bazel"
"android/soong/java"
"github.com/google/blueprint" "github.com/google/blueprint"
"github.com/google/blueprint/proptools"
) )
type declarationsTagType struct { type declarationsTagType struct {
@ -32,7 +35,7 @@ type JavaAconfigDeclarationsLibraryProperties struct {
Aconfig_declarations string Aconfig_declarations string
// whether to generate test mode version of the library // whether to generate test mode version of the library
Test bool Test *bool
} }
type JavaAconfigDeclarationsLibraryCallbacks struct { type JavaAconfigDeclarationsLibraryCallbacks struct {
@ -68,7 +71,7 @@ func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuild
// Generate the action to build the srcjar // Generate the action to build the srcjar
srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar") srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
var mode string var mode string
if callbacks.properties.Test { if proptools.Bool(callbacks.properties.Test) {
mode = "test" mode = "test"
} else { } else {
mode = "production" mode = "production"
@ -89,3 +92,39 @@ func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuild
return srcJarPath return srcJarPath
} }
type bazelJavaAconfigLibraryAttributes struct {
Aconfig_declarations bazel.LabelAttribute
Test *bool
Sdk_version *string
}
func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) Bp2build(ctx android.Bp2buildMutatorContext, module *java.GeneratedJavaLibraryModule) {
if ctx.ModuleType() != "java_aconfig_library" {
return
}
// By default, soong builds the aconfig java library with private_current, however
// bazel currently doesn't support it so we default it to system_current. One reason
// is that the dependency of all java_aconfig_library aconfig-annotations-lib is
// built with system_current. For the java aconfig library itself it doesn't really
// matter whether it uses private API or system API because the only module it uses
// is DeviceConfig which is in system, and the rdeps of the java aconfig library
// won't change its sdk version either, so this should be fine.
// Ideally we should only use the default value if it is not set by the user, but
// bazel only supports a limited sdk versions, for example, the java_aconfig_library
// modules in framework/base use core_platform which is not supported by bazel yet.
// TODO(b/302148527): change soong to default to system_current as well.
sdkVersion := "system_current"
attrs := bazelJavaAconfigLibraryAttributes{
Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, callbacks.properties.Aconfig_declarations).Label),
Test: callbacks.properties.Test,
Sdk_version: &sdkVersion,
}
props := bazel.BazelTargetModuleProperties{
Rule_class: "java_aconfig_library",
Bzl_load_location: "//build/bazel/rules/java:java_aconfig_library.bzl",
}
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: ctx.ModuleName()}, &attrs)
}

View file

@ -523,6 +523,7 @@ var (
} }
Bp2buildModuleAlwaysConvertList = []string{ Bp2buildModuleAlwaysConvertList = []string{
"AconfigJavaHostTest",
// aconfig // aconfig
"libonce_cell", "libonce_cell",
"libanyhow", "libanyhow",
@ -1002,6 +1003,7 @@ var (
"cc_prebuilt_library_static", "cc_prebuilt_library_static",
"combined_apis", "combined_apis",
"droiddoc_exported_dir", "droiddoc_exported_dir",
"java_aconfig_library",
"java_import", "java_import",
"java_import_host", "java_import_host",
"java_sdk_library", "java_sdk_library",

View file

@ -442,7 +442,8 @@ func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string,
otherLabel := labelFromModule(ctx, m) otherLabel := labelFromModule(ctx, m)
// TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets. // TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets.
if tag != "" && m.Name() == "framework-res" { if (tag != "" && m.Name() == "framework-res") ||
(tag == ".generated_srcjars" && ctx.OtherModuleType(m) == "java_aconfig_library") {
otherLabel += tag otherLabel += tag
} }

View file

@ -20,11 +20,13 @@ import (
"android/soong/aconfig" "android/soong/aconfig"
"android/soong/android" "android/soong/android"
"android/soong/cc" "android/soong/cc"
"android/soong/java"
) )
func registerAconfigModuleTypes(ctx android.RegistrationContext) { func registerAconfigModuleTypes(ctx android.RegistrationContext) {
aconfig.RegisterBuildComponents(ctx) aconfig.RegisterBuildComponents(ctx)
ctx.RegisterModuleType("cc_library", cc.LibraryFactory) ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
ctx.RegisterModuleType("java_library", java.LibraryFactory)
} }
func TestAconfigDeclarations(t *testing.T) { func TestAconfigDeclarations(t *testing.T) {
@ -135,3 +137,101 @@ func TestCcAconfigLibrary(t *testing.T) {
StubbedBuildDefinitions: []string{"server_configurable_flags"}, StubbedBuildDefinitions: []string{"server_configurable_flags"},
}) })
} }
func TestJavaAconfigLibrary(t *testing.T) {
bp := `
aconfig_declarations {
name: "foo_aconfig_declarations",
srcs: [
"foo1.aconfig",
],
package: "com.android.foo",
}
java_aconfig_library {
name: "foo",
aconfig_declarations: "foo_aconfig_declarations",
test: true,
}
`
expectedBazelTargets := []string{
MakeBazelTargetNoRestrictions(
"aconfig_declarations",
"foo_aconfig_declarations",
AttrNameToString{
"srcs": `["foo1.aconfig"]`,
"package": `"com.android.foo"`,
},
),
MakeBazelTargetNoRestrictions(
"java_aconfig_library",
"foo",
AttrNameToString{
"aconfig_declarations": `":foo_aconfig_declarations"`,
"test": `True`,
"sdk_version": `"system_current"`,
"target_compatible_with": `["//build/bazel/platforms/os:android"]`,
},
)}
RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
Blueprint: bp,
ExpectedBazelTargets: expectedBazelTargets,
})
}
func TestJavaAconfigLibraryAsTaggedOutput(t *testing.T) {
bp := `
aconfig_declarations {
name: "foo_aconfig_declarations",
srcs: [
"foo.aconfig",
],
package: "com.android.foo",
}
java_library {
name: "foo_library",
srcs: [":foo_aconfig_library{.generated_srcjars}"],
sdk_version: "current",
bazel_module: { bp2build_available: true },
}
java_aconfig_library {
name: "foo_aconfig_library",
aconfig_declarations: "foo_aconfig_declarations",
test: true,
}
`
expectedBazelTargets := []string{
MakeBazelTargetNoRestrictions(
"aconfig_declarations",
"foo_aconfig_declarations",
AttrNameToString{
"srcs": `["foo.aconfig"]`,
"package": `"com.android.foo"`,
},
),
MakeBazelTargetNoRestrictions(
"java_aconfig_library",
"foo_aconfig_library",
AttrNameToString{
"aconfig_declarations": `":foo_aconfig_declarations"`,
"test": `True`,
"sdk_version": `"system_current"`,
"target_compatible_with": `["//build/bazel/platforms/os:android"]`,
},
),
MakeBazelTargetNoRestrictions(
"java_library",
"foo_library",
AttrNameToString{
"srcs": `[":foo_aconfig_library.generated_srcjars"]`,
"sdk_version": `"current"`,
"target_compatible_with": `["//build/bazel/platforms/os:android"]`,
},
),
MakeNeverlinkDuplicateTarget("java_library", "foo_library"),
}
RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
Blueprint: bp,
ExpectedBazelTargets: expectedBazelTargets,
})
}

View file

@ -714,6 +714,10 @@ func (j *Module) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel
return j.SdkVersion(ctx).ApiLevel return j.SdkVersion(ctx).ApiLevel
} }
func (j *Module) GetDeviceProperties() *DeviceProperties {
return &j.deviceProperties
}
func (j *Module) MaxSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { func (j *Module) MaxSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
if j.deviceProperties.Max_sdk_version != nil { if j.deviceProperties.Max_sdk_version != nil {
return android.ApiLevelFrom(ctx, *j.deviceProperties.Max_sdk_version) return android.ApiLevelFrom(ctx, *j.deviceProperties.Max_sdk_version)

View file

@ -35,6 +35,8 @@ type GeneratedJavaLibraryCallbacks interface {
// Called from inside GenerateAndroidBuildActions. Add the build rules to // Called from inside GenerateAndroidBuildActions. Add the build rules to
// make the srcjar, and return the path to it. // make the srcjar, and return the path to it.
GenerateSourceJarBuildActions(module *GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path GenerateSourceJarBuildActions(module *GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path
Bp2build(ctx android.Bp2buildMutatorContext, module *GeneratedJavaLibraryModule)
} }
// GeneratedJavaLibraryModuleFactory provides a utility for modules that are generated // GeneratedJavaLibraryModuleFactory provides a utility for modules that are generated
@ -108,3 +110,7 @@ func (module *GeneratedJavaLibraryModule) GenerateAndroidBuildActions(ctx androi
module.Library.properties.Generated_srcjars = append(module.Library.properties.Generated_srcjars, srcJarPath) module.Library.properties.Generated_srcjars = append(module.Library.properties.Generated_srcjars, srcJarPath)
module.Library.GenerateAndroidBuildActions(ctx) module.Library.GenerateAndroidBuildActions(ctx)
} }
func (module *GeneratedJavaLibraryModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
module.callbacks.Bp2build(ctx, module)
}

View file

@ -41,6 +41,9 @@ func (callbacks *JavaGenLibTestCallbacks) GenerateSourceJarBuildActions(module *
return android.PathForOutput(ctx, "blah.srcjar") return android.PathForOutput(ctx, "blah.srcjar")
} }
func (callbacks *JavaGenLibTestCallbacks) Bp2build(ctx android.Bp2buildMutatorContext, module *GeneratedJavaLibraryModule) {
}
func testGenLib(t *testing.T, errorHandler android.FixtureErrorHandler, bp string) *android.TestResult { func testGenLib(t *testing.T, errorHandler android.FixtureErrorHandler, bp string) *android.TestResult {
return android.GroupFixturePreparers( return android.GroupFixturePreparers(
PrepareForIntegrationTestWithJava, PrepareForIntegrationTestWithJava,