From 52895d677bd116bd5325323c3b0108ffff6bdf88 Mon Sep 17 00:00:00 2001 From: messense Date: Mon, 3 Jan 2022 10:00:06 +0800 Subject: [PATCH] Only use `--print-sysroot` with GNU like compilers Also add support for specifying sysroot using `TARGET_SYSROOT` env var --- src/build_context.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/build_context.rs b/src/build_context.rs index fe81c2304..7df53e20e 100644 --- a/src/build_context.rs +++ b/src/build_context.rs @@ -707,10 +707,13 @@ fn relpath(to: &Path, from: &Path) -> PathBuf { /// Get sysroot path from target C compiler /// /// Currently only gcc is supported, clang doesn't have a `--print-sysroot` option -/// TODO: allow specify sysroot from environment variable? fn get_sysroot_path(target: &Target) -> Result { use crate::target::get_host_target; + if let Some(sysroot) = std::env::var_os("TARGET_SYSROOT") { + return Ok(PathBuf::from(sysroot)); + } + let host_triple = get_host_target()?; let target_triple = target.target_triple(); if host_triple != target_triple { @@ -725,6 +728,10 @@ fn get_sysroot_path(target: &Target) -> Result { let compiler = build .try_get_compiler() .with_context(|| format!("Failed to get compiler for {}", target_triple))?; + // Only GNU like compilers support `--print-sysroot` + if !compiler.is_like_gnu() { + return Ok(PathBuf::from("/")); + } let path = compiler.path(); let out = Command::new(path) .arg("--print-sysroot")