Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect clang/gcc using --version #709

Merged
merged 4 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,16 @@ impl Build {
family.add_force_frame_pointer(cmd);
}

if !cmd.is_like_msvc() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the details anymore, but they are related to tests gnu_i686 and gnu_x86_64

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I got the idea. This happened because gcc() now target x86_64-apple-darwin on macOS.
I can revert this part and add target to gnu_i686 and gnu_x86_64. Is this better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By itself it's a formally wrong move. While the -m32/m64 are accepted by clang, the --target argument [which is always passed to clang by cc-rs] describes the intention sufficiently and more importantly more adequately. In other words the options in question should have remained in the ToolFamily::Gnu section. If they cause problems in the section in question, then it would have been more appropriate to solve it there.

On a tangential note. The powepc64 gcc defaults to -m64, so that it's actually more interesting to pass -m32 than -m64. But of course making it symmetric would be better, i.e. pass -m32 if the target is 32-bit ppc, and -m64 otherwise...

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 => {
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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") {
Expand All @@ -3450,10 +3481,10 @@ impl Tool {
_ => ToolFamily::Clang,
}
} else {
ToolFamily::Gnu
detect_family(&path)
}
} else {
ToolFamily::Gnu
detect_family(&path)
};

Tool {
Expand Down
6 changes: 5 additions & 1 deletion tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 17 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -70,15 +74,23 @@ 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");
}

#[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");
}
Expand All @@ -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")
Expand All @@ -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")
Expand Down