Skip to content

Commit fdcd154

Browse files
committed
remove set and some more refactor
1 parent d5289d7 commit fdcd154

File tree

2 files changed

+51
-73
lines changed

2 files changed

+51
-73
lines changed

src/bootstrap/src/core/config/config.rs

Lines changed: 51 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use crate::core::config::toml::rust::{
4747
use crate::core::config::toml::target::Target;
4848
use crate::core::config::{
4949
DebuginfoLevel, DryRun, GccCiMode, LlvmLibunwind, Merge, ReplaceOpt, RustcLto, SplitDebuginfo,
50-
StringOrBool, set, threads_from_config,
50+
StringOrBool, threads_from_config,
5151
};
5252
use crate::core::download::{
5353
DownloadContext, download_beta_toolchain, is_download_ci_available, maybe_download_rustfmt,
@@ -479,8 +479,6 @@ impl Config {
479479
// Now load the TOML config, as soon as possible
480480
let (mut toml, toml_path) = load_toml_config(&src, flags_config, &get_toml);
481481

482-
let compile_time_deps = flags_compile_time_deps;
483-
let cmd = flags_cmd;
484482
let is_running_on_ci = flags_ci.unwrap_or(CiEnv::is_ci());
485483

486484
postprocess_toml(&mut toml, &src, toml_path.clone(), &exec_ctx, &flags_set, &get_toml);
@@ -654,6 +652,14 @@ impl Config {
654652

655653
let Gcc { download_ci_gcc: gcc_download_ci_gcc } = toml.gcc.unwrap_or_default();
656654

655+
if rust_optimize_.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) {
656+
eprintln!(
657+
"WARNING: setting `optimize` to `false` is known to cause errors and \
658+
should be considered unsupported. Refer to `bootstrap.example.toml` \
659+
for more details."
660+
);
661+
}
662+
657663
// Prefer CLI verbosity flags if set (`flags_verbose` > 0), otherwise take the value from
658664
// TOML.
659665
exec_ctx.set_verbosity(cmp::max(build_verbose.unwrap_or_default() as u8, flags_verbose));
@@ -664,14 +670,6 @@ impl Config {
664670

665671
let path_modification_cache = Arc::new(Mutex::new(HashMap::new()));
666672

667-
if rust_optimize_.as_ref().is_some_and(|v| matches!(v, RustOptimize::Bool(false))) {
668-
eprintln!(
669-
"WARNING: setting `optimize` to `false` is known to cause errors and \
670-
should be considered unsupported. Refer to `bootstrap.example.toml` \
671-
for more details."
672-
);
673-
}
674-
675673
let host_target = flags_build
676674
.or(build_build)
677675
.map(|build| TargetSelection::from_user(&build))
@@ -690,7 +688,6 @@ impl Config {
690688
let mut download_rustc_commit = None;
691689
let llvm_link_shared = Cell::default();
692690
let mut llvm_from_ci = false;
693-
let mut lld_enabled = false;
694691
let mut channel = "dev".to_string();
695692
let mut out = flags_build_dir
696693
.or(build_build_dir.map(PathBuf::from))
@@ -756,12 +753,10 @@ impl Config {
756753
is_running_on_ci,
757754
);
758755

759-
let initial_rustc = if let Some(rustc) = build_rustc {
760-
rustc
761-
} else {
756+
let initial_rustc = build_rustc.unwrap_or_else(|| {
762757
download_beta_toolchain(&dwn_ctx);
763758
out.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
764-
};
759+
});
765760

766761
let initial_sysroot = t!(PathBuf::from_str(
767762
command(&initial_rustc)
@@ -772,20 +767,15 @@ impl Config {
772767
.trim()
773768
));
774769

775-
let initial_cargo_clippy = build_cargo_clippy;
776-
777-
let initial_cargo = if let Some(cargo) = build_cargo {
778-
cargo
779-
} else {
780-
download_beta_toolchain(&dwn_ctx);
770+
let initial_cargo = build_cargo.unwrap_or_else(|| {
771+
download_beta_toolchain(&mut dwn_ctx);
781772
initial_sysroot.join("bin").join(exe("cargo", host_target))
782-
};
773+
});
783774

784775
// NOTE: it's important this comes *after* we set `initial_rustc` just above.
785776
if exec_ctx.dry_run() {
786-
let dir = out.join("tmp-dry-run");
787-
t!(fs::create_dir_all(&dir));
788-
out = dir;
777+
out = out.join("tmp-dry-run");
778+
fs::create_dir_all(&out).expect("Failed to create dry-run directory");
789779
dwn_ctx.out = out.clone();
790780
}
791781

@@ -809,12 +799,6 @@ impl Config {
809799
rust_info = git_info(&exec_ctx, omit_git_hash, &src);
810800
dwn_ctx.rust_info = rust_info.clone();
811801

812-
let vendor = build_vendor.unwrap_or(
813-
rust_info.is_from_tarball()
814-
&& src.join("vendor").exists()
815-
&& src.join(".cargo/config.toml").exists(),
816-
);
817-
818802
if !is_user_configured_rust_channel && rust_info.is_from_tarball() {
819803
channel = ci_channel.into();
820804
}
@@ -858,6 +842,23 @@ impl Config {
858842
dwn_ctx.download_rustc_commit = None;
859843
}
860844

845+
// We need to override `rust.channel` if it's manually specified when using the CI rustc.
846+
// This is because if the compiler uses a different channel than the one specified in bootstrap.toml,
847+
// tests may fail due to using a different channel than the one used by the compiler during tests.
848+
if let Some(commit) = &download_rustc_commit
849+
&& is_user_configured_rust_channel
850+
{
851+
println!(
852+
"WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel."
853+
);
854+
855+
let channel_ = read_file_by_commit(&dwn_ctx, Path::new("src/ci/channel"), commit)
856+
.trim()
857+
.to_owned();
858+
859+
channel = channel_;
860+
}
861+
861862
if let Some(t) = toml.target {
862863
for (triple, cfg) in t {
863864
let mut target = Target::from_triple(&triple);
@@ -932,35 +933,15 @@ impl Config {
932933
// thus, disabled
933934
// - similarly, lld will not be built nor used by default when explicitly asked not to, e.g.
934935
// when the config sets `rust.lld = false`
935-
if default_lld_opt_in_targets().contains(&host_target.triple.to_string())
936+
let lld_enabled = if default_lld_opt_in_targets().contains(&host_target.triple.to_string())
936937
&& hosts == [host_target]
937938
{
938-
let no_llvm_config = target_config
939-
.get(&host_target)
940-
.is_none_or(|target_config| target_config.llvm_config.is_none());
941-
let enable_lld = llvm_from_ci || no_llvm_config;
942-
// Prefer the config setting in case an explicit opt-out is needed.
943-
lld_enabled = rust_lld_enabled.unwrap_or(enable_lld);
939+
let no_llvm_config =
940+
target_config.get(&host_target).map_or(true, |config| config.llvm_config.is_none());
941+
rust_lld_enabled.unwrap_or(llvm_from_ci || no_llvm_config)
944942
} else {
945-
set(&mut lld_enabled, rust_lld_enabled);
946-
}
947-
948-
// We need to override `rust.channel` if it's manually specified when using the CI rustc.
949-
// This is because if the compiler uses a different channel than the one specified in bootstrap.toml,
950-
// tests may fail due to using a different channel than the one used by the compiler during tests.
951-
if let Some(commit) = &download_rustc_commit
952-
&& is_user_configured_rust_channel
953-
{
954-
println!(
955-
"WARNING: `rust.download-rustc` is enabled. The `rust.channel` option will be overridden by the CI rustc's channel."
956-
);
957-
958-
let channel_ = read_file_by_commit(&dwn_ctx, Path::new("src/ci/channel"), commit)
959-
.trim()
960-
.to_owned();
961-
962-
channel = channel_;
963-
}
943+
rust_lld_enabled.unwrap_or(false)
944+
};
964945

965946
if let Some(v) = llvm_link_shared_ {
966947
llvm_link_shared.set(Some(v));
@@ -1024,8 +1005,7 @@ impl Config {
10241005
build_target.llvm_filecheck = Some(ci_llvm_bin.join(exe("FileCheck", host_target)));
10251006
}
10261007

1027-
let initial_rustfmt =
1028-
if let Some(r) = build_rustfmt { Some(r) } else { maybe_download_rustfmt(&dwn_ctx) };
1008+
let initial_rustfmt = build_rustfmt.or_else(|| maybe_download_rustfmt(&dwn_ctx));
10291009

10301010
if matches!(rust_lld_mode.unwrap_or_default(), LldMode::SelfContained)
10311011
&& !lld_enabled
@@ -1042,7 +1022,7 @@ impl Config {
10421022

10431023
let download_rustc = download_rustc_commit.is_some();
10441024

1045-
let stage = match cmd {
1025+
let stage = match flags_cmd {
10461026
Subcommand::Check { .. } => flags_stage.or(build_check_stage).unwrap_or(1),
10471027
Subcommand::Clippy { .. } | Subcommand::Fix => {
10481028
flags_stage.or(build_check_stage).unwrap_or(1)
@@ -1071,7 +1051,7 @@ impl Config {
10711051
};
10721052

10731053
// Now check that the selected stage makes sense, and if not, print a warning and end
1074-
match (stage, &cmd) {
1054+
match (stage, &flags_cmd) {
10751055
(0, Subcommand::Build) => {
10761056
eprintln!("ERROR: cannot build anything on stage 0. Use at least stage 1.");
10771057
exit!(1);
@@ -1087,7 +1067,7 @@ impl Config {
10871067
_ => {}
10881068
}
10891069

1090-
if compile_time_deps && !matches!(cmd, Subcommand::Check { .. }) {
1070+
if flags_compile_time_deps && !matches!(flags_cmd, Subcommand::Check { .. }) {
10911071
eprintln!(
10921072
"WARNING: Can't use --compile-time-deps with any subcommand other than check."
10931073
);
@@ -1097,7 +1077,7 @@ impl Config {
10971077
// CI should always run stage 2 builds, unless it specifically states otherwise
10981078
#[cfg(not(test))]
10991079
if flags_stage.is_none() && is_running_on_ci {
1100-
match cmd {
1080+
match flags_cmd {
11011081
Subcommand::Test { .. }
11021082
| Subcommand::Miri { .. }
11031083
| Subcommand::Doc { .. }
@@ -1351,21 +1331,25 @@ impl Config {
13511331
llvm_thin_lto: llvm_thin_lto_.unwrap_or(false),
13521332
rustc_debug_assertions: rust_rustc_debug_assertions.unwrap_or(rust_debug == Some(true)),
13531333
lld_mode: rust_lld_mode.unwrap_or_default(),
1334+
initial_cargo_clippy: build_cargo_clippy,
1335+
vendor: build_vendor.unwrap_or(
1336+
rust_info.is_from_tarball()
1337+
&& src.join("vendor").exists()
1338+
&& src.join(".cargo/config.toml").exists(),
1339+
),
1340+
cmd: flags_cmd,
13541341
exec_ctx,
13551342
out,
13561343
rust_info,
13571344
initial_cargo,
13581345
initial_rustc,
1359-
initial_cargo_clippy,
13601346
initial_sysroot,
13611347
initial_rustfmt,
13621348
submodules,
1363-
vendor,
13641349
target_config,
13651350
omit_git_hash,
13661351
stage,
13671352
src,
1368-
cmd,
13691353
llvm_from_ci,
13701354
llvm_assertions,
13711355
lld_enabled,

src/bootstrap/src/core/config/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,6 @@ pub enum GccCiMode {
402402
DownloadFromCi,
403403
}
404404

405-
pub fn set<T>(field: &mut T, val: Option<T>) {
406-
if let Some(v) = val {
407-
*field = v;
408-
}
409-
}
410-
411405
pub fn threads_from_config(v: u32) -> u32 {
412406
match v {
413407
0 => std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32,

0 commit comments

Comments
 (0)