Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build gccemacs on MacOS catalina with gcc 10 installed by homebrew.

Use pre-built gccemacs

Fortunatly we could use pre-built gccemacs right now.

Those two repos did the greate job for us.

https://github.com/twlz0ne/nix-gccemacs-darwin

https://github.com/twlz0ne/nix-gccemacs-sierra

Here is the tutorial:

Install nix

https://nixos.org/

For MacOS catalina

Install

nix-env -iA cachix -f https://cachix.org/api/v1/install
cachix use gccemacs-darwin
nix-env -iA emacsGccDarwin -f https://github.com/twlz0ne/nix-gccemacs-darwin/archive/master.zip

Update

nix-env -iA emacsGccDarwin -f https://github.com/twlz0ne/nix-gccemacs-darwin/archive/master.zip

For MacOS sierra

Install

nix-env -iA cachix -f https://cachix.org/api/v1/install
cachix use gccemacs-sierra
nix-env -iA emacsGccSierra -f https://github.com/twlz0ne/nix-gccemacs-sierra/archive/master.zip

Update

nix-env -iA emacsGccSierra -f https://github.com/twlz0ne/nix-gccemacs-sierra/archive/master.zip

It's done

Launch emacs from terminal, and you could find related app bundle's path by right click emacs icon in Docker.

Build gccemacs on MacOS catalina

It's a hard time to make gccemacs work on MacOS catalina. I've met countless problems by following every tutorials I can get from google. Fortunately I finally get it work!

Hope this tutorial could help you.

GCC with libgccjit enabled

cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula

Edit gcc.rb like this

@@ -53,7 +53,7 @@ class Gcc < Formula
     #  - Ada, which requires a pre-existing GCC Ada compiler to bootstrap
     #  - Go, currently not supported on macOS
     #  - BRIG
-    languages = %w[c c++ objc obj-c++ fortran]
+    languages = %w[c c++ objc obj-c++ fortran jit]

     osmajor = `uname -r`.split(".").first
     pkgversion = "Homebrew GCC #{pkg_version} #{build.used_options*" "}".strip
@@ -73,6 +73,7 @@ class Gcc < Formula
       --with-system-zlib
       --with-pkgversion=#{pkgversion}
       --with-bugurl=https://github.com/Homebrew/homebrew-core/issues
+      --enable-host-shared
     ]

     # Xcode 10 dropped 32-bit support

Build gcc by homebrew brew install gcc --build-from-source --force

Get gccemacs source code

Clone https://github.com/emacs-mirror/emacs and checkout feature/native-comp branch.

Build using this script

Create a build.sh outside emacs source code folder.

# native-comp optimization
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:${PATH}"
export CFLAGS="-I/usr/local/Cellar/gcc/10.1.0/include"
export LDFLAGS="-L/usr/local/Cellar/gcc/10.1.0/lib/gcc/10 -I/usr/local/Cellar/gcc/10.1.0/include"
export LIBRARY_PATH="/usr/local/Cellar/gcc/10.1.0/lib/gcc/10:${LIBRARY_PATH:-}"

cd emacs || exit

git clean -xfd

./autogen.sh

./configure \
     --disable-dependency-tracking \
     --disable-silent-rules \
     --enable-locallisppath=/usr/local/share/emacs/28.0.50/site-lisp \
     --prefix=/usr/local/opt/gccemacs \
     --without-dbus \
     --without-imagemagick \
     --with-mailutils \
     --with-ns \
     --disable-ns-self-contained \
     --with-cairo \
     --with-modules \
     --with-xml2 \
     --with-gnutls \
     --with-json \
     --with-rsvg \
     --with-nativecomp

# Ensure /usr/local/opt/gccemacs exists
rm -rf /usr/local/opt/gccemacs
mkdir /usr/local/opt/gccemacs

# Ensure the directory to which we will dump Emacs exists and has the correct
# permissions set.
libexec=/usr/local/libexec/emacs/28.0.50
if [ ! -d $libexec ]; then
  sudo mkdir -p $libexec
  sudo chown $USER $libexec
