Skip to content

Commit ef62af0

Browse files
committed
Auto merge of #149514 - adamgemmell:dev/adagem01/bootstrap-to-library, r=<try>
Move bootstrap configuration to library workspace
2 parents fffc4fc + ffce9ca commit ef62af0

9 files changed

Lines changed: 97 additions & 62 deletions

File tree

library/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ rustflags = ["-Cpanic=abort"]
5454
[profile.release.package.panic_abort]
5555
rustflags = ["-Cpanic=abort"]
5656

57+
# The "dist" profile is used by bootstrap for prebuilt libstd artifacts
58+
# These settings ensure that the prebuilt artifacts support a variety of features
59+
# in the user's profile.
60+
[profile.dist]
61+
inherits = "release"
62+
codegen-units = 1
63+
debug = 1 # "limited"
64+
rustflags = [
65+
# `profile.lto=off` implies `-Cembed-bitcode=no`, but unconditionally embedding
66+
# bitcode is necessary for when users enable LTO.
67+
# Required until Cargo can re-build the standard library based on the value
68+
# of `profile.lto` in the user's profile.
69+
"-Cembed-bitcode=yes",
70+
# Enable frame pointers
71+
"-Zunstable-options",
72+
"-Cforce-frame-pointers=non-leaf",
73+
]
74+
75+
[profile.dist.package.panic_abort]
76+
rustflags = [
77+
"-Cpanic=abort",
78+
"-Cembed-bitcode=yes",
79+
"-Zunstable-options",
80+
"-Cforce-frame-pointers=non-leaf",
81+
]
82+
5783
[patch.crates-io]
5884
# See comments in `library/rustc-std-workspace-core/README.md` for what's going on here
5985
rustc-std-workspace-core = { path = 'rustc-std-workspace-core' }

library/test/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
println!("cargo:rustc-check-cfg=cfg(enable_unstable_features)");
3+
4+
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".into());
5+
let version = std::process::Command::new(rustc).arg("-vV").output().unwrap();
6+
let stdout = String::from_utf8(version.stdout).unwrap();
7+
8+
if stdout.contains("nightly") || stdout.contains("dev") {
9+
println!("cargo:rustc-cfg=enable_unstable_features");
10+
}
11+
}

library/test/src/cli.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,14 @@ fn parse_opts_impl(matches: getopts::Matches) -> OptRes {
314314
Ok(test_opts)
315315
}
316316

317-
// FIXME: Copied from librustc_ast until linkage errors are resolved. Issue #47566
318317
fn is_nightly() -> bool {
319-
// Whether this is a feature-staged build, i.e., on the beta or stable channel
320-
let disable_unstable_features =
321-
option_env!("CFG_DISABLE_UNSTABLE_FEATURES").map(|s| s != "0").unwrap_or(false);
322-
// Whether we should enable unstable features for bootstrapping
318+
// Whether the current rustc version should allow unstable features
319+
let enable_unstable_features = cfg!(enable_unstable_features);
320+
321+
// The runtime override for unstable features
323322
let bootstrap = env::var("RUSTC_BOOTSTRAP").is_ok();
324323

325-
bootstrap || !disable_unstable_features
324+
bootstrap || enable_unstable_features
326325
}
327326

328327
// Gets the CLI options associated with `report-time` feature.

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,6 @@ pub fn std_cargo(
626626
CompilerBuiltins::BuildRustOnly => "",
627627
};
628628

629-
// `libtest` uses this to know whether or not to support
630-
// `-Zunstable-options`.
631-
if !builder.unstable_features() {
632-
cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
633-
}
634-
635629
for krate in crates {
636630
cargo.args(["-p", krate]);
637631
}
@@ -680,13 +674,6 @@ pub fn std_cargo(
680674
}
681675
}
682676

683-
// By default, rustc uses `-Cembed-bitcode=yes`, and Cargo overrides that
684-
// with `-Cembed-bitcode=no` for non-LTO builds. However, libstd must be
685-
// built with bitcode so that the produced rlibs can be used for both LTO
686-
// builds (which use bitcode) and non-LTO builds (which use object code).
687-
// So we override the override here!
688-
cargo.rustflag("-Cembed-bitcode=yes");
689-
690677
if builder.config.rust_lto == RustcLto::Off {
691678
cargo.rustflag("-Clto=off");
692679
}
@@ -701,11 +688,6 @@ pub fn std_cargo(
701688
cargo.rustflag("-Cforce-unwind-tables=yes");
702689
}
703690

