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, one need to build emacs himself in order to use Emacs with support for natively compiled elisp.

2. Install required software

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

scoop install msys2

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

Next step is to install 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 softare switch to the folder wher you downloaded or checked out the Emacs source code and run the following commants.

First run autogen.sh to generate build configuration progran.

./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 native compilation option.
  • --with-json will enable native json support.
  • --prefix defines folder where Emacs will be installed. If not defined Emcas files will be scattered through filesystem after installation.

Now you are ready to start the build.

make NATIVE_FULL_AOT=1

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

make NATIVE_FULL_AOT=1 -j4

NOTE: You can speed up build by omitting 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, but only for packages that are actually used.

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

4. Install

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

make install

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

make install prefix=/c/opt/emacs

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

runemacs.exe -Q

This will start the default Emacs without executing init file.

5. Usage

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

However, before starting Emacs, you need to make sure to add MINGW64/bin folder to the Windows PATH environment variable, so that Emacs can find libraries it requires for the usage.

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

Hopefuly 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 the Emacs itself, so it can be fixed before the next release is publicly available.

Keywords: emacs windows msys2