Skip to content

Commit 8ad8906

Browse files
committed
refactor src/tools/x
Signed-off-by: ozkanonur <[email protected]>
1 parent a8f85ae commit 8ad8906

File tree

9 files changed

+200
-146
lines changed

9 files changed

+200
-146
lines changed

src/bootstrap/bin/bootstrap-shim.rs

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,56 @@
1-
use std::{env, process::Command};
1+
use std::{
2+
env, io,
3+
process::{self, Command, ExitStatus},
4+
};
25

36
use bootstrap::{Flags, MinimalConfig};
47

58
#[path = "../../../src/tools/x/src/main.rs"]
6-
mod run_python;
9+
mod x;
10+
11+
/// We are planning to exclude python logic from x script by executing bootstrap-shim
12+
/// immediately. Since `find_and_run_available_bootstrap_script` executes x script first,
13+
/// any changes on bootstrap will not be seen. To prevent this problem, in bootstrap-shim
14+
/// we want to call the python script directly.
15+
fn find_and_run_py_bootstrap_script() {
16+
#[cfg(unix)]
17+
fn exec_or_status(command: &mut Command) -> io::Result<ExitStatus> {
18+
use std::os::unix::process::CommandExt;
19+
Err(command.exec())
20+
}
21+
22+
#[cfg(not(unix))]
23+
fn exec_or_status(command: &mut Command) -> io::Result<ExitStatus> {
24+
command.status()
25+
}
26+
27+
let current_path = match env::current_dir() {
28+
Ok(dir) => dir,
29+
Err(err) => {
30+
eprintln!("Failed to get current directory: {err}");
31+
process::exit(1);
32+
}
33+
};
34+
35+
for dir in current_path.ancestors() {
36+
let candidate = dir.join("x.py");
37+
if candidate.exists() {
38+
let mut cmd: Command;
39+
cmd = Command::new(x::python());
40+
cmd.arg(&candidate).args(env::args().skip(1)).current_dir(dir);
41+
let result = exec_or_status(&mut cmd);
42+
43+
match result {
44+
Err(error) => {
45+
eprintln!("Failed to invoke `{:?}`: {}", cmd, error);
46+
}
47+
Ok(status) => {
48+
process::exit(status.code().unwrap_or(1));
49+
}
50+
}
51+
}
52+
}
53+
}
754