704-
// Enable frame pointers by default for the library. Note that they are still controlled by a
705-
// separate setting for the compiler.
706-
cargo.rustflag("-Zunstable-options");
707-
cargo.rustflag("-Cforce-frame-pointers=non-leaf");
708-
709691
let html_root =
710692
format!("-Zcrate-attr=doc(html_root_url=\"{}/\")", builder.doc_rust_lang_org_channel(),);
711693
cargo.rustflag(&html_root);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ impl Step for Analysis {
10081008
let src = builder
10091009
.stage_out(compiler, Mode::Std)
10101010
.join(target)
1011-
.join(builder.cargo_dir())
1011+
.join(builder.cargo_dir(Mode::Std))
10121012
.join("deps")
10131013
.join("save-analysis");
10141014

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,9 @@ impl Step for Clippy {
927927

928928
cargo.env("RUSTC_TEST_SUITE", builder.rustc(build_compiler));
929929
cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(build_compiler));
930-
let host_libs =
931-
builder.stage_out(build_compiler, Mode::ToolRustcPrivate).join(builder.cargo_dir());
930+
let host_libs = builder
931+
.stage_out(build_compiler, Mode::ToolRustcPrivate)
932+
.join(builder.cargo_dir(Mode::ToolRustcPrivate));
932933
cargo.env("HOST_LIBS", host_libs);
933934

934935
// Build the standard library that the tests can use.

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl Step for ToolBuild {
133133
RustcLto::ThinLocal => None,
134134
};
135135
if let Some(lto) = lto {
136-
cargo.env(cargo_profile_var("LTO", &builder.config), lto);
136+
cargo.env(cargo_profile_var("LTO", &builder.config, self.mode), lto);
137137
}
138138
}
139139

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

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ pub struct Cargo {
106106
rustdocflags: Rustflags,
107107
hostflags: HostFlags,
108108
allow_features: String,
109-
release_build: bool,
110109
build_compiler_stage: u32,
111110
extra_rustflags: Vec<String>,
111+
profile: Option<&'static str>,
112112
}
113113

