Skip to content

Commit b5d0ecc

Browse files
committed
Current platform's target tuple in std::env::consts
env_host_tuple #146295
1 parent 4056082 commit b5d0ecc

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

library/std/build.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::env;
33
fn main() {
44
println!("cargo:rerun-if-changed=build.rs");
55
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH was not set");
6+
let target = env::var("TARGET").expect("TARGET was not set");
67
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");
78
let target_vendor =
89
env::var("CARGO_CFG_TARGET_VENDOR").expect("CARGO_CFG_TARGET_VENDOR was not set");
@@ -73,5 +74,6 @@ fn main() {
7374
println!("cargo:rustc-check-cfg=cfg(backtrace_in_libstd)");
7475
println!("cargo:rustc-cfg=backtrace_in_libstd");
7576

76-
println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap());
77+
println!("cargo:rustc-env=STD_ENV_ARCH={target_arch}");
78+
println!("cargo:rustc-env=STD_ENV_HOST_TUPLE={target}"); // the target becomes the host from std's perspective
7779
}

library/std/src/env.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,48 @@ pub mod consts {
10451045
#[stable(feature = "env", since = "1.0.0")]
10461046
pub const ARCH: &str = env!("STD_ENV_ARCH");
10471047

1048+
/// A string identifying the platform for which the standard library was built
1049+
#[doc = concat!("(`\"", env!("STD_ENV_HOST_TUPLE"), "\"`).")]
1050+
///
1051+
/// This is also known as a *host tuple*, *target triple*, or *target platform*.
1052+
///
1053+
/// The exact format of this string may vary. For details about the platform,
1054+
/// use the [`ARCH`], [`FAMILY`], and [`OS`] constants.
1055+
///
1056+
/// For platform-specific code, use the [`#[cfg]` attribute][cfg] or [`cfg!` macro](crate::cfg!)
1057+
/// with the predefined [`target_*` options][target_cfg].
1058+
///
1059+
/// When cross-compiling, the `HOST` seen by build scripts and procedural macros is going
1060+
/// to be the platform running the cross-compilation, not the target platform being built.
1061+
///
1062+
/// [cfg]: ../../../reference/conditional-compilation.html#the-cfg-attribute
1063+
/// [target_cfg]: ../../../reference/conditional-compilation.html#r-cfg.options.set
1064+
///
1065+
/// <details><summary>Example values</summary>
1066+
///
1067+
/// * `"aarch64-apple-ios-sim"`
1068+
/// * `"aarch64-unknown-linux-gnu"`
1069+
/// * `"aarch64-unknown-linux-gnu_ilp32"`
1070+
/// * `"avr-none"`
1071+
/// * `"loongarch64-unknown-none-softfloat"`
1072+
/// * `"mipsel-sony-psx"`
1073+
/// * `"nvptx64-nvidia-cuda"`
1074+
/// * `"powerpc64le-unknown-freebsd"`
1075+
/// * `"riscv64-linux-android"`
1076+
/// * `"sparcv9-sun-solaris"`
1077+
/// * `"thumbv7a-uwp-windows-msvc"`
1078+
/// * `"thumbv7neon-unknown-linux-musleabihf"`
1079+
/// * `"thumbv8m.main-none-eabihf"`
1080+
/// * `"wasm32-wasip1"`
1081+
/// * `"x86_64-pc-windows-msvc"`
1082+
/// * `"x86_64-unknown-redox"`
1083+
/// * `"xtensa-esp32s3-none-elf"`
1084+
///
1085+
/// </details>
1086+
#[unstable(feature = "env_host_tuple", issue = "146295")]
1087+
#[doc(alias = "TARGET")]
1088+
pub const HOST: &str = env!("STD_ENV_HOST_TUPLE");
1089+
10481090
/// A string describing the family of the operating system.
10491091
/// An example value may be: `"unix"`, or `"windows"`.
10501092
///

0 commit comments

Comments
 (0)