Remove api_files property from java_api_library

java_api_contribution provides api_surface information, but files
directly passed to java_api_library do not possess such information.
Currently, the api surface is assumed via naming convention for api
files passed via api_files property, but this is fragile.

This change removes the api_files property from java_api_library and
enforce all api files to be passed via java_api_contribution

Test: m nothing --build-from-text-stub
Bug: 300964421
Change-Id: If01d9ed978fe469d4ee0d685582a51629ebecc56
This commit is contained in:
Jihoon Kang 2023-09-20 23:03:01 +00:00
parent c0f7bd1a15
commit 6be0f00671
3 changed files with 74 additions and 68 deletions

View file

@ -1667,11 +1667,6 @@ type JavaApiLibraryProperties struct {
// This is a list of Soong modules
Api_contributions []string
// list of api.txt files relative to this directory that contribute to the
// API surface.
// This is a list of relative paths
Api_files []string `android:"path"`
// List of flags to be passed to the javac compiler to generate jar file
Javacflags []string
@ -1824,7 +1819,7 @@ func (al *ApiLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
var scopeOrderedSourceFileNames = allApiScopes.Strings(
func(s *apiScope) string { return s.apiFilePrefix + "current.txt" })
func (al *ApiLibrary) sortApiFilesByApiScope(ctx android.ModuleContext, srcFilesInfo []JavaApiImportInfo, apiFiles android.Paths) android.Paths {
func (al *ApiLibrary) sortApiFilesByApiScope(ctx android.ModuleContext, srcFilesInfo []JavaApiImportInfo) android.Paths {
var sortedSrcFiles android.Paths
for i, apiScope := range allApiScopes {
@ -1833,20 +1828,14 @@ func (al *ApiLibrary) sortApiFilesByApiScope(ctx android.ModuleContext, srcFiles
sortedSrcFiles = append(sortedSrcFiles, android.PathForSource(ctx, srcFileInfo.ApiFile.String()))
}
}
// TODO: b/300964421 - Remove when api_files property is removed
for _, apiFileName := range apiFiles {
if apiFileName.Base() == scopeOrderedSourceFileNames[i] {
sortedSrcFiles = append(sortedSrcFiles, apiFileName)
}
}
}
if len(srcFilesInfo)+len(apiFiles) != len(sortedSrcFiles) {
if len(srcFilesInfo) != len(sortedSrcFiles) {
var srcFiles android.Paths
for _, srcFileInfo := range srcFilesInfo {
srcFiles = append(srcFiles, srcFileInfo.ApiFile)
}
ctx.ModuleErrorf("Unrecognizable source file found within %s", append(srcFiles, apiFiles...))
ctx.ModuleErrorf("Unrecognizable source file found within %s", srcFiles)
}
return sortedSrcFiles
@ -1892,15 +1881,7 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
}
})
// Add the api_files inputs
// These are api files in the module subdirectory, which are not provided by
// java_api_contribution but provided directly as module property.
var apiFiles android.Paths
for _, api := range al.properties.Api_files {
apiFiles = append(apiFiles, android.PathForModuleSrc(ctx, api))
}
srcFiles := al.sortApiFilesByApiScope(ctx, srcFilesInfo, apiFiles)
srcFiles := al.sortApiFilesByApiScope(ctx, srcFilesInfo)
if srcFiles == nil && !ctx.Config().AllowMissingDependencies() {
ctx.ModuleErrorf("Error: %s has an empty api file.", ctx.ModuleName())

View file

@ -1885,7 +1885,6 @@ func TestJavaApiLibraryAndProviderLink(t *testing.T) {
name: "bar2",
api_surface: "system",
api_contributions: ["foo1", "foo2"],
api_files: ["api1/current.txt", "api2/current.txt"]
}
`,
map[string][]byte{
@ -1903,7 +1902,7 @@ func TestJavaApiLibraryAndProviderLink(t *testing.T) {
},
{
moduleName: "bar2",
sourceTextFileDirs: []string{"a/current.txt", "b/current.txt", "api1/current.txt", "api2/current.txt"},
sourceTextFileDirs: []string{"a/current.txt", "b/current.txt"},
},
}
for _, c := range testcases {
@ -1975,7 +1974,6 @@ func TestJavaApiLibraryAndDefaultsLink(t *testing.T) {
api_surface: "system",
defaults:["baz1", "baz2"],
api_contributions: ["foo4"],
api_files: ["api1/current.txt", "api2/current.txt"]
}
`,
map[string][]byte{
@ -2000,7 +1998,7 @@ func TestJavaApiLibraryAndDefaultsLink(t *testing.T) {
{
moduleName: "bar3",
// API text files need to be sorted from the narrower api scope to the wider api scope
sourceTextFileDirs: []string{"a/current.txt", "b/current.txt", "api1/current.txt", "api2/current.txt", "c/system-current.txt", "d/system-current.txt"},
sourceTextFileDirs: []string{"a/current.txt", "b/current.txt", "c/system-current.txt", "d/system-current.txt"},
},
}
for _, c := range testcases {
@ -2265,29 +2263,6 @@ func TestJavaApiLibraryFullApiSurfaceStub(t *testing.T) {
android.AssertStringDoesContain(t, "Command expected to contain full_api_surface_stub output jar", manifestCommand, "lib1.jar")
}
func TestJavaApiLibraryFilegroupInput(t *testing.T) {
ctx, _ := testJavaWithFS(t, `
filegroup {
name: "default_current.txt",
srcs: ["current.txt"],
}
java_api_library {
name: "foo",
api_files: [":default_current.txt"],
}
`,
map[string][]byte{
"current.txt": nil,
})
m := ctx.ModuleForTests("foo", "android_common")
outputs := fmt.Sprint(m.AllOutputs())
if !strings.Contains(outputs, "foo/foo.jar") {
t.Errorf("Module output does not contain expected jar %s", "foo/foo.jar")
}
}
func TestTradefedOptions(t *testing.T) {
result := PrepareForTestWithJavaBuildComponents.RunTestWithBp(t, `
java_test_host {

View file

@ -424,30 +424,80 @@ func gatherRequiredDepsForTest() string {
`, extra)
}
extraApiLibraryModules := map[string]string{
"android_stubs_current.from-text": "api/current.txt",
"android_system_stubs_current.from-text": "api/system-current.txt",
"android_test_stubs_current.from-text": "api/test-current.txt",
"android_module_lib_stubs_current.from-text": "api/module-lib-current.txt",
"android_module_lib_stubs_current_full.from-text": "api/module-lib-current.txt",
"android_system_server_stubs_current.from-text": "api/system-server-current.txt",
"core.current.stubs.from-text": "api/current.txt",
"legacy.core.platform.api.stubs.from-text": "api/current.txt",
"stable.core.platform.api.stubs.from-text": "api/current.txt",
"core-lambda-stubs.from-text": "api/current.txt",
"android-non-updatable.stubs.from-text": "api/current.txt",
"android-non-updatable.stubs.system.from-text": "api/system-current.txt",
"android-non-updatable.stubs.test.from-text": "api/test-current.txt",
"android-non-updatable.stubs.module_lib.from-text": "api/module-lib-current.txt",
type apiContributionStruct struct {
name string
apiSurface string
apiFile string
}
for libName, apiFile := range extraApiLibraryModules {
var publicApiContribution = apiContributionStruct{
name: "api-stubs-docs-non-updatable.api.contribution",
apiSurface: "public",
apiFile: "api/current.txt",
}
var systemApiContribution = apiContributionStruct{
name: "system-api-stubs-docs-non-updatable.api.contribution",
apiSurface: "system",
apiFile: "api/system-current.txt",
}
var testApiContribution = apiContributionStruct{
name: "test-api-stubs-docs-non-updatable.api.contribution",
apiSurface: "test",
apiFile: "api/test-current.txt",
}
var moduleLibApiContribution = apiContributionStruct{
name: "module-lib-api-stubs-docs-non-updatable.api.contribution",
apiSurface: "module-lib",
apiFile: "api/module-lib-current.txt",
}
var systemServerApiContribution = apiContributionStruct{
// This module does not exist but is named this way for consistency
name: "system-server-api-stubs-docs-non-updatable.api.contribution",
apiSurface: "system-server",
apiFile: "api/system-server-current.txt",
}
var apiContributionStructs = []apiContributionStruct{
publicApiContribution,
systemApiContribution,
testApiContribution,
moduleLibApiContribution,
systemServerApiContribution,
}
extraApiLibraryModules := map[string]apiContributionStruct{
"android_stubs_current.from-text": publicApiContribution,
"android_system_stubs_current.from-text": systemApiContribution,
"android_test_stubs_current.from-text": testApiContribution,
"android_module_lib_stubs_current.from-text": moduleLibApiContribution,
"android_module_lib_stubs_current_full.from-text": moduleLibApiContribution,
"android_system_server_stubs_current.from-text": systemServerApiContribution,
"core.current.stubs.from-text": publicApiContribution,
"legacy.core.platform.api.stubs.from-text": publicApiContribution,
"stable.core.platform.api.stubs.from-text": publicApiContribution,
"core-lambda-stubs.from-text": publicApiContribution,
"android-non-updatable.stubs.from-text": publicApiContribution,
"android-non-updatable.stubs.system.from-text": systemApiContribution,
"android-non-updatable.stubs.test.from-text": testApiContribution,
"android-non-updatable.stubs.module_lib.from-text": moduleLibApiContribution,
}
for _, apiContribution := range apiContributionStructs {
bp += fmt.Sprintf(`
java_api_contribution {
name: "%s",
api_surface: "%s",
api_file: "%s",
}
`, apiContribution.name, apiContribution.apiSurface, apiContribution.apiFile)
}
for libName, apiContribution := range extraApiLibraryModules {
bp += fmt.Sprintf(`
java_api_library {
name: "%s",
api_files: ["%s"],
api_contributions: ["%s"],
}
`, libName, apiFile)
`, libName, apiContribution.name)
}
bp += `