From 4b6b7afbe181ea3e9a02abfee3e74d53a490722b Mon Sep 17 00:00:00 2001 From: Lawrence Tang Date: Wed, 14 Feb 2024 14:18:39 +0000 Subject: [PATCH] Expost host and target sysroot to build scripts. --- src/cargo/core/compiler/custom_build.rs | 11 ++++++++++- src/doc/src/reference/environment-variables.md | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 99311a5ffd16..2bab35cc7fbd 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -32,7 +32,7 @@ //! [instructions]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script use super::{fingerprint, Context, Job, Unit, Work}; -use crate::core::compiler::artifact; +use crate::core::compiler::{artifact, CompileKind}; use crate::core::compiler::context::Metadata; use crate::core::compiler::job_queue::JobState; use crate::core::{profiles::ProfileRoot, PackageId, Target}; @@ -316,6 +316,15 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { cmd.env("CARGO_TRIM_PATHS", trim_paths.to_string()); } + // Pass along sysroots used by the host/target. + let host_sysroot = &bcx.target_data.info(CompileKind::Host).sysroot; + let target_sysroot = match unit.kind { + CompileKind::Host => host_sysroot, + CompileKind::Target(_) => &bcx.target_data.info(unit.kind).sysroot + }; + cmd.env("RUSTC_HOST_SYSROOT", host_sysroot); + cmd.env("RUSTC_TARGET_SYSROOT", target_sysroot); + // Be sure to pass along all enabled features for this package, this is the // last piece of statically known information that we have. for feat in &unit.features { diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index 40a0d32f1ae5..e3f679a340e9 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -392,6 +392,8 @@ let out_dir = env::var("OUT_DIR").unwrap(); changed by editing `.cargo/config.toml`; see the documentation about [cargo configuration][cargo-config] for more information. +* `RUSTC_HOST_SYSROOT` --- The path to the `rustc` sysroot for the host. +* `RUSTC_TARGET_SYSROOT` --- The path to the `rustc` sysroot for the target. * `CARGO_ENCODED_RUSTFLAGS` --- extra flags that Cargo invokes `rustc` with, separated by a `0x1f` character (ASCII Unit Separator). See [`build.rustflags`]. Note that since Rust 1.55, `RUSTFLAGS` is removed from