From 99557fcc660498a0b4fc14c5551aa948683cc866 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sun, 20 Sep 2020 23:02:38 -0700 Subject: [PATCH] Support static linking with glibc and target-feature=+crt-static With this change, it's possible to build on a linux-gnu target and pass RUSTFLAGS='-C target-feature=+crt-static' or the equivalent via a `.cargo/config.toml` file, and get a statically linked executable. Update to libc 0.2.78, which adds support for static linking with glibc. Add `crt_static_respected` to the `linux_base` target spec. Update `android_base` and `linux_musl_base` accordingly. Avoid enabling crt_static_respected on Android platforms, since that hasn't been tested. --- compiler/rustc_target/src/spec/android_base.rs | 1 + compiler/rustc_target/src/spec/linux_base.rs | 1 + compiler/rustc_target/src/spec/linux_musl_base.rs | 2 -- library/std/Cargo.toml | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_target/src/spec/android_base.rs b/compiler/rustc_target/src/spec/android_base.rs index 0ea99af83a1ef..bb11ce8ef28c2 100644 --- a/compiler/rustc_target/src/spec/android_base.rs +++ b/compiler/rustc_target/src/spec/android_base.rs @@ -12,5 +12,6 @@ pub fn opts() -> TargetOptions { base.position_independent_executables = true; base.has_elf_tls = false; base.requires_uwtable = true; + base.crt_static_respected = false; base } diff --git a/compiler/rustc_target/src/spec/linux_base.rs b/compiler/rustc_target/src/spec/linux_base.rs index 52892fc35924e..7ad972b069210 100644 --- a/compiler/rustc_target/src/spec/linux_base.rs +++ b/compiler/rustc_target/src/spec/linux_base.rs @@ -28,6 +28,7 @@ pub fn opts() -> TargetOptions { position_independent_executables: true, relro_level: RelroLevel::Full, has_elf_tls: true, + crt_static_respected: true, ..Default::default() } } diff --git a/compiler/rustc_target/src/spec/linux_musl_base.rs b/compiler/rustc_target/src/spec/linux_musl_base.rs index b90e91d2901a8..16cc3b762f623 100644 --- a/compiler/rustc_target/src/spec/linux_musl_base.rs +++ b/compiler/rustc_target/src/spec/linux_musl_base.rs @@ -10,8 +10,6 @@ pub fn opts() -> TargetOptions { // These targets statically link libc by default base.crt_static_default = true; - // These targets allow the user to choose between static and dynamic linking. - base.crt_static_respected = true; base } diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml index b27b056086a64..33966409c8e04 100644 --- a/library/std/Cargo.toml +++ b/library/std/Cargo.toml @@ -16,7 +16,7 @@ cfg-if = { version = "0.1.8", features = ['rustc-dep-of-std'] } panic_unwind = { path = "../panic_unwind", optional = true } panic_abort = { path = "../panic_abort" } core = { path = "../core" } -libc = { version = "0.2.77", default-features = false, features = ['rustc-dep-of-std'] } +libc = { version = "0.2.78", default-features = false, features = ['rustc-dep-of-std'] } compiler_builtins = { version = "0.1.35" } profiler_builtins = { path = "../profiler_builtins", optional = true } unwind = { path = "../unwind" }