How to compile Emacs with native compilation on Windows

2021-05-03 - Shows how to compile Emacs under Windows using msys2 toolchain and enable native compilation support

1. Before you start

Emacs native compilation landed recently in the source tree. Since there are not yet binary release packages available, you need to build Emacs yourself in order to use Emacs with support for natively compiled Emacs Lisp.

2. Install required software

There are multiple ways to install msys2. I used scoop, the Windows package manager, since it's what I use anyway to install open source software on Windows.

scoop install msys2

Once msys2 is installed, it needs to be started using the mingw64.exe program from the msys2 distribution. This is required so that Emacs can recognize the build type.

Next step is to install the required msys2 packages for the build. Type the following inside a msys shell.

# bring first all packages up to date
pacman -Syu
# install required packages
pacman -S --needed base-devel \
       mingw-w64-x86_64-toolchain \
       mingw-w64-x86_64-xpm-nox \
       mingw-w64-x86_64-libtiff \
       mingw-w64-x86_64-giflib \
       mingw-w64-x86_64-libpng \
       mingw-w64-x86_64-libjpeg-turbo \
       mingw-w64-x86_64-librsvg \
       mingw-w64-x86_64-lcms2 \
       mingw-w64-x86_64-jansson \
       mingw-w64-x86_64-libxml2 \
       mingw-w64-x86_64-gnutls \
       mingw-w64-x86_64-zlib \
       mingw-w64-x86_64-harfbuzz \
       autoconf

3. Build Emacs

After installing the required software, switch to the folder where you downloaded or checked out the Emacs source code and run the following commands.

First run autogen.sh to generate the build configuration program.

./autogen.sh

Now create a build configuration:

./configure --with-native-compilation --with-json --with-modules \
            --without-dbus --without-pop \
            --prefix=/c/opt/emacs
  • --with-native-compilation will enable the native compilation option.
  • --with-json will enable native JSON support.
  • --prefix defines the folder where Emacs will be installed. If not defined, Emacs files will be scattered throughout the filesystem after installation.

Now you are ready to start the build.

make NATIVE_FULL_AOT=1

Build may take a while. If you want to take advantage of multiple CPU cores while building, use the -j option with the `make` command.

make NATIVE_FULL_AOT=1 -j4

NOTE: You can speed up the build by omitting the NATIVE_FULL_AOT option to make. That will speed up the build significantly, but then the native compilation will happen at runtime when Emacs is started, and only for packages that are actually used.

NOTE: You may see a bunch of errors indicating that git is missing, but you can safely ignore them. It is probably because mingw64 / msys2 can't find it.

4. Install

Once the build is complete, it's time to install.

make install

If you forgot to specify the --prefix option to the ./configure command, you can specify it when installing.

make install prefix=/c/opt/emacs

After installation, switch to the install folder defined by the --prefix parameter and test run it.

runemacs.exe -Q

This will start the default Emacs without executing the init file.

5. Usage

You are now ready to use Emacs with the native compilation option.

However, before starting Emacs, you need to ensure that the MINGW64/bin folder is added to the Windows PATH environment variable, so that Emacs can find the libraries it requires.

Alternatively, you can also copy these libraries to the Emacs bin folder, in order not to depend on the mingw environment after compilation.

Hopefully, once the binary distribution for Windows is available, it will contain everything required to run Emacs.

If some Emacs functionality or some package you use doesn't work, please report an issue to the package maintainers or to Emacs itself, so it can be fixed before the next release is publicly available.

Keywords: emacs windows msys2