Skip to content

Commit a445f80

Browse files
onur-ozkanjyn514
authored andcommitted
Unify MinimalConfig::parse and Config::parse
Signed-off-by: ozkanonur <[email protected]>
1 parent bc84983 commit a445f80

File tree

3 files changed

+83
-85
lines changed

3 files changed

+83
-85
lines changed

.github/workflows/ci.yml

+10-10
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ jobs:
316316
os: ubuntu-20.04-xl
317317
- name: dist-x86_64-apple
318318
env:
319-
SCRIPT: "./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
319+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
320320
RUST_CONFIGURE_ARGS: "--enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false --set rust.lto=thin"
321321
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
322322
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -328,7 +328,7 @@ jobs:
328328
os: macos-latest
329329
- name: dist-apple-various
330330
env:
331-
SCRIPT: "./x.py dist bootstrap --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
331+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
332332
RUST_CONFIGURE_ARGS: "--enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
333333
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
334334
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -339,7 +339,7 @@ jobs:
339339
os: macos-latest
340340
- name: dist-x86_64-apple-alt
341341
env:
342-
SCRIPT: "./x.py dist bootstrap --include-default-paths"
342+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths"
343343
RUST_CONFIGURE_ARGS: "--enable-extended --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
344344
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
345345
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -372,7 +372,7 @@ jobs:
372372
os: macos-latest
373373
- name: dist-aarch64-apple
374374
env:
375-
SCRIPT: "./x.py dist bootstrap --include-default-paths --stage 2"
375+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --stage 2"
376376
RUST_CONFIGURE_ARGS: "--build=x86_64-apple-darwin --host=aarch64-apple-darwin --target=aarch64-apple-darwin --enable-full-tools --enable-sanitizers --enable-profiler --disable-docs --set rust.jemalloc --set llvm.ninja=false"
377377
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
378378
SELECT_XCODE: /Applications/Xcode_13.4.1.app
@@ -447,33 +447,33 @@ jobs:
447447
- name: dist-x86_64-msvc
448448
env:
449449
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --host=x86_64-pc-windows-msvc --target=x86_64-pc-windows-msvc --enable-full-tools --enable-profiler --set rust.lto=thin"
450-
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc python src/ci/stage-build.py python x.py dist bootstrap --include-default-paths
450+
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc python src/ci/stage-build.py python x.py dist bootstrap bootstrap-shim --include-default-paths
451451
DIST_REQUIRE_ALL_TOOLS: 1
452452
os: windows-latest-xl
453453
- name: dist-i686-msvc
454454
env:
455455
RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-msvc --host=i686-pc-windows-msvc --target=i686-pc-windows-msvc,i586-pc-windows-msvc --enable-full-tools --enable-profiler"
456-
SCRIPT: python x.py dist bootstrap --include-default-paths
456+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
457457
DIST_REQUIRE_ALL_TOOLS: 1
458458
os: windows-latest-xl
459459
- name: dist-aarch64-msvc
460460
env:
461461
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --host=aarch64-pc-windows-msvc --enable-full-tools --enable-profiler"
462-
SCRIPT: python x.py dist bootstrap --include-default-paths
462+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
463463
DIST_REQUIRE_ALL_TOOLS: 1
464464
WINDOWS_SDK_20348_HACK: 1
465465
os: windows-latest-xl
466466
- name: dist-i686-mingw
467467
env:
468468
RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-gnu --enable-full-tools --enable-profiler"
469469
NO_DOWNLOAD_CI_LLVM: 1
470-
SCRIPT: python x.py dist bootstrap --include-default-paths
470+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
471471
CUSTOM_MINGW: 1
472472
DIST_REQUIRE_ALL_TOOLS: 1
473473
os: windows-latest-xl
474474
- name: dist-x86_64-mingw
475475
env:
476-
SCRIPT: python x.py dist bootstrap --include-default-paths
476+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
477477
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-gnu --enable-full-tools --enable-profiler"
478478
NO_DOWNLOAD_CI_LLVM: 1
479479
CUSTOM_MINGW: 1
@@ -482,7 +482,7 @@ jobs:
482482
- name: dist-x86_64-msvc-alt
483483
env:
484484
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --enable-extended --enable-profiler"
485-
SCRIPT: python x.py dist bootstrap --include-default-paths
485+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
486486
os: windows-latest-xl
487487
timeout-minutes: 600
488488
runs-on: "${{ matrix.os }}"

src/bootstrap/config.rs

