Skip to content

Instantly share code, notes, and snippets.

@briansmith
Created November 20, 2020 22:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save briansmith/dd06e8dfeac46d1032d974000dac1fa6 to your computer and use it in GitHub Desktop.
Save briansmith/dd06e8dfeac46d1032d974000dac1fa6 to your computer and use it in GitHub Desktop.
Bootstrap a working native aarch64-apple-darwin Rust toolchain
#!/bin/bash
# "Bash Strict Mode with logging"
set -eux -o pipefail
IFS=$'\n\t'
case $(uname -p) in
arm)
;;
*)
echo This has only been tested with a native ARM shell.
exit 1
;;
esac
if (file $(which rustup) | grep "arm64"); then
exit 0
fi
# Adapted from
# https://github.com/rust-lang/rustup/issues/2413#issuecomment-729140037
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | arch --x86_64 sh -s -- \
-y \
--default-host aarch64-apple-darwin \
--default-toolchain nightly-2020-11-18
rustup_tempdir=$(mktemp -d)
pushd $rustup_tempdir
source ~/.cargo/env
# Adapted from
# https://github.com/rust-lang/rustup/issues/2413#issuecomment-729173518 and
# https://github.com/rust-lang/rustup/issues/2413#issuecomment-729216870
git clone https://github.com/rust-lang/rustup/ .
git checkout 5674652a45ecf9066128df32fbc51e965586e4b2
RUSTC=$(rustup which rustc) $(rustup which cargo) build
RUSTC=$(rustup which rustc) $(rustup which cargo) dev-install
popd
rm -Rf $rustup_tempdir
if ! (file $(which rustup) | grep -q "arm64"); then
echo rustup did not get converted to ARM64 for some reason.
exit 1
fi
@briansmith
Copy link
Author

Note that this was tested with Xcode 12.2 final release (installed from the App Store).

@briansmith
Copy link
Author

briansmith commented Nov 21, 2020

These steps require you to use the GUI:

  1. The very first thing you should do is install the system update that is already available and reboot. Do this before you install Rosetta 2 since I've been told that Rosetta 2 will have to be re-installed after the update anyway.
  2. Install and run some Intel application before installing Xcode. Installing an Intel application will trigger the installation of Rosetta 2, the software that runs intel binaries. (If you try to install and run Xcode without Rosetta already installed, Xcode may hang while it is prompting you about installing Rosetta; this happened to me and some other people I know.)
  3. Install Xcode 12.2. (I installed it from the App Store app. 12.3 beta also worked for me; in fact it worked without so much hoop jumping.)
  4. Start Xcode, log into your account, go into "Manage certificates" and install a code signing certificate. (ARM Macs won't run any unsigned binaries. The Xcode linker will automatically codesign your executables if you have a code signing certificate installed.)

The rest can be done remotely via SSH if you wish:

  1. You currently must use an ARM-compiled terminal and an ARM-compiled shell for the build to work. When a process runs under Rosetta 2, all of its subprocesses will run under Rosetta 2 and they will all think they are running on an Intel Mac. This isn't what you want. In particular, starship and the fish shell are (as of yesterday) Intel-only, so they can't be used until we have a solution for breaking out of Rosetta 2.
  2. Run the script above to install the Rust toolchain.

FYI: I use a non-Apple USB-A keyboard/mouse (ThinkPad Compact Keyboard with TrackPoint). I found that the USB-A ports stopped working at one point. As a result, I couldn't use the keyboard to wake up the screen and log into the locked machine. I was able to tap the power button on the back of the box (M1 Mac Mini) to wake up the screen, but the USB-A ports never turned on, so I had to power the machine off and on to get the keyboard/mouse working. Probably the machine went into deep sleep and never woke up the USB-A ports. Or maybe it was a fluke. IDK. I developed everything on another machine and used Clion's SSH support to copy the files to the Mac over SSH as I edited them, and SSHed in to build. In other words, I pretty much treat my Mac as a headless server. So, I don't have any tips to share regarding normal Mac stuff.

@briansmith
Copy link
Author

briansmith commented Nov 21, 2020

I'd love some assistance with the following points:

  • How could I change the above script so that it works even if the shell is running under Rosetta?

  • How can I force the installation of Rosetta 2, ideally from the command line, without signing into the App Store?

  • Even better, how can I use Xcode, targetting macOS and iOS, and with the simulators available, without installing Rosetta 2?

  • How can I script the any of the steps 1-4 above so they could be done over SSH?

@briansmith
Copy link
Author

The above is no longer necessary. curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly-2020-11-25 worked for me today. See https://blog.rust-lang.org/2020/11/27/Rustup-1.23.0.html#support-for-apple-m1-devices for more info. (Today's Rust Nightly is broken because of briansmith/ring#1130; tomorrow's should work.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment