Skip to content

Commit f611de0

Browse files
authored
Rollup merge of #93487 - yerke:yerke/fix-link-toolchain-in-setup, r=Mark-Simulacrum
Fix linking stage1 toolchain in `./x.py setup` Closes [92319](#92319) Fix linking stage1 toolchain in `./x.py setup`. I guess this can be considered a follow up to #89212 by `````@Sl1mb0.````` We create 2 directories and 1 file that are required by rustup to [link a custom toolchain from path](https://github.com/rust-lang/rustup/blob/5225e87a5d974ab5f1626bcb2a7b43f76ab883f0/src/toolchain.rs#L479-L497). cc `````@jyn514````` and `````@Mark-Simulacrum````` as they were active in #89206
2 parents 252ff5e + a4112dc commit f611de0

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/bootstrap/setup.rs

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::TargetSelection;
22
use crate::{t, VERSION};
3+
use std::env::consts::EXE_SUFFIX;
34
use std::fmt::Write as _;
4-
use std::path::{Path, PathBuf};
5+
use std::fs::File;
6+
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
57
use std::process::Command;
68
use std::str::FromStr;
79
use std::{
@@ -109,7 +111,8 @@ pub fn setup(src_path: &Path, profile: Profile) {
109111
println!("`x.py` will now use the configuration at {}", include_path.display());
110112

111113
let build = TargetSelection::from_user(&env!("BUILD_TRIPLE"));
112-
let stage_path = ["build", build.rustc_target_arg(), "stage1"].join("/");
114+
let stage_path =
115+
["build", build.rustc_target_arg(), "stage1"].join(&MAIN_SEPARATOR.to_string());
113116

114117
println!();
115118

@@ -171,6 +174,13 @@ fn attempt_toolchain_link(stage_path: &str) {
171174
return;
172175
}
173176

177+
if !ensure_stage1_toolchain_placeholder_exists(stage_path) {
178+
println!(
179+
"Failed to create a template for stage 1 toolchain or confirm that it already exists"
180+
);
181+
return;
182+
}
183+
174184
if try_link_toolchain(&stage_path[..]) {
175185
println!(
176186
"Added `stage1` rustup toolchain; try `cargo +stage1 build` on a separate rust project to run a newly-built toolchain"
@@ -219,6 +229,33 @@ fn try_link_toolchain(stage_path: &str) -> bool {
219229
.map_or(false, |output| output.status.success())
220230
}
221231

232+
fn ensure_stage1_toolchain_placeholder_exists(stage_path: &str) -> bool {
233+
let pathbuf = PathBuf::from(stage_path);
234+
235+
if fs::create_dir_all(pathbuf.join("lib")).is_err() {
236+
return false;
237+
};
238+
239+
let pathbuf = pathbuf.join("bin");
240+
if fs::create_dir_all(&pathbuf).is_err() {
241+
return false;
242+
};
243+
244+
let pathbuf = pathbuf.join(format!("rustc{}", EXE_SUFFIX));
245+
246+
if pathbuf.exists() {
247+
return true;
248+
}
249+
250+
// Take care not to overwrite the file
251+
let result = File::options().append(true).create(true).open(&pathbuf);
252+
if result.is_err() {
253+
return false;
254+
}
255+
256+
return true;
257+
}
258+
222259
// Used to get the path for `Subcommand::Setup`
223260
pub fn interactive_path() -> io::Result<Profile> {
224261
fn abbrev_all() -> impl Iterator<Item = ((String, String), Profile)> {

0 commit comments

Comments
 (0)