rust: Add whole_static_libs, revert static_lib

Revert the static_lib behavior to the previous behavior (pass static
libs to the linker rather than via to rustc using `-lstatic=<lib>`). To
bundle static libraries into libraries, provide the whole_static_libs
property which retains the current static_libs behavior.

Passing all static libraries via -lstatic was resulting in odd bloat
where in some cases static symbols were duplicated in binaries and
libraries. This split makes it possible to be explicit about when static
libraries should be bundled.

Bug: 183182230
Test: mma system/bt; mma system/security/keystore2; mma external/rust

Change-Id: Ic2dde5d1542dca5ce145aa3a3fbd9ea54440d991
This commit is contained in:
Ivan Lozano
2021-03-23 15:53:44 -04:00
parent 6400f20430
commit 63bb7680c9
4 changed files with 55 additions and 27 deletions

View File

@@ -269,14 +269,15 @@ func (mod *Module) SplitPerApiLevel() bool {
}
type Deps struct {
Dylibs []string
Rlibs []string
Rustlibs []string
Stdlibs []string
ProcMacros []string
SharedLibs []string
StaticLibs []string
HeaderLibs []string
Dylibs []string
Rlibs []string
Rustlibs []string
Stdlibs []string
ProcMacros []string
SharedLibs []string
StaticLibs []string
WholeStaticLibs []string
HeaderLibs []string
CrtBegin, CrtEnd string
}
@@ -751,7 +752,7 @@ func (mod *Module) deps(ctx DepsContext) Deps {
deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
return deps
}
@@ -911,16 +912,13 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
exportDep := false
switch {
case cc.IsStaticDepTag(depTag):
// Only pass -lstatic for rlibs as it results in dylib bloat.
if lib, ok := ctx.Module().(*Module).compiler.(libraryInterface); ok && lib.rlib() {
// Link cc static libraries using "-lstatic" so rustc can reason about how to handle these
// (for example, bundling them into rlibs).
//
// rustc does not support linking libraries with the "-l" flag unless they are prefixed by "lib".
// If we need to link a library that isn't prefixed by "lib", we'll just link to it directly through
// linkObjects; such a library may need to be redeclared by static dependents.
if cc.IsWholeStaticLib(depTag) {
// rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
// if the library is not prefixed by "lib".
if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
} else {
ctx.ModuleErrorf("'%q' cannot be listed as a whole_static_library in Rust modules unless the output is prefixed by 'lib'", depName, ctx.ModuleName())
}
}
@@ -1099,7 +1097,10 @@ func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
cc.SharedDepTag(), deps.SharedLibs...)
actx.AddVariationDependencies(append(commonDepVariations,
blueprint.Variation{Mutator: "link", Variation: "static"}),
cc.StaticDepTag(), deps.StaticLibs...)
cc.StaticDepTag(false), deps.StaticLibs...)
actx.AddVariationDependencies(append(commonDepVariations,
blueprint.Variation{Mutator: "link", Variation: "static"}),
cc.StaticDepTag(true), deps.WholeStaticLibs...)
actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)