Skip to content

Commit

Permalink
Unify MinimalConfig::parse and Config::parse
Browse files Browse the repository at this point in the history
Signed-off-by: ozkanonur <work@onurozkan.dev>
  • Loading branch information
onur-ozkan authored and jyn514 committed Feb 2, 2023
1 parent bc84983 commit a445f80
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 85 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ jobs:
os: ubuntu-20.04-xl
- name: dist-x86_64-apple
env:
SCRIPT: "./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
RUST_CONFIGURE_ARGS: "--enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false --set rust.lto=thin"
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.7
Expand All @@ -328,7 +328,7 @@ jobs:
os: macos-latest
- name: dist-apple-various
env:
SCRIPT: "./x.py dist bootstrap --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
RUST_CONFIGURE_ARGS: "--enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.7
Expand All @@ -339,7 +339,7 @@ jobs:
os: macos-latest
- name: dist-x86_64-apple-alt
env:
SCRIPT: "./x.py dist bootstrap --include-default-paths"
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths"
RUST_CONFIGURE_ARGS: "--enable-extended --enable-profiler --set rust.jemalloc --set llvm.ninja=false"
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
MACOSX_DEPLOYMENT_TARGET: 10.7
Expand Down Expand Up @@ -372,7 +372,7 @@ jobs:
os: macos-latest
- name: dist-aarch64-apple
env:
SCRIPT: "./x.py dist bootstrap --include-default-paths --stage 2"
SCRIPT: "./x.py dist bootstrap bootstrap-shim --include-default-paths --stage 2"
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"
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
SELECT_XCODE: /Applications/Xcode_13.4.1.app
Expand Down Expand Up @@ -447,33 +447,33 @@ jobs:
- name: dist-x86_64-msvc
env:
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"
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc python src/ci/stage-build.py python x.py dist bootstrap --include-default-paths
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc python src/ci/stage-build.py python x.py dist bootstrap bootstrap-shim --include-default-paths
DIST_REQUIRE_ALL_TOOLS: 1
os: windows-latest-xl
- name: dist-i686-msvc
env:
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"
SCRIPT: python x.py dist bootstrap --include-default-paths
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
DIST_REQUIRE_ALL_TOOLS: 1
os: windows-latest-xl
- name: dist-aarch64-msvc
env:
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --host=aarch64-pc-windows-msvc --enable-full-tools --enable-profiler"
SCRIPT: python x.py dist bootstrap --include-default-paths
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
DIST_REQUIRE_ALL_TOOLS: 1
WINDOWS_SDK_20348_HACK: 1
os: windows-latest-xl
- name: dist-i686-mingw
env:
RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-gnu --enable-full-tools --enable-profiler"
NO_DOWNLOAD_CI_LLVM: 1
SCRIPT: python x.py dist bootstrap --include-default-paths
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
CUSTOM_MINGW: 1
DIST_REQUIRE_ALL_TOOLS: 1
os: windows-latest-xl
- name: dist-x86_64-mingw
env:
SCRIPT: python x.py dist bootstrap --include-default-paths
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-gnu --enable-full-tools --enable-profiler"
NO_DOWNLOAD_CI_LLVM: 1
CUSTOM_MINGW: 1
Expand All @@ -482,7 +482,7 @@ jobs:
- name: dist-x86_64-msvc-alt
env:
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --enable-extended --enable-profiler"
SCRIPT: python x.py dist bootstrap --include-default-paths
SCRIPT: python x.py dist bootstrap bootstrap-shim --include-default-paths
os: windows-latest-xl
timeout-minutes: 600
runs-on: "${{ matrix.os }}"
Expand Down
41 changes: 7 additions & 34 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use crate::cc_detect::{ndk_compiler, Language};
use crate::channel::{self, GitInfo};
pub use crate::flags::Subcommand;
use crate::flags::{Color, Flags};
use crate::min_config::get_toml;
use crate::min_config::{
deserialize_stage0_metadata, get_toml, set_and_return_toml_config, set_config_output_dir,
};
use crate::util::{exe, output, t};
use crate::MinimalConfig;
use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -768,40 +770,11 @@ impl Config {

// Infer the rest of the configuration.

if cfg!(test) {
// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
config.out = Path::new(
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
)
.parent()
.unwrap()
.to_path_buf();
}

let stage0_json = t!(std::fs::read(&config.src.join("src").join("stage0.json")));
set_config_output_dir(&mut config.out);
config.stage0_metadata = deserialize_stage0_metadata(&config.src);

config.stage0_metadata = t!(serde_json::from_slice::<Stage0Metadata>(&stage0_json));

// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
let toml_path = flags
.config
.clone()
.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
let using_default_path = toml_path.is_none();
let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
if using_default_path && !toml_path.exists() {
toml_path = config.src.join(toml_path);
}

// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
// but not if `config.toml` hasn't been created.
let mut toml = if !using_default_path || toml_path.exists() {
config.config = Some(toml_path.clone());
get_toml(&toml_path)
} else {
config.config = None;
TomlConfig::default()
};
let mut toml: TomlConfig =
set_and_return_toml_config(config.src.clone(), flags.config, &mut config.config);

if let Some(include) = &toml.profile {
let mut include_path = config.src.clone();
Expand Down
107 changes: 66 additions & 41 deletions src/bootstrap/min_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,61 +97,31 @@ impl MinimalConfig {
config.src = src;
}

if cfg!(test) {
// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
config.out = Path::new(
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
)
.parent()
.unwrap()
.to_path_buf();
}
set_config_output_dir(&mut config.out);

let toml: TomlConfig =
set_and_return_toml_config(config.src.clone(), config_flag, &mut config.config);

let toml = if let Some(toml_path) = Self::config_path(config.src.clone(), config_flag) {
config.config = Some(toml_path.clone());
get_toml(&toml_path)
} else {
config.config = None;
TomlConfig::default()
};
if let Some(build) = toml.build.unwrap_or_default().build {
config.build = TargetSelection::from_user(&build);
}

// NOTE: Bootstrap spawns various commands with different working directories.
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
if !config.out.is_absolute() {
// `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.
config.out = crate::util::absolute(&config.out);
}

if config.dry_run() {
let dir = config.out.join("tmp-dry-run");
t!(fs::create_dir_all(&dir));
config.out = dir;
}
// NOTE: Bootstrap spawns various commands with different working directories.
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
else if !config.out.is_absolute() {
// `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.
config.out = crate::util::absolute(&config.out);
}

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

config
}

/// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
///
/// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
/// but not if `config.toml` hasn't been created.
fn config_path(src: PathBuf, config_flag: Option<PathBuf>) -> Option<PathBuf> {
let toml_path =
config_flag.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
let using_default_path = toml_path.is_none();
let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
if using_default_path && !toml_path.exists() {
toml_path = src.join(toml_path);
}

if !using_default_path || toml_path.exists() { Some(toml_path) } else { None }
}
}

