Skip to content

Instantly share code, notes, and snippets.

@kaz-yos
Last active October 15, 2021 20:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaz-yos/379e5f0f56ef703dfe2707f3b6fed4da to your computer and use it in GitHub Desktop.
Save kaz-yos/379e5f0f56ef703dfe2707f3b6fed4da to your computer and use it in GitHub Desktop.

Emacs 28 native-comp experiment

  • Reinstall regular gcc without libgccjit
# https:/stackoverflow.com/questions/9369519/reset-homebrew-formula/
# Revert to original gcc.rb
brew update-reset
# Check languages for the absence of jit
brew edit gcc
# Reinstall regular gcc
brew reinstall gcc
  • libgccjit installation
# https:/formulae.brew.sh/formula/libgccjit/
brew install libgccjit
brew upgrade libgccjit
# Warning: libgccjit 10.2.0 already installed
  • Prepare latest gccemacs
# http://xenodium.com/trying-out-gccemacs-on-macos/index.html
cd ~/src/
rm -rf gccemacs
git clone https://github.com/emacs-mirror/emacs gccemacs
cd gccemacs
git checkout feature/native-comp
  • Configure for gccemacs
../configure-gccemacs.sh
# checking for gcc_jit_context_acquire in -lgccjit... no
# configure: error: elisp native compiler requested but libgccjit not found.
# Please try installing libgccjit or similar package.

# Modified ../configure-gccemacs.sh to use the following
# readonly GCC_DIR="$(realpath $(brew --prefix)/opt/libgccjit)"
../configure-gccemacs.sh
# checking for gcc_jit_context_acquire in -lgccjit... yes
# checking libgccjit.h usability... yes
# checking libgccjit.h presence... yes
# checking for libgccjit.h... yes
# configure: error: Installed libgccjit has failed passing the smoke test.
# You can verify it yourself compiling:
# <https://gcc.gnu.org/onlinedocs/jit/intro/tutorial01.html>.
# Please report the issue to your distribution if libgccjit was installed through
# that.
# Here instructions on how to compile and install libgccjit from source:
# <https://gcc.gnu.org/wiki/JIT>.

# https://gcc.gnu.org/onlinedocs/jit/intro/tutorial01.html
cd ../
gcc tut01-hello-world.c -o tut01-hello-world -lgccjit
# ld: library not found for -lgccjit
# clang: error: linker command failed with exit code 1 (use -v to see invocation)

# Added following @mnewt's advice
# https://github.com/Homebrew/homebrew-core/pull/60338#issuecomment-735275157
readonly GCC_LIBRARY_PATH="$(brew --prefix libgccjit)/lib/gcc/10"
[[ -d $GCC_LIBRARY_PATH ]] ||  { echo "${GCC_LIBRARY_PATH} not found"; exit 1; }
export LIBRARY_PATH="${GCC_LIBRARY_PATH}"

gcc tut01-hello-world.c -o tut01-hello-world -lgccjit
# ld: warning: dylib (/usr/local/opt/libgccjit/lib/gcc/10//libgccjit.so) was built for newer macOS version (10.16) than being linked (10.15.6)

brew reinstall gcc
brew reinstall libgccjit

gcc tut01-hello-world.c -o tut01-hello-world -lgccjit
# ld: warning: dylib (/usr/local/opt/libgccjit/lib/gcc/10//libgccjit.so) was built for newer macOS version (10.16) than being linked (10.15.6)

# Need to reinstall CLT under Big Sur
rm -rf /Library/Developer/CommandLineTools
xcode-select --install

gcc tut01-hello-world.c -o tut01-hello-world -lgccjit
# No error
./tut01-hello-world
# hello world

# Success if the following appears.
#   Does Emacs have native lisp compiler?                   yes
  • Compile
sysctl hw.logicalcpu
# hw.logicalcpu: 8
make -j8
make install
#!/bin/bash

# Original from http://xenodium.com/trying-out-gccemacs-on-macos/index.html

set -o nounset
set -o errexit

# Configures Emacs for building native comp support
# http://akrl.sdf.org/gccemacs.html

# Installing under homebrew's opt, but could be anywhere.
readonly GCCEMACS_PREFIX="$(realpath $(brew --prefix)/opt/gccemacs)"

# Changed from
# readonly GCC_DIR="$(realpath $(brew --prefix)/opt/gcc)"
readonly GCC_DIR="$(realpath $(brew --prefix)/opt/libgccjit)"
[[ -d $GCC_DIR ]] ||  { echo "${GCC_DIR} not found"; exit 1; }

readonly SED_DIR="$(realpath $(brew --prefix)/opt/gnu-sed)"
[[ -d $SED_DIR ]] ||  { echo "${SED_DIR} not found"; exit 1; }

readonly GCC_INCLUDE_DIR=${GCC_DIR}/include
[[ -d $GCC_INCLUDE_DIR ]] ||  { echo "${GCC_INCLUDE_DIR} not found"; exit 1; }

readonly GCC_LIB_DIR=${GCC_DIR}/lib/gcc/10
[[ -d $GCC_LIB_DIR ]] ||  { echo "${GCC_LIB_DIR} not found"; exit 1; }

# Added, following @mnewt
# https://github.com/Homebrew/homebrew-core/pull/60338#issuecomment-735275157
readonly GCC_LIBRARY_PATH="$(brew --prefix libgccjit)/lib/gcc/10"
[[ -d $GCC_LIBRARY_PATH ]] ||  { echo "${GCC_LIBRARY_PATH} not found"; exit 1; }

export PATH="${SED_DIR}/libexec/gnubin:${PATH}"
export CFLAGS="-I${GCC_INCLUDE_DIR}"
export LDFLAGS="-L${GCC_LIB_DIR} -I${GCC_INCLUDE_DIR}"
export DYLD_FALLBACK_LIBRARY_PATH="${GCC_LIB_DIR}"
# Added, following @mnewt
# https://github.com/Homebrew/homebrew-core/pull/60338#issuecomment-735275157
export LIBRARY_PATH="${GCC_LIBRARY_PATH}"

echo "Environment"
echo "-----------"
echo PATH: $PATH
echo CFLAGS: $CFLAGS
echo LDFLAGS: $LDFLAGS
echo DYLD_FALLBACK_LIBRARY_PATH: $DYLD_FALLBACK_LIBRARY_PATH
# Added, following @mnewt
# https://github.com/Homebrew/homebrew-core/pull/60338#issuecomment-735275157
echo LIBRARY_PATH: $LIBRARY_PATH
echo "-----------"

./autogen.sh

./configure \
     --prefix="${GCCEMACS_PREFIX}" \
     --enable-locallisppath="${GCCEMACS_PREFIX}/opt/gccemacs/site-lisp" \
     --with-mailutils \
     --with-ns \
     --with-imagemagick \
     --with-cairo \
     --with-modules \
     --with-xml2 \
     --with-gnutls \
     --with-json \
     --with-rsvg \
     --with-nativecomp \
     --disable-silent-rules \
     --disable-ns-self-contained \
     --without-dbus
@kaz-yos
Copy link
Author

kaz-yos commented Nov 29, 2020

This procedure installs gccemacs using the new libgccjit Homebrew formula (Oct 2020) without having to edit the gcc formula.

@kaz-yos
Copy link
Author

kaz-yos commented Nov 29, 2020

Maybe we don't need to set LIBRARY_PATH.

@bitti
Copy link

bitti commented Apr 24, 2021

Thanks very helpful! Note: --with-nativecomp should be replaced with --with-native-compilation now: https://lists.gnu.org/archive/html/emacs-devel/2021-02/msg02011.html.

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