Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7a90fcfa authored by Yu Liu's avatar Yu Liu Committed by Gerrit Code Review
Browse files

Merge "Convert java_aconfig_library to bazel." into main

parents 33befd0c f2b94010
Loading
Loading
Loading
Loading
+42 −3
Original line number Diff line number Diff line
@@ -15,10 +15,13 @@
package aconfig

import (
	"fmt"

	"android/soong/android"
	"android/soong/bazel"
	"android/soong/java"
	"fmt"
	"github.com/google/blueprint"
	"github.com/google/blueprint/proptools"
)

type declarationsTagType struct {
@@ -32,7 +35,7 @@ type JavaAconfigDeclarationsLibraryProperties struct {
	Aconfig_declarations string

	// whether to generate test mode version of the library
	Test bool
	Test *bool
}

type JavaAconfigDeclarationsLibraryCallbacks struct {
@@ -68,7 +71,7 @@ func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuild
	// Generate the action to build the srcjar
	srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
	var mode string
	if callbacks.properties.Test {
	if proptools.Bool(callbacks.properties.Test) {
		mode = "test"
	} else {
		mode = "production"
@@ -89,3 +92,39 @@ func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuild

	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)
}
+2 −0
Original line number Diff line number Diff line
@@ -524,6 +524,7 @@ var (
	}

	Bp2buildModuleAlwaysConvertList = []string{
		"AconfigJavaHostTest",
		// aconfig
		"libonce_cell",
		"libanyhow",
@@ -1009,6 +1010,7 @@ var (
		"cc_prebuilt_library_static",
		"combined_apis",
		"droiddoc_exported_dir",
		"java_aconfig_library",
		"java_import",
		"java_import_host",
		"java_sdk_library",
+2 −1
Original line number Diff line number Diff line
@@ -442,7 +442,8 @@ func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string,
	otherLabel := labelFromModule(ctx, m)

	// 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
	}

+100 −0
Original line number Diff line number Diff line
@@ -20,11 +20,13 @@ import (
	"android/soong/aconfig"
	"android/soong/android"
	"android/soong/cc"
	"android/soong/java"
)

func registerAconfigModuleTypes(ctx android.RegistrationContext) {
	aconfig.RegisterBuildComponents(ctx)
	ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
	ctx.RegisterModuleType("java_library", java.LibraryFactory)
}

func TestAconfigDeclarations(t *testing.T) {
@@ -135,3 +137,101 @@ func TestCcAconfigLibrary(t *testing.T) {
		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,
	})
}
+4 −0
Original line number Diff line number Diff line
@@ -719,6 +719,10 @@ func (j *Module) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel
	return j.SdkVersion(ctx).ApiLevel
}

func (j *Module) GetDeviceProperties() *DeviceProperties {
	return &j.deviceProperties
}

func (j *Module) MaxSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
	if j.deviceProperties.Max_sdk_version != nil {
		return android.ApiLevelFrom(ctx, *j.deviceProperties.Max_sdk_version)
Loading