Skip to content

Commit dc34c0f

Browse files
committed
Fall back to rustc's default value for crt-static/static_flag
We do this by storing the default target features in `TargetInfo`.
1 parent e552eeb commit dc34c0f

File tree

6 files changed

+332
-18
lines changed

6 files changed

+332
-18
lines changed

dev-tools/gen-target-info/src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
2525
let os = spec.os.as_deref().unwrap_or("none");
2626
let env = spec.env.as_deref().unwrap_or("");
2727
let abi = spec.abi.as_deref().unwrap_or("");
28+
let features = spec.cfgs.target_features.join(",");
2829

2930
// Remove deployment target information from LLVM target triples (we
3031
// will add this in another part of CC).
@@ -54,6 +55,7 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
5455
f,
5556
" unversioned_llvm_target: {unversioned_llvm_target:?},"
5657
)?;
58+
writeln!(f, " features: {features:?},")?;
5759
writeln!(f, " }},")?;
5860
writeln!(f, " ),")?;
5961
}

src/lib.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,10 @@ impl Build {
793793
/// When enabled on systems that support dynamic linking, this prevents
794794
/// linking with the shared libraries.
795795
///
796+
/// If not specified, this falls back to:
797+
/// - `-Ctarget-features=+crt-static` when compiling in a build script.
798+
/// - A target-specific default.
799+
///
796800
/// # Example
797801
///
798802
/// ```no_run
@@ -1301,6 +1305,9 @@ impl Build {
13011305
/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
13021306
///
13031307
/// This option defaults to `false`, and affect only msvc targets.
1308+
///
1309+
/// If not specified, this falls back to `-Ctarget-features=+crt-static`
1310+
/// when compiling in a build script.
13041311
pub fn static_crt(&mut self, static_crt: bool) -> &mut Build {
13051312
self.static_crt = Some(static_crt);
13061313
self
@@ -1944,18 +1951,10 @@ impl Build {
19441951
ToolFamily::Msvc { .. } => {
19451952
cmd.push_cc_arg("-nologo".into());
19461953

1947-
let crt_flag = match self.static_crt {
1948-
Some(true) => "-MT",
1949-
Some(false) => "-MD",
1950-
None => {
1951-
let features = self.getenv("CARGO_CFG_TARGET_FEATURE");
1952-
let features = features.as_deref().unwrap_or_default();
1953-
if features.to_string_lossy().contains("crt-static") {
1954-
"-MT"
1955-
} else {
1956-
"-MD"
1957-
}
1958-
}
1954+
let crt_flag = if self.static_crt.unwrap_or_else(|| target.crt_static()) {
1955+
"-MT"
1956+
} else {
1957+
"-MD"
19591958
};
19601959
cmd.push_cc_arg(crt_flag.into());
19611960

@@ -2142,12 +2141,8 @@ impl Build {
21422141
cmd.args.push("-finput-charset=utf-8".into());
21432142
}
21442143

2145-
if self.static_flag.is_none() {
2146-
let features = self.getenv("CARGO_CFG_TARGET_FEATURE");
2147-
let features = features.as_deref().unwrap_or_default();
2148-
if features.to_string_lossy().contains("crt-static") {
2149-
cmd.args.push("-static".into());
2150-
}
2144+
if self.static_flag.is_none() && target.crt_static() {
2145+
cmd.args.push("-static".into());
21512146
}
21522147

21532148
// armv7 targets get to use armv7 instructions

src/target.rs

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::str::FromStr;
77
use crate::{Error, ErrorKind};
88

99
mod apple;
10+
mod feature;
1011
mod generated;
1112
mod llvm;
1213
mod parser;
@@ -43,6 +44,14 @@ pub(crate) struct TargetInfo<'a> {
4344
///
4445
/// This is the same as the value of `cfg!(target_abi)`.
4546
pub abi: &'a str,
47+
/// The set of target features (including `crt-static`), separated by
48+
/// commas.
49+
///
50+
/// This is the same as the value of `CARGO_CFG_TARGET_FEATURE`, and
51+
/// can be overwritten by the user with `-Ctarget-feature=...`.
52+
///
53+
/// See also <https://doc.rust-lang.org/reference/attributes/codegen.html#the-target_feature-attribute>
54+
features: &'a str,
4655
/// The unversioned LLVM/Clang target triple.
4756
unversioned_llvm_target: &'a str,
4857
}

src/target/feature.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use super::TargetInfo;
2+
3+
impl TargetInfo<'_> {
4+
/// See <https://doc.rust-lang.org/reference/linkage.html#static-and-dynamic-c-runtimes>
5+
pub(crate) fn crt_static(&self) -> bool {
6+
self.features.contains("crt-static")
7+
}
8+
}

0 commit comments

Comments
 (0)