Skip to content

Commit b3e26e0

Browse files
authored
Absolutify CXX as well as CC (#969)
Also add the `--sysroot` flag if needed This is needed for in-tree CXX values because we chdir before execing the build script binary. Fixes #950
1 parent d7db4d3 commit b3e26e0

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

cargo/cargo_build_script_runner/bin.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ fn run_buildrs() -> Result<(), String> {
8888
}
8989
}
9090

91-
if let Some(cc_path) = env::var_os("CC") {
92-
let mut cc_path = exec_root.join(cc_path).into_os_string();
93-
if let Some(sysroot_path) = env::var_os("SYSROOT") {
94-
cc_path.push(" --sysroot=");
95-
cc_path.push(&exec_root.join(sysroot_path));
91+
for compiler_env_var in &["CC", "CXX"] {
92+
if let Some(compiler_path) = env::var_os(compiler_env_var) {
93+
let mut compiler_path = exec_root.join(compiler_path).into_os_string();
94+
if let Some(sysroot_path) = env::var_os("SYSROOT") {
95+
compiler_path.push(" --sysroot=");
96+
compiler_path.push(&exec_root.join(sysroot_path));
97+
}
98+
command.env(compiler_env_var, compiler_path);
9699
}
97-
command.env("CC", cc_path);
98100
}
99101

100102
if let Some(ar_path) = env::var_os("AR") {

test/cargo_build_script/build.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,24 @@ fn main() {
44
"cargo:rustc-env=TOOL_PATH={}",
55
std::env::var("TOOL").unwrap()
66
);
7+
8+
// Assert that the CC and CXX env vars existed and were executable.
9+
// We don't assert what happens when they're executed (in particular, we don't check for a
10+
// non-zero exit code), but this asserts that it's an existing file which is executable.
11+
//
12+
// Unfortunately we need to shlex the path, because we add a `--sysroot=...` arg to the env var.
13+
for env_var in &["CC", "CXX"] {
14+
let v = std::env::var(env_var)
15+
.unwrap_or_else(|err| panic!("Error getting {}: {}", env_var, err));
16+
let (path, args) = if let Some(index) = v.find("--sysroot") {
17+
let (path, args) = v.split_at(index);
18+
(path, Some(args))
19+
} else {
20+
(v.as_str(), None)
21+
};
22+
std::process::Command::new(path)
23+
.args(args.into_iter())
24+
.status()
25+
.unwrap();
26+
}
727
}

0 commit comments

Comments
 (0)