Export SDK library names

java_library, java_import, and android_library export SDK library names
that they are using directly or indirectly via its dependencies. When
building an apk, the manifest fixer uses the SDK lib names to
automatically add <uses-library> tags.

The SDK lib names are exported to the make world via
LOCAL_EXPORT_SDK_LIBRARIES flag.

Bug: 77575606
Test: m -j
Change-Id: I4fe606eb7ed23843c58eebe6a324405fe1da34e5
This commit is contained in:
Jiyong Park
2018-05-28 18:02:19 +09:00
parent c08f46fdfc
commit 1be9691910
5 changed files with 72 additions and 3 deletions

View File

@@ -475,6 +475,10 @@ func (a *AARImport) AidlIncludeDirs() android.Paths {
return nil return nil
} }
func (a *AARImport) ExportedSdkLibs() []string {
return nil
}
var _ android.PrebuiltInterface = (*Import)(nil) var _ android.PrebuiltInterface = (*Import)(nil)
func AARImportFactory() android.Module { func AARImportFactory() android.Module {

View File

@@ -63,6 +63,10 @@ func (library *Library) AndroidMk() android.AndroidMkData {
fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", library.jacocoReportClassesFile.String()) fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", library.jacocoReportClassesFile.String())
} }
if len(library.exportedSdkLibs) != 0 {
fmt.Fprintln(w, "LOCAL_EXPORT_SDK_LIBRARIES :=", strings.Join(library.exportedSdkLibs, " "))
}
// Temporary hack: export sources used to compile framework.jar to Make // Temporary hack: export sources used to compile framework.jar to Make
// to be used for droiddoc // to be used for droiddoc
// TODO(ccross): remove this once droiddoc is in soong // TODO(ccross): remove this once droiddoc is in soong

View File

@@ -281,6 +281,9 @@ type Module struct {
// list of extra progurad flag files // list of extra progurad flag files
extraProguardFlagFiles android.Paths extraProguardFlagFiles android.Paths
// list of SDK lib names that this java moudule is exporting
exportedSdkLibs []string
} }
func (j *Module) Srcs() android.Paths { func (j *Module) Srcs() android.Paths {
@@ -293,6 +296,7 @@ type Dependency interface {
HeaderJars() android.Paths HeaderJars() android.Paths
ImplementationJars() android.Paths ImplementationJars() android.Paths
AidlIncludeDirs() android.Paths AidlIncludeDirs() android.Paths
ExportedSdkLibs() []string
} }
type SdkLibraryDependency interface { type SdkLibraryDependency interface {
@@ -714,10 +718,14 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...) deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
case libTag: case libTag:
deps.classpath = append(deps.classpath, dep.HeaderJars()...) deps.classpath = append(deps.classpath, dep.HeaderJars()...)
// sdk lib names from dependencies are re-exported
j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
case staticLibTag: case staticLibTag:
deps.classpath = append(deps.classpath, dep.HeaderJars()...) deps.classpath = append(deps.classpath, dep.HeaderJars()...)
deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...) deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...) deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
// sdk lib names from dependencies are re-exported
j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
case frameworkResTag: case frameworkResTag:
if ctx.ModuleName() == "framework" { if ctx.ModuleName() == "framework" {
// framework.jar has a one-off dependency on the R.java and Manifest.java files // framework.jar has a one-off dependency on the R.java and Manifest.java files
@@ -748,6 +756,8 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
switch tag { switch tag {
case libTag: case libTag:
deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...) deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
// names of sdk libs that are directly depended are exported
j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
default: default:
ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName) ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
} }
@@ -785,6 +795,8 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
} }
}) })
j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
return deps return deps
} }
@@ -1197,6 +1209,10 @@ func (j *Module) AidlIncludeDirs() android.Paths {
return j.exportAidlIncludeDirs return j.exportAidlIncludeDirs
} }
func (j *Module) ExportedSdkLibs() []string {
return j.exportedSdkLibs
}
var _ logtagsProducer = (*Module)(nil) var _ logtagsProducer = (*Module)(nil)
func (j *Module) logtags() android.Paths { func (j *Module) logtags() android.Paths {
@@ -1398,6 +1414,9 @@ type ImportProperties struct {
Sdk_version *string Sdk_version *string
Installable *bool Installable *bool
// List of shared java libs that this module has dependencies to
Libs []string
} }
type Import struct { type Import struct {
@@ -1408,6 +1427,7 @@ type Import struct {
classpathFiles android.Paths classpathFiles android.Paths
combinedClasspathFile android.Path combinedClasspathFile android.Path
exportedSdkLibs []string
} }
func (j *Import) Prebuilt() *android.Prebuilt { func (j *Import) Prebuilt() *android.Prebuilt {
@@ -1423,6 +1443,7 @@ func (j *Import) Name() string {
} }
func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) { func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
} }
func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
@@ -1431,6 +1452,28 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
outputFile := android.PathForModuleOut(ctx, "classes.jar") outputFile := android.PathForModuleOut(ctx, "classes.jar")
TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil) TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
j.combinedClasspathFile = outputFile j.combinedClasspathFile = outputFile
ctx.VisitDirectDeps(func(module android.Module) {
otherName := ctx.OtherModuleName(module)
tag := ctx.OtherModuleDependencyTag(module)
switch dep := module.(type) {
case Dependency:
switch tag {
case libTag, staticLibTag:
// sdk lib names from dependencies are re-exported
j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
}
case SdkLibraryDependency:
switch tag {
case libTag:
// names of sdk libs that are directly depended are exported
j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
}
}
})
j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
} }
var _ Dependency = (*Import)(nil) var _ Dependency = (*Import)(nil)
@@ -1447,6 +1490,10 @@ func (j *Import) AidlIncludeDirs() android.Paths {
return nil return nil
} }
func (j *Import) ExportedSdkLibs() []string {
return j.exportedSdkLibs
}
var _ android.PrebuiltInterface = (*Import)(nil) var _ android.PrebuiltInterface = (*Import)(nil)
func ImportFactory() android.Module { func ImportFactory() android.Module {

View File

@@ -1095,6 +1095,12 @@ func TestJavaSdkLibrary(t *testing.T) {
libs: ["foo", "bar"], libs: ["foo", "bar"],
sdk_version: "system_current", sdk_version: "system_current",
} }
java_library {
name: "qux",
srcs: ["c.java"],
libs: ["baz"],
sdk_version: "system_current",
}
`) `)
// check the existence of the internal modules // check the existence of the internal modules
@@ -1127,4 +1133,13 @@ func TestJavaSdkLibrary(t *testing.T) {
t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
"foo.stubs.jar") "foo.stubs.jar")
} }
// test if baz has exported SDK lib names foo and bar to qux
qux := ctx.ModuleForTests("qux", "android_common")
if quxLib, ok := qux.Module().(*Library); ok {
sdkLibs := quxLib.ExportedSdkLibs()
if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
}
}
} }

View File

@@ -67,9 +67,8 @@ var (
// classpath at runtime if requested via <uses-library>. // classpath at runtime if requested via <uses-library>.
// //
// TODO: these are big features that are currently missing // TODO: these are big features that are currently missing
// 1) ensuring that apps have appropriate <uses-library> tag // 1) disallowing linking to the runtime shared lib
// 2) disallowing linking to the runtime shared lib // 2) HTML generation
// 3) HTML generation
func init() { func init() {
android.RegisterModuleType("java_sdk_library", sdkLibraryFactory) android.RegisterModuleType("java_sdk_library", sdkLibraryFactory)