Files
nixpkgs/lib/systems/rustc-target-env.nix
Sam Pointon 4a3079eda3 systems: set rust.platform.env as rustc does
rustc allows code to perform compile-time tests against target_env [0], 
which is a function of the target triple. There isn't provision in 
Nixpkgs for learning target_env, however. That doesn't affect rustc's 
evaluation of cfg guards in Rust code - rustc knows perfectly well what 
target_env is - but it _does_ affect the env vars passed to a build 
script set by buildRustCrate, which is presently hard-coded to gnu, and 
also affects any Nix code looking to branch on target_env.

Being able to access target_env is very relevant to, for example, WASI 
as (using Rust terminology) wasm32-wasip1 and wasm32-wasip2 differ only 
by target_env, with identical target_arch and target_os values. Properly 
reflecting target_env may also fix some random musl issues if you're a 
bit lucky.

This commit adds a new attr, rust.platform.env, which reflects the 
target_env that rustc will set for the target, and wires it up in 
buildRustCrate. In isolation, this change mostly only affects build 
scripts checking target_env, but crate2nix will greatly benefit from 
being able to accurately resolve the dep graph (which can also vary 
depending on target_env).

The target triple -> target_env function resists Kolmogorov compression: 
it's irregular and, though there are some patterns, there are lots of 
special cases. So, I have done the stupidest possible thing and scraped 
out all of the targets with non-empty target_env values and dumped that 
into an attrset. This attrset will progressively get out of date as 
rustc adds new platforms, but updating it should be simple enough - I've 
included the generation script as a comment.

There are some other configuration options not being reflected in Nix. I 
have left those alone, but, in the future, maybe this can be extended to 
just dumping all of them into an attrset and then reflecting them in 
rust.platform. It might even make sense to convert the existing code 
producing rust.platform to just looking up from an attrset and to 'do 
what rustc does'.

I would have liked to have added a test targetting a platform with a 
non-empty non-GNU target_env, but all of the yaks were quite hairy.

Fixes https://github.com/NixOS/nixpkgs/issues/436832

[0]: https://doc.rust-lang.org/reference/conditional-compilation.html#r-cfg.target_env
2026-06-15 16:21:13 +01:00

161 lines
5.8 KiB
Nix

