From 30224365ecf196ea6b2b26bf260e92416b47f061 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Wed, 13 Oct 2021 11:48:02 +0100 Subject: [PATCH 1/2] Absolutify CXX as well as CC Also add the `--sysroot` flag if needed --- cargo/cargo_build_script_runner/bin.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cargo/cargo_build_script_runner/bin.rs b/cargo/cargo_build_script_runner/bin.rs index baee4ff724..8eff96366d 100644 --- a/cargo/cargo_build_script_runner/bin.rs +++ b/cargo/cargo_build_script_runner/bin.rs @@ -88,13 +88,15 @@ fn run_buildrs() -> Result<(), String> { } } - if let Some(cc_path) = env::var_os("CC") { - let mut cc_path = exec_root.join(cc_path).into_os_string(); - if let Some(sysroot_path) = env::var_os("SYSROOT") { - cc_path.push(" --sysroot="); - cc_path.push(&exec_root.join(sysroot_path)); + for compiler_env_var in &["CC", "CXX"] { + if let Some(compiler_path) = env::var_os(compiler_env_var) { + let mut compiler_path = exec_root.join(compiler_path).into_os_string(); + if let Some(sysroot_path) = env::var_os("SYSROOT") { + compiler_path.push(" --sysroot="); + compiler_path.push(&exec_root.join(sysroot_path)); + } + command.env(compiler_env_var, compiler_path); } - command.env("CC", cc_path); } if let Some(ar_path) = env::var_os("AR") { From e8515767493d49080282a9fd6eecaea8857046c3 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Wed, 13 Oct 2021 13:50:52 +0100 Subject: [PATCH 2/2] Add tests for executability of CC and CXX --- test/cargo_build_script/build.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/cargo_build_script/build.rs b/test/cargo_build_script/build.rs index 7b8b980d0f..68cef72321 100644 --- a/test/cargo_build_script/build.rs +++ b/test/cargo_build_script/build.rs @@ -4,4 +4,24 @@ fn main() { "cargo:rustc-env=TOOL_PATH={}", std::env::var("TOOL").unwrap() ); + + // Assert that the CC and CXX env vars existed and were executable. + // We don't assert what happens when they're executed (in particular, we don't check for a + // non-zero exit code), but this asserts that it's an existing file which is executable. + // + // Unfortunately we need to shlex the path, because we add a `--sysroot=...` arg to the env var. + for env_var in &["CC", "CXX"] { + let v = std::env::var(env_var) + .unwrap_or_else(|err| panic!("Error getting {}: {}", env_var, err)); + let (path, args) = if let Some(index) = v.find("--sysroot") { + let (path, args) = v.split_at(index); + (path, Some(args)) + } else { + (v.as_str(), None) + }; + std::process::Command::new(path) + .args(args.into_iter()) + .status() + .unwrap(); + } }