From 5001c38359aa3d2c6630c5d56a37e7eb05444f65 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 065f012b94f..a6c77ef78bf 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -32,10 +32,10 @@ //! [instructions]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script use super::{fingerprint, BuildRunner, Job, Unit, Work}; -use crate::core::compiler::artifact; use crate::core::compiler::build_runner::Metadata; use crate::core::compiler::fingerprint::DirtyReason; use crate::core::compiler::job_queue::JobState; +use crate::core::compiler::{artifact, CompileKind}; use crate::core::{profiles::ProfileRoot, PackageId, Target}; use crate::util::errors::CargoResult; use crate::util::machine_message::{self, Message}; @@ -317,6 +317,15 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul 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 40a0d32f1ae..e3f679a340e 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