Skip to content

Commit 8ae63b1

Browse files
committed
Unify MinimalConfig::parse and Config::parse
Signed-off-by: ozkanonur <[email protected]>
1 parent ea813a0 commit 8ae63b1

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
@@ -311,7 +311,7 @@ jobs:
311311
os: ubuntu-20.04-8core-32gb
312312
- name: dist-x86_64-apple
313313
env:
314-
SCRIPT: "./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
314+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
315315
RUST_CONFIGURE_ARGS: "--enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false --set rust.lto=thin"
316316
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
317317
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -323,7 +323,7 @@ jobs:
323323
os: macos-latest
324324
- name: dist-apple-various
325325
env:
326-
SCRIPT: "./x.py dist bootstrap --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
326+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
327327
RUST_CONFIGURE_ARGS: "--enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
328328
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
329329
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -334,7 +334,7 @@ jobs:
334334
os: macos-latest
335335
- name: dist-x86_64-apple-alt
336336
env:
337-
SCRIPT: "./x.py dist bootstrap --include-default-paths"
337+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths"
338338
RUST_CONFIGURE_ARGS: "--enable-extended --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
339339
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
340340
MACOSX_DEPLOYMENT_TARGET: 10.7
@@ -367,7 +367,7 @@ jobs:
367367
os: macos-latest
368368
- name: dist-aarch64-apple
369369
env:
370-
SCRIPT: "./x.py dist bootstrap --include-default-paths --stage 2"
370+
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --stage 2"
371371
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"
372372
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
373373
SELECT_XCODE: /Applications/Xcode_13.4.1.app
@@ -442,33 +442,33 @@ jobs:
442442
- name: dist-x86_64-msvc
443443
env:
444444
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"
445-
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc python src/ci/stage-build.py python x.py dist bootstrap --include-default-paths
445+
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc python src/ci/stage-build.py python x.py dist bootstrap bootstrap-shim --include-default-paths
446446
DIST_REQUIRE_ALL_TOOLS: 1
447447
os: windows-2019-8core-32gb
448448
- name: dist-i686-msvc
449449
env:
450450
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"
451-
SCRIPT: python x.py dist bootstrap --include-default-paths
451+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
452452
DIST_REQUIRE_ALL_TOOLS: 1
453453
os: windows-2019-8core-32gb
454454
- name: dist-aarch64-msvc
455455
env:
456456
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --host=aarch64-pc-windows-msvc --enable-full-tools --enable-profiler"
457-
SCRIPT: python x.py dist bootstrap --include-default-paths
457+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
458458
DIST_REQUIRE_ALL_TOOLS: 1
459459
WINDOWS_SDK_20348_HACK: 1
460460
os: windows-2019-8core-32gb
461461
- name: dist-i686-mingw
462462
env:
463463
RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-gnu --enable-full-tools --enable-profiler"
464464
NO_DOWNLOAD_CI_LLVM: 1
465-
SCRIPT: python x.py dist bootstrap --include-default-paths
465+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
466466
CUSTOM_MINGW: 1
467467
DIST_REQUIRE_ALL_TOOLS: 1
468468
os: windows-2019-8core-32gb
469469
- name: dist-x86_64-mingw
470470
env:
471-
SCRIPT: python x.py dist bootstrap --include-default-paths
471+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
472472
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-gnu --enable-full-tools --enable-profiler"
473473
NO_DOWNLOAD_CI_LLVM: 1
474474
CUSTOM_MINGW: 1
@@ -477,7 +477,7 @@ jobs:
477477
- name: dist-x86_64-msvc-alt
478478
env:
479479
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --enable-extended --enable-profiler"
480-
SCRIPT: python x.py dist bootstrap --include-default-paths
480+
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
481481
os: windows-2019-8core-32gb
482482
timeout-minutes: 600
483483
runs-on: "${{ matrix.os }}"

src/bootstrap/config.rs

+7-34
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use crate::cc_detect::{ndk_compiler, Language};
2222
use crate::channel::{self, GitInfo};
2323
pub use crate::flags::Subcommand;
2424
use crate::flags::{Color, Flags, Warnings};
25-
use crate::min_config::get_toml;
25+
use crate::min_config::{
26+
deserialize_stage0_metadata, get_toml, set_and_return_toml_config, set_config_output_dir,
27+
};
2628
use crate::util::{exe, output, t};
2729
use crate::MinimalConfig;
2830
use once_cell::sync::OnceCell;
@@ -817,40 +819,11 @@ impl Config {
817819

818820
// Infer the rest of the configuration.
819821

820-
if cfg!(test) {
821-
// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
822-
config.out = Path::new(
823-
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
824-
)
825-
.parent()
826-
.unwrap()
827-
.to_path_buf();
828-
}
829-
830-
let stage0_json = t!(std::fs::read(&config.src.join("src").join("stage0.json")));
831-
832-
config.stage0_metadata = t!(serde_json::from_slice::<Stage0Metadata>(&stage0_json));
822+
set_config_output_dir(&mut config.out);
823+
config.stage0_metadata = deserialize_stage0_metadata(&config.src);
833824

834-
// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
835-
let toml_path = flags
836-
.config
837-
.clone()
838-
.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
839-
let using_default_path = toml_path.is_none();
840-
let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
841-
if using_default_path && !toml_path.exists() {
842-
toml_path = config.src.join(toml_path);
843-
}
844-
845-
// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
846-
// but not if `config.toml` hasn't been created.
847-
let mut toml = if !using_default_path || toml_path.exists() {
848-
config.config = Some(toml_path.clone());
849-
get_toml(&toml_path)
850-
} else {
851-
config.config = None;
852-
TomlConfig::default()
853-
};
825+
let mut toml: TomlConfig =
826+
set_and_return_toml_config(config.src.clone(), flags.config, &mut config.config);
854827

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

src/bootstrap/min_config.rs

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

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

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

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

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

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

158128
impl MinimalConfig {
@@ -236,10 +206,12 @@ impl MinimalConfig {
236206
}
237207

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

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

0 commit comments

Comments
 (0)