impl MinimalConfig {
Expand Down Expand Up @@ -235,10 +205,12 @@ impl MinimalConfig {
}

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

/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
///
/// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
#[allow(unused_variables)]
pub(crate) fn set_config_output_dir(output_path: &mut PathBuf) {
#[cfg(test)]
{
*output_path = Path::new(
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
)
.parent()
.unwrap()
.to_path_buf();
}
}

/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
pub(crate) fn set_and_return_toml_config<T: Deserialize<'static> + Default>(
src: PathBuf,
config_flag: Option<PathBuf>,
cfg_path: &mut Option<PathBuf>,
) -> T {
/// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
///
/// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
/// but not if `config.toml` hasn't been created.
fn config_path(src: &PathBuf, config_flag: Option<PathBuf>) -> Option<PathBuf> {
let toml_path =
config_flag.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
let using_default_path = toml_path.is_none();
let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
if using_default_path && !toml_path.exists() {
toml_path = src.join(toml_path);
}

if !using_default_path || toml_path.exists() { Some(toml_path) } else { None }
}

if let Some(toml_path) = config_path(&src, config_flag) {
*cfg_path = Some(toml_path.clone());
get_toml(&toml_path)
} else {
*cfg_path = None;
T::default()
}
}

/// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
pub(crate) fn deserialize_stage0_metadata(stage0_metadata_path: &PathBuf) -> Stage0Metadata {
let stage0_json = t!(std::fs::read(stage0_metadata_path.join("src").join("stage0.json")));
t!(serde_json::from_slice::<Stage0Metadata>(&stage0_json))
}

fn src() -> Option<PathBuf> {
// Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary,
// running on a completely machine from where it was compiled.
Expand Down

0 comments on commit a445f80

Please sign in to comment.