Skip to content

Commit

Permalink
Fall back to rustc's default value for crt-static/static_flag
Browse files Browse the repository at this point in the history
We do this by storing the default target features in `TargetInfo`.
  • Loading branch information
madsmtm committed Nov 3, 2024
1 parent 2b37978 commit 75fb59b
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 22 deletions.
2 changes: 2 additions & 0 deletions dev-tools/gen-target-info/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
let os = spec.os.as_deref().unwrap_or("none");
let env = spec.env.as_deref().unwrap_or("");
let abi = spec.abi.as_deref().unwrap_or("");
let features = spec.cfgs.target_features.join(",");

// Remove deployment target information from LLVM target triples (we
// will add this in another part of CC).
Expand Down Expand Up @@ -54,6 +55,7 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
f,
" unversioned_llvm_target: {unversioned_llvm_target:?},"
)?;
writeln!(f, " features: {features:?},")?;
writeln!(f, " }},")?;
writeln!(f, " ),")?;
}
Expand Down
30 changes: 9 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,10 @@ impl Build {
/// When enabled on systems that support dynamic linking, this prevents
/// linking with the shared libraries.
///
/// If not specified, this falls back to:
/// - `-Ctarget-features=+crt-static` when compiling in a build script.
/// - A target-specific default.
///
/// # Example
///
/// ```no_run
Expand Down Expand Up @@ -1944,18 +1948,10 @@ impl Build {
ToolFamily::Msvc { .. } => {
cmd.push_cc_arg("-nologo".into());

let crt_flag = match self.static_crt {
Some(true) => "-MT",
Some(false) => "-MD",
None => {
let features = self.getenv("CARGO_CFG_TARGET_FEATURE");
let features = features.as_deref().unwrap_or_default();
if features.to_string_lossy().contains("crt-static") {
"-MT"
} else {
"-MD"
}
}
let crt_flag = if self.static_crt.unwrap_or_else(|| target.crt_static()) {
"-MT"
} else {
"-MD"
};
cmd.push_cc_arg(crt_flag.into());

Expand Down Expand Up @@ -2142,14 +2138,6 @@ impl Build {
cmd.args.push("-finput-charset=utf-8".into());
}

if self.static_flag.is_none() {
let features = self.getenv("CARGO_CFG_TARGET_FEATURE");
let features = features.as_deref().unwrap_or_default();
if features.to_string_lossy().contains("crt-static") {
cmd.args.push("-static".into());
}
}

// armv7 targets get to use armv7 instructions
if (target.full_arch.starts_with("armv7")
|| target.full_arch.starts_with("thumbv7"))
Expand Down Expand Up @@ -2318,7 +2306,7 @@ impl Build {
self.apple_flags(cmd)?;
}

if self.static_flag.unwrap_or(false) {
if self.static_flag.unwrap_or_else(|| target.crt_static()) {
cmd.args.push("-static".into());
}
if self.shared_flag.unwrap_or(false) {
Expand Down
9 changes: 9 additions & 0 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::str::FromStr;
use crate::{Error, ErrorKind};

mod apple;
mod feature;
mod generated;
mod llvm;
mod parser;
Expand Down Expand Up @@ -43,6 +44,14 @@ pub(crate) struct TargetInfo<'a> {
///
/// This is the same as the value of `cfg!(target_abi)`.
pub abi: &'a str,
/// The set of target features (including `crt-static`), separated by
/// commas.
///
/// This is the same as the value of `CARGO_CFG_TARGET_FEATURE`, and
/// can be overwritten by the user with `-Ctarget-feature=...`.
///
/// See also <https://doc.rust-lang.org/reference/attributes/codegen.html#the-target_feature-attribute>
features: &'a str,
/// The unversioned LLVM/Clang target triple.
unversioned_llvm_target: &'a str,
}
Expand Down
8 changes: 8 additions & 0 deletions src/target/feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::TargetInfo;

impl TargetInfo<'_> {
/// See <https://doc.rust-lang.org/reference/linkage.html#static-and-dynamic-c-runtimes>
pub(crate) fn crt_static(&self) -> bool {
self.features.contains("crt-static")
}
}
Loading

0 comments on commit 75fb59b

Please sign in to comment.