Merge changes I697a65e4,Iaac6aaf6

* changes:
  Do not propagate <uses-library> deps through static SDK component libs.
  Make error message more precise.
This commit is contained in:
Ulyana Trafimovich 2020-12-07 10:01:40 +00:00 committed by Gerrit Code Review
commit 678ddb9d24
5 changed files with 57 additions and 17 deletions

View File

@ -437,7 +437,11 @@ func validateClassLoaderContextRec(sdkVer int, clcs []*ClassLoaderContext) (bool
if sdkVer == AnySdkVersion { if sdkVer == AnySdkVersion {
// Return error if dexpreopt doesn't know paths to one of the <uses-library> // Return error if dexpreopt doesn't know paths to one of the <uses-library>
// dependencies. In the future we may need to relax this and just disable dexpreopt. // dependencies. In the future we may need to relax this and just disable dexpreopt.
return false, fmt.Errorf("invalid path for <uses-library> \"%s\"", clc.Name) if clc.Host == nil {
return false, fmt.Errorf("invalid build path for <uses-library> \"%s\"", clc.Name)
} else {
return false, fmt.Errorf("invalid install path for <uses-library> \"%s\"", clc.Name)
}
} else { } else {
// No error for compatibility libraries, as Soong doesn't know if they are needed // No error for compatibility libraries, as Soong doesn't know if they are needed
// (this depends on the targetSdkVersion in the manifest), but the CLC is invalid. // (this depends on the targetSdkVersion in the manifest), but the CLC is invalid.

View File

@ -195,7 +195,7 @@ func TestCLCMaybeAdd(t *testing.T) {
// But class loader context in such cases should raise an error on validation. // But class loader context in such cases should raise an error on validation.
t.Run("validate", func(t *testing.T) { t.Run("validate", func(t *testing.T) {
_, err := validateClassLoaderContext(m) _, err := validateClassLoaderContext(m)
checkError(t, err, "invalid path for <uses-library> \"a\"") checkError(t, err, "invalid build path for <uses-library> \"a\"")
}) })
} }

View File

@ -407,6 +407,7 @@ func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext, classLoaderConte
ctx.VisitDirectDeps(func(module android.Module) { ctx.VisitDirectDeps(func(module android.Module) {
depName := ctx.OtherModuleName(module) depName := ctx.OtherModuleName(module)
depTag := ctx.OtherModuleDependencyTag(module)
var exportPackage android.Path var exportPackage android.Path
aarDep, _ := module.(AndroidLibraryDependency) aarDep, _ := module.(AndroidLibraryDependency)
@ -414,7 +415,7 @@ func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext, classLoaderConte
exportPackage = aarDep.ExportPackage() exportPackage = aarDep.ExportPackage()
} }
switch ctx.OtherModuleDependencyTag(module) { switch depTag {
case instrumentationForTag: case instrumentationForTag:
// Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2. // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
case libTag: case libTag:
@ -439,7 +440,6 @@ func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext, classLoaderConte
transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...) transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
transitiveStaticLibs = append(transitiveStaticLibs, exportPackage) transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...) transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
classLoaderContexts.AddContextMap(aarDep.ClassLoaderContexts(), depName)
if aarDep.ExportedAssets().Valid() { if aarDep.ExportedAssets().Valid() {
assets = append(assets, aarDep.ExportedAssets().Path()) assets = append(assets, aarDep.ExportedAssets().Path())
} }
@ -458,11 +458,8 @@ func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext, classLoaderConte
} }
} }
// Add nested dependencies after processing the direct dependency: if it is a <uses-library>, // Merge dep's CLC after processing the dep itself (which may add its own <uses-library>).
// nested context is added as its subcontext, and should not be re-added at the top-level. maybeAddCLCFromDep(module, depTag, depName, classLoaderContexts)
if dep, ok := module.(Dependency); ok {
classLoaderContexts.AddContextMap(dep.ClassLoaderContexts(), depName)
}
}) })
deps = append(deps, sharedLibs...) deps = append(deps, sharedLibs...)

View File

