Merge "Revert "Restrict java_sdk_library in libs"" into main

This commit is contained in:
Jihoon Kang
2024-09-20 17:17:32 +00:00
committed by Gerrit Code Review
7 changed files with 264 additions and 195 deletions

View File

@@ -7101,7 +7101,7 @@ func TestJavaSDKLibrary_WithinApex(t *testing.T) {
java_library {
name: "bar",
srcs: ["a.java"],
libs: ["foo.impl"],
libs: ["foo"],
apex_available: ["myapex"],
sdk_version: "none",
system_modules: "none",
@@ -7154,7 +7154,7 @@ func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
java_library {
name: "bar",
srcs: ["a.java"],
libs: ["foo.stubs"],
libs: ["foo"],
sdk_version: "none",
system_modules: "none",
}
@@ -7208,7 +7208,7 @@ func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
java_library {
name: "bar",
srcs: ["a.java"],
libs: ["foo.impl"],
libs: ["foo"],
apex_available: ["myapex"],
sdk_version: "none",
system_modules: "none",

View File

@@ -3241,7 +3241,7 @@ func TestUsesLibraries(t *testing.T) {
java_library {
name: "static-runtime-helper",
srcs: ["a.java"],
libs: ["runtime-library.impl"],
libs: ["runtime-library"],
sdk_version: "current",
}
@@ -3305,7 +3305,7 @@ func TestUsesLibraries(t *testing.T) {
name: "app",
srcs: ["a.java"],
libs: [
"qux.impl",
"qux",
"quuz.stubs"
],
static_libs: [

View File

@@ -2414,13 +2414,18 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
return
}
if _, ok := module.(SdkLibraryDependency); ok {
if dep, ok := module.(SdkLibraryDependency); ok {
switch tag {
case sdkLibTag, libTag, staticLibTag:
sdkInfo, _ := android.OtherModuleProvider(ctx, module, SdkLibraryInfoProvider)
generatingLibsString := android.PrettyConcat(
getGeneratingLibs(ctx, j.SdkVersion(ctx), module.Name(), sdkInfo), true, "or")
ctx.ModuleErrorf("cannot depend directly on java_sdk_library %q; try depending on %s instead", module.Name(), generatingLibsString)
case sdkLibTag, libTag:
depHeaderJars := dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))
deps.classpath = append(deps.classpath, depHeaderJars...)
deps.dexClasspath = append(deps.dexClasspath, depHeaderJars...)
// TODO: SDK libraries should export a provider with TransitiveClasspathHeaderJars
depHeaderJarsSet := android.NewDepSet(android.PREORDER, depHeaderJars, nil)
transitiveClasspathHeaderJars = append(transitiveClasspathHeaderJars, depHeaderJarsSet)
case staticLibTag:
ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
}
} else if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
if sdkLinkType != javaPlatform {

View File

@@ -670,7 +670,7 @@ func TestPrebuilts(t *testing.T) {
java_library {
name: "foo",
srcs: ["a.java", ":stubs-source"],
libs: ["bar", "sdklib.stubs"],
libs: ["bar", "sdklib"],
static_libs: ["baz"],
}

View File

@@ -124,8 +124,8 @@ func parseFinalizedPrebuiltPath(ctx android.LoadHookContext, p string, allowIncr
return
}
func prebuiltApiModuleName(moduleName, module, scope, version string) string {
return fmt.Sprintf("%s_%s_%s_%s", moduleName, scope, version, module)
func prebuiltApiModuleName(mctx android.LoadHookContext, module, scope, version string) string {
return fmt.Sprintf("%s_%s_%s_%s", mctx.ModuleName(), scope, version, module)
}
func createImport(mctx android.LoadHookContext, module, scope, version, path, sdkVersion string, compileDex bool) {
props := struct {
@@ -135,7 +135,7 @@ func createImport(mctx android.LoadHookContext, module, scope, version, path, sd
Installable *bool
Compile_dex *bool
}{
Name: proptools.StringPtr(prebuiltApiModuleName(mctx.ModuleName(), module, scope, version)),
Name: proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, version)),
Jars: []string{path},
Sdk_version: proptools.StringPtr(sdkVersion),
Installable: proptools.BoolPtr(false),
@@ -257,8 +257,8 @@ func createSystemModules(mctx android.LoadHookContext, version, scope string) {
Name *string
Libs []string
}{}
props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx.ModuleName(), "system_modules", scope, version))
props.Libs = append(props.Libs, prebuiltApiModuleName(mctx.ModuleName(), "core-for-system-modules", scope, version))
props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", scope, version))
props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", scope, version))
mctx.CreateModule(systemModulesImportFactory, &props)
}

View File

@@ -1260,36 +1260,9 @@ type SdkLibraryDependency interface {
// sharedLibrary returns true if this can be used as a shared library.
sharedLibrary() bool
// getImplLibraryModule returns the pointer to the implementation library submodule of this
// sdk library.
getImplLibraryModule() *Library
}
type SdkLibraryInfo struct {
// GeneratingLibs is the names of the library modules that this sdk library
// generates. Note that this only includes the name of the modules that other modules can
// depend on, and is not a holistic list of generated modules.
GeneratingLibs []string
}
var SdkLibraryInfoProvider = blueprint.NewProvider[SdkLibraryInfo]()
func getGeneratingLibs(ctx android.ModuleContext, sdkVersion android.SdkSpec, sdkLibraryModuleName string, sdkInfo SdkLibraryInfo) []string {
apiLevel := sdkVersion.ApiLevel
if apiLevel.IsPreview() {
return sdkInfo.GeneratingLibs
}
generatingPrebuilts := []string{}
for _, apiScope := range AllApiScopes {
scopePrebuiltModuleName := prebuiltApiModuleName("sdk", sdkLibraryModuleName, apiScope.name, apiLevel.String())
if ctx.OtherModuleExists(scopePrebuiltModuleName) {
generatingPrebuilts = append(generatingPrebuilts, scopePrebuiltModuleName)
}
}
return generatingPrebuilts
}
type SdkLibrary struct {
Library
@@ -1612,22 +1585,9 @@ func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext)
}
android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
module.setOutputFiles(ctx)
var generatingLibs []string
for _, apiScope := range AllApiScopes {
if _, ok := module.scopePaths[apiScope]; ok {
generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
}
}
if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
generatingLibs = append(generatingLibs, module.implLibraryModuleName())
setOutputFiles(ctx, module.implLibraryModule.Module)
}
android.SetProvider(ctx, SdkLibraryInfoProvider, SdkLibraryInfo{
GeneratingLibs: generatingLibs,
})
}
func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
@@ -2805,25 +2765,10 @@ func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleCo
}
}
var generatingLibs []string
for _, apiScope := range AllApiScopes {
if scopeProperties, ok := module.scopeProperties[apiScope]; ok {
if len(scopeProperties.Jars) == 0 {
continue
}
generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
}
}
module.setOutputFiles(ctx)
if module.implLibraryModule != nil {
generatingLibs = append(generatingLibs, module.implLibraryModuleName())
setOutputFiles(ctx, module.implLibraryModule.Module)
}
android.SetProvider(ctx, SdkLibraryInfoProvider, SdkLibraryInfo{
GeneratingLibs: generatingLibs,
})
}
// to satisfy UsesLibraryDependency interface

