Skip to content

Commit d3bb61a

Browse files
authored
Merge pull request #2453 from workingjubilee/update-os-cleanup
Refactor self_update* to compile less OS-specific code
2 parents d8eed08 + bfcde68 commit d3bb61a

File tree

4 files changed

+65
-47
lines changed

4 files changed

+65
-47
lines changed

src/bin/rustup-init.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
use std::path::PathBuf;
1818

19+
use cfg_if::cfg_if;
1920
use rs_tracing::*;
2021

2122
use rustup::cli::common;
2223
use rustup::cli::errors::*;
2324
use rustup::cli::proxy_mode;
2425
use rustup::cli::rustup_mode;
26+
#[cfg(windows)]
2527
use rustup::cli::self_update;
2628
use rustup::cli::setup_mode;
2729
use rustup::currentprocess::{process, with, OSProcess};
@@ -83,7 +85,13 @@ fn run_rustup_inner() -> Result<utils::ExitCode> {
8385
Some(n) if n.starts_with("rustup-gc-") => {
8486
// This is the final uninstallation stage on windows where
8587
// rustup deletes its own exe
86-
self_update::complete_windows_uninstall()
88+
cfg_if! {
89+
if #[cfg(windows)] {
90+
self_update::complete_windows_uninstall()
91+
} else {
92+
unreachable!("Attempted to use Windows-specific code on a non-Windows platform. Aborting.")
93+
}
94+
}
8795
}
8896
Some(_) => proxy_mode::main(),
8997
None => {

src/cli/self_update.rs

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use std::fs;
5151
use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};
5252
use std::process::Command;
5353

54+
use cfg_if::cfg_if;
5455
use same_file::Handle;
5556

5657
use super::common::{self, ignorable_error, Confirm};
@@ -65,7 +66,9 @@ use crate::utils::Notification;
6566
use crate::{Cfg, UpdateStatus};
6667
use crate::{DUP_TOOLS, TOOLS};
6768
use os::*;
68-
pub use os::{complete_windows_uninstall, delete_rustup_and_cargo_home, run_update, self_replace};
69+
pub use os::{delete_rustup_and_cargo_home, run_update, self_replace};
70+
#[cfg(windows)]
71+
pub use windows::complete_windows_uninstall;
6972

7073
pub struct InstallOpts<'a> {
7174
pub default_host_triple: Option<String>,
@@ -123,7 +126,7 @@ these changes will be reverted.
123126
};
124127
}
125128