fi

make -j4
make install 

rm -rf "/Applications/Emacs.app"
mv nextstep/Emacs.app "/Applications/"

cd /usr/local/bin || exit
rm emacs
rm emacsclient
ln -s /usr/local/opt/gccemacs/bin/emacs .
ln -s /usr/local/opt/gccemacs/bin/emacsclient .


cd /Applications/Emacs.app/Contents || exit
ln -s /usr/local/opt/gccemacs/share/emacs/28.0.50/lisp .

To use Spacemacs

No special actions are needed.

To use Doom

Latest doom already support to compile all modules with native-comp.

Update to latest version.

To do so, upgrade doom to latest version. doom upgrade

Compile all modules

After that, do doom sync to compile all modules.

Generate local env file

Run doom env to generate local env file.

It's done.

Open emacs and feel the performance boost. Enjoy!

@ashton314

This comment has been minimized.

Copy link

@ashton314 ashton314 commented Aug 9, 2020

This looks fantastic! I tried compiling gccemacs not three weeks ago and gave up because it was so difficult. Have you had any issues with stability?

@AllenDang

This comment has been minimized.

Copy link
Owner Author

@AllenDang AllenDang commented Aug 10, 2020

@ashton314 So far so good!

@ashton314

This comment has been minimized.

Copy link

@ashton314 ashton314 commented Aug 11, 2020

@AllenDang I'm getting an error while make is running:

checking AppKit/AppKit.h usability... no
checking AppKit/AppKit.h presence... yes
configure: WARNING: AppKit/AppKit.h: present but cannot be compiled
configure: WARNING: AppKit/AppKit.h:     check for missing prerequisite headers?
configure: WARNING: AppKit/AppKit.h: see the Autoconf documentation
configure: WARNING: AppKit/AppKit.h:     section "Present But Cannot Be Compiled"
configure: WARNING: AppKit/AppKit.h: proceeding with the compiler's result
configure: WARNING:     ## ------------------------------------ ##
configure: WARNING:     ## Report this to bug-gnu-emacs@gnu.org ##
configure: WARNING:     ## ------------------------------------ ##
checking for AppKit/AppKit.h... no
configure: error: The include files (AppKit/AppKit.h etc) that
are required for a Nextstep build are missing or cannot be compiled.
Either fix this, or re-configure with the option '--without-ns'.
make: *** [Makefile] Error 1

I've fixed this error in the past by running CC=/usr/bin/clang ./configure, but obviously this is gcc emacs, so I don't think that would cut it. :) Any ideas on how I could fix this? I'm not exactly a GCC wizard.

@quangIO

This comment has been minimized.

Copy link

@quangIO quangIO commented Aug 11, 2020

At first I thought using gcc iss necessary but you should use Clang. This part already links emacs with libgccjit

export CFLAGS="-I/usr/local/Cellar/gcc/10.1.0/include"
export LDFLAGS="-L/usr/local/Cellar/gcc/10.1.0/lib/gcc/10 -I/usr/local/Cellar/gcc/10.1.0/include"
export LIBRARY_PATH="/usr/local/Cellar/gcc/10.1.0/lib/gcc/10:${LIBRARY_PATH:-}"

By the way, you may also want to add -O2 -march=native to trick yourself that things are faster :)

@ashton314

This comment has been minimized.

Copy link

@ashton314 ashton314 commented Aug 11, 2020

That worked! I did get an error:

fatal error: 'libxml/tree.h' file not found

But running

export LIBXML2_CFLAGS="-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2"

