From 4dfe254edcafeb755ae6c37d1f9c00d8e2f2b2b3 Mon Sep 17 00:00:00 2001 From: rami3l Date: Mon, 14 Aug 2023 11:54:40 +0800 Subject: [PATCH 1/2] macOS `uname -m` can lie due to Rosetta shenanigans Fix originally provided in https://github.com/JuliaLang/juliaup/pull/701 --- rustup-init.sh | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/rustup-init.sh b/rustup-init.sh index ddc99f2472..7e8de44ea5 100755 --- a/rustup-init.sh +++ b/rustup-init.sh @@ -255,10 +255,19 @@ get_architecture() { fi fi - if [ "$_ostype" = Darwin ] && [ "$_cputype" = i386 ]; then - # Darwin `uname -m` lies - if sysctl hw.optional.x86_64 | grep -q ': 1'; then - _cputype=x86_64 + if [ "$_ostype" = Darwin ]; then + # Darwin `uname -m` can lie due to Rosetta shenanigans. If you manage to + # invoke a native shell binary and then a native uname binary, you can + # get the real answer, but that's hard to ensure, so instead we use + # `sysctl` (which doesn't lie) to check for the actual architecture. + if [ "$_cputype" = i386 ]; then + if sysctl hw.optional.x86_64 | grep -q ': 1'; then + _cputype=x86_64 + fi + elif [ "$_cputype" = x86_64 ]; then + if sysctl hw.optional.arm64 | grep -q ': 1'; then + _cputype=arm64 + fi fi fi From e2c2cf8de62e4ce9001507236fb1d577b921662c Mon Sep 17 00:00:00 2001 From: rami3l Date: Tue, 15 Aug 2023 09:45:10 +0800 Subject: [PATCH 2/2] Avoid `sysctl: unknown oid` stderr output and/or non-zero exit code --- rustup-init.sh | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/rustup-init.sh b/rustup-init.sh index 7e8de44ea5..e783bcfa61 100755 --- a/rustup-init.sh +++ b/rustup-init.sh @@ -261,11 +261,22 @@ get_architecture() { # get the real answer, but that's hard to ensure, so instead we use # `sysctl` (which doesn't lie) to check for the actual architecture. if [ "$_cputype" = i386 ]; then - if sysctl hw.optional.x86_64 | grep -q ': 1'; then + # Handling i386 compatibility mode in older macOS versions (<10.15) + # running on x86_64-based Macs. + # Starting from 10.15, macOS explicitly bans all i386 binaries from running. + # See: + + # Avoid `sysctl: unknown oid` stderr output and/or non-zero exit code. + if sysctl hw.optional.x86_64 2> /dev/null || true | grep -q ': 1'; then _cputype=x86_64 fi elif [ "$_cputype" = x86_64 ]; then - if sysctl hw.optional.arm64 | grep -q ': 1'; then + # Handling x86-64 compatibility mode (a.k.a. Rosetta 2) + # in newer macOS versions (>=11) running on arm64-based Macs. + # Rosetta 2 is built exclusively for x86-64 and cannot run i386 binaries. + + # Avoid `sysctl: unknown oid` stderr output and/or non-zero exit code. + if sysctl hw.optional.arm64 2> /dev/null || true | grep -q ': 1'; then _cputype=arm64 fi fi