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 authored and djc committed Oct 26, 2023
1 parent 33ad33e commit 0907089
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ use crate::currentprocess::{
terminalsource,
varsource::VarSource,
};
use crate::dist::dist::{TargetTriple, ToolchainDesc};
use crate::install::UpdateStatus;
use crate::utils::notifications as util_notifications;
use crate::utils::notify::NotificationLevel;
use crate::utils::utils;
use crate::{dist::dist::ToolchainDesc, install::UpdateStatus};
use crate::{
dist::notifications as dist_notifications, toolchain::distributable::DistributableToolchain,
};
Expand Down Expand Up @@ -674,3 +675,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.");
}
}
5 changes: 5 additions & 0 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,8 @@ fn maybe_upgrade_data(cfg: &Cfg, m: &ArgMatches) -> Result<bool> {
}

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

if let Some(toolchain) = m.get_one::<MaybeResolvableToolchainName>("toolchain") {
match toolchain.to_owned() {
MaybeResolvableToolchainName::None => {
Expand Down Expand Up @@ -917,6 +919,7 @@ 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 @@ -1050,6 +1053,8 @@ fn which(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {

#[cfg_attr(feature = "otel", tracing::instrument(skip_all))]
fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
common::warn_if_host_is_emulated();

let verbose = m.get_flag("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 @@ -604,6 +604,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 @@ -1073,6 +1075,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 @@ -253,6 +253,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 0907089

Please sign in to comment.