Skip to content

Commit 205a6ba

Browse files
authored
Merge pull request #159 from liranringel/explicitly-set-static-crt
Explicitly set static crt
2 parents 88eb074 + 8890a6f commit 205a6ba

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/lib.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub struct Config {
8181
archiver: Option<PathBuf>,
8282
cargo_metadata: bool,
8383
pic: Option<bool>,
84+
static_crt: Option<bool>,
8485
}
8586

8687
/// Configuration used to represent an invocation of a C compiler.
@@ -187,6 +188,7 @@ impl Config {
187188
archiver: None,
188189
cargo_metadata: true,
189190
pic: None,
191+
static_crt: None,
190192
}
191193
}
192194

@@ -363,6 +365,14 @@ impl Config {
363365
self
364366
}
365367

368+
/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
369+
///
370+
/// This option defaults to `false`, and affect only msvc targets.
371+
pub fn static_crt(&mut self, static_crt: bool) -> &mut Config {
372+
self.static_crt = Some(static_crt);
373+
self
374+
}
375+
366376

367377
#[doc(hidden)]
368378
pub fn __set_env<A, B>(&mut self, a: A, b: B) -> &mut Config
@@ -534,13 +544,22 @@ impl Config {
534544
match cmd.family {
535545
ToolFamily::Msvc => {
536546
cmd.args.push("/nologo".into());
537-
let features = env::var("CARGO_CFG_TARGET_FEATURE")
547+
548+
let crt_flag = match self.static_crt {
549+
Some(true) => "/MT",
550+
Some(false) => "/MD",
551+
None => {
552+
let features = env::var("CARGO_CFG_TARGET_FEATURE")
538553
.unwrap_or(String::new());
539-
if features.contains("crt-static") {
540-
cmd.args.push("/MT".into());
541-
} else {
542-
cmd.args.push("/MD".into());
543-
}
554+
if features.contains("crt-static") {
555+
"/MT"
556+
} else {
557+
"/MD"
558+
}
559+
},
560+
};
561+
cmd.args.push(crt_flag.into());
562+
544563
match &opt_level[..] {
545564
"z" | "s" => cmd.args.push("/Os".into()),
546565
"1" => cmd.args.push("/O1".into()),

tests/test.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ fn msvc_smoke() {
180180
.must_have("/O2")
181181
.must_have("foo.c")
182182
.must_not_have("/Z7")
183-
.must_have("/c");
183+
.must_have("/c")
184+
.must_have("/MD");
184185
test.cmd(1).must_have(test.td.path().join("foo.o"));
185186
}
186187

@@ -227,3 +228,25 @@ fn msvc_define() {
227228

228229
test.cmd(0).must_have("/DFOO=bar").must_have("/DBAR");
229230
}
231+
232+
#[test]
233+
fn msvc_static_crt() {
234+
let test = Test::msvc();
235+
test.gcc()
236+
.static_crt(true)
237+
.file("foo.c")
238+
.compile("libfoo.a");
239+
240+
test.cmd(0).must_have("/MT");
241+
}
242+
243+
#[test]
244+
fn msvc_no_static_crt() {
245+
let test = Test::msvc();
246+
test.gcc()
247+
.static_crt(false)
248+
.file("foo.c")
249+
.compile("libfoo.a");
250+
251+
test.cmd(0).must_have("/MD");
252+
}

0 commit comments

Comments
 (0)