summaryrefslogtreecommitdiff
path: root/rust/binary.go
diff options
context:
space:
mode:
authorIvan Lozano <ivanlozano@google.com>2020-10-02 10:03:23 -0400
committerIvan Lozano <ivanlozano@google.com>2020-10-02 12:31:23 -0400
commitbf63d00c5487d18d69e1040a94400546f0750476 (patch)
tree8785122fb3c3b81f91499da801b4887c6506ddd2 /rust/binary.go
parent45dda43df0b32122d833d43166f2fa2f9a170889 (diff)
rust: Add static binary support
Adds the "static_executable" property to rust_binary modules which allows for building fully static executables. This only impacts bionic targets. Bug: 169434439 Test: rust_binary module with static_executable true builds, runs on device. Change-Id: I83c19fddd070859b7e56d248237cfd73e1768519
Diffstat (limited to 'rust/binary.go')
-rw-r--r--rust/binary.go27
1 files changed, 23 insertions, 4 deletions
diff --git a/rust/binary.go b/rust/binary.go
index 2758ae077..af39d383d 100644
--- a/rust/binary.go
+++ b/rust/binary.go
@@ -28,6 +28,12 @@ type BinaryCompilerProperties struct {
// 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.
+ //
+ // Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static
+ // binary, but will still implicitly imply prefer_rlib true.
+ Static_executable *bool `android:"arch_variant"`
}
type binaryDecorator struct {
@@ -72,6 +78,11 @@ func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Fla
"-Wl,--gc-sections",
"-Wl,-z,nocopyreloc",
"-Wl,--no-undefined-version")
+
+ if Bool(binary.Properties.Static_executable) {
+ flags.LinkFlags = append(flags.LinkFlags, "-static")
+ flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
+ }
}
return flags
@@ -81,8 +92,12 @@ func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
deps = binary.baseCompiler.compilerDeps(ctx, deps)
if ctx.toolchain().Bionic() {
- deps = bionicDeps(deps)
- deps.CrtBegin = "crtbegin_dynamic"
+ deps = bionicDeps(deps, Bool(binary.Properties.Static_executable))
+ if Bool(binary.Properties.Static_executable) {
+ deps.CrtBegin = "crtbegin_static"
+ } else {
+ deps.CrtBegin = "crtbegin_dynamic"
+ }
deps.CrtEnd = "crtend_android"
}
@@ -99,6 +114,10 @@ func (binary *binaryDecorator) nativeCoverage() bool {
return true
}
+func (binary *binaryDecorator) preferRlib() bool {
+ return Bool(binary.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
+}
+
func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
@@ -135,7 +154,7 @@ func (binary *binaryDecorator) coverageOutputZipPath() android.OptionalPath {
func (binary *binaryDecorator) autoDep(ctx BaseModuleContext) autoDep {
// Binaries default to dylib dependencies for device, rlib for host.
- if Bool(binary.Properties.Prefer_rlib) {
+ if binary.preferRlib() {
return rlibAutoDep
}
if ctx.Device() {
@@ -146,7 +165,7 @@ func (binary *binaryDecorator) autoDep(ctx BaseModuleContext) autoDep {
}
func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
- if Bool(binary.Properties.Prefer_rlib) {
+ if binary.preferRlib() {
return RlibLinkage
}
return binary.baseCompiler.stdLinkage(ctx)