@ -2729,6 +2729,13 @@ func TestUsesLibraries(t *testing.T) {
sdk_version: "current", sdk_version: "current",
} }
java_sdk_library {
name: "fred",
srcs: ["a.java"],
api_packages: ["fred"],
sdk_version: "current",
}
java_sdk_library { java_sdk_library {
name: "bar", name: "bar",
srcs: ["a.java"], srcs: ["a.java"],
@ -2753,7 +2760,12 @@ func TestUsesLibraries(t *testing.T) {
name: "app", name: "app",
srcs: ["a.java"], srcs: ["a.java"],
libs: ["qux", "quuz.stubs"], libs: ["qux", "quuz.stubs"],
static_libs: ["static-runtime-helper"], static_libs: [
"static-runtime-helper",
// statically linked component libraries should not pull their SDK libraries,
// so "fred" should not be added to class loader context
"fred.stubs",
],
uses_libs: ["foo"], uses_libs: ["foo"],
sdk_version: "current", sdk_version: "current",
optional_uses_libs: [ optional_uses_libs: [

View File

@ -1081,7 +1081,6 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
switch tag { switch tag {
case libTag: case libTag:
deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...) deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
// names of sdk libs that are directly depended are exported
j.classLoaderContexts.MaybeAddContext(ctx, dep.OptionalImplicitSdkLibrary(), j.classLoaderContexts.MaybeAddContext(ctx, dep.OptionalImplicitSdkLibrary(),
dep.DexJarBuildPath(), dep.DexJarInstallPath()) dep.DexJarBuildPath(), dep.DexJarInstallPath())
case staticLibTag: case staticLibTag:
@ -1093,7 +1092,6 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...) deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
case libTag, instrumentationForTag: case libTag, instrumentationForTag:
deps.classpath = append(deps.classpath, dep.HeaderJars()...) deps.classpath = append(deps.classpath, dep.HeaderJars()...)
// sdk lib names from dependencies are re-exported
j.classLoaderContexts.AddContextMap(dep.ClassLoaderContexts(), otherName) j.classLoaderContexts.AddContextMap(dep.ClassLoaderContexts(), otherName)
deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...) deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
pluginJars, pluginClasses, disableTurbine := dep.ExportedPlugins() pluginJars, pluginClasses, disableTurbine := dep.ExportedPlugins()
@ -1106,8 +1104,6 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
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()...)
deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...) deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
// sdk lib names from dependencies are re-exported
j.classLoaderContexts.AddContextMap(dep.ClassLoaderContexts(), otherName)
deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...) deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
pluginJars, pluginClasses, disableTurbine := dep.ExportedPlugins() pluginJars, pluginClasses, disableTurbine := dep.ExportedPlugins()
addPlugins(&deps, pluginJars, pluginClasses...) addPlugins(&deps, pluginJars, pluginClasses...)
@ -1182,6 +1178,9 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
deps.systemModules = &systemModules{outputDir, outputDeps} deps.systemModules = &systemModules{outputDir, outputDeps}
} }
} }
// Merge dep's CLC after processing the dep itself (which may add its own <uses-library>).
maybeAddCLCFromDep(module, tag, otherName, j.classLoaderContexts)
}) })
return deps return deps
@ -2815,8 +2814,6 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
switch tag { switch tag {
case libTag, staticLibTag: case libTag, staticLibTag:
flags.classpath = append(flags.classpath, dep.HeaderJars()...) flags.classpath = append(flags.classpath, dep.HeaderJars()...)
// sdk lib names from dependencies are re-exported
j.classLoaderContexts.AddContextMap(dep.ClassLoaderContexts(), otherName)
case bootClasspathTag: case bootClasspathTag:
flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars()...) flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars()...)
} }
@ -2824,10 +2821,12 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
switch tag { switch tag {
case libTag: case libTag:
flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...) flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
// names of sdk libs that are directly depended are exported
j.classLoaderContexts.AddContext(ctx, otherName, dep.DexJarBuildPath(), dep.DexJarInstallPath()) j.classLoaderContexts.AddContext(ctx, otherName, dep.DexJarBuildPath(), dep.DexJarInstallPath())
} }
} }
// Merge dep's CLC after processing the dep itself (which may add its own <uses-library>).
maybeAddCLCFromDep(module, tag, otherName, j.classLoaderContexts)
}) })
var installFile android.Path var installFile android.Path
@ -3248,3 +3247,31 @@ var Bool = proptools.Bool
var BoolDefault = proptools.BoolDefault var BoolDefault = proptools.BoolDefault
var String = proptools.String var String = proptools.String
var inList = android.InList var inList = android.InList
// Add class loader context of a given dependency to the given class loader context, provided that
// all the necessary conditions are met.
func maybeAddCLCFromDep(depModule android.Module, depTag blueprint.DependencyTag,
depName string, clcMap dexpreopt.ClassLoaderContextMap) {
if dep, ok := depModule.(Dependency); ok {
if depTag == libTag {
// Ok, propagate <uses-library> through non-static library dependencies.
} else if depTag == staticLibTag {
// Propagate <uses-library> through static library dependencies, unless it is a
// component library (such as stubs). Component libraries have a dependency on their
// SDK library, which should not be pulled just because of a static component library.
if comp, isComp := depModule.(SdkLibraryComponentDependency); isComp {
if compName := comp.OptionalImplicitSdkLibrary(); compName != nil {
dep = nil
}
}
} else {
// Don't propagate <uses-library> for other dependency tags.
dep = nil
}
if dep != nil {
clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
}
}
}