Skip to content

Commit cdd182c

Browse files
committed
Auto merge of #115514 - onur-ozkan:bootstrap-codebase-improvements, r=albertlarsan68
optimize and cleanup bootstrap source I suggest reviewing this commit by commit.
2 parents 3ecc563 + 9971008 commit cdd182c

13 files changed

+97
-105
lines changed

src/bootstrap/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# rustbuild - Bootstrapping Rust
22

3-
This is an in-progress README which is targeted at helping to explain how Rust
4-
is bootstrapped and in general, some of the technical details of the build
5-
system.
3+
This README is aimed at helping to explain how Rust is bootstrapped and in general,
4+
some of the technical details of the build system.
65

76
Note that this README only covers internal information, not how to use the tool.
87
Please check [bootstrapping dev guide][bootstrapping-dev-guide] for further information.

src/bootstrap/bin/_helper.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// Parses the value of the "RUSTC_VERBOSE" environment variable and returns it as a `usize`.
2+
/// If it was not defined, returns 0 by default.
3+
///
4+
/// Panics if "RUSTC_VERBOSE" is defined with the value that is not an unsigned integer.
5+
fn parse_rustc_verbose() -> usize {
6+
use std::str::FromStr;
7+
8+
match std::env::var("RUSTC_VERBOSE") {
9+
Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
10+
Err(_) => 0,
11+
}
12+
}
13+
14+
/// Parses the value of the "RUSTC_STAGE" environment variable and returns it as a `String`.
15+
///
16+
/// If "RUSTC_STAGE" was not set, the program will be terminated with 101.
17+
fn parse_rustc_stage() -> String {
18+
std::env::var("RUSTC_STAGE").unwrap_or_else(|_| {
19+
// Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead.
20+
eprintln!("rustc shim: fatal: RUSTC_STAGE was not set");
21+
eprintln!("rustc shim: note: use `x.py build -vvv` to see all environment variables set by bootstrap");
22+
exit(101);
23+
})
24+
}

src/bootstrap/bin/rustc.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,25 @@
1616
//! never get replaced.
1717
1818
include!("../dylib_util.rs");
19+
include!("./_helper.rs");
1920

2021
use std::env;
2122
use std::path::PathBuf;
2223
use std::process::{exit, Child, Command};
23-
use std::str::FromStr;
2424
use std::time::Instant;
2525

