Skip to content

Commit

Permalink
[msvc] Use -MT -Zl unconditionally.
Browse files Browse the repository at this point in the history
Since this crate doesn't actually make decisions about
final application linking, it's only appropriate to not
interfere with the process. This can be achieved with
-MT -Zl, because object modules compiled with these flags
can be linked with either VCCRT version, static or dynamic,
debug or release...

For reference, -Zl doesn't affect linking directives set with
in Windows headers to add references to not-so-common libraries
like mfc.lib, windowscodecs.lib to name just a pair.
  • Loading branch information
dot-asm committed Sep 17, 2022
1 parent 53fb72c commit 9a8d1f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 35 deletions.
27 changes: 10 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,9 @@ impl Build {
self
}

/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
/// No-op option retained for backward compatibility.
///
/// This option defaults to `false`, and affect only msvc targets.
/// Used to configure whether the /MT flag or the /MD flag will be passed to msvc build tools.
pub fn static_crt(&mut self, static_crt: bool) -> &mut Build {
self.static_crt = Some(static_crt);
self
Expand Down Expand Up @@ -1487,21 +1487,14 @@ 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());
// Since this crate doesn't actually make decisions about
// final application linking, it's only appropriate to not
// interfere with the process. This can be achieved with
// -MT -Zl, because object modules compiled with these flags
// can be linked with either VCCRT version, static or dynamic,
// debug or release...
cmd.push_cc_arg("-MT".into());
cmd.push_cc_arg("-Zl".into());

match &opt_level[..] {
// Msvc uses /O1 to enable all optimizations that minimize code size.
Expand Down
19 changes: 1 addition & 18 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,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 @@ -404,22 +403,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

0 comments on commit 9a8d1f9

Please sign in to comment.