Skip to content

Commit fccbd5f

Browse files
committed
Unify MinimalConfig::parse and Config::parse
Signed-off-by: ozkanonur <work@onurozkan.dev>
1 parent 83dbc10 commit fccbd5f

File tree

3 files changed

+83
-85
lines changed

3 files changed

+83
-85
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ jobs:
313313
os: ubuntu-20.04-16core-64gb
314314
- name: dist-x86_64-apple
315315
env:
316-
SCRIPT: "./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
316+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
317317
RUST_CONFIGURE_ARGS: "--enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false --set rust.lto=thin"
318318
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
319319
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -325,7 +325,7 @@ jobs:
325325
os: macos-12-xl
326326
- name: dist-apple-various
327327
env:
328-
SCRIPT: "./x.py dist bootstrap --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
328+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
329329
RUST_CONFIGURE_ARGS: "--enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
330330
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
331331
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -336,7 +336,7 @@ jobs:
336336
os: macos-12-xl
337337
- name: dist-x86_64-apple-alt
338338
env:
339-
SCRIPT: "./x.py dist bootstrap --include-default-paths"
339+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths"
340340
RUST_CONFIGURE_ARGS: "--enable-extended --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
341341
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
342342
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -369,7 +369,7 @@ jobs:
369369
os: macos-12-xl
370370
- name: dist-aarch64-apple
371371
env:
372-
SCRIPT: "./x.py dist bootstrap --include-default-paths --stage 2"
372+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --stage 2"
373373
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"
374374
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
375375
SELECT_XCODE: /Applications/Xcode_13.4.1.app
@@ -444,33 +444,33 @@ jobs:
444444
- name: dist-x86_64-msvc
445445
env:
446446
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"
447-
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc python src/ci/stage-build.py python x.py dist bootstrap --include-default-paths
447+
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc python src/ci/stage-build.py python x.py dist bootstrap bootstrap-shim --include-default-paths
448448
DIST_REQUIRE_ALL_TOOLS: 1
449449
os: windows-2019-8core-32gb
450450
- name: dist-i686-msvc
451451
env:
452452
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"
453-
SCRIPT: python x.py dist bootstrap --include-default-paths
453+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
454454
DIST_REQUIRE_ALL_TOOLS: 1
455455
os: windows-2019-8core-32gb
456456
- name: dist-aarch64-msvc
457457
env:
458458
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --host=aarch64-pc-windows-msvc --enable-full-tools --enable-profiler"
459-
SCRIPT: python x.py dist bootstrap --include-default-paths
459+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
460460
DIST_REQUIRE_ALL_TOOLS: 1
461461
WINDOWS_SDK_20348_HACK: 1
462462
os: windows-2019-8core-32gb
463463
- name: dist-i686-mingw
464464
env:
465465
RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-gnu --enable-full-tools --enable-profiler"
466466
NO_DOWNLOAD_CI_LLVM: 1
467-
SCRIPT: python x.py dist bootstrap --include-default-paths
467+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
468468
CUSTOM_MINGW: 1
469469
DIST_REQUIRE_ALL_TOOLS: 1
470470
os: windows-2019-8core-32gb
471471
- name: dist-x86_64-mingw
472472
env:
473-
SCRIPT: python x.py dist bootstrap --include-default-paths
473+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
474474
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-gnu --enable-full-tools --enable-profiler"
475475
NO_DOWNLOAD_CI_LLVM: 1
476476
CUSTOM_MINGW: 1
@@ -479,7 +479,7 @@ jobs:
479479
- name: dist-x86_64-msvc-alt
480480
env:
481481
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --enable-extended --enable-profiler"
482-
SCRIPT: python x.py dist bootstrap --include-default-paths
482+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
483483
os: windows-2019-8core-32gb
484484
timeout-minutes: 600
485485
runs-on: "${{ matrix.os }}"

src/bootstrap/config.rs

Lines changed: 7 additions & 34 deletions
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;
@@ -790,40 +792,11 @@ impl Config {
790792

791793
// Infer the rest of the configuration.
792794

793-
if cfg!(test) {
794-
// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
795-
config.out = Path::new(
796-
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
797-
)
798-
.parent()
799-
.unwrap()
800-
.to_path_buf();
801-
}
802-
803-
let stage0_json = t!(std::fs::read(&config.src.join("src").join("stage0.json")));
795+
set_config_output_dir(&mut config.out);
796+
config.stage0_metadata = deserialize_stage0_metadata(&config.src);
804797

805-
config.stage0_metadata = t!(serde_json::from_slice::<Stage0Metadata>(&stage0_json));
806-
807-
// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
808-
let toml_path = flags
809-
.config
810-
.clone()
811-
.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
812-
let using_default_path = toml_path.is_none();
813-
let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
814-
if using_default_path && !toml_path.exists() {
815-
toml_path = config.src.join(toml_path);
816-
}
817-
818-
// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
819-
// but not if `config.toml` hasn't been created.
820-
let mut toml = if !using_default_path || toml_path.exists() {
821-
config.config = Some(toml_path.clone());
822-
get_toml(&toml_path)
823-
} else {
824-
config.config = None;
825-
TomlConfig::default()
826-
};
798+
let mut toml: TomlConfig =
799+
set_and_return_toml_config(config.src.clone(), flags.config, &mut config.config);
827800

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

src/bootstrap/min_config.rs

Lines changed: 66 additions & 41 deletions
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)