2626
fn main() {
2727
let args = env::args_os().skip(1).collect::<Vec<_>>();
2828
let arg = |name| args.windows(2).find(|args| args[0] == name).and_then(|args| args[1].to_str());
2929

30+
let stage = parse_rustc_stage();
31+
let verbose = parse_rustc_verbose();
32+
3033
// Detect whether or not we're a build script depending on whether --target
3134
// is passed (a bit janky...)
3235
let target = arg("--target");
3336
let version = args.iter().find(|w| &**w == "-vV");
3437

35-
let verbose = match env::var("RUSTC_VERBOSE") {
36-
Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
37-
Err(_) => 0,
38-
};
39-
4038
// Use a different compiler for build scripts, since there may not yet be a
4139
// libstd for the real compiler to use. However, if Cargo is attempting to
4240
// determine the version of the compiler, the real compiler needs to be
@@ -47,12 +45,7 @@ fn main() {
4745
} else {
4846
("RUSTC_REAL", "RUSTC_LIBDIR")
4947
};
50-
let stage = env::var("RUSTC_STAGE").unwrap_or_else(|_| {
51-
// Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead.
52-
eprintln!("rustc shim: fatal: RUSTC_STAGE was not set");
53-
eprintln!("rustc shim: note: use `x.py build -vvv` to see all environment variables set by bootstrap");
54-
exit(101);
55-
});
48+
5649
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
5750
let on_fail = env::var_os("RUSTC_ON_FAIL").map(Command::new);
5851

src/bootstrap/bin/rustdoc.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use std::process::{exit, Command};
99

1010
include!("../dylib_util.rs");
1111

12+
include!("./_helper.rs");
13+
1214
fn main() {
1315
let args = env::args_os().skip(1).collect::<Vec<_>>();
14-
let stage = env::var("RUSTC_STAGE").unwrap_or_else(|_| {
15-
// Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead.
16-
eprintln!("rustc shim: fatal: RUSTC_STAGE was not set");
17-
eprintln!("rustc shim: note: use `x.py build -vvv` to see all environment variables set by bootstrap");
18-
exit(101);
19-
});
16+
17+
let stage = parse_rustc_stage();
18+
let verbose = parse_rustc_verbose();
19+
2020
let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set");
2121
let libdir = env::var_os("RUSTDOC_LIBDIR").expect("RUSTDOC_LIBDIR was not set");
2222
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
@@ -25,13 +25,6 @@ fn main() {
2525
// is passed (a bit janky...)
2626
let target = args.windows(2).find(|w| &*w[0] == "--target").and_then(|w| w[1].to_str());
2727

28-
use std::str::FromStr;
29-
30-
let verbose = match env::var("RUSTC_VERBOSE") {
31-
Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
32-
Err(_) => 0,
33-
};
34-
3528
let mut dylib_path = dylib_path();
3629
dylib_path.insert(0, PathBuf::from(libdir.clone()));
3730

src/bootstrap/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'a> ShouldRun<'a> {
525525
.iter()
526526
.map(|p| {
527527
// assert only if `p` isn't submodule
528-
if !submodules_paths.iter().find(|sm_p| p.contains(*sm_p)).is_some() {
528+
if submodules_paths.iter().find(|sm_p| p.contains(*sm_p)).is_none() {
529529
assert!(
530530
self.builder.src.join(p).exists(),
531531
"`should_run.paths` should correspond to real on-disk paths - use `alias` if there is no relevant path: {}",

src/bootstrap/compile.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -876,10 +876,8 @@ impl Step for Rustc {
876876
cargo.rustflag("-Clto=off");
877877
}
878878
}
879-
} else {
880-
if builder.config.rust_lto == RustcLto::Off {
881-
cargo.rustflag("-Clto=off");
882-
}
879+
} else if builder.config.rust_lto == RustcLto::Off {
880+
cargo.rustflag("-Clto=off");
883881
}
884882

885883
for krate in &*self.crates {

src/bootstrap/config.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -322,33 +322,23 @@ pub struct RustfmtMetadata {
322322
pub version: String,
323323
}
324324

325-
#[derive(Clone, Debug)]
325+
#[derive(Clone, Debug, Default)]
326326
pub enum RustfmtState {
327327
SystemToolchain(PathBuf),
328328
Downloaded(PathBuf),
329329
Unavailable,
330+
#[default]
330331
LazyEvaluated,
331332
}
332333

333-
impl Default for RustfmtState {
334-
fn default() -> Self {
335-
RustfmtState::LazyEvaluated
336-
}
337-
}
338-
339-
#[derive(Debug, Clone, Copy, PartialEq)]
334+
#[derive(Debug, Default, Clone, Copy, PartialEq)]
340335
pub enum LlvmLibunwind {
336+
#[default]
341337
No,
342338
InTree,
343339
System,
344340
}
345341

346-
impl Default for LlvmLibunwind {
347-
fn default() -> Self {
348-
Self::No
349-
}
350-
}
351-
352342
impl FromStr for LlvmLibunwind {
353343
type Err = String;
354344

@@ -362,19 +352,14 @@ impl FromStr for LlvmLibunwind {
362352
}
363353
}
364354

365-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
355+
#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
366356
pub enum SplitDebuginfo {
367357
Packed,
368358
Unpacked,
359+
#[default]
369360
Off,
370361
}
371362

372-
impl Default for SplitDebuginfo {
373-
fn default() -> Self {
374-
SplitDebuginfo::Off
375-
}
376-
}
377-
378363
impl std::str::FromStr for SplitDebuginfo {
379364
type Err = ();
380365

@@ -1529,7 +1514,7 @@ impl Config {
15291514
let asserts = llvm_assertions.unwrap_or(false);
15301515
config.llvm_from_ci = match llvm.download_ci_llvm {
15311516
Some(StringOrBool::String(s)) => {
1532-
assert!(s == "if-available", "unknown option `{s}` for download-ci-llvm");
1517+
assert_eq!(s, "if-available", "unknown option `{s}` for download-ci-llvm");
15331518
crate::llvm::is_ci_llvm_available(&config, asserts)
15341519
}
15351520
Some(StringOrBool::Bool(b)) => b,

src/bootstrap/config/tests.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ build-config = {}
136136
"setting string value without quotes"
137137
);
138138
assert_eq!(config.gdb, Some("bar".into()), "setting string value with quotes");
139-
assert_eq!(config.deny_warnings, false, "setting boolean value");
139+
assert!(!config.deny_warnings, "setting boolean value");
140140
assert_eq!(
141141
config.tools,
142142
Some(["cargo".to_string()].into_iter().collect()),
@@ -181,13 +181,13 @@ fn profile_user_dist() {
181181

182182
#[test]
183183
fn rust_optimize() {
184-
assert_eq!(parse("").rust_optimize.is_release(), true);
185-
assert_eq!(parse("rust.optimize = false").rust_optimize.is_release(), false);
186-
assert_eq!(parse("rust.optimize = true").rust_optimize.is_release(), true);
187-
assert_eq!(parse("rust.optimize = 0").rust_optimize.is_release(), false);
188-
assert_eq!(parse("rust.optimize = 1").rust_optimize.is_release(), true);
184+
assert!(parse("").rust_optimize.is_release());
185+
assert!(!parse("rust.optimize = false").rust_optimize.is_release());
186+
assert!(parse("rust.optimize = true").rust_optimize.is_release());
187+
assert!(!parse("rust.optimize = 0").rust_optimize.is_release());
188+
assert!(parse("rust.optimize = 1").rust_optimize.is_release());
189+
assert!(parse("rust.optimize = \"s\"").rust_optimize.is_release());
189190
assert_eq!(parse("rust.optimize = 1").rust_optimize.get_opt_level(), Some("1".to_string()));
190-
assert_eq!(parse("rust.optimize = \"s\"").rust_optimize.is_release(), true);
191191
assert_eq!(parse("rust.optimize = \"s\"").rust_optimize.get_opt_level(), Some("s".to_string()));
192192
}
193193

src/bootstrap/download.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl Config {
441441
}
442442

443443
pub(crate) fn download_beta_toolchain(&self) {
444-
self.verbose(&format!("downloading stage0 beta artifacts"));
444+
self.verbose("downloading stage0 beta artifacts");
445445

446446
let date = &self.stage0_metadata.compiler.date;
447447
let version = &self.stage0_metadata.compiler.version;

src/bootstrap/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub const VERSION: usize = 2;
116116

117117
/// Extra --check-cfg to add when building
118118
/// (Mode restriction, config name, config values (if any))
119-
const EXTRA_CHECK_CFGS: &[(Option<Mode>, &'static str, Option<&[&'static str]>)] = &[
119+
const EXTRA_CHECK_CFGS: &[(Option<Mode>, &str, Option<&[&'static str]>)] = &[
120120
(None, "bootstrap", None),
121121
(Some(Mode::Rustc), "parallel_compiler", None),
122122
(Some(Mode::ToolRustc), "parallel_compiler", None),
@@ -1757,10 +1757,11 @@ to download LLVM rather than building it.
17571757
//
17581758
// In these cases we automatically enable Ninja if we find it in the
17591759
// environment.
1760-
if !self.config.ninja_in_file && self.config.build.contains("msvc") {
1761-
if cmd_finder.maybe_have("ninja").is_some() {
1762-
return true;
1763-
}
1760+
if !self.config.ninja_in_file
1761+
&& self.config.build.contains("msvc")
1762+
&& cmd_finder.maybe_have("ninja").is_some()
1763+
{
1764+
return true;
17641765
}
17651766

17661767
self.config.ninja_in_file

src/bootstrap/llvm.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub(crate) fn detect_llvm_sha(config: &Config, is_git: bool) -> String {
155155
"".to_owned()
156156
};
157157

158-
if &llvm_sha == "" {
158+
if llvm_sha.is_empty() {
159159
eprintln!("error: could not find commit hash for downloading LLVM");
160160
eprintln!("help: maybe your repository history is too shallow?");
161161
eprintln!("help: consider disabling `download-ci-llvm`");
@@ -208,10 +208,10 @@ pub(crate) fn is_ci_llvm_available(config: &Config, asserts: bool) -> bool {
208208
("x86_64-unknown-netbsd", false),
209209
];
210210

211-
if !supported_platforms.contains(&(&*config.build.triple, asserts)) {
212-
if asserts == true || !supported_platforms.contains(&(&*config.build.triple, true)) {
213-
return false;
214-
}
211+
if !supported_platforms.contains(&(&*config.build.triple, asserts))
212+
&& (asserts || !supported_platforms.contains(&(&*config.build.triple, true)))
213+
{
214+
return false;
215215
}
216216

217217
if is_ci_llvm_modified(config) {
@@ -497,11 +497,11 @@ impl Step for Llvm {
497497
let mut cmd = Command::new(&res.llvm_config);
498498
let version = output(cmd.arg("--version"));
499499
let major = version.split('.').next().unwrap();
500-
let lib_name = match &llvm_version_suffix {
500+
501+
match &llvm_version_suffix {
501502
Some(version_suffix) => format!("libLLVM-{major}{version_suffix}.{extension}"),
502503
None => format!("libLLVM-{major}.{extension}"),
503-
};
504-
lib_name
504+
}
505505
};
506506

507507
// When building LLVM with LLVM_LINK_LLVM_DYLIB for macOS, an unversioned
@@ -756,13 +756,15 @@ fn configure_cmake(
756756

757757
// For distribution we want the LLVM tools to be *statically* linked to libstdc++.
758758
// We also do this if the user explicitly requested static libstdc++.
759-
if builder.config.llvm_static_stdcpp {
760-
if !target.contains("msvc") && !target.contains("netbsd") && !target.contains("solaris") {
761-
if target.contains("apple") || target.contains("windows") {
762-
ldflags.push_all("-static-libstdc++");
763-
} else {
764-
ldflags.push_all("-Wl,-Bsymbolic -static-libstdc++");
765-
}
759+
if builder.config.llvm_static_stdcpp
760+
&& !target.contains("msvc")
761+
&& !target.contains("netbsd")
762+
&& !target.contains("solaris")
763+
{
764+
if target.contains("apple") || target.contains("windows") {
765+
ldflags.push_all("-static-libstdc++");
766+
} else {
767+
ldflags.push_all("-Wl,-Bsymbolic -static-libstdc++");
766768
}
767769
}
768770

src/bootstrap/sanity.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,19 @@ pub fn check(build: &mut Build) {
9595
.unwrap_or(true)
9696
})
9797
.any(|build_llvm_ourselves| build_llvm_ourselves);
98+
9899
let need_cmake = building_llvm || build.config.any_sanitizers_enabled();
99-
if need_cmake {
100-
if cmd_finder.maybe_have("cmake").is_none() {
101-
eprintln!(
102-
"
100+
if need_cmake && cmd_finder.maybe_have("cmake").is_none() {
101+
eprintln!(
102+
"
103103
Couldn't find required command: cmake
104104
105105
You should install cmake, or set `download-ci-llvm = true` in the
106106
`[llvm]` section of `config.toml` to download LLVM rather
107107
than building it.
108108
"
109-
);
110-
crate::exit!(1);
111-
}
109+
);
110+
crate::exit!(1);
112111
}
113112

114113
build.config.python = build
@@ -202,10 +201,10 @@ than building it.
202201
.entry(*target)
203202
.or_insert_with(|| Target::from_triple(&target.triple));
204203

205-
if target.contains("-none-") || target.contains("nvptx") {
206-
if build.no_std(*target) == Some(false) {
207-
panic!("All the *-none-* and nvptx* targets are no-std targets")
208-
}
204+
if (target.contains("-none-") || target.contains("nvptx"))
205+
&& build.no_std(*target) == Some(false)
206+
{
207+
panic!("All the *-none-* and nvptx* targets are no-std targets")
209208
}
210209

211210
// Some environments don't want or need these tools, such as when testing Miri.

0 commit comments

Comments
 (0)