Move prefer_rlib from binary to base compiler.

Moves the prefer_rlib property out from being exclusively a binary
property to one thats part of the base compiler properties. This
provides a little more control over the libstd linkage in our libraries.
Specifically, this enables a usecase where rust_ffi_shared needs to link
against libstd statically rather than dynamically.

Bug: 175121262
Test: New Soong tests pass.
Change-Id: If68014c684a75ba70e9d7dacbb01c7d360dc25a1
This commit is contained in:
Ivan Lozano 2020-12-08 14:43:00 -05:00
parent 110d13bef3
commit ea08613dd3
4 changed files with 46 additions and 18 deletions

View File

@ -24,11 +24,6 @@ func init() {
} }
type BinaryCompilerProperties struct { type BinaryCompilerProperties struct {
// Change the rustlibs linkage to select rlib linkage by default for device targets.
// Also link libstd as an rlib as well on device targets.
// Note: This is the default behavior for host targets.
Prefer_rlib *bool `android:"arch_variant"`
// Builds this binary as a static binary. Implies prefer_rlib true. // Builds this binary as a static binary. Implies prefer_rlib true.
// //
// Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static // Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static
@ -115,7 +110,7 @@ func (binary *binaryDecorator) nativeCoverage() bool {
} }
func (binary *binaryDecorator) preferRlib() bool { func (binary *binaryDecorator) preferRlib() bool {
return Bool(binary.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable) return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
} }
func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
@ -156,8 +151,7 @@ func (binary *binaryDecorator) autoDep(ctx BaseModuleContext) autoDep {
// Binaries default to dylib dependencies for device, rlib for host. // Binaries default to dylib dependencies for device, rlib for host.
if binary.preferRlib() { if binary.preferRlib() {
return rlibAutoDep return rlibAutoDep
} } else if ctx.Device() {
if ctx.Device() {
return dylibAutoDep return dylibAutoDep
} else { } else {
return rlibAutoDep return rlibAutoDep

View File

@ -122,6 +122,17 @@ type BaseCompilerProperties struct {
// whether to suppress inclusion of standard crates - defaults to false // whether to suppress inclusion of standard crates - defaults to false
No_stdlibs *bool No_stdlibs *bool
// Change the rustlibs linkage to select rlib linkage by default for device targets.
// Also link libstd as an rlib as well on device targets.
// Note: This is the default behavior for host targets.
//
// This is primarily meant for rust_binary and rust_ffi modules where the default
// linkage of libstd might need to be overridden in some use cases. This should
// generally be avoided with other module types since it may cause collisions at
// linkage if all dependencies of the root binary module do not link against libstd\
// the same way.
Prefer_rlib *bool `android:"arch_variant"`
} }
type baseCompiler struct { type baseCompiler struct {
@ -154,9 +165,15 @@ func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
panic("baseCompiler does not implement coverageOutputZipPath()") panic("baseCompiler does not implement coverageOutputZipPath()")
} }
func (compiler *baseCompiler) preferRlib() bool {
return Bool(compiler.Properties.Prefer_rlib)
}
func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage { func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
// For devices, we always link stdlibs in as dylibs by default. // For devices, we always link stdlibs in as dylibs by default.
if ctx.Device() { if compiler.preferRlib() {
return RlibLinkage
} else if ctx.Device() {
return DylibLinkage return DylibLinkage
} else { } else {
return RlibLinkage return RlibLinkage

View File

@ -159,14 +159,6 @@ func (library *libraryDecorator) static() bool {
return library.MutatedProperties.VariantIsStatic return library.MutatedProperties.VariantIsStatic
} }
func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
// libraries should only request the RlibLinkage when building a static FFI or when variant is StaticStd
if library.static() || library.MutatedProperties.VariantIsStaticStd {
return RlibLinkage
}
return DefaultLinkage
}
func (library *libraryDecorator) source() bool { func (library *libraryDecorator) source() bool {
return library.MutatedProperties.VariantIsSource return library.MutatedProperties.VariantIsSource
} }
@ -228,7 +220,9 @@ func (library *libraryDecorator) setSource() {
} }
func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep { func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep {
if library.rlib() || library.static() { if library.preferRlib() {
return rlibAutoDep
} else if library.rlib() || library.static() {
return rlibAutoDep return rlibAutoDep
} else if library.dylib() || library.shared() { } else if library.dylib() || library.shared() {
return dylibAutoDep return dylibAutoDep
@ -237,6 +231,15 @@ func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep {
} }
} }
func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
if library.static() || library.MutatedProperties.VariantIsStaticStd {
return RlibLinkage
} else if library.baseCompiler.preferRlib() {
return RlibLinkage
}
return DefaultLinkage
}
var _ compiler = (*libraryDecorator)(nil) var _ compiler = (*libraryDecorator)(nil)
var _ libraryInterface = (*libraryDecorator)(nil) var _ libraryInterface = (*libraryDecorator)(nil)
var _ exportedFlagsProducer = (*libraryDecorator)(nil) var _ exportedFlagsProducer = (*libraryDecorator)(nil)

View File

@ -251,6 +251,13 @@ func TestLibstdLinkage(t *testing.T) {
srcs: ["foo.rs"], srcs: ["foo.rs"],
crate_name: "bar", crate_name: "bar",
rustlibs: ["libfoo"], rustlibs: ["libfoo"],
}
rust_ffi {
name: "libbar.prefer_rlib",
srcs: ["foo.rs"],
crate_name: "bar",
rustlibs: ["libfoo"],
prefer_rlib: true,
}`) }`)
libfooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module) libfooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
@ -260,6 +267,9 @@ func TestLibstdLinkage(t *testing.T) {
libbarShared := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module().(*Module) libbarShared := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module().(*Module)
libbarStatic := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Module().(*Module) libbarStatic := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Module().(*Module)
// prefer_rlib works the same for both rust_library and rust_ffi, so a single check is sufficient here.
libbarRlibStd := ctx.ModuleForTests("libbar.prefer_rlib", "android_arm64_armv8-a_shared").Module().(*Module)
if !android.InList("libstd", libfooRlibStatic.Properties.AndroidMkRlibs) { if !android.InList("libstd", libfooRlibStatic.Properties.AndroidMkRlibs) {
t.Errorf("rlib-std variant for device rust_library_rlib does not link libstd as an rlib") t.Errorf("rlib-std variant for device rust_library_rlib does not link libstd as an rlib")
} }
@ -279,4 +289,8 @@ func TestLibstdLinkage(t *testing.T) {
if !android.InList("libfoo.rlib-std", libbarStatic.Properties.AndroidMkRlibs) { if !android.InList("libfoo.rlib-std", libbarStatic.Properties.AndroidMkRlibs) {
t.Errorf("Device rust_ffi_static does not link dependent rustlib rlib-std variant") t.Errorf("Device rust_ffi_static does not link dependent rustlib rlib-std variant")
} }
if !android.InList("libstd", libbarRlibStd.Properties.AndroidMkRlibs) {
t.Errorf("rust_ffi with prefer_rlib does not link libstd as an rlib")
}
} }