Skip to content

Commit

Permalink
Warn when running under Rosetta emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Dec 5, 2022
1 parent 3331f34 commit f614a93
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use term2::Terminal;

use super::self_update;
use super::term2;
use crate::dist::dist::TargetTriple;
use crate::dist::notifications as dist_notifications;
use crate::process;
use crate::toolchain::DistributableToolchain;
Expand Down Expand Up @@ -617,3 +618,14 @@ pub(crate) fn ignorable_error(error: &'static str, no_prompt: bool) -> Result<()
Err(error)
}
}

/// Warns if rustup is running under emulation, such as macOS Rosetta
pub(crate) fn warn_if_host_is_emulated() {
if TargetTriple::is_host_emulated() {
warn!(
"Rustup is not running natively. It's running under emulation of {}.",
TargetTriple::from_host_or_build()
);
warn!("For best compatibility and performance you should reinstall rustup for your native CPU.");
}
}
6 changes: 6 additions & 0 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,8 @@ fn default_bare_triple_check(cfg: &Cfg, name: &str) -> Result<()> {
}

fn default_(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
common::warn_if_host_is_emulated();

if m.is_present("toolchain") {
let toolchain = m.value_of("toolchain").unwrap();
default_bare_triple_check(cfg, toolchain)?;
Expand Down Expand Up @@ -939,6 +941,8 @@ fn check_updates(cfg: &Cfg) -> Result<utils::ExitCode> {
}

fn update(cfg: &mut Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
common::warn_if_host_is_emulated();

let self_update_mode = cfg.get_self_update_mode()?;
// Priority: no-self-update feature > self_update_mode > no-self-update args.
// Update only if rustup does **not** have the no-self-update feature,
Expand Down Expand Up @@ -1070,6 +1074,8 @@ fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
}

fn show(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
common::warn_if_host_is_emulated();

let verbose = m.is_present("verbose");

// Print host triple
Expand Down
4 changes: 4 additions & 0 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ fn do_pre_install_sanity_checks(no_prompt: bool) -> Result<()> {
}

fn do_pre_install_options_sanity_checks(opts: &InstallOpts<'_>) -> Result<()> {
common::warn_if_host_is_emulated();

// Verify that the installation options are vaguely sane
(|| {
let host_triple = opts
Expand Down Expand Up @@ -1029,6 +1031,8 @@ pub(crate) fn uninstall(no_prompt: bool) -> Result<utils::ExitCode> {
/// rustup-init is stored in `CARGO_HOME`/bin, and then deleted next
/// time rustup runs.
pub(crate) fn update(cfg: &Cfg) -> Result<utils::ExitCode> {
common::warn_if_host_is_emulated();

use common::SelfUpdatePermission::*;
let update_permitted = if NEVER_SELF_UPDATE {
HardFail
Expand Down
22 changes: 22 additions & 0 deletions src/dist/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,28 @@ impl TargetTriple {
}
}

#[cfg(not(target_os = "macos"))]
pub(crate) fn is_host_emulated() -> bool {
false
}

/// Detects Rosetta emulation on macOS
#[cfg(target_os = "macos")]
pub(crate) fn is_host_emulated() -> bool {
unsafe {
let mut ret: libc::c_int = 0;
let mut size = std::mem::size_of::<libc::c_int>() as libc::size_t;
let err = libc::sysctlbyname(
b"sysctl.proc_translated\0".as_ptr().cast(),
(&mut ret) as *mut _ as *mut libc::c_void,
&mut size,
std::ptr::null_mut(),
0,
);
err == 0 && ret != 0
}
}

pub(crate) fn from_host() -> Option<Self> {
#[cfg(windows)]
fn inner() -> Option<TargetTriple> {
Expand Down

0 comments on commit f614a93

Please sign in to comment.