Skip to content

Commit 1db8bf1

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

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,45 @@ pub mod consts {
10451045
#[stable(feature = "env", since = "1.0.0")]
10461046
pub const ARCH: &str = env!("STD_ENV_ARCH");
10471047

1048+
/// An identifier used to select 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 *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+
/// [cfg]: ../../../reference/conditional-compilation.html#the-cfg-attribute
1060+
/// [target_cfg]: ../../../reference/conditional-compilation.html#r-cfg.options.set
1061+
///
1062+
/// <details><summary>Example values</summary>
1063+
///
1064+
/// * `"aarch64-apple-ios-sim"`
1065+
/// * `"aarch64-unknown-linux-gnu"`
1066+
/// * `"aarch64-unknown-linux-gnu_ilp32"`
1067+
/// * `"avr-none"`
1068+
/// * `"loongarch64-unknown-none-softfloat"`
1069+
/// * `"mipsel-sony-psx"`
1070+
/// * `"nvptx64-nvidia-cuda"`
1071+
/// * `"powerpc64le-unknown-freebsd"`
1072+
/// * `"riscv64-linux-android"`
1073+
/// * `"sparcv9-sun-solaris"`
1074+
/// * `"thumbv7a-uwp-windows-msvc"`
1075+
/// * `"thumbv7neon-unknown-linux-musleabihf"`
1076+
/// * `"thumbv8m.main-none-eabihf"`
1077+
/// * `"wasm32-wasip1"`
1078+
/// * `"x86_64-pc-windows-msvc"`
1079+
/// * `"x86_64-unknown-redox"`
1080+
/// * `"xtensa-esp32s3-none-elf"`
1081+
///
1082+
/// </details>
1083+
#[unstable(feature = "env_host_tuple", issue = "146295")]
1084+
#[doc(alias = "TARGET")]
1085+
pub const HOST_TUPLE: &str = env!("STD_ENV_HOST_TUPLE");
1086+
10481087
/// A string describing the family of the operating system.
10491088
/// An example value may be: `"unix"`, or `"windows"`.
10501089
///

0 commit comments

Comments
 (0)