855
fn main() {
956
let args = env::args().skip(1).collect::<Vec<_>>();
@@ -15,16 +62,17 @@ fn main() {
1562
let bootstrap_bin = if let Some(commit) = last_modified_bootstrap_commit(&config) {
1663
config.download_bootstrap(&commit)
1764
} else {
18-
return run_python::main();
65+
return find_and_run_py_bootstrap_script();
1966
};
2067

2168
let args: Vec<_> = std::env::args().skip(1).collect();
69+
println!("Running pre-compiled bootstrap binary");
2270
Command::new(bootstrap_bin).args(args).status().expect("failed to spawn bootstrap binairy");
2371
}
2472

2573
fn last_modified_bootstrap_commit(config: &MinimalConfig) -> Option<String> {
2674
config.last_modified_commit(
27-
&["src/bootstrap", "src/tools/build_helper"],
75+
&["src/bootstrap", "src/tools/build_helper", "src/tools/x"],
2876
"download-bootstrap",
2977
true,
3078
)

src/bootstrap/builder.rs

-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,6 @@ impl<'a> Builder<'a> {
704704
check::RustAnalyzer,
705705
check::Rustfmt,
706706
check::Bootstrap,
707-
check::BootstrapShim,
708707
),
709708
Kind::Test => describe!(
710709
crate::toolstate::ToolStateCheck,

src/bootstrap/check.rs

-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ tool_check_step!(MiroptTestTools, "src/tools/miropt-test-tools", SourceType::InT
450450

451451
// FIXME: currently these are marked as ToolRustc, but they should be ToolBootstrap instead to avoid having to build the compiler first
452452
tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);
453-
tool_check_step!(BootstrapShim, "src/bootstrap/bin/bootstrap-shim", SourceType::InTree, false);
454453

455454
/// Cargo's output path for the standard library in a given stage, compiled
456455
/// by a particular compiler for the specified target.

src/bootstrap/config.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ pub struct Config {
8585
pub jobs: Option<u32>,
8686
pub cmd: Subcommand,
8787
pub incremental: bool,
88-
pub dry_run: DryRun,
8988
/// Arguments appearing after `--` to be forwarded to tools,
9089
/// e.g. `--fix-broken` or test arguments.
9190
pub free_args: Vec<String>,
@@ -395,7 +394,7 @@ impl Target {
395394
/// `Config` structure.
396395
#[derive(Deserialize, Default)]
397396
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
398-
pub struct TomlConfig {
397+
struct TomlConfig {
399398
changelog_seen: Option<usize>,
400399
build: Option<Build>,
401400
install: Option<Install>,
@@ -774,17 +773,19 @@ impl Config {
774773
args
775774
}
776775

777-
pub fn parse(args: &[String], custom_toml_config: Option<TomlConfig>) -> Config {
776+
pub fn parse(args: &[String], custom_toml_config: Option<&str>) -> Config {
778777
let mut flags = Flags::parse(&args);
779778
let mut config = Config::default_opts();
780779

781-
let mut toml: TomlConfig = custom_toml_config.unwrap_or_else(|| {
780+
let mut toml: TomlConfig = if let Some(custom_toml_config) = custom_toml_config {
781+
toml::from_str(custom_toml_config).unwrap()
782+
} else {
782783
set_cfg_path_and_return_toml_cfg(
783784
config.src.clone(),
784785
flags.config.clone(),
785786
&mut config.config,
786787
)
787-
});
788+
};
788789

789790
config.minimal_config = MinimalConfig::parse(&flags, toml.build.clone());
790791

@@ -814,11 +815,6 @@ impl Config {
814815
crate::detail_exit(1);
815816
}
816817

817-
let build = toml.build.clone().unwrap_or_default();
818-
if let Some(file_build) = build.build.as_ref() {
819-
config.build = TargetSelection::from_user(file_build);
820-
};
821-
822818
if let Some(include) = &toml.profile {
823819
let mut include_path = config.src.clone();
824820
include_path.push("src");
@@ -831,6 +827,11 @@ impl Config {
831827

832828
config.changelog_seen = toml.changelog_seen;
833829

830+
let build = toml.build.unwrap_or_default();
831+
if let Some(file_build) = build.build {
832+
config.build = TargetSelection::from_user(&file_build);
833+
};
834+
834835
set(&mut config.out, flags.build_dir.or_else(|| build.build_dir.map(PathBuf::from)));
835836
// NOTE: Bootstrap spawns various commands with different working directories.
836837
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.

src/bootstrap/config/tests.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
use super::{Config, Flags, TomlConfig};
1+
use super::{Config, Flags};
22
use clap::CommandFactory;
33
use std::{env, path::Path};
44

5-
fn toml(config: &str) -> TomlConfig {
6-
toml::from_str(config).unwrap()
7-
}
8-
95
fn parse(config: &str) -> Config {
10-
Config::parse(&["check".to_owned(), "--config=/does/not/exist".to_owned()], Some(toml(config)))
6+
Config::parse(&["check".to_owned(), "--config=/does/not/exist".to_owned()], Some(config))
117
}
128

139
#[test]

src/bootstrap/dist.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,6 @@ impl Step for BootstrapShim {
23272327
const ONLY_HOSTS: bool = true;
23282328

23292329
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2330-
// Create this even with only `dist bootstrap` to avoid having to update all CI builders.
23312330
run.alias("bootstrap-shim")
23322331
}
23332332

0 commit comments

Comments
 (0)