Skip to content

Commit cdc3a37

Browse files
committed
Fix --target cflag on FreeBSD when compiling against clang
Clang does not/did not support being invoked with a target triple that does not contain at least the major os version tacked on to the end, and will fail to find standard library headers if it's not present [0]. e.g. invoking `clang++` with `--target=x86_64-unknown-freebsd` may present errors like ``` warning: In file included from src/cxx.cc:1: warning: src/../include/cxx.h:2:10: fatal error: 'algorithm' file not found warning: #include <algorithm> warning: ^~~~~~~~~~~ warning: 1 error generated. error: failed to run custom build command for `cxx v1.0.81` ``` This is rectified by detecting cases where clang is used and the target triple is set to `xxx-xxx-freebsd` and then invoking `uname -r` to obtain the os version and appending it to the triple before converting it into a `--target=...` argument to the compiler. This has been fixed upstream in the LLVM project for Clang 14 and above [1], but systems not running the latest bleeding edge will not benefit from that. This issue may be reproduced with clang 13.0.0 on FreeBSD 13.1. Closes #463. [0]: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=238556 [1]: https://reviews.llvm.org/D77776
1 parent 9e50fb8 commit cdc3a37

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,20 @@ impl Build {
17221722
} else if target.contains("aarch64") {
17231723
cmd.args.push("--target=aarch64-unknown-windows-gnu".into())
17241724
}
1725+
} else if target.ends_with("-freebsd") {
1726+
// clang 13 on FreeBSD doesn't support a target triple without at least the
1727+
// major os version number appended; e.g. use x86_64-unknown-freebsd13
1728+
// instead of x86-64-unknown-freebsd
1729+
let uname_r = String::from_utf8(
1730+
std::process::Command::new("uname")
1731+
.args(&["-r"])
1732+
.output()
1733+
.expect("Failed to execute uname!")
1734+
.stdout,
1735+
)
1736+
.expect("Failure parsing uname output as UTF-8!");
1737+
let os_ver = uname_r.as_str().split('-').next().unwrap();
1738+
cmd.push_cc_arg(format!("--target={}{}", target, os_ver).into());
17251739
} else {
17261740
cmd.push_cc_arg(format!("--target={}", target).into());
17271741
}

0 commit comments

Comments
 (0)