Skip to content

Commit d765f30

Browse files
youknowonethomcc
andauthored
detect clang/gcc using --version (#709)
* put m32/m64 always to gcc/clang * try to detect clang/gcc * Set target to apple-darwin for macos to avoid cross-compile * Apply suggestions from code review Co-authored-by: Thom Chiovoloni <[email protected]> --------- Co-authored-by: Thom Chiovoloni <[email protected]>
1 parent 0fc8091 commit d765f30

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

src/lib.rs

+41-10
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,16 @@ impl Build {
18111811
family.add_force_frame_pointer(cmd);
18121812
}
18131813

1814+
if !cmd.is_like_msvc() {
1815+
if target.contains("i686") || target.contains("i586") {
1816+
cmd.args.push("-m32".into());
1817+
} else if target == "x86_64-unknown-linux-gnux32" {
1818+
cmd.args.push("-mx32".into());
1819+
} else if target.contains("x86_64") || target.contains("powerpc64") {
1820+
cmd.args.push("-m64".into());
1821+
}
1822+
}
1823+
18141824
// Target flags
18151825
match cmd.family {
18161826
ToolFamily::Clang => {
@@ -1954,14 +1964,6 @@ impl Build {
19541964
}
19551965
}
19561966
ToolFamily::Gnu => {
1957-
if target.contains("i686") || target.contains("i586") {
1958-
cmd.args.push("-m32".into());
1959-
} else if target == "x86_64-unknown-linux-gnux32" {
1960-
cmd.args.push("-mx32".into());
1961-
} else if target.contains("x86_64") || target.contains("powerpc64") {
1962-
cmd.args.push("-m64".into());
1963-
}
1964-
19651967
if target.contains("darwin") {
19661968
if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target)
19671969
{
@@ -3525,6 +3527,35 @@ impl Tool {
35253527
}
35263528

35273529
fn with_features(path: PathBuf, clang_driver: Option<&str>, cuda: bool) -> Self {
3530+
fn detect_family(path: &Path) -> ToolFamily {
3531+
let mut cmd = Command::new(path);
3532+
cmd.arg("--version");
3533+
3534+
let stdout = match run_output(&mut cmd, &path.to_string_lossy())
3535+
.ok()
3536+
.and_then(|o| String::from_utf8(o).ok())
3537+
{
3538+
Some(s) => s,
3539+
None => {
3540+
// --version failed. fallback to gnu
3541+
println!("cargo-warning:Failed to run: {:?}", cmd);
3542+
return ToolFamily::Gnu;
3543+
}
3544+
};
3545+
if stdout.contains("clang") {
3546+
ToolFamily::Clang
3547+
} else if stdout.contains("GCC") {
3548+
ToolFamily::Gnu
3549+
} else {
3550+
// --version doesn't include clang for GCC
3551+
println!(
3552+
"cargo-warning:Compiler version doesn't include clang or GCC: {:?}",
3553+
cmd
3554+
);
3555+
ToolFamily::Gnu
3556+
}
3557+
}
3558+
35283559
// Try to detect family of the tool from its name, falling back to Gnu.
35293560
let family = if let Some(fname) = path.file_name().and_then(|p| p.to_str()) {
35303561
if fname.contains("clang-cl") {
@@ -3537,10 +3568,10 @@ impl Tool {
35373568
_ => ToolFamily::Clang,
35383569
}
35393570
} else {
3540-
ToolFamily::Gnu
3571+
detect_family(&path)
35413572
}
35423573
} else {
3543-
ToolFamily::Gnu
3574+
detect_family(&path)
35443575
};
35453576

35463577
Tool {

tests/support/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ impl Test {
7676
let target = if self.msvc {
7777
"x86_64-pc-windows-msvc"
7878
} else {
79-
"x86_64-unknown-linux-gnu"
79+
if cfg!(target_os = "macos") {
80+
"x86_64-apple-darwin"
81+
} else {
82+
"x86_64-unknown-linux-gnu"
83+
}
8084
};
8185

8286
cfg.target(target)

tests/test.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ fn gnu_opt_level_s() {
5555
#[test]
5656
fn gnu_debug() {
5757
let test = Test::gnu();
58-
test.gcc().debug(true).file("foo.c").compile("foo");
58+
test.gcc()
59+
.target("x86_64-unknown-linux")
60+
.debug(true)
61+
.file("foo.c")
62+
.compile("foo");
5963
test.cmd(0).must_have("-gdwarf-4");
6064

6165
let test = Test::gnu();
@@ -70,15 +74,23 @@ fn gnu_debug() {
7074
#[test]
7175
fn gnu_debug_fp_auto() {
7276
let test = Test::gnu();
73-
test.gcc().debug(true).file("foo.c").compile("foo");
77+
test.gcc()
78+
.target("x86_64-unknown-linux")
79+
.debug(true)
80+
.file("foo.c")
81+
.compile("foo");
7482
test.cmd(0).must_have("-gdwarf-4");
7583
test.cmd(0).must_have("-fno-omit-frame-pointer");
7684
}
7785

7886
#[test]
7987
fn gnu_debug_fp() {
8088
let test = Test::gnu();
81-
test.gcc().debug(true).file("foo.c").compile("foo");
89+
test.gcc()
90+
.target("x86_64-unknown-linux")
91+
.debug(true)
92+
.file("foo.c")
93+
.compile("foo");
8294
test.cmd(0).must_have("-gdwarf-4");
8395
test.cmd(0).must_have("-fno-omit-frame-pointer");
8496
}
@@ -89,6 +101,7 @@ fn gnu_debug_nofp() {
89101

90102
let test = Test::gnu();
91103
test.gcc()
104+
.target("x86_64-unknown-linux")
92105
.debug(true)
93106
.force_frame_pointer(false)
94107
.file("foo.c")
@@ -98,6 +111,7 @@ fn gnu_debug_nofp() {
98111

99112
let test = Test::gnu();
100113
test.gcc()
114+
.target("x86_64-unknown-linux")
101115
.force_frame_pointer(false)
102116
.debug(true)
103117
.file("foo.c")

0 commit comments

Comments
 (0)