cleared it up. (I'd encountered that one before.)

It's running now! Thanks for your help!

Do I need to do anything special in my .emacs or something to compile all my modules?

I also noticed that after a while of working on an Elixir project with lsp-mode enabled (<- not sure if that's relevant, but I thought I'd include it) that scrolling started to stutter and Emacs' CPU usage shot up much higher than scrolling should cause it to. Any ideas?

@AllenDang

This comment has been minimized.

Copy link
Owner Author

@AllenDang AllenDang commented Aug 12, 2020

@ashton314 In my system, if I use CC=/usr/bin/clang ./configure will result in all text becomes invisible in GUI window. But this issue seems doesn't happen in your case.

You could run following lisp code in scatch buffer by M-x 'eval-buffer' to compile all the modules.

(native-compile-async "~/.emacs.d/elpa/" 4 t)

And since I'm not a Elixir programmer, I'm afraid I cannot provide more help for the scrolling issue. Sorry :P

@Strayer

This comment has been minimized.

Copy link

@Strayer Strayer commented Aug 12, 2020

I wouldn't be surprised about bad scrolling performance on macOS. I've been fighting with this for quite a while now and never got it to an acceptable level: hlissner/doom-emacs#2217

It would be nice if this branch could help with scrolling performance, but I expect the issue is more related to rendering the UI than emacs itself.

@Strayer

This comment has been minimized.

Copy link

@Strayer Strayer commented Aug 12, 2020

Sidenote: I was curious and tried this too, but ./configure fails with this error:

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.
Here instructions on how to compile and install libgccjit from source:
<https://gcc.gnu.org/wiki/JIT>.

Couldn't find anything helpful with a quick google search except other people having this issue when trying to compile emacs with nativecomp.

@AllenDang

This comment has been minimized.

Copy link
Owner Author

@AllenDang AllenDang commented Aug 12, 2020

@Strayer It seems the gcc doesn't be built with libgccjit. Can you find libgccjit.so manually?

@Strayer

This comment has been minimized.

Copy link

@Strayer Strayer commented Aug 12, 2020

Well, that was stupid :D It is built with libgccjit, but I didn't realize Homebrew installed GCC 10.2.0, not 10.1.0 as specified in the build.sh above. It is building now.

@matteha

This comment has been minimized.

Copy link

@matteha matteha commented Aug 12, 2020

I haven't gotten it to work yet, this is what I tried.

I updated brew gcc accordingly (to 10.2.0), but gcc was still pointed at the Xcode one, I guess this was the reason resulting in the smoke error:

which gcc

gave me:

/usr/bin/gcc

And

gcc -v

gave me

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

So I created a symlink in /usr/local/bin to the new gcc 10.2.0 by brew:

/usr/local/bin ln -s gcc-10 gcc

And now it picks the right gcc:

gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/10.2.0/libexec/gcc/x86_64-apple-darwin19/10.2.0/lto-wrapper
Target: x86_64-apple-darwin19
Configured with: ../configure --build=x86_64-apple-darwin19 --prefix=/usr/local/Cellar/gcc/10.2.0 --libdir=/usr/local/Cellar/gcc/10.2.0/lib/gcc/10 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-10 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --with-pkgversion='Homebrew GCC 10.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk SED=/usr/bin/sed
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (Homebrew GCC 10.2.0)

Now when I try to compile I get Appkit.h errors that I didn't get before unfortunately (can't compile them). Any ideas to this?

@AllenDang

This comment has been minimized.

Copy link
Owner Author

@AllenDang AllenDang commented Aug 12, 2020

@matteha Seems same issue with ashton314, you could try to use CC=/usr/bin/clang ./configure and see if it works.

@matteha

This comment has been minimized.

Copy link

@matteha matteha commented Aug 12, 2020

@AllenDang thanks, I will give it a try.

By the way, this may work better:

