Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion library/std/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::env;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH was not set");
let target = env::var("TARGET").expect("TARGET was not set");
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");
let target_vendor =
env::var("CARGO_CFG_TARGET_VENDOR").expect("CARGO_CFG_TARGET_VENDOR was not set");
Expand Down Expand Up @@ -73,5 +74,6 @@ fn main() {
println!("cargo:rustc-check-cfg=cfg(backtrace_in_libstd)");
println!("cargo:rustc-cfg=backtrace_in_libstd");

println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap());
println!("cargo:rustc-env=STD_ENV_ARCH={target_arch}");
println!("cargo:rustc-env=STD_ENV_HOST_TUPLE={target}"); // the target becomes the host from std's perspective
}
42 changes: 42 additions & 0 deletions library/std/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,48 @@ pub mod consts {
#[stable(feature = "env", since = "1.0.0")]
pub const ARCH: &str = env!("STD_ENV_ARCH");

/// A string identifying the platform for which the standard library was built
#[doc = concat!("(`\"", env!("STD_ENV_HOST_TUPLE"), "\"`).")]
///
/// This is also known as a *host tuple*, *target triple*, or *target platform*.
///
/// The exact format of this string may vary. For details about the platform,
/// use the [`ARCH`], [`FAMILY`], and [`OS`] constants.
///
/// For platform-specific code, use the [`#[cfg]` attribute][cfg] or [`cfg!` macro](crate::cfg!)
/// with the predefined [`target_*` options][target_cfg].
///
/// When cross-compiling, the `HOST` seen by build scripts and procedural macros is going
/// to be the platform running the cross-compilation, not the target platform of the build.
///
/// [cfg]: ../../../reference/conditional-compilation.html#the-cfg-attribute
/// [target_cfg]: ../../../reference/conditional-compilation.html#r-cfg.options.set
///
/// <details><summary>Example values</summary>
///
/// * `"aarch64-apple-ios-sim"`
/// * `"aarch64-unknown-linux-gnu"`
/// * `"aarch64-unknown-linux-gnu_ilp32"`
/// * `"avr-none"`
/// * `"loongarch64-unknown-none-softfloat"`
/// * `"mipsel-sony-psx"`
/// * `"nvptx64-nvidia-cuda"`
/// * `"powerpc64le-unknown-freebsd"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add powerpc64le-... and powerpc64-..., since those can't easily be reconstructed from their parts (unfortunately RustSec decided to delete the upstream repo, making its history very hard to find, here's the corresponding PR)

/// * `"riscv64-linux-android"`
/// * `"sparcv9-sun-solaris"`
/// * `"thumbv7a-uwp-windows-msvc"`
/// * `"thumbv7neon-unknown-linux-musleabihf"`
/// * `"thumbv8m.main-none-eabihf"`
/// * `"wasm32-wasip1"`
/// * `"x86_64-pc-windows-msvc"`
/// * `"x86_64-unknown-redox"`
/// * `"xtensa-esp32s3-none-elf"`
///
/// </details>
#[unstable(feature = "env_host_tuple", issue = "146295")]
#[doc(alias = "TARGET")]
pub const HOST: &str = env!("STD_ENV_HOST_TUPLE");

/// A string describing the family of the operating system.
/// An example value may be: `"unix"`, or `"windows"`.
///
Expand Down
Loading