Skip to content

Commit beb8abe

Browse files
committed
Introduce temporary target feature crt_static_respected
This feature allows targets to opt in to full support of the crt-static feature. Currently, crt-static is allowed on all targets, even those that really can't or really shouldn't support it. This works because it is very loose in the specification of its effects. Changing the behavior of crt-static to be more strict in how it chooses libraries and links executables would likely cause compilation to fail on these platforms. To avoid breaking existing uses of crt-static, whitelist targets that support the new, stricter behavior. For all other targets, this changes crt-static from being "mostly a no-op" to "explicitly a no-op".
1 parent 3cb9878 commit beb8abe

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

src/librustc/session/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ impl Session {
430430
}
431431

432432
pub fn crt_static(&self) -> bool {
433+
// If the target does not opt in to crt-static support, use its default.
434+
if self.target.target.options.crt_static_respected {
435+
self.crt_static_feature()
436+
} else {
437+
self.target.target.options.crt_static_default
438+
}
439+
}
440+
441+
pub fn crt_static_feature(&self) -> bool {
433442
let requested_features = self.opts.cg.target_feature.split(',');
434443
let found_negative = requested_features.clone().any(|r| r == "-crt-static");
435444
let found_positive = requested_features.clone().any(|r| r == "+crt-static");

src/librustc_back/target/linux_musl_base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ pub fn opts() -> TargetOptions {
6969

7070
// These targets statically link libc by default
7171
base.crt_static_default = true;
72+
// These targets allow the user to choose between static and dynamic linking.
73+
base.crt_static_respected = true;
7274

7375
base
7476
}

src/librustc_back/target/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ pub struct TargetOptions {
418418

419419
/// Whether or not the CRT is statically linked by default.
420420
pub crt_static_default: bool,
421+
/// Whether or not crt-static is respected by the compiler (or is a no-op).
422+
pub crt_static_respected: bool,
421423

422424
/// Whether or not stack probes (__rust_probestack) are enabled
423425
pub stack_probes: bool,
@@ -479,6 +481,7 @@ impl Default for TargetOptions {
479481
panic_strategy: PanicStrategy::Unwind,
480482
abi_blacklist: vec![],
481483
crt_static_default: false,
484+
crt_static_respected: false,
482485
stack_probes: false,
483486
}
484487
}
@@ -715,6 +718,7 @@ impl Target {
715718
key!(min_atomic_width, Option<u64>);
716719
try!(key!(panic_strategy, PanicStrategy));
717720
key!(crt_static_default, bool);
721+
key!(crt_static_respected, bool);
718722
key!(stack_probes, bool);
719723

720724
if let Some(array) = obj.find("abi-blacklist").and_then(Json::as_array) {
@@ -903,6 +907,7 @@ impl ToJson for Target {
903907
target_option_val!(max_atomic_width);
904908
target_option_val!(panic_strategy);
905909
target_option_val!(crt_static_default);
910+
target_option_val!(crt_static_respected);
906911
target_option_val!(stack_probes);
907912

908913
if default.abi_blacklist != self.options.abi_blacklist {

src/librustc_back/target/windows_msvc_base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub fn opts() -> TargetOptions {
6363
is_like_windows: true,
6464
is_like_msvc: true,
6565
pre_link_args: args,
66+
crt_static_respected: true,
6667

6768
.. Default::default()
6869
}

src/librustc_driver/target_features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) {
2525
cfg.insert((tf, Some(feat)));
2626
}
2727

28-
if sess.crt_static() {
28+
if sess.crt_static_feature() {
2929
cfg.insert((tf, Some(Symbol::intern("crt-static"))));
3030
}
3131
}

0 commit comments

Comments
 (0)