# As of rustc 1.96.0. Empty `target_env` values are omitted.
#
# Generation script:
# #!/bin/bash
# rustc --print target-list | while read -r target ; do
# env=$(rustc --print cfg --target "$target" | grep '^target_env=' | sed 's/# ^target_env="//;s/"$//')
# [[ -z "$env" ]] && continue
# printf ' %s = "%s";\n' "$target" "$env"
# done
{
aarch64-apple-ios-macabi = "macabi";
aarch64-apple-ios-sim = "sim";
aarch64-apple-tvos-sim = "sim";
aarch64-apple-visionos-sim = "sim";
aarch64-apple-watchos-sim = "sim";
aarch64-pc-windows-gnullvm = "gnu";
aarch64-pc-windows-msvc = "msvc";
aarch64-unknown-linux-gnu = "gnu";
aarch64-unknown-linux-gnu_ilp32 = "gnu";
aarch64-unknown-linux-musl = "musl";
aarch64-unknown-linux-ohos = "ohos";
aarch64-unknown-managarm-mlibc = "mlibc";
aarch64-unknown-nto-qnx700 = "nto70";
aarch64-unknown-nto-qnx710 = "nto71";
aarch64-unknown-nto-qnx710_iosock = "nto71_iosock";
aarch64-unknown-nto-qnx800 = "nto80";
aarch64-unknown-redox = "relibc";
aarch64-uwp-windows-msvc = "msvc";
aarch64-wrs-vxworks = "gnu";
aarch64_be-unknown-linux-gnu = "gnu";
aarch64_be-unknown-linux-gnu_ilp32 = "gnu";
aarch64_be-unknown-linux-musl = "musl";
arm-unknown-linux-gnueabi = "gnu";
arm-unknown-linux-gnueabihf = "gnu";
arm-unknown-linux-musleabi = "musl";
arm-unknown-linux-musleabihf = "musl";
arm64ec-pc-windows-msvc = "msvc";
armeb-unknown-linux-gnueabi = "gnu";
armv4t-unknown-linux-gnueabi = "gnu";
armv5te-unknown-linux-gnueabi = "gnu";
armv5te-unknown-linux-musleabi = "musl";
armv5te-unknown-linux-uclibceabi = "uclibc";
armv6k-nintendo-3ds = "newlib";
armv7-rtems-eabihf = "newlib";
armv7-sony-vita-newlibeabihf = "newlib";
armv7-unknown-linux-gnueabi = "gnu";
armv7-unknown-linux-gnueabihf = "gnu";
armv7-unknown-linux-musleabi = "musl";
armv7-unknown-linux-musleabihf = "musl";
armv7-unknown-linux-ohos = "ohos";
armv7-unknown-linux-uclibceabi = "uclibc";
armv7-unknown-linux-uclibceabihf = "uclibc";
armv7-wrs-vxworks-eabihf = "gnu";
armv7a-vex-v5 = "v5";
csky-unknown-linux-gnuabiv2 = "gnu";
csky-unknown-linux-gnuabiv2hf = "gnu";
hexagon-unknown-linux-musl = "musl";
i386-apple-ios = "sim";
i586-unknown-linux-gnu = "gnu";
i586-unknown-linux-musl = "musl";
i586-unknown-redox = "relibc";
i686-pc-nto-qnx700 = "nto70";
i686-pc-windows-gnu = "gnu";
i686-pc-windows-gnullvm = "gnu";
i686-pc-windows-msvc = "msvc";
i686-unknown-hurd-gnu = "gnu";
i686-unknown-linux-gnu = "gnu";
i686-unknown-linux-musl = "musl";
i686-uwp-windows-gnu = "gnu";
i686-uwp-windows-msvc = "msvc";
i686-win7-windows-gnu = "gnu";
i686-win7-windows-msvc = "msvc";
i686-wrs-vxworks = "gnu";
loongarch64-unknown-linux-gnu = "gnu";
loongarch64-unknown-linux-musl = "musl";
loongarch64-unknown-linux-ohos = "ohos";
m68k-unknown-linux-gnu = "gnu";
mips-unknown-linux-gnu = "gnu";
mips-unknown-linux-musl = "musl";
mips-unknown-linux-uclibc = "uclibc";
mips64-openwrt-linux-musl = "musl";
mips64-unknown-linux-gnuabi64 = "gnu";
mips64-unknown-linux-muslabi64 = "musl";
mips64el-unknown-linux-gnuabi64 = "gnu";
mips64el-unknown-linux-muslabi64 = "musl";
mipsel-unknown-linux-gnu = "gnu";
mipsel-unknown-linux-musl = "musl";
mipsel-unknown-linux-uclibc = "uclibc";
mipsisa32r6-unknown-linux-gnu = "gnu";
mipsisa32r6el-unknown-linux-gnu = "gnu";
mipsisa64r6-unknown-linux-gnuabi64 = "gnu";
mipsisa64r6el-unknown-linux-gnuabi64 = "gnu";
powerpc-unknown-linux-gnu = "gnu";
powerpc-unknown-linux-gnuspe = "gnu";
powerpc-unknown-linux-musl = "musl";
powerpc-unknown-linux-muslspe = "musl";
powerpc-wrs-vxworks = "gnu";
powerpc-wrs-vxworks-spe = "gnu";
powerpc64-unknown-linux-gnu = "gnu";
powerpc64-unknown-linux-musl = "musl";
powerpc64-wrs-vxworks = "gnu";
powerpc64le-unknown-linux-gnu = "gnu";
powerpc64le-unknown-linux-musl = "musl";
riscv32-wrs-vxworks = "gnu";
riscv32gc-unknown-linux-gnu = "gnu";
riscv32gc-unknown-linux-musl = "musl";
riscv32imac-esp-espidf = "newlib";
riscv32imafc-esp-espidf = "newlib";
riscv32imc-esp-espidf = "newlib";
riscv64-wrs-vxworks = "gnu";
riscv64a23-unknown-linux-gnu = "gnu";
riscv64gc-unknown-linux-gnu = "gnu";
riscv64gc-unknown-linux-musl = "musl";
riscv64gc-unknown-managarm-mlibc = "mlibc";
riscv64gc-unknown-redox = "relibc";
s390x-unknown-linux-gnu = "gnu";
s390x-unknown-linux-musl = "musl";
sparc-unknown-linux-gnu = "gnu";
sparc64-unknown-linux-gnu = "gnu";
thumbv7a-pc-windows-msvc = "msvc";
thumbv7a-uwp-windows-msvc = "msvc";
thumbv7neon-unknown-linux-gnueabihf = "gnu";
thumbv7neon-unknown-linux-musleabihf = "musl";
wasm32-wali-linux-musl = "musl";
wasm32-wasip1 = "p1";
wasm32-wasip1-threads = "p1";
wasm32-wasip2 = "p2";
wasm32-wasip3 = "p3";
x86_64-apple-ios = "sim";
x86_64-apple-ios-macabi = "macabi";
x86_64-apple-tvos = "sim";
x86_64-apple-watchos-sim = "sim";
x86_64-fortanix-unknown-sgx = "sgx";
x86_64-pc-nto-qnx710 = "nto71";
x86_64-pc-nto-qnx710_iosock = "nto71_iosock";
x86_64-pc-nto-qnx800 = "nto80";
x86_64-pc-windows-gnu = "gnu";
x86_64-pc-windows-gnullvm = "gnu";
x86_64-pc-windows-msvc = "msvc";
x86_64-unikraft-linux-musl = "musl";
x86_64-unknown-hurd-gnu = "gnu";
x86_64-unknown-l4re-uclibc = "uclibc";
x86_64-unknown-linux-gnu = "gnu";
x86_64-unknown-linux-gnuasan = "gnu";
x86_64-unknown-linux-gnumsan = "gnu";
x86_64-unknown-linux-gnutsan = "gnu";
x86_64-unknown-linux-gnux32 = "gnu";
x86_64-unknown-linux-musl = "musl";
x86_64-unknown-linux-ohos = "ohos";
x86_64-unknown-managarm-mlibc = "mlibc";
x86_64-unknown-redox = "relibc";
x86_64-uwp-windows-gnu = "gnu";
x86_64-uwp-windows-msvc = "msvc";
x86_64-win7-windows-gnu = "gnu";
x86_64-win7-windows-msvc = "msvc";
x86_64-wrs-vxworks = "gnu";
xtensa-esp32-espidf = "newlib";
xtensa-esp32s2-espidf = "newlib";
xtensa-esp32s3-espidf = "newlib";
}