diff --git a/src/lib.rs b/src/lib.rs index bdd51dd..79b926d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,6 +81,7 @@ pub struct Config { archiver: Option, cargo_metadata: bool, pic: Option, + static_crt: Option, } /// Configuration used to represent an invocation of a C compiler. @@ -187,6 +188,7 @@ impl Config { archiver: None, cargo_metadata: true, pic: None, + static_crt: None, } } @@ -363,6 +365,14 @@ impl Config { 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. + pub fn static_crt(&mut self, static_crt: bool) -> &mut Config { + self.static_crt = Some(static_crt); + self + } + #[doc(hidden)] pub fn __set_env(&mut self, a: A, b: B) -> &mut Config @@ -534,13 +544,22 @@ impl Config { match cmd.family { ToolFamily::Msvc => { cmd.args.push("/nologo".into()); - let features = env::var("CARGO_CFG_TARGET_FEATURE") + + let crt_flag = match self.static_crt { + Some(true) => "/MT", + Some(false) => "/MD", + None => { + let features = env::var("CARGO_CFG_TARGET_FEATURE") .unwrap_or(String::new()); - if features.contains("crt-static") { - cmd.args.push("/MT".into()); - } else { - cmd.args.push("/MD".into()); - } + if features.contains("crt-static") { + "/MT" + } else { + "/MD" + } + }, + }; + cmd.args.push(crt_flag.into()); + match &opt_level[..] { "z" | "s" => cmd.args.push("/Os".into()), "1" => cmd.args.push("/O1".into()), diff --git a/tests/test.rs b/tests/test.rs index 1c51e09..8fda3ed 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -180,7 +180,8 @@ fn msvc_smoke() { .must_have("/O2") .must_have("foo.c") .must_not_have("/Z7") - .must_have("/c"); + .must_have("/c") + .must_have("/MD"); test.cmd(1).must_have(test.td.path().join("foo.o")); } @@ -227,3 +228,25 @@ 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("libfoo.a"); + + 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("libfoo.a"); + + test.cmd(0).must_have("/MD"); +}