Skip to content

Commit 646f2a5

Browse files
committed
Rustbuild: enable -Zsplit-metadata for stage != 0
1 parent 5ba7221 commit 646f2a5

File tree

12 files changed

+44
-20
lines changed

12 files changed

+44
-20
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_span::{
3333
};
3434
use tracing::{debug, instrument, trace};
3535

36-
use crate::errors::{FailCreateFileEncoder, FailWriteFile};
36+
use crate::errors::{FailCreateFileEncoder, FailWriteFile, FailedCreateFile};
3737
use crate::rmeta::*;
3838

3939
pub(super) struct EncodeContext<'a, 'tcx> {
@@ -2340,6 +2340,10 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: &Path) {
23402340
});
23412341
header.position.get()
23422342
});
2343+
} else {
2344+
std::fs::File::create(&ref_path).unwrap_or_else(|err| {
2345+
tcx.dcx().emit_fatal(FailedCreateFile { filename: &ref_path, err });
2346+
});
23432347
}
23442348
}
23452349

src/bootstrap/src/bin/rustc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ fn main() {
130130
}
131131
}
132132

133+
if orig_args.iter().any(|arg| arg == "-Zsplit-metadata")
134+
&& orig_args.windows(2).any(|args| args[0] == "--crate-type" && args[1] == "dylib")
135+
{
136+
cmd.arg("--emit").arg("metadata");
137+
}
138+
133139
// Print backtrace in case of ICE
134140
if env::var("RUSTC_BACKTRACE_ON_ICE").is_ok() && env::var("RUST_BACKTRACE").is_err() {
135141
cmd.env("RUST_BACKTRACE", "1");

src/bootstrap/src/core/build_steps/compile.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -2050,14 +2050,12 @@ pub fn run_cargo(
20502050
|| filename.ends_with(".a")
20512051
|| is_debug_info(&filename)
20522052
|| is_dylib(Path::new(&*filename))
2053+
|| filename.ends_with(".rmeta")
20532054
{
2054-
// Always keep native libraries, rust dylibs and debuginfo
2055+
// Always keep native libraries, rust dylibs, debuginfo and crate metadata
20552056
keep = true;
20562057
}
2057-
if is_check && filename.ends_with(".rmeta") {
2058-
// During check builds we need to keep crate metadata
2059-
keep = true;
2060-
} else if rlib_only_metadata {
2058+
if !is_check && rlib_only_metadata {
20612059
if filename.contains("jemalloc_sys")
20622060
|| filename.contains("rustc_smir")
20632061
|| filename.contains("stable_mir")
@@ -2069,7 +2067,6 @@ pub fn run_cargo(
20692067
// Distribute the rest of the rustc crates as rmeta files only to reduce
20702068
// the tarball sizes by about 50%. The object files are linked into
20712069
// librustc_driver.so, so it is still possible to link against them.
2072-
keep |= filename.ends_with(".rmeta");
20732070
}
20742071
} else {
20752072
// In all other cases keep all rlibs
@@ -2115,7 +2112,12 @@ pub fn run_cargo(
21152112
let file_stem = parts.next().unwrap().to_owned();
21162113
let extension = parts.next().unwrap().to_owned();
21172114

2118-
toplevel.push((file_stem, extension, expected_len));
2115+
if extension == "so" || extension == "dylib" {
2116+
// FIXME workaround for the fact that cargo doesn't understand `-Zsplit-metadata`
2117+
toplevel.push((file_stem.clone(), "rmeta".to_owned(), None));
2118+
}
2119+
2120+
toplevel.push((file_stem, extension, Some(expected_len)));
21192121
}
21202122
});
21212123

@@ -2136,7 +2138,7 @@ pub fn run_cargo(
21362138
.collect::<Vec<_>>();
21372139
for (prefix, extension, expected_len) in toplevel {
21382140
let candidates = contents.iter().filter(|&(_, filename, meta)| {
2139-
meta.len() == expected_len
2141+
expected_len.is_none_or(|expected_len| meta.len() == expected_len)
21402142
&& filename
21412143
.strip_prefix(&prefix[..])
21422144
.map(|s| s.starts_with('-') && s.ends_with(&extension[..]))
@@ -2147,6 +2149,7 @@ pub fn run_cargo(
21472149
});
21482150
let path_to_add = match max {
21492151
Some(triple) => triple.0.to_str().unwrap(),
2152+
None if extension == "rmeta" => continue, // cfg(not(bootstrap)) remove this once -Zsplit-metadata is passed for all stages
21502153
None => panic!("no output generated for {prefix:?} {extension:?}"),
21512154
};
21522155
if is_dylib(Path::new(path_to_add)) {

src/bootstrap/src/core/builder/cargo.rs

+12
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,18 @@ impl Builder<'_> {
626626
hostflags.arg("-Zunstable-options");
627627
hostflags.arg("--check-cfg=cfg(bootstrap)");
628628

629+
match mode {
630+
Mode::Std | Mode::Rustc | Mode::Codegen => {
631+
// cfg(bootstrap) unconditionally pass this once the bootstrap compiler understands it
632+
if stage != 0 {
633+
// FIXME remove once cargo enables this by default
634+
rustflags.arg("-Zsplit-metadata");
635+
}
636+
}
637+
// Tools may not expect to be compiled with -Zsplit-metadata
638+
Mode::ToolBootstrap | Mode::ToolStd | Mode::ToolRustc => {}
639+
}
640+
629641
// FIXME: It might be better to use the same value for both `RUSTFLAGS` and `RUSTDOCFLAGS`,
630642
// but this breaks CI. At the very least, stage0 `rustdoc` needs `--cfg bootstrap`. See
631643
// #71458.

tests/ui/duplicate_entry_error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ normalize-stderr: "loaded from .*libstd-.*.rlib" -> "loaded from SYSROOT/libstd-*.rlib"
1+
//@ normalize-stderr: "loaded from .*libstd-.*.rmeta" -> "loaded from SYSROOT/libstd-*.rmeta"
22
// note-pattern: first defined in crate `std`.
33

44
// Test for issue #31788 and E0152
@@ -11,7 +11,7 @@ use core::panic::PanicInfo;
1111

1212
#[lang = "panic_impl"]
1313
fn panic_impl(info: &PanicInfo) -> ! {
14-
//~^ ERROR: found duplicate lang item `panic_impl`
14+
//~^ ERROR: found duplicate lang item `panic_impl`
1515
loop {}
1616
}
1717

tests/ui/duplicate_entry_error.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | | }
88
| |_^
99
|
1010
= note: the lang item is first defined in crate `std` (which `duplicate_entry_error` depends on)
11-
= note: first definition in `std` loaded from SYSROOT/libstd-*.rlib
11+
= note: first definition in `std` loaded from SYSROOT/libstd-*.rmeta
1212
= note: second definition in the local crate (`duplicate_entry_error`)
1313

1414
error: aborting due to 1 previous error

tests/ui/error-codes/E0152.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
//@ normalize-stderr: "loaded from .*liballoc-.*.rlib" -> "loaded from SYSROOT/liballoc-*.rlib"
1+
//@ normalize-stderr: "loaded from .*liballoc-.*.rmeta" -> "loaded from SYSROOT/liballoc-*.rmeta"
22
#![feature(lang_items)]
33

44
#[lang = "owned_box"]
55
struct Foo<T>(T); //~ ERROR E0152
66

7-
fn main() {
8-
}
7+
fn main() {}

tests/ui/error-codes/E0152.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | struct Foo<T>(T);
55
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: the lang item is first defined in crate `alloc` (which `std` depends on)
8-
= note: first definition in `alloc` loaded from SYSROOT/liballoc-*.rlib
8+
= note: first definition in `alloc` loaded from SYSROOT/liballoc-*.rmeta
99
= note: second definition in the local crate (`E0152`)
1010

1111
error: aborting due to 1 previous error

tests/ui/lang-items/duplicate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ normalize-stderr: "loaded from .*libcore-.*.rlib" -> "loaded from SYSROOT/libcore-*.rlib"
1+
//@ normalize-stderr: "loaded from .*libcore-.*.rmeta" -> "loaded from SYSROOT/libcore-*.rmeta"
22
#![feature(lang_items)]
33

44
#[lang = "sized"]

tests/ui/lang-items/duplicate.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | trait Sized {}
55
| ^^^^^^^^^^^^^^
66
|
77
= note: the lang item is first defined in crate `core` (which `std` depends on)
8-
= note: first definition in `core` loaded from SYSROOT/libcore-*.rlib
8+
= note: first definition in `core` loaded from SYSROOT/libcore-*.rmeta
99
= note: second definition in the local crate (`duplicate`)
1010

1111
error: aborting due to 1 previous error

tests/ui/panic-handler/panic-handler-std.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ normalize-stderr: "loaded from .*libstd-.*.rlib" -> "loaded from SYSROOT/libstd-*.rlib"
1+
//@ normalize-stderr: "loaded from .*libstd-.*.rmeta" -> "loaded from SYSROOT/libstd-*.rmeta"
22
//@ error-pattern: found duplicate lang item `panic_impl`
33

44
extern crate core;

tests/ui/panic-handler/panic-handler-std.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | | }
77
| |_^
88
|
99
= note: the lang item is first defined in crate `std` (which `panic_handler_std` depends on)
10-
= note: first definition in `std` loaded from SYSROOT/libstd-*.rlib
10+
= note: first definition in `std` loaded from SYSROOT/libstd-*.rmeta
1111
= note: second definition in the local crate (`panic_handler_std`)
1212

1313
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)