From 4d9a4786163a9a9831bf4e283b4e408be03a169b Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Fri, 29 Aug 2014 12:46:04 -0700 Subject: [PATCH] add workaround for mingw `ld --force-exe-suffix` behavior --- src/librustc/back/write.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/librustc/back/write.rs b/src/librustc/back/write.rs index d87dd84bb32b2..506d358501696 100644 --- a/src/librustc/back/write.rs +++ b/src/librustc/back/write.rs @@ -607,6 +607,19 @@ pub fn run_passes(sess: &Session, }; let link_obj = |output_path: &Path| { + // Some builds of MinGW GCC will pass --force-exe-suffix to ld, which + // will automatically add a .exe extension if the extension is not + // already .exe or .dll. To ensure consistent behavior on Windows, we + // add the .exe suffix explicitly and then rename the output file to + // the desired path. This will give the correct behavior whether or + // not GCC adds --force-exe-suffix. + let windows_output_path = + if sess.targ_cfg.os == abi::OsWindows { + Some(output_path.with_extension("o.exe")) + } else { + None + }; + let pname = get_cc_prog(sess); let mut cmd = Command::new(pname.as_slice()); @@ -617,7 +630,9 @@ pub fn run_passes(sess: &Session, cmd.arg(crate_output.with_extension(format!("{}.o", index).as_slice())); } - cmd.arg("-r").arg("-o").arg(output_path); + cmd.arg("-r") + .arg("-o") + .arg(windows_output_path.as_ref().unwrap_or(output_path)); if (sess.opts.debugging_opts & config::PRINT_LINK_ARGS) != 0 { println!("{}", &cmd); @@ -635,6 +650,15 @@ pub fn run_passes(sess: &Session, sess.abort_if_errors(); }, } + + match windows_output_path { + Some(ref windows_path) => { + fs::rename(windows_path, output_path).unwrap(); + }, + None => { + // The file is already named according to `output_path`. + } + } }; // Flag to indicate whether the user explicitly requested bitcode.