Skip to content

Commit 0907089

Browse files
kornelskidjc
authored andcommitted
Warn when running under Rosetta emulation
1 parent 33ad33e commit 0907089

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/cli/common.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ use crate::currentprocess::{
2020
terminalsource,
2121
varsource::VarSource,
2222
};
23+
use crate::dist::dist::{TargetTriple, ToolchainDesc};
24+
use crate::install::UpdateStatus;
2325
use crate::utils::notifications as util_notifications;
2426
use crate::utils::notify::NotificationLevel;
2527
use crate::utils::utils;
26-
use crate::{dist::dist::ToolchainDesc, install::UpdateStatus};
2728
use crate::{
2829
dist::notifications as dist_notifications, toolchain::distributable::DistributableToolchain,
2930
};
@@ -674,3 +675,14 @@ pub(crate) fn ignorable_error(error: &'static str, no_prompt: bool) -> Result<()
674675
Err(error)
675676
}
676677
}
678+
679+
/// Warns if rustup is running under emulation, such as macOS Rosetta
680+
pub(crate) fn warn_if_host_is_emulated() {
681+
if TargetTriple::is_host_emulated() {
682+
warn!(
683+
"Rustup is not running natively. It's running under emulation of {}.",
684+
TargetTriple::from_host_or_build()
685+
);
686+
warn!("For best compatibility and performance you should reinstall rustup for your native CPU.");
687+
}
688+
}

src/cli/rustup_mode.rs

+5
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ fn maybe_upgrade_data(cfg: &Cfg, m: &ArgMatches) -> Result<bool> {
840840
}
841841

842842
fn default_(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
843+
common::warn_if_host_is_emulated();
844+
843845
if let Some(toolchain) = m.get_one::<MaybeResolvableToolchainName>("toolchain") {
844846
match toolchain.to_owned() {
845847
MaybeResolvableToolchainName::None => {
@@ -917,6 +919,7 @@ fn check_updates(cfg: &Cfg) -> Result<utils::ExitCode> {
917919
}
918920

919921
fn update(cfg: &mut Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
922+
common::warn_if_host_is_emulated();
920923
let self_update_mode = cfg.get_self_update_mode()?;
921924
// Priority: no-self-update feature > self_update_mode > no-self-update args.
922925
// Update only if rustup does **not** have the no-self-update feature,
@@ -1050,6 +1053,8 @@ fn which(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
10501053

10511054
#[cfg_attr(feature = "otel", tracing::instrument(skip_all))]
10521055
fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1056+
common::warn_if_host_is_emulated();
1057+
10531058
let verbose = m.get_flag("verbose");
10541059

10551060
// Print host triple

src/cli/self_update.rs

+4
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ fn do_pre_install_sanity_checks(no_prompt: bool) -> Result<()> {
604604
}
605605

606606
fn do_pre_install_options_sanity_checks(opts: &InstallOpts<'_>) -> Result<()> {
607+
common::warn_if_host_is_emulated();
608+
607609
// Verify that the installation options are vaguely sane
608610
(|| {
609611
let host_triple = opts
@@ -1073,6 +1075,8 @@ pub(crate) fn uninstall(no_prompt: bool) -> Result<utils::ExitCode> {
10731075
/// rustup-init is stored in `CARGO_HOME`/bin, and then deleted next
10741076
/// time rustup runs.
10751077
pub(crate) fn update(cfg: &Cfg) -> Result<utils::ExitCode> {
1078+
common::warn_if_host_is_emulated();
1079+
10761080
use common::SelfUpdatePermission::*;
10771081
let update_permitted = if NEVER_SELF_UPDATE {
10781082
HardFail

src/dist/dist.rs

+22
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,28 @@ impl TargetTriple {
253253
}
254254
}
255255

256+
#[cfg(not(target_os = "macos"))]
257+
pub(crate) fn is_host_emulated() -> bool {
258+
false
259+
}
260+
261+
/// Detects Rosetta emulation on macOS
262+
#[cfg(target_os = "macos")]
263+
pub(crate) fn is_host_emulated() -> bool {
264+
unsafe {
265+
let mut ret: libc::c_int = 0;
266+
let mut size = std::mem::size_of::<libc::c_int>() as libc::size_t;
267+
let err = libc::sysctlbyname(
268+
b"sysctl.proc_translated\0".as_ptr().cast(),
269+
(&mut ret) as *mut _ as *mut libc::c_void,
270+
&mut size,
271+
std::ptr::null_mut(),
272+
0,
273+
);
274+
err == 0 && ret != 0
275+
}
276+
}
277+
256278
pub(crate) fn from_host() -> Option<Self> {
257279
#[cfg(windows)]
258280
fn inner() -> Option<TargetTriple> {

0 commit comments

Comments
 (0)