Skip to content

[msvc] Don't use -M* flags. #723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,9 +976,20 @@ impl Build {
self
}

/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
///
/// This option defaults to `false`, and affect only msvc targets.
/// No-op option retained for backward compatibility.
///
/// Originally the method was introduced to harmonize with externally
/// enforced `-MT` flag and resolve warnings in Rust bootstrap procedure
/// on `*-windows-msvc` targets. It's argued that a more versatile approach
/// is to **not** specify any of the `-M*` flags in cc-rs, but rely on
/// the default MSVC compiler behaviour or externally enforced flag[s].
/// The default behaviour allows forced linking with either VCCRT library,
/// static or dynamic, whichever chosen by rustc at link time.
///
/// On a related note, if you compile Rust static library for use with an
/// external application, it's likely to be appropriate to pass `-Zl` flag
/// in order to not interfere with the target application's linking
/// procedure.
pub fn static_crt(&mut self, static_crt: bool) -> &mut Build {
self.static_crt = Some(static_crt);
self
Expand Down Expand Up @@ -1549,22 +1560,6 @@ 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")
.unwrap_or(String::new());
if features.contains("crt-static") {
"-MT"
} else {
"-MD"
}
}
};
cmd.push_cc_arg(crt_flag.into());

match &opt_level[..] {
// Msvc uses /O1 to enable all optimizations that minimize code size.
"z" | "s" | "1" => cmd.push_opt_unless_duplicate("-O1".into()),
Expand Down
19 changes: 1 addition & 18 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ fn msvc_smoke() {
.must_have("-O2")
.must_have("foo.c")
.must_not_have("-Z7")
.must_have("-c")
.must_have("-MD");
.must_have("-c");
test.cmd(1).must_have(test.td.path().join("foo.o"));
}

Expand Down Expand Up @@ -419,22 +418,6 @@ fn msvc_define() {
test.cmd(0).must_have("-DFOO=bar").must_have("-DBAR");
}

#[test]
fn msvc_static_crt() {
let test = Test::msvc();
test.gcc().static_crt(true).file("foo.c").compile("foo");

test.cmd(0).must_have("-MT");
}

#[test]
fn msvc_no_static_crt() {
let test = Test::msvc();
test.gcc().static_crt(false).file("foo.c").compile("foo");

test.cmd(0).must_have("-MD");
}

#[test]
fn msvc_no_dash_dash() {
let test = Test::msvc();
Expand Down