Skip to content
Merged
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
31 changes: 25 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub struct Config {
archiver: Option<PathBuf>,
cargo_metadata: bool,
pic: Option<bool>,
static_crt: Option<bool>,
}

/// Configuration used to represent an invocation of a C compiler.
Expand Down Expand Up @@ -187,6 +188,7 @@ impl Config {
archiver: None,
cargo_metadata: true,
pic: None,
static_crt: None,
}
}

Expand Down Expand Up @@ -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<A, B>(&mut self, a: A, b: B) -> &mut Config
Expand Down Expand Up @@ -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()),
Expand Down
25 changes: 24 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down Expand Up @@ -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");
}