Skip to content

Commit ce85672

Browse files
committed
move min_config related options from config module
Signed-off-by: ozkanonur <[email protected]>
1 parent fd0e479 commit ce85672

File tree

8 files changed

+63
-60
lines changed

8 files changed

+63
-60
lines changed

src/bootstrap/bin/bootstrap-shim.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
use std::{env, process::Command};
22

3-
use bootstrap::{t, MinimalConfig};
3+
use bootstrap::{Flags, MinimalConfig};
44

55
#[path = "../../../src/tools/x/src/main.rs"]
66
mod run_python;
77

88
fn main() {
99
let args = env::args().skip(1).collect::<Vec<_>>();
10-
let mut opts = getopts::Options::new();
11-
opts.optopt("", "config", "TOML configuration file for build", "FILE");
12-
let matches = t!(opts.parse(args));
10+
let flags = Flags::parse(&args);
1311

1412
// If there are no untracked changes to bootstrap, download it from CI.
1513
// Otherwise, build it from source. Use python to build to avoid duplicating the code between python and rust.
16-
let config = MinimalConfig::parse(t!(matches.opt_get("config")));
14+
let config = MinimalConfig::parse(&flags, None);
1715
let bootstrap_bin = if let Some(commit) = last_modified_bootstrap_commit(&config) {
1816
config.download_bootstrap(&commit)
1917
} else {

src/bootstrap/bin/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bootstrap::{Build, Config, Subcommand, VERSION};
1313

1414
fn main() {
1515
let args = env::args().skip(1).collect::<Vec<_>>();
16-
let config = Config::parse(&args);
16+
let config = Config::parse(&args, None);
1717

1818
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
1919
{

src/bootstrap/builder/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn configure(cmd: &str, host: &[&str], target: &[&str]) -> Config {
77
}
88

99
fn configure_with_args(cmd: &[String], host: &[&str], target: &[&str]) -> Config {
10-
let mut config = Config::parse(cmd);
10+
let mut config = Config::parse(cmd, None);
1111
// don't save toolstates
1212
config.save_toolstates = None;
1313
config.dry_run = DryRun::SelfCheck;
@@ -18,7 +18,7 @@ fn configure_with_args(cmd: &[String], host: &[&str], target: &[&str]) -> Config
1818
let submodule_build = Build::new(Config {
1919
// don't include LLVM, so CI doesn't require ninja/cmake to be installed
2020
rust_codegen_backends: vec![],
21-
..Config::parse(&["check".to_owned()])
21+
..Config::parse(&["check".to_owned()], None)
2222
});
2323
submodule_build.update_submodule(Path::new("src/doc/book"));
2424
submodule_build.update_submodule(Path::new("src/tools/rust-analyzer"));

src/bootstrap/config.rs

+23-38
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
mod tests;
88

99
use std::cell::{Cell, RefCell};
10-
use std::cmp;
1110
use std::collections::{HashMap, HashSet};
12-
use std::env;
1311
use std::fs;
1412
use std::ops::{Deref, DerefMut};
1513
use std::path::{Path, PathBuf};
@@ -21,9 +19,7 @@ use crate::cc_detect::{ndk_compiler, Language};
2119
use crate::channel::{self, GitInfo};
2220
pub use crate::flags::Subcommand;
2321
use crate::flags::{Color, Flags};
24-
use crate::min_config::{
25-
deserialize_stage0_metadata, get_toml, set_and_return_toml_config, set_config_output_dir,
26-
};
22+
use crate::min_config::{get_toml, set_cfg_path_and_return_toml_cfg};
2723
use crate::util::{exe, output, t};
2824
use crate::MinimalConfig;
2925
use once_cell::sync::OnceCell;
@@ -387,14 +383,15 @@ impl Target {
387383
target
388384
}
389385
}
386+
390387
/// Structure of the `config.toml` file that configuration is read from.
391388
///
392389
/// This structure uses `Decodable` to automatically decode a TOML configuration
393390
/// file into this format, and then this is traversed and written into the above
394391
/// `Config` structure.
395392
#[derive(Deserialize, Default)]
396393
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
397-
struct TomlConfig {
394+
pub struct TomlConfig {
398395
changelog_seen: Option<usize>,
399396
build: Option<Build>,
400397
install: Option<Install>,
@@ -439,8 +436,8 @@ macro_rules! define_config {
439436
$($field:ident: Option<$field_ty:ty> = $field_key:literal,)*
440437
}) => {
441438
$(#[$attr])*
442-
struct $name {
443-
$($field: Option<$field_ty>,)*
439+
pub struct $name {
440+
$(pub(crate) $field: Option<$field_ty>,)*
444441
}
445442

446443
impl Merge for $name {
@@ -522,7 +519,7 @@ macro_rules! define_config {
522519

523520
define_config! {
524521
/// TOML representation of various global build decisions.
525-
#[derive(Default)]
522+
#[derive(Clone, Default)]
526523
struct Build {
527524
build: Option<String> = "build",
528525
host: Option<Vec<String>> = "host",
@@ -628,7 +625,7 @@ define_config! {
628625

629626
#[derive(Debug, Deserialize)]
630627
#[serde(untagged)]
631-
enum StringOrBool {
628+
pub(crate) enum StringOrBool {
632629
String(String),
633630
Bool(bool),
634631
}
@@ -745,21 +742,22 @@ impl Config {
745742
config.stdout_is_tty = atty::is(atty::Stream::Stdout);
746743
config.stderr_is_tty = atty::is(atty::Stream::Stderr);
747744

748-
// set by build.rs
749-
config.build = TargetSelection::from_user(&env!("BUILD_TRIPLE"));
750-
751-
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
752-
// Undo `src/bootstrap`
753-
config.src = manifest_dir.parent().unwrap().parent().unwrap().to_owned();
754-
config.out = PathBuf::from("build");
755-
756745
config
757746
}
758747

759-
pub fn parse(args: &[String]) -> Config {
748+
pub fn parse(args: &[String], custom_toml_config: Option<TomlConfig>) -> Config {
760749
let flags = Flags::parse(&args);
761750
let mut config = Config::default_opts();
762-
config.minimal_config = MinimalConfig::parse(flags.config.clone());
751+
752+
let mut toml: TomlConfig = custom_toml_config.unwrap_or_else(|| {
753+
set_cfg_path_and_return_toml_cfg(
754+
config.src.clone(),
755+
flags.config.clone(),
756+
&mut config.config,
757+
)
758+
});
759+
760+
config.minimal_config = MinimalConfig::parse(&flags, toml.build.clone());
763761

764762
// Set flags.
765763
config.exclude = flags.exclude.into_iter().map(|path| TaskPath::parse(path)).collect();
@@ -770,7 +768,6 @@ impl Config {
770768
config.jobs = flags.jobs.map(threads_from_config);
771769
config.cmd = flags.cmd;
772770
config.incremental = flags.incremental;
773-
config.dry_run = if flags.dry_run { DryRun::UserSelected } else { DryRun::Disabled };
774771
config.keep_stage = flags.keep_stage;
775772
config.keep_stage_std = flags.keep_stage_std;
776773
config.color = flags.color;
@@ -790,13 +787,10 @@ impl Config {
790787
crate::detail_exit(1);
791788
}
792789

793-
// Infer the rest of the configuration.
794-
795-
set_config_output_dir(&mut config.out);
796-
config.stage0_metadata = deserialize_stage0_metadata(&config.src);
797-
798-
let mut toml: TomlConfig =
799-
set_and_return_toml_config(config.src.clone(), flags.config, &mut config.config);
790+
let build = toml.build.clone().unwrap_or_default();
791+
if let Some(file_build) = build.build.as_ref() {
792+
config.build = TargetSelection::from_user(file_build);
793+
};
800794

801795
if let Some(include) = &toml.profile {
802796
let mut include_path = config.src.clone();
@@ -810,11 +804,6 @@ impl Config {
810804

811805
config.changelog_seen = toml.changelog_seen;
812806

813-
let build = toml.build.unwrap_or_default();
814-
if let Some(file_build) = build.build {
815-
config.build = TargetSelection::from_user(&file_build);
816-
};
817-
818807
set(&mut config.out, flags.build_dir.or_else(|| build.build_dir.map(PathBuf::from)));
819808
// NOTE: Bootstrap spawns various commands with different working directories.
820809
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
@@ -872,17 +861,13 @@ impl Config {
872861
set(&mut config.full_bootstrap, build.full_bootstrap);
873862
set(&mut config.extended, build.extended);
874863
config.tools = build.tools;
875-
set(&mut config.verbose, build.verbose);
876864
set(&mut config.sanitizers, build.sanitizers);
877865
set(&mut config.profiler, build.profiler);
878866
set(&mut config.cargo_native_static, build.cargo_native_static);
879867
set(&mut config.configure_args, build.configure_args);
880868
set(&mut config.local_rebuild, build.local_rebuild);
881869
set(&mut config.print_step_timings, build.print_step_timings);
882870
set(&mut config.print_step_rusage, build.print_step_rusage);
883-
set(&mut config.patch_binaries_for_nix, build.patch_binaries_for_nix);
884-
885-
config.verbose = cmp::max(config.verbose, flags.verbose);
886871

887872
if let Some(install) = toml.install {
888873
config.prefix = install.prefix.map(PathBuf::from);
@@ -1446,7 +1431,7 @@ impl Config {
14461431
}
14471432
}
14481433

1449-
fn set<T>(field: &mut T, val: Option<T>) {
1434+
pub(crate) fn set<T>(field: &mut T, val: Option<T>) {
14501435
if let Some(v) = val {
14511436
*field = v;
14521437
}

src/bootstrap/config/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use super::{Config, TomlConfig};
22
use std::{env, path::Path};
33

4-
fn toml(config: &str) -> impl '_ + Fn(&Path) -> TomlConfig {
5-
|&_| toml::from_str(config).unwrap()
4+
fn toml(config: &str) -> TomlConfig {
5+
toml::from_str(config).unwrap()
66
}
77

88
fn parse(config: &str) -> Config {
9-
Config::parse_inner(&["check".to_owned(), "--config=/does/not/exist".to_owned()], toml(config))
9+
Config::parse(&["check".to_owned(), "--config=/does/not/exist".to_owned()], Some(toml(config)))
1010
}
1111

1212
#[test]

src/bootstrap/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
366366
} else {
367367
"setup"
368368
};
369-
let config = Config::parse(&[cmd.to_string()]);
369+
let config = Config::parse(&[cmd.to_string()], None);
370370
let build = Build::new(config);
371371
let paths = Builder::get_help(&build, subcommand);
372372

src/bootstrap/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ mod job {
8989
pub use crate::builder::PathSet;
9090
use crate::cache::{Interned, INTERNER};
9191
pub use crate::config::Config;
92+
pub use crate::flags::Flags;
9293
pub use crate::flags::Subcommand;
9394
use termcolor::{ColorChoice, StandardStream, WriteColor};
9495
pub use crate::min_config::MinimalConfig;

src/bootstrap/min_config.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::fmt;
22
use std::{
3+
cmp,
34
collections::HashMap,
45
env, fs,
56
path::{Path, PathBuf},
@@ -10,6 +11,8 @@ use serde::Deserialize;
1011

1112
use crate::{
1213
cache::{Interned, INTERNER},
14+
config::{self, set},
15+
flags::Flags,
1316
t,
1417
util::output,
1518
};
@@ -90,22 +93,40 @@ impl MinimalConfig {
9093
}
9194
}
9295

93-
pub fn parse(config_flag: Option<PathBuf>) -> MinimalConfig {
96+
fn set_shared_fields_from_parent(&mut self, parent_build_config: config::Build) {
97+
set(&mut self.verbose, parent_build_config.verbose);
98+
set(&mut self.patch_binaries_for_nix, parent_build_config.patch_binaries_for_nix);
99+
}
100+
101+
pub fn parse(flags: &Flags, parent_build_config: Option<config::Build>) -> MinimalConfig {
94102
let mut config = Self::default_opts();
95103

104+
if let Some(parent_build_config) = parent_build_config {
105+
config.set_shared_fields_from_parent(parent_build_config);
106+
};
107+
96108
if let Some(src) = src() {
97109
config.src = src;
98110
}
99111

100112
set_config_output_dir(&mut config.out);
101113

102-
let toml: TomlConfig =
103-
set_and_return_toml_config(config.src.clone(), config_flag, &mut config.config);
114+
config.stage0_metadata = deserialize_stage0_metadata(&config.src);
115+
config.dry_run = if flags.dry_run { DryRun::UserSelected } else { DryRun::Disabled };
116+
117+
let toml: TomlConfig = set_cfg_path_and_return_toml_cfg(
118+
config.src.clone(),
119+
flags.config.clone(),
120+
&mut config.config,
121+
);
104122

105-
if let Some(build) = toml.build.unwrap_or_default().build {
106-
config.build = TargetSelection::from_user(&build);
123+
let build = toml.build.unwrap_or_default();
124+
if let Some(file_build) = build.build {
125+
config.build = TargetSelection::from_user(&file_build);
107126
}
108127

128+
config.verbose = cmp::max(config.verbose, flags.verbose);
129+
109130
if config.dry_run() {
110131
let dir = config.out.join("tmp-dry-run");
111132
t!(fs::create_dir_all(&dir));
@@ -228,10 +249,8 @@ pub(crate) fn get_toml<T: Deserialize<'static> + Default>(file: &Path) -> T {
228249
/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
229250
///
230251
/// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
231-
#[allow(unused_variables)]
232-
pub(crate) fn set_config_output_dir(output_path: &mut PathBuf) {
233-
#[cfg(test)]
234-
{
252+
fn set_config_output_dir(output_path: &mut PathBuf) {
253+
if cfg!(test) {
235254
*output_path = Path::new(
236255
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
237256
)
@@ -242,7 +261,7 @@ pub(crate) fn set_config_output_dir(output_path: &mut PathBuf) {
242261
}
243262

244263
/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
245-
pub(crate) fn set_and_return_toml_config<T: Deserialize<'static> + Default>(
264+
pub(crate) fn set_cfg_path_and_return_toml_cfg<T: Deserialize<'static> + Default>(
246265
src: PathBuf,
247266
config_flag: Option<PathBuf>,
248267
cfg_path: &mut Option<PathBuf>,

0 commit comments

Comments
 (0)