diff --git a/src/lib.rs b/src/lib.rs index 8dd19e10d..cbf1b370b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1808,6 +1808,16 @@ impl Build { family.add_force_frame_pointer(cmd); } + if !cmd.is_like_msvc() { + if target.contains("i686") || target.contains("i586") { + cmd.args.push("-m32".into()); + } else if target == "x86_64-unknown-linux-gnux32" { + cmd.args.push("-mx32".into()); + } else if target.contains("x86_64") || target.contains("powerpc64") { + cmd.args.push("-m64".into()); + } + } + // Target flags match cmd.family { ToolFamily::Clang => { @@ -1937,14 +1947,6 @@ impl Build { } } ToolFamily::Gnu => { - if target.contains("i686") || target.contains("i586") { - cmd.args.push("-m32".into()); - } else if target == "x86_64-unknown-linux-gnux32" { - cmd.args.push("-mx32".into()); - } else if target.contains("x86_64") || target.contains("powerpc64") { - cmd.args.push("-m64".into()); - } - if target.contains("darwin") { if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) { @@ -3438,6 +3440,35 @@ impl Tool { } fn with_features(path: PathBuf, clang_driver: Option<&str>, cuda: bool) -> Self { + fn detect_family(path: &Path) -> ToolFamily { + let mut cmd = Command::new(path); + cmd.arg("--version"); + + let stdout = match run_output(&mut cmd, &path.to_string_lossy()) + .ok() + .and_then(|o| String::from_utf8(o).ok()) + { + Some(s) => s, + None => { + // --version failed. fallback to gnu + println!("cargo-warning:Failed to run: {:?}", cmd); + return ToolFamily::Gnu; + } + }; + if stdout.contains("clang") { + ToolFamily::Clang + } else if stdout.contains("GCC") { + ToolFamily::Gnu + } else { + // --version doesn't include clang for GCC + println!( + "cargo-warning:Compiler version doesn't include clang or GCC: {:?}", + cmd + ); + ToolFamily::Gnu + } + } + // Try to detect family of the tool from its name, falling back to Gnu. let family = if let Some(fname) = path.file_name().and_then(|p| p.to_str()) { if fname.contains("clang-cl") { @@ -3450,10 +3481,10 @@ impl Tool { _ => ToolFamily::Clang, } } else { - ToolFamily::Gnu + detect_family(&path) } } else { - ToolFamily::Gnu + detect_family(&path) }; Tool { diff --git a/tests/support/mod.rs b/tests/support/mod.rs index f3c04405a..3ec191113 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -76,7 +76,11 @@ impl Test { let target = if self.msvc { "x86_64-pc-windows-msvc" } else { - "x86_64-unknown-linux-gnu" + if cfg!(target_os = "macos") { + "x86_64-apple-darwin" + } else { + "x86_64-unknown-linux-gnu" + } }; cfg.target(target) diff --git a/tests/test.rs b/tests/test.rs index 7f1ddb218..10f1570fc 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -55,7 +55,11 @@ fn gnu_opt_level_s() { #[test] fn gnu_debug() { let test = Test::gnu(); - test.gcc().debug(true).file("foo.c").compile("foo"); + test.gcc() + .target("x86_64-unknown-linux") + .debug(true) + .file("foo.c") + .compile("foo"); test.cmd(0).must_have("-gdwarf-4"); let test = Test::gnu(); @@ -70,7 +74,11 @@ fn gnu_debug() { #[test] fn gnu_debug_fp_auto() { let test = Test::gnu(); - test.gcc().debug(true).file("foo.c").compile("foo"); + test.gcc() + .target("x86_64-unknown-linux") + .debug(true) + .file("foo.c") + .compile("foo"); test.cmd(0).must_have("-gdwarf-4"); test.cmd(0).must_have("-fno-omit-frame-pointer"); } @@ -78,7 +86,11 @@ fn gnu_debug_fp_auto() { #[test] fn gnu_debug_fp() { let test = Test::gnu(); - test.gcc().debug(true).file("foo.c").compile("foo"); + test.gcc() + .target("x86_64-unknown-linux") + .debug(true) + .file("foo.c") + .compile("foo"); test.cmd(0).must_have("-gdwarf-4"); test.cmd(0).must_have("-fno-omit-frame-pointer"); } @@ -89,6 +101,7 @@ fn gnu_debug_nofp() { let test = Test::gnu(); test.gcc() + .target("x86_64-unknown-linux") .debug(true) .force_frame_pointer(false) .file("foo.c") @@ -98,6 +111,7 @@ fn gnu_debug_nofp() { let test = Test::gnu(); test.gcc() + .target("x86_64-unknown-linux") .force_frame_pointer(false) .debug(true) .file("foo.c")