Skip to content

Instantly share code, notes, and snippets.

@naokton
Last active October 2, 2021 02:02
Show Gist options
  • Save naokton/762bea1b1a4a6301bc986d57b6a3c223 to your computer and use it in GitHub Desktop.
Save naokton/762bea1b1a4a6301bc986d57b6a3c223 to your computer and use it in GitHub Desktop.
Build Emacs 27.1 (--with-ns / GUI) on Apple Silicon Mac (aarc64) with Homebrew
#!/bin/bash
if [ ! -f /opt/homebrew/bin/brew ]; then
echo "Install ARM homebrew to /opt/homebrew"
exit
fi
message() {
TIME=$(date "+%Y-%m-%dT%H:%M:%S")
MSG=$@
echo "$TIME | $MSG"
}
# Use ARM Homebrew binaries
export PATH=/opt/homebrew/bin:$PATH
message "Install dependencies as arm64 binary"
brew install pkg-config gnutls jansson
# Emacs source code
message "Prepare Emacs source code"
[ -f emacs-27.1.tar.xz ] ||
curl -sLO https://ftpmirror.gnu.org/emacs/emacs-27.1.tar.xz
[ -d emacs-27.1 ] && rm -rf emacs-27.1
tar xf emacs-27.1.tar.xz
# Patches to build on Apple Silicon
[ -f patch1 ] ||
curl -L -o patch1 'https://github.com/emacs-mirror/emacs/commit/868f51324ac96bc3af49a826e1db443548c9d6cc.patch?full_index=1'
[ -f patch2 ] ||
curl -L -o patch2 https://raw.githubusercontent.com/Homebrew/formula-patches/25c1e1797d4004a9e5b9453779399afc63d04b97/emacs/arm.patch
cd emacs-27.1
patch -p1 < ../patch1
patch -p1 < ../patch2
# Exclude Intel homebrew PATH
export PATH=$(echo $PATH | tr ':' '\n' | grep -v '/usr/local' | tr '\n' ':' | sed -E 's/^(.*):$/\1/')
# exclude intel homebrew pkg-config default search path
export PKG_CONFIG_LIBDIR=$(pkg-config --variable pc_path pkg-config | tr ':' '\n' | grep -v '/usr/local' | tr '\n' ':' | sed -E 's/^(.*):$/\1/')
# Build
message "Configure"
./configure
# Default options:
# --with-gnutls\
# --with-xml2\
# --with-modules\
# --with-ns\
# --without-x\
# --without-dbus\
message "Make"
NCPU=$(getconf _NPROCESSORS_ONLN)
make -j$NCPU
make install -j$NCPU
# Package dynamic libraries into .app
# https://github.com/takaxp/ns-inline-patch/blob/master/build/rudix/emacs-27.1.sh
# https://www.reddit.com/r/emacs/comments/bclyyc/building_emacs_from_source_on_macos/
message "Copy dynamic libraries and re-sign codes"
APP=nextstep/Emacs.app
mkdir $APP/Contents/Frameworks
/usr/bin/python -m macholib standalone $APP
codesign -s - --preserve-metadata=entitlements,requirements,flags,runtime --deep -f $APP
message "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment