Skip to content

Commit 219847a

Browse files
committed
Warn when running under Rosetta emulation
1 parent e0ffef9 commit 219847a

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/cli/common.rs

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use term2::Terminal;
1313

1414
use super::self_update;
1515
use super::term2;
16+
use crate::dist::dist::TargetTriple;
1617
use crate::dist::notifications as dist_notifications;
1718
use crate::process;
1819
use crate::toolchain::DistributableToolchain;
@@ -617,3 +618,14 @@ pub(crate) fn ignorable_error(error: &'static str, no_prompt: bool) -> Result<()
617618
Err(error)
618619
}
619620
}
621+
622+
/// Warns if rustup is running under emulation, such as macOS Rosetta
623+
pub(crate) fn warn_if_host_is_emulated() {
624+
if TargetTriple::is_host_emulated() {
625+
warn!(
626+
"Rustup is not running natively. It's running under emulation of {}.",
627+
TargetTriple::from_host_or_build()
628+
);
629+
warn!("For best compatibility and performance you should reinstall rustup for your native CPU.");
630+
}
631+
}

src/cli/rustup_mode.rs

+6
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,8 @@ fn default_bare_triple_check(cfg: &Cfg, name: &str) -> Result<()> {
853853
}
854854

855855
fn default_(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
856+
common::warn_if_host_is_emulated();
857+
856858
if m.is_present("toolchain") {
857859
let toolchain = m.value_of("toolchain").unwrap();
858860
default_bare_triple_check(cfg, toolchain)?;
@@ -939,6 +941,8 @@ fn check_updates(cfg: &Cfg) -> Result<utils::ExitCode> {
939941
}
940942

941943
fn update(cfg: &mut Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
944+
common::warn_if_host_is_emulated();
945+
942946
let self_update_mode = cfg.get_self_update_mode()?;
943947
// Priority: no-self-update feature > self_update_mode > no-self-update args.
944948
// Update only if rustup does **not** have the no-self-update feature,
@@ -1070,6 +1074,8 @@ fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
10701074
}
10711075

10721076
fn show(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
1077+
common::warn_if_host_is_emulated();
1078+
10731079
let verbose = m.is_present("verbose");
10741080

10751081
// Print host triple

src/cli/self_update.rs

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

591591
fn do_pre_install_options_sanity_checks(opts: &InstallOpts<'_>) -> Result<()> {
592+
common::warn_if_host_is_emulated();
593+
592594
// Verify that the installation options are vaguely sane
593595
(|| {
594596
let host_triple = opts
@@ -1029,6 +1031,8 @@ pub(crate) fn uninstall(no_prompt: bool) -> Result<utils::ExitCode> {
10291031
/// rustup-init is stored in `CARGO_HOME`/bin, and then deleted next
10301032
/// time rustup runs.
10311033
pub(crate) fn update(cfg: &Cfg) -> Result<utils::ExitCode> {
1034+
common::warn_if_host_is_emulated();
1035+
10321036
use common::SelfUpdatePermission::*;
10331037
let update_permitted = if NEVER_SELF_UPDATE {
10341038
HardFail

src/dist/dist.rs

+22
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,28 @@ impl TargetTriple {
225225
}
226226
}
227227

228+
#[cfg(not(target_os = "macos"))]
229+
pub(crate) fn is_host_emulated() -> bool {
230+
false
231+
}
232+
233+
/// Detects Rosetta emulation on macOS
234+
#[cfg(target_os = "macos")]
235+
pub(crate) fn is_host_emulated() -> bool {
236+
unsafe {
237+
let mut ret: libc::c_int = 0;
238+
let mut size = std::mem::size_of::<libc::c_int>() as libc::size_t;
239+
let err = libc::sysctlbyname(
240+
b"sysctl.proc_translated\0".as_ptr().cast(),
241+
(&mut ret) as *mut _ as *mut libc::c_void,
242+
&mut size,
243+
std::ptr::null_mut(),
244+
0,
245+
);
246+
err == 0 && ret != 0
247+
}
248+
}
249+
228250
pub(crate) fn from_host() -> Option<Self> {
229251
#[cfg(windows)]
230252
fn inner() -> Option<TargetTriple> {

0 commit comments

Comments
 (0)