Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nauhygon/f3b44f51b34e89bc54f8 to your computer and use it in GitHub Desktop.
Save nauhygon/f3b44f51b34e89bc54f8 to your computer and use it in GitHub Desktop.
Step-by-step instructions to build Emacs for Windows 64 bit with MSYS2 and MinGW-w64. Now `native-comp` supported.

Build Emacs-w64 with MSYS2/MinGW-w64 with Native Compilation

Instructions are modified from emacs-w64 Wiki page by zklhp. Many thanks for sharing!

  1. Download the latest MSYS2 from this download page.

  2. Install MSYS2 to, for example, C:\msys2 (make sure no space in path to avoid unwanted problems).

  3. Optionally prettify the MSYS2 console mintty with ~/.minttyrc to make it more pleasing to eyes. Thanks to this awesome theme!

ForegroundColour=131,148,150
BackgroundColour=0,43,54
CursorColour=192,192,192
Black=7,54,66
BoldBlack=0,43,54
Red=220,50,47
BoldRed=203,75,22
Green=133,153,0
BoldGreen=88,110,117
Yellow=181,137,0
BoldYellow=101,123,131
Blue=38,139,210
BoldBlue=131,148,150
Magenta=211,54,130
BoldMagenta=108,113,196
Cyan=42,161,152
BoldCyan=147,161,161
White=238,232,213
BoldWhite=253,246,227
BoldAsFont=yes
Font=Consolas
FontHeight=12
Transparency=off
OpaqueWhenFocused=no
ScrollbackLines=200000
FontSmoothing=full
CursorType=underscore
CursorBlinks=yes
ScrollMod=off
RightClickAction=paste
ClickTargetMod=off
  1. Remove all /c/* paths in $PATH by adding this to .bashrc and restart mintty.

This is important to avoid interference from programs installed on the same machine, especially if you have Cygwin installed.

# Remove '/c/*' from PATH if running under Msys to avoid possible 
# interference from programs already installed on system. Removal 
# with awk is copied from http://stackoverflow.com/a/370192.
if [ $(uname -o) == 'Msys' ]; then
    export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/c\// {next} {print}' | sed 's/:*$//'`
fi

The whole .bashrc is copied here.

# .bashrc
 
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
 
##############
### Basics ###
##############
 
# Don't wait for job termination notification
set -o notify
 
# Enables UTF-8 in Putty.
# See http://www.earth.li/~huggie/blog/tech/mobile/putty-utf-8-trick.html
echo -ne '\e%G\e[?47h\e%G\e[?47l'
 
# My pretty prompt (Yay!)
PS1='\[\e[32;40m\]\u\[\e[0m\]\[\e[34;40m\]\H\[\e[0m\]\[\e[40;1m\]\w\[\e[0m\] '
 
############
### PATH ###
############
 
# Remove '/c/*' from PATH if running under Msys to avoid possible 
# interference from programs already installed on system. Removal 
# with awk is copied from http://stackoverflow.com/a/370192.
if [ $(uname -o) == 'Msys' ]; then
    export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/c\// {next} {print}' | sed 's/:*$//'`
fi
 
###############
### Aliases ###
###############
 
 
alias less='less -r'
alias rm='rm -i'
alias whence='type -a'
alias ls='ls -F --color=auto'
alias dir='ls --color=auto --format=long -L'
alias vdir='ls --color=auto --format=long'
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'
alias md='mkdir'
alias pico='nano'
alias cls='clear'
  1. Install packages required to build Emacs.

Follow instructions on this page first.

pacman -Syu
pacman -Sy
pacman -Sy\
    --needed \
    filesystem \
    msys2-runtime \
    bash \
    libreadline \
    libiconv \
    libarchive \
    libgpgme \
    libcurl \
    pacman \
    ncurses \
    libintl
    

Close mintty and restart it, do this again.

pacman -Su

Then let us install all required libs for building Emacs.

pacman -Su \
    autoconf \
    autogen \
    automake \
    automake-wrapper \
    diffutils \
    git \
    guile \
    libgc \
    libguile \
    libidn-devel \
    libltdl \
    libnettle-devel \
    libopenssl \
    libp11-kit-devel \
    libtasn1-devel \
    libunistring \
    make \
    mingw-w64-x86_64-binutils \
    mingw-w64-x86_64-bzip2 \
    mingw-w64-x86_64-cairo \
    mingw-w64-x86_64-crt-git \
    mingw-w64-x86_64-dbus \
    mingw-w64-x86_64-expat \
    mingw-w64-x86_64-fontconfig \
    mingw-w64-x86_64-freetype \
    mingw-w64-x86_64-gcc \
    mingw-w64-x86_64-gcc-libs \
    mingw-w64-x86_64-gdk-pixbuf2 \
    mingw-w64-x86_64-gettext \
    mingw-w64-x86_64-giflib \
    mingw-w64-x86_64-glib2 \
    mingw-w64-x86_64-gmp \
    mingw-w64-x86_64-gnutls \
    mingw-w64-x86_64-harfbuzz \
    mingw-w64-x86_64-headers-git \
    mingw-w64-x86_64-imagemagick \
    mingw-w64-x86_64-isl \
    mingw-w64-x86_64-jansson \
    mingw-w64-x86_64-libffi \
    mingw-w64-x86_64-libgccjit \
    mingw-w64-x86_64-libiconv \
    mingw-w64-x86_64-libidn2 \
    mingw-w64-x86_64-libjpeg-turbo \
    mingw-w64-x86_64-libpng \
    mingw-w64-x86_64-librsvg \
    mingw-w64-x86_64-libsystre \
    mingw-w64-x86_64-libtasn1 \
    mingw-w64-x86_64-libtiff \
    mingw-w64-x86_64-libunistring \
    mingw-w64-x86_64-libwinpthread-git \
    mingw-w64-x86_64-libxml2 \
    mingw-w64-x86_64-mpc \
    mingw-w64-x86_64-mpfr \
    mingw-w64-x86_64-nettle \
    mingw-w64-x86_64-p11-kit \
    mingw-w64-x86_64-pango \
    mingw-w64-x86_64-pixman \
    mingw-w64-x86_64-winpthreads \
    mingw-w64-x86_64-xpm-nox \
    mingw-w64-x86_64-xz \
    mingw-w64-x86_64-zlib \
    mingw-w64-x86_64-jbigkit \
    nano \
    openssl \
    pkgconf \
    tar \
    texinfo \
    wget
      
  1. Get emacs source.
mkdir emacs; cd emacs
git clone http://git.savannah.gnu.org/r/emacs.git emacs
git config core.autocrlf false
  1. Build Emacs!

Change target accordingly, the directory in which the freshly built Emacs binaries, libraries, and docs live.

target=/c/emacs

export PATH=/mingw64/bin:$PATH
 
mkdir build; cd build

(cd ../emacs; ./autogen.sh)

export PKG_CONFIG_PATH=/mingw64/lib/pkgconfig

../emacs/configure \
    --host=x86_64-w64-mingw32 \
    --target=x86_64-w64-mingw32 \
    --build=x86_64-w64-mingw32 \
    --with-native-compilation \
    --with-gnutls \
    --with-imagemagick \
    --with-jpeg \
    --with-json \
    --with-png \
    --with-rsvg \
    --with-tiff \
    --with-wide-int \
    --with-xft \
    --with-xml2 \
    --with-xpm \
    'CFLAGS=-I/mingw64/include/noX' \
    prefix=$target
 
make
 
make install prefix=$target

# Only needed for standalone dist (wo MSYS2 env)
# cp /mingw64/bin/*.dll $target/bin
@JonatanSahar
Copy link

I do have:

ls /mingw64/bin |grep xpm
cxpm-noX.exe

and

 echo $PATH
/mingw64/bin:/ucrt64/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/msys64/mingw64/bin/

@JonatanSahar
Copy link

I've removed the CFLGAS including nox, and I notice that already here I get a lot of

checking for WebPGetInfo... no
checking for libwebpdemux >= 0.6.0 libwebpdecoder >= 0.6.0... no
checking for sqlite3_open_v2 in -lsqlite3... no
checking for getaddrinfo_a in -lanl... no
checking for malloc_trim... no
checking for dbus-1 >= 1.0... no
checking for lgetfilecon in -lselinux... no
checking for gnutls >= 2.12.2... no
checking for libsystemd >= 222... no
checking for jansson >= 2.7... no
checking for tree-sitter >= 0.20.2... no
checking for tree-sitter >= 0.6.3... no
checking for windows.h... yes
checking for harfbuzz >= 1.2.3... no
checking for X11/xpm.h... no
checking for jpeglib 6b or later... no
checking for lcms2... no
checking for library containing inflateEnd... -lz
checking for dladdr... no
checking for dlfunc... no
checking for gcc_jit_context_acquire in -lgccjit... no

at least some of which were installed in previous steps.. any idea why the compiler will not pick up on them? the PATH var I posted looks alright?

@JonatanSahar
Copy link

where does configure look for these packages? I looked around and it looks like it's using test but I really don't know much about makefiles etc.

@knelby
Copy link

knelby commented Oct 30, 2023

Hi. Thanks for posting this. I'm very much a newbie so this is super helpful. However, the link to SourceForge (http://sourceforge.net/p/msys2/wiki/MSYS2%20installation/) is dead for me.

@nauhygon
Copy link
Author

Hi. Thanks for posting this. I'm very much a newbie so this is super helpful. However, the link to SourceForge (http://sourceforge.net/p/msys2/wiki/MSYS2%20installation/) is dead for me.

Please ignore the sf link and just follow the steps below. I was trying to give due credits to the authors of that page.

@knelby
Copy link

knelby commented Nov 1, 2023

Thanks!

@Flinner
Copy link

Flinner commented May 4, 2024

thanks! consider using make -j$(nproc).

@HarryTheFairy
Copy link

HarryTheFairy commented Jun 3, 2024

Hello, thanks for this guide.
For me it worked but only until I closed the shell. Meaning I performed the steps above in an MSYS2 UCRT64 shell and after make install is finished I could run emacs/bin/emacs and the GUI is visible. Closed the MSYS2 UCRT64 shell. Reopened it and there is no response in the shell (no warnings no errors). When trying to run emacs.exe or runemacs.exe from the windows file explorer I get the messages for missing .dll : libgmp-10.dll, libMagickCore-7.Q16HDRI-10.dll, libMagickWand-7.Q16HDRI-10.dll, libdbus-1-3.dll .

Any Idea why those .dll files were accessbile before but not after the shell restart?

Update:
export PATH=/mingw64/bin:$PATH is missing.
I misunderstood the ,bashrc . Found one in C:\msys2\etc\skel and edited it, but with no effect. Apparently there needs to be a .bashrc and a .bash_profile (just calling .bashrc in this case) in the home directory. --> tested and working.

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