diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index a3aa7594c6c..23dcaf27c2c 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -430,6 +430,15 @@ impl Session { } pub fn crt_static(&self) -> bool { + // If the target does not opt in to crt-static support, use its default. + if self.target.target.options.crt_static_respected { + self.crt_static_feature() + } else { + self.target.target.options.crt_static_default + } + } + + pub fn crt_static_feature(&self) -> bool { let requested_features = self.opts.cg.target_feature.split(','); let found_negative = requested_features.clone().any(|r| r == "-crt-static"); let found_positive = requested_features.clone().any(|r| r == "+crt-static"); diff --git a/src/librustc_back/target/linux_musl_base.rs b/src/librustc_back/target/linux_musl_base.rs index 236f2c1ef0a..e77a40e3a3a 100644 --- a/src/librustc_back/target/linux_musl_base.rs +++ b/src/librustc_back/target/linux_musl_base.rs @@ -69,6 +69,8 @@ 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/src/librustc_back/target/mod.rs b/src/librustc_back/target/mod.rs index 08b94d5a01c..0ff633ffe37 100644 --- a/src/librustc_back/target/mod.rs +++ b/src/librustc_back/target/mod.rs @@ -418,6 +418,8 @@ pub struct TargetOptions { /// Whether or not the CRT is statically linked by default. pub crt_static_default: bool, + /// Whether or not crt-static is respected by the compiler (or is a no-op). + pub crt_static_respected: bool, /// Whether or not stack probes (__rust_probestack) are enabled pub stack_probes: bool, @@ -479,6 +481,7 @@ impl Default for TargetOptions { panic_strategy: PanicStrategy::Unwind, abi_blacklist: vec![], crt_static_default: false, + crt_static_respected: false, stack_probes: false, } } @@ -715,6 +718,7 @@ impl Target { key!(min_atomic_width, Option); try!(key!(panic_strategy, PanicStrategy)); key!(crt_static_default, bool); + key!(crt_static_respected, bool); key!(stack_probes, bool); if let Some(array) = obj.find("abi-blacklist").and_then(Json::as_array) { @@ -903,6 +907,7 @@ impl ToJson for Target { target_option_val!(max_atomic_width); target_option_val!(panic_strategy); target_option_val!(crt_static_default); + target_option_val!(crt_static_respected); target_option_val!(stack_probes); if default.abi_blacklist != self.options.abi_blacklist { diff --git a/src/librustc_back/target/windows_msvc_base.rs b/src/librustc_back/target/windows_msvc_base.rs index c07321e418e..f44a9b44426 100644 --- a/src/librustc_back/target/windows_msvc_base.rs +++ b/src/librustc_back/target/windows_msvc_base.rs @@ -63,6 +63,7 @@ pub fn opts() -> TargetOptions { is_like_windows: true, is_like_msvc: true, pre_link_args: args, + crt_static_respected: true, .. Default::default() } diff --git a/src/librustc_driver/target_features.rs b/src/librustc_driver/target_features.rs index 4616db3ae87..96264472b5f 100644 --- a/src/librustc_driver/target_features.rs +++ b/src/librustc_driver/target_features.rs @@ -25,7 +25,7 @@ pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) { cfg.insert((tf, Some(feat))); } - if sess.crt_static() { + if sess.crt_static_feature() { cfg.insert((tf, Some(Symbol::intern("crt-static")))); } }