+7-34
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use crate::cc_detect::{ndk_compiler, Language};
2121
use crate::channel::{self, GitInfo};
2222
pub use crate::flags::Subcommand;
2323
use crate::flags::{Color, Flags};
24-
use crate::min_config::get_toml;
24+
use crate::min_config::{
25+
deserialize_stage0_metadata, get_toml, set_and_return_toml_config, set_config_output_dir,
26+
};
2527
use crate::util::{exe, output, t};
2628
use crate::MinimalConfig;
2729
use once_cell::sync::OnceCell;
@@ -768,40 +770,11 @@ impl Config {
768770

769771
// Infer the rest of the configuration.
770772

771-
if cfg!(test) {
772-
// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
773-
config.out = Path::new(
774-
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
775-
)
776-
.parent()
777-
.unwrap()
778-
.to_path_buf();
779-
}
780-
781-
let stage0_json = t!(std::fs::read(&config.src.join("src").join("stage0.json")));
773+
set_config_output_dir(&mut config.out);
774+
config.stage0_metadata = deserialize_stage0_metadata(&config.src);
782775

783-
config.stage0_metadata = t!(serde_json::from_slice::<Stage0Metadata>(&stage0_json));
784-
785-
// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
786-
let toml_path = flags
787-
.config
788-
.clone()
789-
.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
790-
let using_default_path = toml_path.is_none();
791-
let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
792-
if using_default_path && !toml_path.exists() {
793-
toml_path = config.src.join(toml_path);
794-
}
795-
796-
// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
797-
// but not if `config.toml` hasn't been created.
798-
let mut toml = if !using_default_path || toml_path.exists() {
799-
config.config = Some(toml_path.clone());
800-
get_toml(&toml_path)
801-
} else {
802-
config.config = None;
803-
TomlConfig::default()
804-
};
776+
let mut toml: TomlConfig =
777+
set_and_return_toml_config(config.src.clone(), flags.config, &mut config.config);
805778

806779
if let Some(include) = &toml.profile {
807780
let mut include_path = config.src.clone();

src/bootstrap/min_config.rs

+66-41
Original file line numberDiff line numberDiff line change
@@ -97,61 +97,31 @@ impl MinimalConfig {
9797
config.src = src;
9898
}
9999

100-
if cfg!(test) {
101-
// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
102-
config.out = Path::new(
103-
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
104-
)
105-
.parent()
106-
.unwrap()
107-
.to_path_buf();
108-
}
100+
set_config_output_dir(&mut config.out);
101+
102+
let toml: TomlConfig =
103+
set_and_return_toml_config(config.src.clone(), config_flag, &mut config.config);
109104

110-
let toml = if let Some(toml_path) = Self::config_path(config.src.clone(), config_flag) {
111-
config.config = Some(toml_path.clone());
112-
get_toml(&toml_path)
113-
} else {
114-
config.config = None;
115-
TomlConfig::default()
116-
};
117105
if let Some(build) = toml.build.unwrap_or_default().build {
118106
config.build = TargetSelection::from_user(&build);
119107
}
120108

121-
// NOTE: Bootstrap spawns various commands with different working directories.
122-
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
123-
if !config.out.is_absolute() {
124-
// `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.
125-
config.out = crate::util::absolute(&config.out);
126-
}
127-
128109
if config.dry_run() {
129110
let dir = config.out.join("tmp-dry-run");
130111
t!(fs::create_dir_all(&dir));
131112
config.out = dir;
132113
}
114+
// NOTE: Bootstrap spawns various commands with different working directories.
115+
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
116+
else if !config.out.is_absolute() {
117+
// `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.
118+
config.out = crate::util::absolute(&config.out);
119+
}
133120

134-
let stage0_json = t!(std::fs::read(&config.src.join("src").join("stage0.json")));
135-
config.stage0_metadata = t!(serde_json::from_slice::<Stage0Metadata>(&stage0_json));
121+
config.stage0_metadata = deserialize_stage0_metadata(&config.src);
136122

137123
config
138124
}
139-
140-
/// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
141-
///
142-
/// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
143-
/// but not if `config.toml` hasn't been created.
144-
fn config_path(src: PathBuf, config_flag: Option<PathBuf>) -> Option<PathBuf> {
145-
let toml_path =
146-
config_flag.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
147-
let using_default_path = toml_path.is_none();
148-
let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
149-
if using_default_path && !toml_path.exists() {
150-
toml_path = src.join(toml_path);
151-
}
152-
153-
if !using_default_path || toml_path.exists() { Some(toml_path) } else { None }
154-
}
155125
}
156126

157127
impl MinimalConfig {
@@ -235,10 +205,12 @@ impl MinimalConfig {
235205
}
236206

237207
#[cfg(test)]
208+
/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
238209
pub(crate) fn get_toml<T: Deserialize<'static> + Default>(_file: &Path) -> T {
239210
T::default()
240211
}
241212
#[cfg(not(test))]
213+
/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
242214
pub(crate) fn get_toml<T: Deserialize<'static> + Default>(file: &Path) -> T {
243215
let contents =
244216
t!(fs::read_to_string(file), format!("config file {} not found", file.display()));
@@ -253,6 +225,59 @@ pub(crate) fn get_toml<T: Deserialize<'static> + Default>(file: &Path) -> T {
253225
}
254226
}
255227

228+
/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
229+
///
230+
/// 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+
{
235+
*output_path = Path::new(
236+
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
237+
)
238+
.parent()
239+
.unwrap()
240+
.to_path_buf();
241+
}
242+
}
243+
244+
/// 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>(
246+
src: PathBuf,
247+
config_flag: Option<PathBuf>,
248+
cfg_path: &mut Option<PathBuf>,
249+
) -> T {
250+
/// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
251+
///
252+
/// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
253+
/// but not if `config.toml` hasn't been created.
254+
fn config_path(src: &PathBuf, config_flag: Option<PathBuf>) -> Option<PathBuf> {
255+
let toml_path =
256+
config_flag.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
257+
let using_default_path = toml_path.is_none();
258+
let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
259+
if using_default_path && !toml_path.exists() {
260+
toml_path = src.join(toml_path);
261+
}
262+
263+
if !using_default_path || toml_path.exists() { Some(toml_path) } else { None }
264+
}
265+
266+
if let Some(toml_path) = config_path(&src, config_flag) {
267+
*cfg_path = Some(toml_path.clone());
268+
get_toml(&toml_path)
269+
} else {
270+
*cfg_path = None;
271+
T::default()
272+
}
273+
}
274+
275+
/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
276+
pub(crate) fn deserialize_stage0_metadata(stage0_metadata_path: &PathBuf) -> Stage0Metadata {
277+
let stage0_json = t!(std::fs::read(stage0_metadata_path.join("src").join("stage0.json")));
278+
t!(serde_json::from_slice::<Stage0Metadata>(&stage0_json))
279+
}
280+
256281
fn src() -> Option<PathBuf> {
257282
// Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary,
258283
// running on a completely machine from where it was compiled.

0 commit comments

Comments
 (0)