Add libs property to java_api_library module

Module Lib API surface takes modules that are not part of the API
surface as classpath during compilation of the JAR file. Thus, add libs
property to add deps in bp module definition.

Test: m
Change-Id: I8967d53430fa4cc8e4aa431770ba4e97893f8c61
This commit is contained in:
Jihoon Kang
2023-01-20 19:44:07 +00:00
parent d623192eea
commit 362aa9db12
2 changed files with 107 additions and 9 deletions

View File

@@ -1618,6 +1618,10 @@ type JavaApiLibraryProperties struct {
// List of flags to be passed to the javac compiler to generate jar file // List of flags to be passed to the javac compiler to generate jar file
Javacflags []string Javacflags []string
// List of shared java libs that this module has dependencies to and
// should be passed as classpath in javac invocation
Libs []string
} }
func ApiLibraryFactory() android.Module { func ApiLibraryFactory() android.Module {
@@ -1689,6 +1693,7 @@ func (al *ApiLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
for _, apiContributionName := range apiContributions { for _, apiContributionName := range apiContributions {
ctx.AddDependency(ctx.Module(), javaApiContributionTag, apiContributionName) ctx.AddDependency(ctx.Module(), javaApiContributionTag, apiContributionName)
} }
ctx.AddVariationDependencies(nil, libTag, al.properties.Libs...)
} }
func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
@@ -1706,10 +1711,18 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
homeDir := android.PathForModuleOut(ctx, "metalava", "home") homeDir := android.PathForModuleOut(ctx, "metalava", "home")
var srcFiles []android.Path var srcFiles android.Paths
ctx.VisitDirectDepsWithTag(javaApiContributionTag, func(dep android.Module) { var classPaths android.Paths
provider := ctx.OtherModuleProvider(dep, JavaApiImportProvider).(JavaApiImportInfo) ctx.VisitDirectDeps(func(dep android.Module) {
srcFiles = append(srcFiles, android.PathForSource(ctx, provider.ApiFile.String())) tag := ctx.OtherModuleDependencyTag(dep)
switch tag {
case javaApiContributionTag:
provider := ctx.OtherModuleProvider(dep, JavaApiImportProvider).(JavaApiImportInfo)
srcFiles = append(srcFiles, android.PathForSource(ctx, provider.ApiFile.String()))
case libTag:
provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
classPaths = append(classPaths, provider.HeaderJars...)
}
}) })
// Add the api_files inputs // Add the api_files inputs
@@ -1739,11 +1752,16 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
var flags javaBuilderFlags var flags javaBuilderFlags
flags.javaVersion = getStubsJavaVersion() flags.javaVersion = getStubsJavaVersion()
flags.javacFlags = strings.Join(al.properties.Javacflags, " ") flags.javacFlags = strings.Join(al.properties.Javacflags, " ")
flags.classpath = classpath(classPaths)
TransformJavaToClasses(ctx, al.stubsJar, 0, android.Paths{}, TransformJavaToClasses(ctx, al.stubsJar, 0, android.Paths{},
android.Paths{al.stubsSrcJar}, flags, android.Paths{}) android.Paths{al.stubsSrcJar}, flags, android.Paths{})
ctx.Phony(ctx.ModuleName(), al.stubsJar) ctx.Phony(ctx.ModuleName(), al.stubsJar)
ctx.SetProvider(JavaInfoProvider, JavaInfo{
HeaderJars: android.PathsIfNonNil(al.stubsJar),
})
} }
// //

View File

@@ -1895,22 +1895,26 @@ func TestJavaApiLibraryAndProviderLink(t *testing.T) {
} }
func TestJavaApiLibraryAndDefaultsLink(t *testing.T) { func TestJavaApiLibraryAndDefaultsLink(t *testing.T) {
provider_bp_a := `java_api_contribution { provider_bp_a := `
java_api_contribution {
name: "foo1", name: "foo1",
api_file: "foo1.txt", api_file: "foo1.txt",
} }
` `
provider_bp_b := `java_api_contribution { provider_bp_b := `
java_api_contribution {
name: "foo2", name: "foo2",
api_file: "foo2.txt", api_file: "foo2.txt",
} }
` `
provider_bp_c := `java_api_contribution { provider_bp_c := `
java_api_contribution {
name: "foo3", name: "foo3",
api_file: "foo3.txt", api_file: "foo3.txt",
} }
` `
provider_bp_d := `java_api_contribution { provider_bp_d := `
java_api_contribution {
name: "foo4", name: "foo4",
api_file: "foo4.txt", api_file: "foo4.txt",
} }
@@ -1989,7 +1993,8 @@ func TestJavaApiLibraryJarGeneration(t *testing.T) {
api_file: "foo1.txt", api_file: "foo1.txt",
} }
` `
provider_bp_b := `java_api_contribution { provider_bp_b := `
java_api_contribution {
name: "foo2", name: "foo2",
api_file: "foo2.txt", api_file: "foo2.txt",
} }
@@ -2034,6 +2039,81 @@ func TestJavaApiLibraryJarGeneration(t *testing.T) {
} }
} }
func TestJavaApiLibraryLibsLink(t *testing.T) {
provider_bp_a := `
java_api_contribution {
name: "foo1",
api_file: "foo1.txt",
}
`
provider_bp_b := `
java_api_contribution {
name: "foo2",
api_file: "foo2.txt",
}
`
lib_bp_a := `
java_library {
name: "lib1",
srcs: ["Lib.java"],
}
`
lib_bp_b := `
java_library {
name: "lib2",
srcs: ["Lib.java"],
}
`
ctx, _ := testJavaWithFS(t, `
java_api_library {
name: "bar1",
api_surface: "public",
api_contributions: ["foo1"],
libs: ["lib1"],
}
java_api_library {
name: "bar2",
api_surface: "system",
api_contributions: ["foo1", "foo2"],
libs: ["lib1", "lib2", "bar1"],
}
`,
map[string][]byte{
"a/Android.bp": []byte(provider_bp_a),
"b/Android.bp": []byte(provider_bp_b),
"c/Android.bp": []byte(lib_bp_a),
"c/Lib.java": {},
"d/Android.bp": []byte(lib_bp_b),
"d/Lib.java": {},
})
testcases := []struct {
moduleName string
classPathJarNames []string
}{
{
moduleName: "bar1",
classPathJarNames: []string{"lib1.jar"},
},
{
moduleName: "bar2",
classPathJarNames: []string{"lib1.jar", "lib2.jar", "bar1/android.jar"},
},
}
for _, c := range testcases {
m := ctx.ModuleForTests(c.moduleName, "android_common")
javacRules := m.Rule("javac")
classPathArgs := javacRules.Args["classpath"]
for _, jarName := range c.classPathJarNames {
if !strings.Contains(classPathArgs, jarName) {
t.Errorf("Module output does not contain expected jar %s", jarName)
}
}
}
}
func TestTradefedOptions(t *testing.T) { func TestTradefedOptions(t *testing.T) {
result := PrepareForTestWithJavaBuildComponents.RunTestWithBp(t, ` result := PrepareForTestWithJavaBuildComponents.RunTestWithBp(t, `
java_test_host { java_test_host {