From 8c4954b58316851c2bfb8cdbd42bf71fd99d6eab Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 1 Aug 2022 04:21:41 +0900 Subject: [PATCH 1/4] put m32/m64 always to gcc/clang --- src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8dd19e10d..498ba8115 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) { From fe92c3be8423d8eeb1e863a6ed89b6f90e0a46a6 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 1 Aug 2022 04:30:15 +0900 Subject: [PATCH 2/4] try to detect clang/gcc --- src/lib.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 498ba8115..d7488a71b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3440,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:Running failed: {:?}", 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") { @@ -3452,10 +3481,10 @@ impl Tool { _ => ToolFamily::Clang, } } else { - ToolFamily::Gnu + detect_family(&path) } } else { - ToolFamily::Gnu + detect_family(&path) }; Tool { From 404d2c4e4bb12d002ea879310959dae104ff2880 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 1 Aug 2022 03:46:18 +0900 Subject: [PATCH 3/4] Set target to apple-darwin for macos to avoid cross-compile --- tests/support/mod.rs | 6 +++++- tests/test.rs | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) 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") From cd3695a1bdfc2f116ae32bb4f82db82fba683801 Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" <69878+youknowone@users.noreply.github.com> Date: Mon, 11 Sep 2023 17:37:35 +0900 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Thom Chiovoloni --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d7488a71b..cbf1b370b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3451,7 +3451,7 @@ impl Tool { Some(s) => s, None => { // --version failed. fallback to gnu - println!("cargo-warning:Running failed: {:?}", cmd); + println!("cargo-warning:Failed to run: {:?}", cmd); return ToolFamily::Gnu; } };