Skip to content

Commit 176e545

Browse files
committedAug 4, 2024
Auto merge of #128534 - bjorn3:split_stdlib_workspace, r=Mark-Simulacrum
Move the standard library to a separate workspace This ensures that the Cargo.lock packaged for it in the rust-src component is up-to-date, allowing rust-analyzer to run cargo metadata on the standard library even when the rust-src component is stored in a read-only location as is necessary for loading crates.io dependencies of the standard library. This also simplifies tidy's license check for runtime dependencies as it can now look at all entries in library/Cargo.lock without having to filter for just the dependencies of runtime crates. In addition this allows removing an exception in check_runtime_license_exceptions that was necessary due to the compiler enabling a feature on the object crate which pulls in a dependency not allowed for the standard library. While cargo workspaces normally enable dependencies of multiple targets to be reused, for the standard library we do not want this reusing to prevent conflicts between dependencies of the sysroot and of tools that are built using this sysroot. For this reason we already use an unstable cargo feature to ensure that any dependencies which would otherwise be shared get a different -Cmetadata argument as well as using separate build dirs. This doesn't change the situation around vendoring. We already have several cargo workspaces that need to be vendored. Adding another one doesn't change much. There are also no cargo profiles that are shared between the root workspace and the library workspace anyway, so it doesn't add any extra work when changing cargo profiles.
2 parents ab1527f + 178886e commit 176e545

17 files changed

+615
-404
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ build/
4848
/dist/
4949
/unicode-downloads
5050
/target
51+
/library/target
5152
/src/bootstrap/target
5253
/src/tools/x/target
5354
# Created by default with `src/ci/docker/run.sh`

‎Cargo.lock

+2-302
Large diffs are not rendered by default.

‎Cargo.toml

