Skip to content

Commit d093c9d

Browse files
committed
Rustbuild: enable -Zsplit-metadata for stage != 0
1 parent d292dca commit d093c9d

File tree

13 files changed

+45
-20
lines changed

13 files changed

+45
-20
lines changed

compiler/rustc_codegen_cranelift/build_system/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ impl<'a> TestRunner<'a> {
433433
cmd.arg(&self.target_compiler.triple);
434434
cmd.arg("-Cpanic=abort");
435435
cmd.arg("--check-cfg=cfg(jit)");
436+
cmd.arg("--emit=metadata,link");
436437
cmd.args(args);
437438
cmd
438439
}

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> {
@@ -2312,6 +2312,10 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: &Path) {
23122312
});
23132313
header.position.get()
23142314
});
2315+
} else {
2316+
std::fs::File::create(&ref_path).unwrap_or_else(|err| {
2317+
tcx.dcx().emit_fatal(FailedCreateFile { filename: &ref_path, err });
2318+
});
23152319
}
23162320
}
23172321

src/bootstrap/src/bin/rustc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ fn main() {
127127
}
128128
}
129129

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

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -2070,14 +2070,12 @@ pub fn run_cargo(
20702070
|| filename.ends_with(".a")
20712071
|| is_debug_info(&filename)
20722072
|| is_dylib(&filename)
2073+
|| filename.ends_with(".rmeta")
20732074
{
2074-
// Always keep native libraries, rust dylibs and debuginfo
2075+
// Always keep native libraries, rust dylibs, debuginfo and crate metadata
20752076
keep = true;
20762077
}
2077-
if is_check && filename.ends_with(".rmeta") {
2078-
// During check builds we need to keep crate metadata
2079-
keep = true;
2080-
} else if rlib_only_metadata {
2078+
if !is_check && rlib_only_metadata {
20812079
if filename.contains("jemalloc_sys")
20822080
|| filename.contains("rustc_smir")
20832081
|| filename.contains("stable_mir")
@@ -2089,7 +2087,6 @@ pub fn run_cargo(
20892087
// Distribute the rest of the rustc crates as rmeta files only to reduce
20902088
// the tarball sizes by about 50%. The object files are linked into
20912089
// librustc_driver.so, so it is still possible to link against them.
2092-
keep |= filename.ends_with(".rmeta");
20932090
}
20942091
} else {
20952092
// In all other cases keep all rlibs
@@ -2135,7 +2132,12 @@ pub fn run_cargo(
21352132
let file_stem = parts.next().unwrap().to_owned();
21362133
let extension = parts.next().unwrap().to_owned();
21372134

2138-
toplevel.push((file_stem, extension, expected_len));
2135+
if extension == "so" || extension == "dylib" {
2136+
// FIXME workaround for the fact that cargo doesn't understand `-Zsplit-metadata`
2137+
toplevel.push((file_stem.clone(), "rmeta".to_owned(), None));
2138+
}
2139+
2140+
toplevel.push((file_stem, extension, Some(expected_len)));
21392141
}
21402142
});
21412143

@@ -2156,7 +2158,7 @@ pub fn run_cargo(
21562158
.collect::<Vec<_>>();
21572159
for (prefix, extension, expected_len) in toplevel {
21582160
let candidates = contents.iter().filter(|&(_, filename, meta)| {
2159-
meta.len() == expected_len
2161+
expected_len.map_or(true, |expected_len| meta.len() == expected_len)
21602162
&& filename
21612163
.strip_prefix(&prefix[..])
21622164
.map(|s| s.starts_with('-') && s.ends_with(&extension[..]))
@@ -2167,6 +2169,7 @@ pub fn run_cargo(
21672169
});
21682170
let path_to_add = match max {
21692171
Some(triple) => triple.0.to_str().unwrap(),
2172+
None if extension == "rmeta" => continue, // cfg(not(bootstrap)) remove this once -Zsplit-metadata is passed for all stages
21702173
None => panic!("no output generated for {prefix:?} {extension:?}"),
21712174
};
21722175
if is_dylib(path_to_add) {

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

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

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

tests/ui/duplicate_entry_error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ normalize-stderr-test: "loaded from .*libstd-.*.rlib" -> "loaded from SYSROOT/libstd-*.rlib"
1+
//@ normalize-stderr-test: "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-test: "loaded from .*liballoc-.*.rlib" -> "loaded from SYSROOT/liballoc-*.rlib"
1+
//@ normalize-stderr-test: "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-test: "loaded from .*libcore-.*.rlib" -> "loaded from SYSROOT/libcore-*.rlib"
1+
//@ normalize-stderr-test: "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-test: "loaded from .*libstd-.*.rlib" -> "loaded from SYSROOT/libstd-*.rlib"
1+
//@ normalize-stderr-test: "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)