126-
#[cfg(unix)]
129+
#[cfg(not(windows))]
127130
macro_rules! pre_install_msg_unix {
128131
() => {
129132
pre_install_msg_template!(
@@ -154,6 +157,7 @@ but will not be added automatically."
154157
};
155158
}
156159

160+
#[cfg(not(windows))]
157161
macro_rules! post_install_msg_unix {
158162
() => {
159163
r"# Rust is installed now. Great!
@@ -167,6 +171,7 @@ To configure your current shell run `source {cargo_home}/env`
167171
};
168172
}
169173

174+
#[cfg(windows)]
170175
macro_rules! post_install_msg_win {
171176
() => {
172177
r"# Rust is installed now. Great!
@@ -178,6 +183,7 @@ correct environment, but you may need to restart your current shell.
178183
};
179184
}
180185

186+
#[cfg(not(windows))]
181187
macro_rules! post_install_msg_unix_no_modify_path {
182188
() => {
183189
r"# Rust is installed now. Great!
@@ -190,6 +196,7 @@ To configure your current shell run `source {cargo_home}/env`
190196
};
191197
}
192198

199+
#[cfg(windows)]
193200
macro_rules! post_install_msg_win_no_modify_path {
194201
() => {
195202
r"# Rust is installed now. Great!
@@ -211,6 +218,7 @@ This will uninstall all Rust toolchains and data, and remove
211218
};
212219
}
213220

221+
#[cfg(windows)]
214222
static MSVC_MESSAGE: &str = r#"# Rust Visual C++ prerequisites
215223
216224
Rust requires the Microsoft C++ build tools for Visual Studio 2013 or
@@ -249,10 +257,12 @@ fn canonical_cargo_home() -> Result<Cow<'static, str>> {
249257
.unwrap_or_else(|| PathBuf::from("."))
250258
.join(".cargo");
251259
Ok(if default_cargo_home == path {
252-
if cfg!(unix) {
253-
"$HOME/.cargo".into()
254-
} else {
255-
r"%USERPROFILE%\.cargo".into()
260+
cfg_if! {
261+
if #[cfg(windows)] {
262+
r"%USERPROFILE%\.cargo".into()
263+
} else {
264+
"$HOME/.cargo".into()
265+
}
256266
}
257267
} else {
258268
path.to_string_lossy().into_owned().into()
@@ -288,6 +298,8 @@ pub fn install(
288298
do_anti_sudo_check(no_prompt)?;
289299

290300
let mut term = term2::stdout();
301+
302+
#[cfg(windows)]
291303
if !do_msvc_check(&opts)? {
292304
if no_prompt {
293305
warn!("installing msvc toolchain without its prerequisites");
@@ -349,10 +361,9 @@ pub fn install(
349361
// that may have opened just for this purpose, give
350362
// the user an opportunity to see the error before the
351363
// window closes.
352-
if cfg!(windows) && !no_prompt {
353-
writeln!(process().stdout(),)?;
354-
writeln!(process().stdout(), "Press the Enter key to continue.")?;
355-
common::read_line()?;
364+
#[cfg(windows)]
365+
if !no_prompt {
366+
ensure_prompt()?;
356367
}
357368

358369
return Ok(utils::ExitCode(1));
@@ -361,29 +372,32 @@ pub fn install(
361372
let cargo_home = canonical_cargo_home()?;
362373
#[cfg(windows)]
363374
let cargo_home = cargo_home.replace('\\', r"\\");
364-
let msg = match (opts.no_modify_path, cfg!(unix)) {
365-
(false, true) => format!(post_install_msg_unix!(), cargo_home = cargo_home),
366-
(false, false) => format!(post_install_msg_win!(), cargo_home = cargo_home),
367-
(true, true) => format!(
368-
post_install_msg_unix_no_modify_path!(),
369-
cargo_home = cargo_home
370-
),
371-
(true, false) => format!(
375+
#[cfg(windows)]
376+
let msg = if opts.no_modify_path {
377+
format!(
372378
post_install_msg_win_no_modify_path!(),
373379
cargo_home = cargo_home
374-
),
380+
)
381+
} else {
382+
format!(post_install_msg_win!(), cargo_home = cargo_home)
383+
};
384+
#[cfg(not(windows))]
385+
let msg = if opts.no_modify_path {
386+
format!(
387+
post_install_msg_unix_no_modify_path!(),
388+
cargo_home = cargo_home
389+
)
390+
} else {
391+
format!(post_install_msg_unix!(), cargo_home = cargo_home)
375392
};
376393
md(&mut term, msg);
377394

395+
#[cfg(windows)]
378396
if !no_prompt {
379397
// On windows, where installation happens in a console
380398
// that may have opened just for this purpose, require
381399
// the user to press a key to continue.
382-
if cfg!(windows) {
383-
writeln!(process().stdout())?;
384-
writeln!(process().stdout(), "Press the Enter key to continue.")?;
385-
common::read_line()?;
386-
}
400+
ensure_prompt()?;
387401
}
388402

389403
Ok(utils::ExitCode(0))
@@ -503,11 +517,6 @@ fn do_pre_install_options_sanity_checks(opts: &InstallOpts<'_>) -> Result<()> {
503517
Ok(())
504518
}
505519

506-
#[cfg(not(windows))]
507-
fn do_msvc_check(_opts: &InstallOpts<'_>) -> Result<bool> {
508-
Ok(true)
509-
}
510-
511520
fn pre_install_msg(no_modify_path: bool) -> Result<String> {
512521
let cargo_home = utils::cargo_home()?;
513522
let cargo_home_bin = cargo_home.join("bin");
@@ -988,19 +997,16 @@ pub fn prepare_update() -> Result<Option<PathBuf>> {
988997
}
989998

990999
// Get build triple
991-
let build_triple = dist::TargetTriple::from_build();
992-
let triple = if cfg!(windows) {
993-
// For windows x86 builds seem slow when used with windows defender.
994-
// The website defaulted to i686-windows-gnu builds for a long time.
995-
// This ensures that we update to a version thats appropriate for users
996-
// and also works around if the website messed up the detection.
997-
// If someone really wants to use another version, he still can enforce
998-
// that using the environment variable RUSTUP_OVERRIDE_HOST_TRIPLE.
999-
1000-
dist::TargetTriple::from_host().unwrap_or(build_triple)
1001-
} else {
1002-
build_triple
1003-
};
1000+
let triple = dist::TargetTriple::from_build();
1001+
1002+
// For windows x86 builds seem slow when used with windows defender.
1003+
// The website defaulted to i686-windows-gnu builds for a long time.
1004+
// This ensures that we update to a version thats appropriate for users
1005+
// and also works around if the website messed up the detection.
1006+
// If someone really wants to use another version, they still can enforce
1007+
// that using the environment variable RUSTUP_OVERRIDE_HOST_TRIPLE.
1008+
#[cfg(windows)]
1009+
let triple = dist::TargetTriple::from_host().unwrap_or(triple);
10041010

10051011
let update_root = process()
10061012
.var("RUSTUP_UPDATE_ROOT")

src/cli/self_update/unix.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ pub fn delete_rustup_and_cargo_home() -> Result<()> {
5353
Ok(())
5454
}
5555

56-
pub fn complete_windows_uninstall() -> Result<utils::ExitCode> {
57-
panic!("stop doing that")
58-
}
59-
6056
pub fn do_remove_from_path() -> Result<()> {
6157
for sh in shell::get_available_shells() {
6258
let source_bytes = format!("{}\n", sh.source_string()?).into_bytes();

src/cli/self_update/windows.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ use std::path::Path;
33
use std::process::Command;
44

55
use super::super::errors::*;
6+
use super::common;
67
use super::{install_bins, InstallOpts};
78
use crate::dist::dist::TargetTriple;
89
use crate::process;
910
use crate::utils::utils;
1011
use crate::utils::Notification;
1112

13+
pub fn ensure_prompt() -> Result<()> {
14+
writeln!(process().stdout(),)?;
15+
writeln!(process().stdout(), "Press the Enter key to continue.")?;
16+
common::read_line()?;
17+
Ok(())
18+
}
19+
1220
// Provide guidance about setting up MSVC if it doesn't appear to be
1321
// installed
1422
pub fn do_msvc_check(opts: &InstallOpts<'_>) -> Result<bool> {

0 commit comments

Comments
 (0)