HOMEBREW_NO_AUTO_UPDATE=1 brew install gcc --build-from-source --force`

At first, brew seemed like it was updating and overwriting my edited gcc.rb and then installing

@Strayer

This comment has been minimized.

Copy link

@Strayer Strayer commented Aug 12, 2020

So, compilation worked, but now running emacs fails with this:

emacs: dlopen(/usr/local/bin/../lisp/emacs-lisp/eln-x86_64-apple-darwin19.6.0-89a730c11dec204e/lisp-mode.eln, 1): image not found

Really weird.

@AllenDang

This comment has been minimized.

Copy link
Owner Author

@AllenDang AllenDang commented Aug 12, 2020

@Strayer Make sure these two lines works with correct path setting.

cd /Applications/Emacs.app/Contents || exit
ln -s /usr/local/opt/gccemacs/share/emacs/28.0.50/lisp .
@quangIO

This comment has been minimized.

Copy link

@quangIO quangIO commented Aug 12, 2020

@Strayer, just move or symlink the lisp folder in your emacs build to /usr/local/lisp/

@Strayer

This comment has been minimized.

Copy link

@Strayer Strayer commented Aug 12, 2020

Yeah, I already checked those paths because they felt relevant, but they seem to be correct. I think emacs tries to find that file in the wrong path, not sure where this /usr/local/bin/../lisp/ comes from. Creating a symlink a suggested by @quangIO works, even though it feels wrong ;)

@quangIO

This comment has been minimized.

Copy link

@quangIO quangIO commented Aug 12, 2020

I believe emacs searches the load-path https://www.emacswiki.org/emacs/LoadPath. But symlinking is quicker :)

@Strayer

This comment has been minimized.

Copy link

@Strayer Strayer commented Aug 12, 2020

After letting doom upgrade, everything seems to run fine! Sadly, the scrolling performance is still horrible on HiDPI screens… oh well.

@t0ster

This comment has been minimized.

Copy link

@t0ster t0ster commented Aug 13, 2020

Thanks for this guide! Built successfully, however for me performance is worse than d12frosted/emacs-plus@27 brew version at least in doom emacs.

@agzam

This comment has been minimized.

Copy link

@agzam agzam commented Aug 14, 2020

I can't figure out how to build with GUI, hitting:

configure: error: The include files (AppKit/AppKit.h etc) that
are required for a Nextstep build are missing or cannot be compiled.
Either fix this, or re-configure with the option '--without-ns'.

I need the GUI. Any ideas guys?

@AllenDang

This comment has been minimized.

Copy link
Owner Author

@AllenDang AllenDang commented Aug 14, 2020

@agzam Same issue with ashton314, you could try to use CC=/usr/bin/clang ./configure and see if it works.

@ashton314

This comment has been minimized.

Copy link

@ashton314 ashton314 commented Aug 14, 2020

I haven’t been able to compile my elisp files. Compiling Emacs itself with clang works fine, but then when I try compiling the elisp Emacs throws me into the debugger, saying that it wasn’t built with JIT support.

@pcf000

This comment has been minimized.

Copy link

@pcf000 pcf000 commented Aug 23, 2020

Note that if "brew install gcc" updates brew and its formulae, it's likely to undo the changes to gcc.rb that enable building libgccjit.

I also had an issue with /usr/bin/install and the -D option, which is a Linux thing, but maybe I just have a weird setup. The solution for me was "brew install coreutils" and then making sure the Makefile used /usr/local/opt/coreutils/libexec/gnubin/install instead of /usr/bin/install.

@zzkt

This comment has been minimized.

Copy link

@zzkt zzkt commented Aug 26, 2020

as of "update 11" you'll also need to symlink the eln cache for Emacs.app to run. something like...

ln -s /usr/local/opt/gccemacs/libexec/emacs/28.0.50/x86_64-apple-darwin18.7.0/eln-cache /Applications/Emacs.app/Contents/.

@howardabrams

This comment has been minimized.

Copy link

@howardabrams howardabrams commented Aug 27, 2020

I needed to add the following in order to find the correct GCC:

export PATH="${GCC_DIR}/bin:${SED_DIR}/libexec/gnubin:${PATH}"
export CC="gcc-10"
@oguzserbetci

This comment has been minimized.

Copy link

@oguzserbetci oguzserbetci commented Sep 24, 2020

Just noting for someone running into the same problem as I did, make sure the version of gcc you have is the same as the env vars use. Brew updated mine to 10.2.0 , and even though there was no folder for 10.1 the compilation didn't throw any meaningful errors...

@ThiagoLira

This comment has been minimized.

Copy link

@ThiagoLira ThiagoLira commented Sep 27, 2020

My compilation process halts after a bunch of repeated errors like these ones:
Does anyone knows what might be wrong with my instalation?

./lisp.h:2639:53: error: implicit declaration of function 'typeof' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  return FIXNUM_OVERFLOW_P (n) ? make_biguint (n) : make_fixnum (n);
                                                    ^
./lisp.h:444:26: note: expanded from macro 'make_fixnum'
#  define make_fixnum(n) lisp_h_make_fixnum (n)
                         ^
./lisp.h:395:9: note: expanded from macro 'lisp_h_make_fixnum'
     ({ typeof (+(n)) lisp_h_make_fixnum_n = n; \
        ^
./lisp.h:2639:53: error: expected ';' after expression
./lisp.h:444:26: note: expanded from macro 'make_fixnum'
#  define make_fixnum(n) lisp_h_make_fixnum (n)
                         ^
./lisp.h:395:23: note: expanded from macro 'lisp_h_make_fixnum'
     ({ typeof (+(n)) lisp_h_make_fixnum_n = n; \
                      ^
./lisp.h:2639:53: error: use of undeclared identifier 'lisp_h_make_fixnum_n'
./lisp.h:444:26: note: expanded from macro 'make_fixnum'
#  define make_fixnum(n) lisp_h_make_fixnum (n)
                         ^
./lisp.h:395:23: note: expanded from macro 'lisp_h_make_fixnum'
     ({ typeof (+(n)) lisp_h_make_fixnum_n = n; \
                      ^
./lisp.h:2639:53: error: use of undeclared identifier 'lisp_h_make_fixnum_n'
./lisp.h:444:26: note: expanded from macro 'make_fixnum'
#  define make_fixnum(n) lisp_h_make_fixnum (n)
                         ^
./lisp.h:396:31: note: expanded from macro 'lisp_h_make_fixnum'
        eassert (!FIXNUM_OVERFLOW_P (lisp_h_make_fixnum_n)); \
                                     ^
./lisp.h:2639:53: error: use of undeclared identifier 'lisp_h_make_fixnum_n'
./lisp.h:444:26: note: expanded from macro 'make_fixnum'
#  define make_fixnum(n) lisp_h_make_fixnum (n)
                         ^
./lisp.h:396:31: note: expanded from macro 'lisp_h_make_fixnum'
        eassert (!FIXNUM_OVERFLOW_P (lisp_h_make_fixnum_n)); \


@Grekkor

This comment has been minimized.

Copy link

@Grekkor Grekkor commented Oct 4, 2020

Getting the same errors as @ThiagoLira

@zikajk

This comment has been minimized.

Copy link

@zikajk zikajk commented Oct 19, 2020

Well, that was stupid :D It is built with libgccjit, but I didn't realize Homebrew installed GCC 10.2.0, not 10.1.0 as specified in the build.sh above. It is building now.

Have you solved it? I can see that others are using 10.2.0.
I see that libgccjit is in /usr/local/opt but i encounter the same error as you do.

@oguzserbetci

This comment has been minimized.

Copy link

@oguzserbetci oguzserbetci commented Oct 19, 2020

I had similar problems, which were solved by changing the environment variables in the beginning of the build.sh to the installed versions:

# native-comp optimization
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:${PATH}"
export CFLAGS="-I/usr/local/Cellar/gcc/10.2.0/include"
export LDFLAGS="-L/usr/local/Cellar/gcc/10.2.0/lib/gcc/10 -I/usr/local/Cellar/gcc/10.2.0/include"
export LIBRARY_PATH="/usr/local/Cellar/gcc/10.2.0/lib/gcc/10:${LIBRARY_PATH:-}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.