View File

@@ -22,6 +22,8 @@ import (
"testing"
"android/soong/android"
"github.com/google/blueprint/proptools"
)
func TestJavaSdkLibrary(t *testing.T) {
@@ -53,7 +55,7 @@ func TestJavaSdkLibrary(t *testing.T) {
java_library {
name: "baz",
srcs: ["c.java"],
libs: ["foo.stubs.system", "bar.stubs"],
libs: ["foo", "bar.stubs"],
sdk_version: "system_current",
}
java_sdk_library {
@@ -90,25 +92,25 @@ func TestJavaSdkLibrary(t *testing.T) {
java_library {
name: "qux",
srcs: ["c.java"],
libs: ["baz", "fred.stubs", "quuz.stubs", "wilma.stubs", "barney.stubs.system", "betty.stubs.system"],
libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
sdk_version: "system_current",
}
java_library {
name: "baz-test",
srcs: ["c.java"],
libs: ["foo.stubs.test"],
libs: ["foo"],
sdk_version: "test_current",
}
java_library {
name: "baz-29",
srcs: ["c.java"],
libs: ["sdk_system_29_foo"],
libs: ["foo"],
sdk_version: "system_29",
}
java_library {
name: "baz-module-30",
srcs: ["c.java"],
libs: ["sdk_module-lib_30_foo"],
libs: ["foo"],
sdk_version: "module_30",
}
`)
@@ -160,11 +162,11 @@ func TestJavaSdkLibrary(t *testing.T) {
baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
// tests if baz-29 is actually linked to the system 29 stubs lib
android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/sdk_system_29_foo/android_common/combined/sdk_system_29_foo.jar")
android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
// tests if "baz-module-30" is actually linked to the module 30 stubs lib
android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/sdk_module-lib_30_foo/android_common/combined/sdk_module-lib_30_foo.jar")
android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar")
// test if baz has exported SDK lib names foo and bar to qux
qux := result.ModuleForTests("qux", "android_common")
@@ -443,7 +445,7 @@ func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
java_library {
name: "bar",
srcs: ["b.java"],
libs: ["foo.stubs"],
libs: ["foo"],
}
`)
@@ -761,7 +763,7 @@ func TestJavaSdkLibrary_SystemServer_AccessToStubScopeLibs(t *testing.T) {
java_library {
name: "bar",
srcs: ["a.java"],
libs: ["foo-public.stubs", "foo-system.stubs.system", "foo-module-lib.stubs.module_lib", "foo-system-server.stubs.system_server"],
libs: ["foo-public", "foo-system", "foo-module-lib", "foo-system-server"],
sdk_version: "system_server_current",
}
`)
@@ -787,26 +789,102 @@ func TestJavaSdkLibrary_SystemServer_AccessToStubScopeLibs(t *testing.T) {
}
}
func TestJavaSdkLibrary_MissingScope(t *testing.T) {
prepareForJavaTest.
ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
RunTestWithBp(t, `
java_sdk_library {
name: "foo",
srcs: ["a.java"],
public: {
enabled: false,
},
}
java_library {
name: "baz",
srcs: ["a.java"],
libs: ["foo"],
sdk_version: "module_current",
}
`)
}
func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
android.GroupFixturePreparers(
prepareForJavaTest,
PrepareForTestWithJavaSdkLibraryFiles,
FixtureWithLastReleaseApis("foo"),
).RunTestWithBp(t, `
java_sdk_library {
name: "foo",
srcs: ["a.java"],
system: {
enabled: true,
},
}
java_library {
name: "baz",
srcs: ["a.java"],
libs: ["foo"],
// foo does not have module-lib scope so it should fallback to system
sdk_version: "module_current",
}
`)
}
func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
result := android.GroupFixturePreparers(
prepareForJavaTest,
PrepareForTestWithJavaSdkLibraryFiles,
FixtureWithLastReleaseApis("foo"),
).RunTestWithBp(t, `
java_sdk_library {
name: "foo",
srcs: ["a.java"],
system: {
enabled: true,
},
default_to_stubs: true,
}
java_library {
name: "baz",
srcs: ["a.java"],
libs: ["foo"],
// does not have sdk_version set, should fallback to module,
// which will then fallback to system because the module scope
// is not enabled.
}
`)
// The baz library should depend on the system stubs jar.
bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
t.Errorf("expected %q, found %#q", expected, actual)
}
}
func TestJavaSdkLibraryImport(t *testing.T) {
result := prepareForJavaTest.RunTestWithBp(t, `
java_library {
name: "foo",
srcs: ["a.java"],
libs: ["sdklib.stubs"],
libs: ["sdklib"],
sdk_version: "current",
}
java_library {
name: "foo.system",
srcs: ["a.java"],
libs: ["sdklib.stubs.system"],
libs: ["sdklib"],
sdk_version: "system_current",
}
java_library {
name: "foo.test",
srcs: ["a.java"],
libs: ["sdklib.stubs.test"],
libs: ["sdklib"],
sdk_version: "test_current",
}
@@ -939,7 +1017,7 @@ func testJavaSdkLibraryImport_Preferred(t *testing.T, prefer string, preparer an
java_library {
name: "public",
srcs: ["a.java"],
libs: ["sdklib.stubs"],
libs: ["sdklib"],
sdk_version: "current",
}
`)
@@ -1112,6 +1190,155 @@ func TestSdkLibraryImport_MetadataModuleSupersedesPreferred(t *testing.T) {
}
}
func TestJavaSdkLibraryEnforce(t *testing.T) {
partitionToBpOption := func(partition string) string {
switch partition {
case "system":
return ""
case "vendor":
return "soc_specific: true,"
case "product":
return "product_specific: true,"
default:
panic("Invalid partition group name: " + partition)
}
}
type testConfigInfo struct {
libraryType string
fromPartition string
toPartition string
enforceProductInterface bool
enforceJavaSdkLibraryCheck bool
allowList []string
}
createPreparer := func(info testConfigInfo) android.FixturePreparer {
bpFileTemplate := `
java_library {
name: "foo",
srcs: ["foo.java"],
libs: ["bar"],
sdk_version: "current",
%s
}
%s {
name: "bar",
srcs: ["bar.java"],
sdk_version: "current",
%s
}
`
bpFile := fmt.Sprintf(bpFileTemplate,
partitionToBpOption(info.fromPartition),
info.libraryType,
partitionToBpOption(info.toPartition))
return android.GroupFixturePreparers(
PrepareForTestWithJavaSdkLibraryFiles,
FixtureWithLastReleaseApis("bar"),
android.FixtureWithRootAndroidBp(bpFile),
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
variables.InterPartitionJavaLibraryAllowList = info.allowList
}),
)
}
runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
errorHandler := android.FixtureExpectsNoErrors
if expectedErrorPattern != "" {
errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
}
android.GroupFixturePreparers(
prepareForJavaTest,
createPreparer(info),
).
ExtendWithErrorHandler(errorHandler).
RunTest(t)
})
}
errorMessage := "is not allowed across the partitions"
runTest(t, testConfigInfo{
libraryType: "java_library",
fromPartition: "product",
toPartition: "system",
enforceProductInterface: true,
enforceJavaSdkLibraryCheck: false,
}, "")
runTest(t, testConfigInfo{
libraryType: "java_library",
fromPartition: "product",
toPartition: "system",
enforceProductInterface: false,
enforceJavaSdkLibraryCheck: true,
}, "")
runTest(t, testConfigInfo{
libraryType: "java_library",
fromPartition: "product",
toPartition: "system",
enforceProductInterface: true,
enforceJavaSdkLibraryCheck: true,
}, errorMessage)
runTest(t, testConfigInfo{
libraryType: "java_library",
fromPartition: "vendor",
toPartition: "system",
enforceProductInterface: true,
enforceJavaSdkLibraryCheck: true,
}, errorMessage)
runTest(t, testConfigInfo{
libraryType: "java_library",
fromPartition: "vendor",
toPartition: "system",
enforceProductInterface: true,
enforceJavaSdkLibraryCheck: true,
allowList: []string{"bar"},
}, "")
runTest(t, testConfigInfo{
libraryType: "java_library",
fromPartition: "vendor",
toPartition: "product",
enforceProductInterface: true,
enforceJavaSdkLibraryCheck: true,
}, errorMessage)
runTest(t, testConfigInfo{
libraryType: "java_sdk_library",
fromPartition: "product",
toPartition: "system",
enforceProductInterface: true,
enforceJavaSdkLibraryCheck: true,
}, "")
runTest(t, testConfigInfo{
libraryType: "java_sdk_library",
fromPartition: "vendor",
toPartition: "system",
enforceProductInterface: true,
enforceJavaSdkLibraryCheck: true,
}, "")
runTest(t, testConfigInfo{
libraryType: "java_sdk_library",
fromPartition: "vendor",
toPartition: "product",
enforceProductInterface: true,
enforceJavaSdkLibraryCheck: true,
}, "")
}
func TestJavaSdkLibraryDist(t *testing.T) {
result := android.GroupFixturePreparers(
PrepareForTestWithJavaBuildComponents,
@@ -1430,7 +1657,7 @@ func TestSdkLibraryDependency(t *testing.T) {
name: "bar",
srcs: ["c.java", "b.java"],
libs: [
"foo.stubs",
"foo",
],
uses_libs: [
"foo",
@@ -1525,7 +1752,7 @@ func TestStubResolutionOfJavaSdkLibraryInLibs(t *testing.T) {
name: "mymodule",
srcs: ["a.java"],
sdk_version: "current",
libs: ["sdklib.stubs",], // this should be dynamically resolved to sdklib.stubs (source) or prebuilt_sdklib.stubs (prebuilt)
libs: ["sdklib",], // this should be dynamically resolved to sdklib.stubs (source) or prebuilt_sdklib.stubs (prebuilt)
}
`
@@ -1666,111 +1893,3 @@ func TestStubLinkType(t *testing.T) {
}
`)
}
func TestSdkLibDirectDependency(t *testing.T) {
android.GroupFixturePreparers(
prepareForJavaTest,
PrepareForTestWithJavaSdkLibraryFiles,
FixtureWithLastReleaseApis("foo", "bar"),
).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{
`module "baz" variant "android_common": cannot depend directly on java_sdk_library ` +
`"foo"; try depending on "foo.stubs", or "foo.impl" instead`,
`module "baz" variant "android_common": cannot depend directly on java_sdk_library ` +
`"prebuilt_bar"; try depending on "bar.stubs", or "bar.impl" instead`,
}),
).RunTestWithBp(t, `
java_sdk_library {
name: "foo",
srcs: ["a.java"],
sdk_version: "current",
public: {
enabled: true,
},
}
java_sdk_library_import {
name: "foo",
public: {
jars: ["a.jar"],
stub_srcs: ["a.java"],
current_api: "current.txt",
removed_api: "removed.txt",
annotations: "annotations.zip",
},
}
java_sdk_library {
name: "bar",
srcs: ["a.java"],
sdk_version: "current",
public: {
enabled: true,
},
}
java_sdk_library_import {
name: "bar",
prefer: true,
public: {
jars: ["a.jar"],
stub_srcs: ["a.java"],
current_api: "current.txt",
removed_api: "removed.txt",
annotations: "annotations.zip",
},
}
java_library {
name: "baz",
srcs: ["b.java"],
libs: [
"foo",
"bar",
],
}
`)
}
func TestSdkLibDirectDependencyWithPrebuiltSdk(t *testing.T) {
android.GroupFixturePreparers(
prepareForJavaTest,
PrepareForTestWithJavaSdkLibraryFiles,
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Platform_sdk_version = intPtr(34)
variables.Platform_sdk_codename = stringPtr("VanillaIceCream")
variables.Platform_version_active_codenames = []string{"VanillaIceCream"}
variables.Platform_systemsdk_versions = []string{"33", "34", "VanillaIceCream"}
variables.DeviceSystemSdkVersions = []string{"VanillaIceCream"}
}),
FixtureWithPrebuiltApis(map[string][]string{
"33": {"foo"},
"34": {"foo"},
"35": {"foo"},
}),
).ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(
`module "baz" variant "android_common": cannot depend directly on java_sdk_library "foo"; `+
`try depending on "sdk_public_33_foo", "sdk_system_33_foo", "sdk_test_33_foo", `+
`"sdk_module-lib_33_foo", or "sdk_system-server_33_foo" instead`),
).RunTestWithBp(t, `
java_sdk_library {
name: "foo",
srcs: ["a.java"],
sdk_version: "current",
public: {
enabled: true,
},
system: {
enabled: true,
},
}
java_library {
name: "baz",
srcs: ["b.java"],
libs: [
"foo",
],
sdk_version: "system_33",
}
`)
}