Skip to content

Commit 31eddb2

Browse files
authored
Rollup merge of rust-lang#120232 - c272:json-buildstd, r=Mark-Simulacrum
Add support for custom JSON targets when using build-std. Currently, when building with `build-std`, some library build scripts check properties of the target by inspecting the target triple at `env::TARGET`, which is simply set to the filename of the JSON file when using JSON target files. This patch alters these build scripts to use `env::CARGO_CFG_*` to fetch target information instead, allowing JSON target files describing platforms without `restricted_std` to build correctly when using `-Z build-std`. There are some weak assertions here (for example, `nintendo && newlib`), however this seems at least a marginal improvement on the existing solution. Fixes rust-lang/wg-cargo-std-aware#60.
2 parents 2dd3020 + 797cf59 commit 31eddb2

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

library/profiler_builtins/build.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ fn main() {
1212
return;
1313
}
1414

15-
let target = env::var("TARGET").expect("TARGET was not set");
15+
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");
16+
let target_env = env::var("CARGO_CFG_TARGET_ENV").expect("CARGO_CFG_TARGET_ENV was not set");
1617
let cfg = &mut cc::Build::new();
1718

1819
// FIXME: `rerun-if-changed` directives are not currently emitted and the build script
@@ -40,7 +41,7 @@ fn main() {
4041
"InstrProfilingBiasVar.c",
4142
];
4243

43-
if target.contains("msvc") {
44+
if target_env == "msvc" {
4445
// Don't pull in extra libraries on MSVC
4546
cfg.flag("/Zl");
4647
profile_sources.push("WindowsMMap.c");
@@ -55,7 +56,7 @@ fn main() {
5556
cfg.flag("-fno-builtin");
5657
cfg.flag("-fomit-frame-pointer");
5758
cfg.define("VISIBILITY_HIDDEN", None);
58-
if !target.contains("windows") {
59+
if target_os != "windows" {
5960
cfg.flag("-fvisibility=hidden");
6061
cfg.define("COMPILER_RT_HAS_UNAME", Some("1"));
6162
} else {

library/std/build.rs

+41-36
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,46 @@ use std::env;
22

33
fn main() {
44
println!("cargo:rerun-if-changed=build.rs");
5-
let target = env::var("TARGET").expect("TARGET was not set");
6-
if target.contains("linux")
7-
|| target.contains("netbsd")
8-
|| target.contains("dragonfly")
9-
|| target.contains("openbsd")
10-
|| target.contains("freebsd")
11-
|| target.contains("solaris")
12-
|| target.contains("illumos")
13-
|| target.contains("apple-darwin")
14-
|| target.contains("apple-ios")
15-
|| target.contains("apple-tvos")
16-
|| target.contains("apple-watchos")
17-
|| target.contains("uwp")
18-
|| target.contains("windows")
19-
|| target.contains("fuchsia")
20-
|| (target.contains("sgx") && target.contains("fortanix"))
21-
|| target.contains("hermit")
22-
|| target.contains("l4re")
23-
|| target.contains("redox")
24-
|| target.contains("haiku")
25-
|| target.contains("vxworks")
26-
|| target.contains("wasm32")
27-
|| target.contains("wasm64")
28-
|| target.contains("espidf")
29-
|| target.contains("solid")
30-
|| target.contains("nintendo-3ds")
31-
|| target.contains("vita")
32-
|| target.contains("aix")
33-
|| target.contains("nto")
34-
|| target.contains("xous")
35-
|| target.contains("hurd")
36-
|| target.contains("uefi")
37-
|| target.contains("teeos")
38-
|| target.contains("zkvm")
39-
// See src/bootstrap/synthetic_targets.rs
5+
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH was not set");
6+
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");
7+
let target_vendor =
8+
env::var("CARGO_CFG_TARGET_VENDOR").expect("CARGO_CFG_TARGET_VENDOR was not set");
9+
let target_env = env::var("CARGO_CFG_TARGET_ENV").expect("CARGO_CFG_TARGET_ENV was not set");
10+
11+
if target_os == "linux"
12+
|| target_os == "netbsd"
13+
|| target_os == "dragonfly"
14+
|| target_os == "openbsd"
15+
|| target_os == "freebsd"
16+
|| target_os == "solaris"
17+
|| target_os == "illumos"
18+
|| target_os == "macos"
19+
|| target_os == "ios"
20+
|| target_os == "tvos"
21+
|| target_os == "watchos"
22+
|| target_os == "windows"
23+
|| target_os == "fuchsia"
24+
|| (target_vendor == "fortranix" && target_env == "sgx")
25+
|| target_os == "hermit"
26+
|| target_os == "l4re"
27+
|| target_os == "redox"
28+
|| target_os == "haiku"
29+
|| target_os == "vxworks"
30+
|| target_arch == "wasm32"
31+
|| target_arch == "wasm64"
32+
|| target_os == "espidf"
33+
|| target_os.starts_with("solid")
34+
|| (target_vendor == "nintendo" && target_env == "newlib")
35+
|| target_os == "vita"
36+
|| target_os == "aix"
37+
|| target_os == "nto"
38+
|| target_os == "xous"
39+
|| target_os == "hurd"
40+
|| target_os == "uefi"
41+
|| target_os == "teeos"
42+
|| target_os == "zkvm"
43+
44+
// See src/bootstrap/src/core/build_steps/synthetic_targets.rs
4045
|| env::var("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET").is_ok()
4146
{
4247
// These platforms don't have any special requirements.
@@ -48,7 +53,7 @@ fn main() {
4853
// - mipsel-sony-psp
4954
// - nvptx64-nvidia-cuda
5055
// - arch=avr
51-
// - JSON targets
56+
// - JSON targets not describing an excluded target above.
5257
// - Any new targets that have not been explicitly added above.
5358
println!("cargo:rustc-cfg=feature=\"restricted-std\"");
5459
}

0 commit comments

Comments
 (0)