-37
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
resolver = "1"
33
members = [
44
"compiler/rustc",
5-
"library/std",
6-
"library/sysroot",
75
"src/etc/test-float-parse",
86
"src/rustdoc-json-types",
97
"src/tools/build_helper",
@@ -61,43 +59,15 @@ exclude = [
6159
# not all `Cargo.toml` files are available, so we exclude the `x` binary,
6260
# so it can be invoked before the current checkout is set up.
6361
"src/tools/x",
64-
# stdarch has its own Cargo workspace
65-
"library/stdarch",
6662
]
6763

68-
[profile.release.package.compiler_builtins]
69-
# For compiler-builtins we always use a high number of codegen units.
70-
# The goal here is to place every single intrinsic into its own object
71-
# file to avoid symbol clashes with the system libgcc if possible. Note
72-
# that this number doesn't actually produce this many object files, we
73-
# just don't create more than this number of object files.
74-
#
75-
# It's a bit of a bummer that we have to pass this here, unfortunately.
76-
# Ideally this would be specified through an env var to Cargo so Cargo
77-
# knows how many CGUs are for this specific crate, but for now
78-
# per-crate configuration isn't specifiable in the environment.
79-
codegen-units = 10000
80-
8164
[profile.release.package.rustc-rayon-core]
8265
# The rustc fork of Rayon has deadlock detection code which intermittently
8366
# causes overflows in the CI (see https://github.com/rust-lang/rust/issues/90227)
8467
# so we turn overflow checks off for now.
8568
# FIXME: This workaround should be removed once #90227 is fixed.
8669
overflow-checks = false
8770

88-
# These dependencies of the standard library implement symbolication for
89-
# backtraces on most platforms. Their debuginfo causes both linking to be slower
90-
# (more data to chew through) and binaries to be larger without really all that
91-
# much benefit. This section turns them all to down to have no debuginfo which
92-
# helps to improve link times a little bit.
93-
[profile.release.package]
94-
addr2line.debug = 0
95-
adler.debug = 0
96-
gimli.debug = 0
97-
miniz_oxide.debug = 0
98-
object.debug = 0
99-
rustc-demangle.debug = 0
100-
10171
# These are very thin wrappers around executing lld with the right binary name.
10272
# Basically nothing within them can go wrong without having been explicitly logged anyway.
10373
# We ship these in every rustc tarball and even after compression they add up
@@ -120,10 +90,3 @@ codegen-units = 1
12090
# FIXME: LTO cannot be enabled for binaries in a workspace
12191
# <https://github.com/rust-lang/cargo/issues/9330>
12292
# lto = true
123-
124-
[patch.crates-io]
125-
# See comments in `library/rustc-std-workspace-core/README.md` for what's going on
126-
# here
127-
rustc-std-workspace-core = { path = 'library/rustc-std-workspace-core' }
128-
rustc-std-workspace-alloc = { path = 'library/rustc-std-workspace-alloc' }
129-
rustc-std-workspace-std = { path = 'library/rustc-std-workspace-std' }

‎library/Cargo.lock

+489
Large diffs are not rendered by default.

‎library/Cargo.toml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[workspace]
2+
resolver = "1"
3+
members = [
4+
"std",
5+
"sysroot",
6+
]
7+
8+
exclude = [
9+
# stdarch has its own Cargo workspace
10+
"stdarch",
11+
]
12+
13+
[profile.release.package.compiler_builtins]
14+
# For compiler-builtins we always use a high number of codegen units.
15+
# The goal here is to place every single intrinsic into its own object
16+
# file to avoid symbol clashes with the system libgcc if possible. Note
17+
# that this number doesn't actually produce this many object files, we
18+
# just don't create more than this number of object files.
19+
#
20+
# It's a bit of a bummer that we have to pass this here, unfortunately.
21+
# Ideally this would be specified through an env var to Cargo so Cargo
22+
# knows how many CGUs are for this specific crate, but for now
23+
# per-crate configuration isn't specifiable in the environment.
24+
codegen-units = 10000
25+
26+
# These dependencies of the standard library implement symbolication for
27+
# backtraces on most platforms. Their debuginfo causes both linking to be slower
28+
# (more data to chew through) and binaries to be larger without really all that
29+
# much benefit. This section turns them all to down to have no debuginfo which
30+
# helps to improve link times a little bit.
31+
[profile.release.package]
32+
addr2line.debug = 0
33+
adler.debug = 0
34+
gimli.debug = 0
35+
miniz_oxide.debug = 0
36+
object.debug = 0
37+
rustc-demangle.debug = 0
38+
39+
[patch.crates-io]
40+
# See comments in `library/rustc-std-workspace-core/README.md` for what's going on
41+
# here
42+
rustc-std-workspace-core = { path = 'rustc-std-workspace-core' }
43+
rustc-std-workspace-alloc = { path = 'rustc-std-workspace-alloc' }
44+
rustc-std-workspace-std = { path = 'rustc-std-workspace-std' }

‎src/bootstrap/src/core/build_steps/dist.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,8 @@ impl Step for PlainSourceTarball {
10341034
.arg("--sync")
10351035
.arg(builder.src.join("./compiler/rustc_codegen_gcc/Cargo.toml"))
10361036
.arg("--sync")
1037+
.arg(builder.src.join("./library/Cargo.toml"))
1038+
.arg("--sync")
10371039
.arg(builder.src.join("./src/bootstrap/Cargo.toml"))
10381040
.arg("--sync")
10391041
.arg(builder.src.join("./src/tools/opt-dist/Cargo.toml"))

‎src/bootstrap/src/core/build_steps/vendor.rs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl Step for Vendor {
4747
"src/tools/rust-analyzer/Cargo.toml",
4848
"compiler/rustc_codegen_cranelift/Cargo.toml",
4949
"compiler/rustc_codegen_gcc/Cargo.toml",
50+
"library/Cargo.toml",
5051
"src/bootstrap/Cargo.toml",
5152
"src/tools/rustbook/Cargo.toml",
5253
] {

‎src/bootstrap/src/core/metadata.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ fn workspace_members(build: &Build) -> Vec<Package> {
8686
packages
8787
};
8888

89-
// Collects `metadata.packages` from all workspaces.
90-
collect_metadata("Cargo.toml")
89+
// Collects `metadata.packages` from the root and library workspaces.
90+
let mut packages = vec![];
91+
packages.extend(collect_metadata("Cargo.toml"));
92+
packages.extend(collect_metadata("library/Cargo.toml"));
93+
packages
9194
}

‎src/tools/tidy/src/deps.rs

+55-49
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type ExceptionList = &'static [(&'static str, &'static str)];
5454
pub(crate) const WORKSPACES: &[(&str, ExceptionList, Option<(&[&str], &[&str])>, &[&str])] = &[
5555
// The root workspace has to be first for check_rustfix to work.
5656
(".", EXCEPTIONS, Some((&["rustc-main"], PERMITTED_RUSTC_DEPENDENCIES)), &[]),
57+
("library", EXCEPTIONS_STDLIB, Some((&["sysroot"], PERMITTED_STDLIB_DEPENDENCIES)), &[]),
5758
// Outside of the alphabetical section because rustfmt formats it using multiple lines.
5859
(
5960
"compiler/rustc_codegen_cranelift",
@@ -90,7 +91,6 @@ const EXCEPTIONS: ExceptionList = &[
9091
("colored", "MPL-2.0"), // rustfmt
9192
("dissimilar", "Apache-2.0"), // rustdoc, rustc_lexer (few tests) via expect-test, (dev deps)
9293
("fluent-langneg", "Apache-2.0"), // rustc (fluent translations)
93-
("fortanix-sgx-abi", "MPL-2.0"), // libstd but only for `sgx` target. FIXME: this dependency violates the documentation comment above.
9494
("instant", "BSD-3-Clause"), // rustc_driver/tracing-subscriber/parking_lot
9595
("mdbook", "MPL-2.0"), // mdbook
9696
("option-ext", "MPL-2.0"), // cargo-miri (via `directories`)
@@ -108,6 +108,17 @@ const EXCEPTIONS: ExceptionList = &[
108108
// tidy-alphabetical-end
109109
];
110110

111+
/// These are exceptions to Rust's permissive licensing policy, and
112+
/// should be considered bugs. Exceptions are only allowed in Rust
113+
/// tooling. It is _crucial_ that no exception crates be dependencies
114+
/// of the Rust runtime (std/test).
115+
#[rustfmt::skip]
116+
const EXCEPTIONS_STDLIB: ExceptionList = &[
117+
// tidy-alphabetical-start
118+
("fortanix-sgx-abi", "MPL-2.0"), // libstd but only for `sgx` target. FIXME: this dependency violates the documentation comment above.
119+
// tidy-alphabetical-end
120+
];
121+
111122
// FIXME uncomment once rust-lang/stdarch#1462 lands
112123
/*
113124
const EXCEPTIONS_STDARCH: ExceptionList = &[
@@ -228,10 +239,6 @@ const EXCEPTIONS_NON_STANDARD_LICENSE_DEPS: &[&str] = &[
228239
"ring",
229240
];
230241

231-
/// These are the root crates that are part of the runtime. The licenses for
232-
/// these and all their dependencies *must not* be in the exception list.
233-
const RUNTIME_CRATES: &[&str] = &["std", "core", "alloc", "test", "panic_abort", "panic_unwind"];
234-
235242
const PERMITTED_DEPS_LOCATION: &str = concat!(file!(), ":", line!());
236243

237244
/// Crates rustc is allowed to depend on. Avoid adding to the list if possible.
@@ -240,7 +247,6 @@ const PERMITTED_DEPS_LOCATION: &str = concat!(file!(), ":", line!());
240247
/// rustc. Please check with the compiler team before adding an entry.
241248
const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
242249
// tidy-alphabetical-start
243-
"addr2line",
244250
"adler",
245251
"ahash",
246252
"aho-corasick",
@@ -256,7 +262,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
256262
"cc",
257263
"cfg-if",
258264
"cfg_aliases",
259-
"compiler_builtins",
260265
"cpufeatures",
261266
"crc32fast",
262267
"crossbeam-channel",
@@ -276,7 +281,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
276281
"digest",
277282
"displaydoc",
278283
"dissimilar",
279-
"dlmalloc",
280284
"either",
281285
"elsa",
282286
"ena",
@@ -291,7 +295,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
291295
"fluent-langneg",
292296
"fluent-syntax",
293297
"fnv",
294-
"fortanix-sgx-abi",
295298
"generic-array",
296299
"getopts",
297300
"getrandom",
@@ -354,12 +357,9 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
354357
"pulldown-cmark-escape",
355358
"punycode",
356359
"quote",
357-
"r-efi",
358-
"r-efi-alloc",
359360
"rand",
360361
"rand_chacha",
361362
"rand_core",
362-
"rand_xorshift",
363363
"rand_xoshiro",
364364
"redox_syscall",
365365
"regex",
@@ -429,7 +429,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
429429
"unicode-security",
430430
"unicode-width",
431431
"unicode-xid",
432-
"unwinding",
433432
"valuable",
434433
"version_check",
435434
"wasi",
@@ -463,6 +462,46 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
463462
// tidy-alphabetical-end
464463
];
465464

465+
const PERMITTED_STDLIB_DEPENDENCIES: &[&str] = &[
466+
// tidy-alphabetical-start
467+
"addr2line",
468+
"adler",
469+
"allocator-api2",
470+
"cc",
471+
"cfg-if",
472+
"compiler_builtins",
473+
"dlmalloc",
474+
"fortanix-sgx-abi",
475+
"getopts",
476+
"gimli",
477+
"hashbrown",
478+
"hermit-abi",
479+
"libc",
480+
"memchr",
481+
"miniz_oxide",
482+
"object",
483+
"r-efi",
484+
"r-efi-alloc",
485+
"rand",
486+
"rand_core",
487+
"rand_xorshift",
488+
"rustc-demangle",
489+
"unicode-width",
490+
"unwinding",
491+
"wasi",
492+
"windows-sys",
493+
"windows-targets",
494+
"windows_aarch64_gnullvm",
495+
"windows_aarch64_msvc",
496+
"windows_i686_gnu",
497+
"windows_i686_gnullvm",
498+
"windows_i686_msvc",
499+
"windows_x86_64_gnu",
500+
"windows_x86_64_gnullvm",
501+
"windows_x86_64_msvc",
502+
// tidy-alphabetical-end
503+
];
504+
466505
const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
467506
// tidy-alphabetical-start
468507
"ahash",
@@ -556,9 +595,8 @@ pub fn check(root: &Path, cargo: &Path, bad: &mut bool) {
556595
check_permitted_dependencies(&metadata, workspace, permitted_deps, crates, bad);
557596
}
558597

559-
if workspace == "." {
560-
let runtime_ids = compute_runtime_crates(&metadata);
561-
check_runtime_license_exceptions(&metadata, runtime_ids, bad);
598+
if workspace == "library" {
599+
check_runtime_license_exceptions(&metadata, bad);
562600
checked_runtime_licenses = true;
563601
}
564602
}
@@ -583,16 +621,8 @@ pub fn has_missing_submodule(root: &Path, submodules: &[&str]) -> bool {
583621
///
584622
/// Unlike for tools we don't allow exceptions to the `LICENSES` list for the runtime with the sole
585623
/// exception of `fortanix-sgx-abi` which is only used on x86_64-fortanix-unknown-sgx.
586-
fn check_runtime_license_exceptions(
587-
metadata: &Metadata,
588-
runtime_ids: HashSet<&PackageId>,
589-
bad: &mut bool,
590-
) {
624+
fn check_runtime_license_exceptions(metadata: &Metadata, bad: &mut bool) {
591625
for pkg in &metadata.packages {
592-
if !runtime_ids.contains(&pkg.id) {
593-
// Only checking dependencies of runtime libraries here.
594-
continue;
595-
}
596626
if pkg.source.is_none() {
597627
// No need to check local packages.
598628
continue;
@@ -613,20 +643,6 @@ fn check_runtime_license_exceptions(
613643
continue;
614644
}
615645

616-
// This exception is due to the fact that the feature set of the
617-
// `object` crate is different between rustc and libstd. In the
618-
// standard library only a conservative set of features are enabled
619-
// which notably does not include the `wasm` feature which pulls in
620-
// this dependency. In the compiler, however, the `wasm` feature is
621-
// enabled. This exception is intended to be here so long as the
622-
// `EXCEPTIONS` above contains `wasmparser`, but once that goes away
623-
// this can be removed.
624-
if pkg.name == "wasmparser"
625-
&& pkg.license.as_deref() == Some("Apache-2.0 WITH LLVM-exception")
626-
{
627-
continue;
628-
}
629-
630646
tidy_error!(bad, "invalid license `{}` in `{}`", license, pkg.id);
631647
}
632648
}
@@ -758,16 +774,6 @@ fn pkg_from_id<'a>(metadata: &'a Metadata, id: &PackageId) -> &'a Package {
758774
metadata.packages.iter().find(|p| &p.id == id).unwrap()
759775
}
760776

761-
/// Finds all the packages that are in the rust runtime.
762-
fn compute_runtime_crates<'a>(metadata: &'a Metadata) -> HashSet<&'a PackageId> {
763-
let mut result = HashSet::new();
764-
for name in RUNTIME_CRATES {
765-
let id = &pkg_from_name(metadata, name).id;
766-
deps_of(metadata, id, &mut result);
767-
}
768-
result
769-
}
770-
771777
/// Recursively find all dependencies.
772778
fn deps_of<'a>(metadata: &'a Metadata, pkg_id: &'a PackageId, result: &mut HashSet<&'a PackageId>) {
773779
if !result.insert(pkg_id) {

‎src/tools/tidy/src/edition.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ use std::path::Path;
44

55
use crate::walk::{filter_dirs, walk};
66

7-
fn is_edition_2021(mut line: &str) -> bool {
8-
line = line.trim();
9-
line == "edition = \"2021\""
10-
}
11-
127
pub fn check(path: &Path, bad: &mut bool) {
138
walk(path, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
149
let file = entry.path();
@@ -17,8 +12,15 @@ pub fn check(path: &Path, bad: &mut bool) {
1712
return;
1813
}
1914

20-
let is_2021 = contents.lines().any(is_edition_2021);
21-
if !is_2021 {
15+
let is_2021 = contents.lines().any(|line| line.trim() == "edition = \"2021\"");
16+
17+
let is_workspace = contents.lines().any(|line| line.trim() == "[workspace]");
18+
let is_package = contents.lines().any(|line| line.trim() == "[package]");
19+
assert!(is_workspace || is_package);
20+
21+
// Check that all packages use the 2021 edition. Virtual workspaces don't allow setting an
22+
// edition, so these shouldn't be checked.
23+
if is_package && !is_2021 {
2224
tidy_error!(
2325
bad,
2426
"{} doesn't have `edition = \"2021\"` on a separate line",
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
1+
thread 'main' panicked at core/src/panicking.rs:$LINE:$COL:
22
attempted to compute the size or alignment of extern type `Opaque`
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44
thread caused non-unwinding panic. aborting.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
1+
thread 'main' panicked at core/src/panicking.rs:$LINE:$COL:
22
attempted to compute the size or alignment of extern type `A`
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44
thread caused non-unwinding panic. aborting.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
1+
thread 'main' panicked at core/src/panicking.rs:$LINE:$COL:
22
attempted to compute the size or alignment of extern type `A`
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44
thread caused non-unwinding panic. aborting.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
thread 'main' panicked at library/alloc/src/raw_vec.rs:LL:CC:
1+
thread 'main' panicked at alloc/src/raw_vec.rs:LL:CC:
22
capacity overflow
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

‎tests/ui/panics/panic-in-cleanup.run.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44
thread 'main' panicked at $DIR/panic-in-cleanup.rs:16:9:
55
BOOM
66
stack backtrace:
7-
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
7+
thread 'main' panicked at core/src/panicking.rs:$LINE:$COL:
88
panic in a destructor during cleanup
99
thread caused non-unwinding panic. aborting.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
thread 'main' panicked at $DIR/panic-in-ffi.rs:12:5:
22
Test
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
4-
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
4+
thread 'main' panicked at core/src/panicking.rs:$LINE:$COL:
55
panic in a function that cannot unwind
66
stack backtrace:
77
thread caused non-unwinding panic. aborting.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
thread 'main' panicked at library/std/src/io/stdio.rs:LL:CC:
1+
thread 'main' panicked at std/src/io/stdio.rs:LL:CC:
22
failed printing to stdout: Broken pipe (os error 32)
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 commit comments

Comments
 (0)
Please sign in to comment.