114114
impl Cargo {
@@ -137,7 +137,11 @@ impl Cargo {
137137
}
138138

139139
pub fn release_build(&mut self, release_build: bool) {
140-
self.release_build = release_build;
140+
self.profile = if release_build { Some("release") } else { None };
141+
}
142+
143+
pub fn profile(&mut self, profile: &'static str) {
144+
self.profile = Some(profile)
141145
}
142146

143147
pub fn compiler(&self) -> Compiler {
@@ -407,8 +411,8 @@ impl Cargo {
407411

408412
impl From<Cargo> for BootstrapCommand {
409413
fn from(mut cargo: Cargo) -> BootstrapCommand {
410-
if cargo.release_build {
411-
cargo.args.insert(0, "--release".into());
414+
if let Some(profile) = cargo.profile {
415+
cargo.args.insert(0, format!("--profile={profile}").into());
412416
}
413417

414418
for arg in &cargo.extra_rustflags {
@@ -598,7 +602,7 @@ impl Builder<'_> {
598602
build_stamp::clear_if_dirty(self, &my_out, &rustdoc);
599603
}
600604

601-
let profile_var = |name: &str| cargo_profile_var(name, &self.config);
605+
let profile_var = |name: &str| cargo_profile_var(name, &self.config, mode);
602606

603607
// See comment in rustc_llvm/build.rs for why this is necessary, largely llvm-config
604608
// needs to not accidentally link to libLLVM in stage0/lib.
@@ -660,23 +664,15 @@ impl Builder<'_> {
660664
rustflags.arg(sysroot_str);
661665
}
662666

663-
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
664-
Some(setting) => {
665-
// If an explicit setting is given, use that
666-
setting
667-
}
668-
// Per compiler-team#938, v0 mangling is used on nightly
669-
None if self.config.channel == "dev" || self.config.channel == "nightly" => true,
670-
None => {
671-
if mode == Mode::Std {
672-
// The standard library defaults to the legacy scheme
673-
false
674-
} else {
675-
// The compiler and tools default to the new scheme
676-
true
677-
}
667+
let use_new_symbol_mangling = self.config.rust_new_symbol_mangling.or_else(|| {
668+
if mode != Mode::Std {
669+
// The compiler and tools default to the new scheme
670+
Some(true)
671+
} else {
672+
// std follows the flag's default, which per compiler-team#938 is v0 on nightly
673+
None
678674
}
679-
};
675+
});
680676

681677
// By default, windows-rs depends on a native library that doesn't get copied into the
682678
// sysroot. Passing this cfg enables raw-dylib support instead, which makes the native
@@ -687,10 +683,12 @@ impl Builder<'_> {
687683
rustflags.arg("--cfg=windows_raw_dylib");
688684
}
689685

690-
if use_new_symbol_mangling {
691-
rustflags.arg("-Csymbol-mangling-version=v0");
692-
} else {
693-
rustflags.arg("-Csymbol-mangling-version=legacy");
686+
if let Some(usm) = use_new_symbol_mangling {
687+
rustflags.arg(if usm {
688+
"-Csymbol-mangling-version=v0"
689+
} else {
690+
"-Csymbol-mangling-version=legacy"
691+
});
694692
}
695693

696694
// Always enable move/copy annotations for profiler visibility (non-stage0 only).
@@ -1434,9 +1432,18 @@ impl Builder<'_> {
14341432
.unwrap_or(&self.config.rust_rustflags)
14351433
.clone();
14361434

1437-
let release_build = self.config.rust_optimize.is_release() &&
1438-
// cargo bench/install do not accept `--release` and miri doesn't want it
1439-
!matches!(cmd_kind, Kind::Bench | Kind::Install | Kind::Miri | Kind::MiriSetup | Kind::MiriTest);
1435+
let profile =
1436+
if matches!(cmd_kind, Kind::Bench | Kind::Miri | Kind::MiriSetup | Kind::MiriTest) {
1437+
// Use the default profile for bench/miri
1438+
None
1439+
} else {
1440+
match (mode, self.config.rust_optimize.is_release()) {
1441+
// Some std configuration exists in its own profile
1442+
(Mode::Std, _) => Some("dist"),
1443+
(_, true) => Some("release"),
1444+
(_, false) => Some("dev"),
1445+
}
1446+
};
14401447

14411448
Cargo {
14421449
command: cargo,
@@ -1448,14 +1455,19 @@ impl Builder<'_> {
14481455
rustdocflags,
14491456
hostflags,
14501457
allow_features,
1451-
release_build,
14521458
build_compiler_stage,
14531459
extra_rustflags,
1460+
profile,
14541461
}
14551462
}
14561463
}
14571464

1458-
pub fn cargo_profile_var(name: &str, config: &Config) -> String {
1459-
let profile = if config.rust_optimize.is_release() { "RELEASE" } else { "DEV" };
1465+
pub fn cargo_profile_var(name: &str, config: &Config, mode: Mode) -> String {
1466+
let profile = match (mode, config.rust_optimize.is_release()) {
1467+
// Some std configuration exists in its own profile
1468+
(Mode::Std, _) => "DIST",
1469+
(_, true) => "RELEASE",
1470+
(_, false) => "DEV",
1471+
};
14601472
format!("CARGO_PROFILE_{profile}_{name}")
14611473
}

src/bootstrap/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,12 @@ impl Build {
898898

899899
/// Component directory that Cargo will produce output into (e.g.
900900
/// release/debug)
901-
fn cargo_dir(&self) -> &'static str {
902-
if self.config.rust_optimize.is_release() { "release" } else { "debug" }
901+
fn cargo_dir(&self, mode: Mode) -> &'static str {
902+
match (mode, self.config.rust_optimize.is_release()) {
903+
(Mode::Std, _) => "dist",
904+
(_, true) => "release",
905+
(_, false) => "debug",
906+
}
903907
}
904908

905909
fn tools_dir(&self, build_compiler: Compiler) -> PathBuf {
@@ -956,7 +960,7 @@ impl Build {
956960
/// running a particular compiler, whether or not we're building the
957961
/// standard library, and targeting the specified architecture.
958962
fn cargo_out(&self, build_compiler: Compiler, mode: Mode, target: TargetSelection) -> PathBuf {
959-
self.stage_out(build_compiler, mode).join(target).join(self.cargo_dir())
963+
self.stage_out(build_compiler, mode).join(target).join(self.cargo_dir(mode))
960964
}
961965

962966
/// Root output directory of LLVM for `target`

0 commit comments

Comments
 (0)