Skip to content

Commit 54508a2

Browse files
committed
Auto merge of rust-lang#78924 - bjorn3:less_sysroot_build_scripts, r=Mark-Simulacrum
Make the libstd build script smaller Of all sysroot crates currently only compiler_builtins, miniz_oxide and std require a build script. compiler_builtins uses to conditionally enable certain features and possibly compile a C version ([source](https://github.com/rust-lang/compiler-builtins/blob/63ccaf11f08fb5d0b39cc33884c5a1a63f547ace/build.rs)), miniz_oxide only uses it to detect if liballoc is supported as the MSRV is 1.34.0 instead of the 1.36.0 which stabilized liballoc ([source](https://github.com/Frommi/miniz_oxide/blob/28514ec09f0b1ce74bfb2d561de52a6652ce377a/miniz_oxide/build.rs)). std now only uses it to enable `freebsd12` when the `RUST_STD_FREEBSD_12_ABI` env var is set, to determine if `restricted-std` should be set, to set the `STD_ENV_ARCH` env var identical to `CARGO_CFG_TARGET_ARCH`, and to unconditionally enable `backtrace_in_libstd`. If all build scripts were to be removed, it would be possible for rustc to completely compile it's own sysroot. It currently requires a rustc version that already has an available libstd to compile the build scripts. If rustc can completely compile it's own sysroot, rustbuild could be simplified to not forcefully use the bootstrap compiler for build scripts. `@rustbot` modify labels: +T-compiler +libs-impl
2 parents b5c37e8 + 6f3872a commit 54508a2

File tree

5 files changed

+86
-58
lines changed

5 files changed

+86
-58
lines changed

library/std/build.rs

+14-57
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,23 @@ use std::env;
33
fn main() {
44
println!("cargo:rerun-if-changed=build.rs");
55
let target = env::var("TARGET").expect("TARGET was not set");
6-
if target.contains("linux") {
7-
if target.contains("android") {
8-
println!("cargo:rustc-link-lib=dl");
9-
println!("cargo:rustc-link-lib=log");
10-
println!("cargo:rustc-link-lib=gcc");
11-
}
12-
} else if target.contains("freebsd") {
13-
println!("cargo:rustc-link-lib=execinfo");
14-
println!("cargo:rustc-link-lib=pthread");
6+
if target.contains("freebsd") {
157
if env::var("RUST_STD_FREEBSD_12_ABI").is_ok() {
168
println!("cargo:rustc-cfg=freebsd12");
179
}
18-
} else if target.contains("netbsd") {
19-
println!("cargo:rustc-link-lib=pthread");
20-
println!("cargo:rustc-link-lib=rt");
21-
} else if target.contains("dragonfly") || target.contains("openbsd") {
22-
println!("cargo:rustc-link-lib=pthread");
23-
} else if target.contains("solaris") {
24-
println!("cargo:rustc-link-lib=socket");
25-
println!("cargo:rustc-link-lib=posix4");
26-
println!("cargo:rustc-link-lib=pthread");
27-
println!("cargo:rustc-link-lib=resolv");
28-
} else if target.contains("illumos") {
29-
println!("cargo:rustc-link-lib=socket");
30-
println!("cargo:rustc-link-lib=posix4");
31-
println!("cargo:rustc-link-lib=pthread");
32-
println!("cargo:rustc-link-lib=resolv");
33-
println!("cargo:rustc-link-lib=nsl");
34-
// Use libumem for the (malloc-compatible) allocator
35-
println!("cargo:rustc-link-lib=umem");
36-
} else if target.contains("apple-darwin") {
37-
println!("cargo:rustc-link-lib=System");
38-
39-
// res_init and friends require -lresolv on macOS/iOS.
40-
// See #41582 and http://blog.achernya.com/2013/03/os-x-has-silly-libsystem.html
41-
println!("cargo:rustc-link-lib=resolv");
42-
} else if target.contains("apple-ios") {
43-
println!("cargo:rustc-link-lib=System");
44-
println!("cargo:rustc-link-lib=objc");
45-
println!("cargo:rustc-link-lib=framework=Security");
46-
println!("cargo:rustc-link-lib=framework=Foundation");
47-
println!("cargo:rustc-link-lib=resolv");
48-
} else if target.contains("uwp") {
49-
println!("cargo:rustc-link-lib=ws2_32");
50-
// For BCryptGenRandom
51-
println!("cargo:rustc-link-lib=bcrypt");
52-
} else if target.contains("windows") {
53-
println!("cargo:rustc-link-lib=advapi32");
54-
println!("cargo:rustc-link-lib=ws2_32");
55-
println!("cargo:rustc-link-lib=userenv");
56-
} else if target.contains("fuchsia") {
57-
println!("cargo:rustc-link-lib=zircon");
58-
println!("cargo:rustc-link-lib=fdio");
59-
} else if target.contains("cloudabi") {
60-
if cfg!(feature = "backtrace") {
61-
println!("cargo:rustc-link-lib=unwind");
62-
}
63-
println!("cargo:rustc-link-lib=c");
64-
println!("cargo:rustc-link-lib=compiler_rt");
65-
} else if (target.contains("sgx") && target.contains("fortanix"))
10+
} else if target.contains("linux")
11+
|| target.contains("netbsd")
12+
|| target.contains("dragonfly")
13+
|| target.contains("openbsd")
14+
|| target.contains("solaris")
15+
|| target.contains("illumos")
16+
|| target.contains("apple-darwin")
17+
|| target.contains("apple-ios")
18+
|| target.contains("uwp")
19+
|| target.contains("windows")
20+
|| target.contains("fuchsia")
21+
|| target.contains("cloudabi")
22+
|| (target.contains("sgx") && target.contains("fortanix"))
6623
|| target.contains("hermit")
6724
|| target.contains("l4re")
6825
|| target.contains("redox")

library/std/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,5 +566,5 @@ include!("keyword_docs.rs");
566566
// This is required to avoid an unstable error when `restricted-std` is not
567567
// enabled. The use of #![feature(restricted_std)] in rustc-std-workspace-std
568568
// is unconditional, so the unstable feature needs to be defined somewhere.
569-
#[cfg_attr(not(feature = "restricted-std"), unstable(feature = "restricted_std", issue = "none"))]
569+
#[unstable(feature = "restricted_std", issue = "none")]
570570
mod __restricted_std_workaround {}

library/std/src/sys/cloudabi/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ pub fn hashmap_random_keys() -> (u64, u64) {
6666
v.assume_init()
6767
}
6868
}
69+
70+
#[cfg_attr(feature = "backtrace", link(name = "unwind"))]
71+
#[link(name = "c")]
72+
#[link(name = "compiler_rt")]
73+
extern "C" {}

library/std/src/sys/unix/mod.rs

+52
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,55 @@ pub fn cvt_nz(error: libc::c_int) -> crate::io::Result<()> {
236236
pub fn abort_internal() -> ! {
237237
unsafe { libc::abort() }
238238
}
239+
240+
cfg_if::cfg_if! {
241+
if #[cfg(target_os = "android")] {
242+
#[link(name = "dl")]
243+
#[link(name = "log")]
244+
#[link(name = "gcc")]
245+
extern "C" {}
246+
} else if #[cfg(target_os = "freebsd")] {
247+
#[link(name = "execinfo")]
248+
#[link(name = "pthread")]
249+
extern "C" {}
250+
} else if #[cfg(target_os = "netbsd")] {
251+
#[link(name = "pthread")]
252+
#[link(name = "rt")]
253+
extern "C" {}
254+
} else if #[cfg(any(target_os = "dragonfly", target_os = "openbsd"))] {
255+
#[link(name = "pthread")]
256+
extern "C" {}
257+
} else if #[cfg(target_os = "solaris")] {
258+
#[link(name = "socket")]
259+
#[link(name = "posix4")]
260+
#[link(name = "pthread")]
261+
#[link(name = "resolv")]
262+
extern "C" {}
263+
} else if #[cfg(target_os = "illumos")] {
264+
#[link(name = "socket")]
265+
#[link(name = "posix4")]
266+
#[link(name = "pthread")]
267+
#[link(name = "resolv")]
268+
#[link(name = "nsl")]
269+
// Use libumem for the (malloc-compatible) allocator
270+
#[link(name = "umem")]
271+
extern "C" {}
272+
} else if #[cfg(target_os = "macos")] {
273+
#[link(name = "System")]
274+
// res_init and friends require -lresolv on macOS/iOS.
275+
// See #41582 and http://blog.achernya.com/2013/03/os-x-has-silly-libsystem.html
276+
#[link(name = "resolv")]
277+
extern "C" {}
278+
} else if #[cfg(target_os = "ios")] {
279+
#[link(name = "System")]
280+
#[link(name = "objc")]
281+
#[link(name = "Security", kind = "framework")]
282+
#[link(name = "Foundation", kind = "framework")]
283+
#[link(name = "resolv")]
284+
extern "C" {}
285+
} else if #[cfg(target_os = "fuchsia")] {
286+
#[link(name = "zircon")]
287+
#[link(name = "fdio")]
288+
extern "C" {}
289+
}
290+
}

library/std/src/sys/windows/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,17 @@ pub fn abort_internal() -> ! {
270270
}
271271
crate::intrinsics::abort();
272272
}
273+
274+
cfg_if::cfg_if! {
275+
if #[cfg(target_vendor = "uwp")] {
276+
#[link(name = "ws2_32")]
277+
// For BCryptGenRandom
278+
#[link(name = "bcrypt")]
279+
extern "C" {}
280+
} else {
281+
#[link(name = "advapi32")]
282+
#[link(name = "ws2_32")]
283+
#[link(name = "userenv")]
284+
extern "C" {}
285+
}
286+
}

0 commit comments

Comments
 (0)