UP | HOME

emacs-用MSYS2编译Windows的Emacs

[2024-11-04 周一 14:13]

原文: https://gist.github.com/LdBeth/d663a7d3ea27776bfe211241ad7fa5e5

作者:LdBeth

原文摘录如下:

This document is an up-to-date guide on compile Emacs 31 on windows
with MSYS2, and make a installation with native compile that can work
without MSYS2.

The idea is after get a working Emacs, you may delete MSYS2
environment to free some disk space, and forget about rebuilding Emacs
until the next time you feel need to, without need to figure out
everything again.

Specially thanks to @include-yy on emacs-china.org for providing
guides on UCRT64 and use GCCJIT.

1. How to get MSYS2 (without installer)

Get from https://github.com/msys2/msys2-installer/releases/

You may use the 7zip SFX one.

2. Update MSYS2 base environment

We are using the UCRT64.

sed -i "s#https\?://mirror.msys2.org/#https://mirrors.tuna.tsinghua.edu.cn/msys2/#g" /etc/pacman.d/mirrorlist*

pacman -Syu

After complete, restart terminal, and run

pacman -Su

To install the required packages for building Emacs.
(Maybe other people can came up with a better list)

pacman -Su \
  autoconf \
  autogen \
  automake \
  automake-wrapper \
  git \
  libidn-devel \
  libltdl \
  libnettle-devel \
  libopenssl \
  libp11-kit-devel \
  libtasn1-devel \
  libunistring \
  make \
  mingw-w64-ucrt-x86_64-toolchain \
  mingw-w64-ucrt-x86_64-bzip2 \
  mingw-w64-ucrt-x86_64-cairo \
  mingw-w64-ucrt-x86_64-crt-git \
  mingw-w64-ucrt-x86_64-expat \
  mingw-w64-ucrt-x86_64-fontconfig \
  mingw-w64-ucrt-x86_64-freetype \
  mingw-w64-ucrt-x86_64-gcc \
  mingw-w64-ucrt-x86_64-gcc-libs \
  mingw-w64-ucrt-x86_64-gdk-pixbuf2 \
  mingw-w64-ucrt-x86_64-gettext \
  mingw-w64-ucrt-x86_64-giflib \
  mingw-w64-ucrt-x86_64-glib2 \
  mingw-w64-ucrt-x86_64-gmp \
  mingw-w64-ucrt-x86_64-gnutls \
  mingw-w64-ucrt-x86_64-harfbuzz \
  mingw-w64-ucrt-x86_64-headers-git \
  mingw-w64-ucrt-x86_64-imagemagick \
  mingw-w64-ucrt-x86_64-jansson \
  mingw-w64-ucrt-x86_64-libgccjit \
  mingw-w64-ucrt-x86_64-libiconv \
  mingw-w64-ucrt-x86_64-libidn2 \
  mingw-w64-ucrt-x86_64-libjpeg-turbo \
  mingw-w64-ucrt-x86_64-libpng \
  mingw-w64-ucrt-x86_64-librsvg \
  mingw-w64-ucrt-x86_64-sqlite3 \
  mingw-w64-ucrt-x86_64-libtree-sitter \
  mingw-w64-ucrt-x86_64-libtiff \
  mingw-w64-ucrt-x86_64-libunistring \
  mingw-w64-ucrt-x86_64-libxml2 \
  mingw-w64-ucrt-x86_64-nettle \
  mingw-w64-ucrt-x86_64-p11-kit \
  mingw-w64-ucrt-x86_64-winpthreads \
  mingw-w64-ucrt-x86_64-xpm-nox \
  mingw-w64-ucrt-x86_64-xz \
  mingw-w64-ucrt-x86_64-zlib \
  mingw-w64-ucrt-x86_64-jbigkit \
  pkgconf \
  texinfo 

3. Fetch Emacs source

git config --global core.autocrlf false
git config --global http.postBuffer 524288000

git config --global https.proxy "http://127.0.0.1:7890"

git clone https://github.com/emacs-mirror/emacs --depth 1

4. Build Emacs

It is recommended to disable Windows Defender scan for MSYS2 or use
Window 11 Dev Drive feature to speed up compilation.

4.1. Configuration

It is important to --with-gnutls, because otherwise Emacs won't even
able to use ELPA outside MSYS2. Also, dbus is not useful for Emacs on
Windows. You may change other flags as you see appropriate.

Set prefix to somewhere appropriate.

cd emacs
./autogen.sh
mkdir build
cd build
target=/d/cemacs
../configure prefix=$target \
    --with-gnutls \
    --without-dbus \
    --with-native-compilation=no \
    --without-pop \
    --with-xpm \
    --with-imagemagick \
    --with-tree-sitter

Check if the configure log is appropriate.

4.2. Building Emacs

备注:记得编译时关闭杀毒软件的实时防护功能,否则CPU占用率会很低。

Then make -j$(nproc) bootstrap it (for develop branch). After building completes,
make install and open the $target directory.

5. Post building

5.1. DLL Hell

The easiest way is to copy all the dlls from /ucrt64/bin, and
exploit the property that Windows won't allow delete currently opened
files to trim it. Still that doesn't mean all the successfully deleted
DLLs are safe to purge. Before you hit "Clean Recycle Bin", tt is a
good idea to check against the Windows binary distributed by GNU for
DLL used by Emacs.

cp /ucrt64/bin/*.dll $target/bin

The good things is, even if you have found certain DLL is missing
afterwards, you can still download the individual package without reinstall
MSYS2.

5.1.1. XPM image

This requires libXpm-noX4.dll. Emacs can start without this DLL
but then it won't be able to display XPM image.

5.1.2. Treesitter

libtree-sitter.dll depends on wasmtime.dll.

5.2. GCCJIT

Additional works are required to make native compile work without complete
MSYS2 environment.

mkdir $target/lib/gcc
cp /ucrt64/lib/{crtbegin,crtend,dllcrt2}.o $target/lib/gcc
cp /ucrt64/lib/lib{advapi32,gcc_s,mingw32,msvcrt,shell32,kernel32,mingwex,pthread,user32}.a $target/lib/gcc
  # adjust path according to gcc version
cp /ucrt64/lib/gcc/x86_64-w64-mingw32/14.2.0/libgcc.a $target/lib/gcc
cp /ucrt64/bin/{ld,as}.exe $target/lib/gcc

▲ 编辑于 [2024-11-21 周四 00:02] | © Published by Emacs 31.0.50 (Org mode 9.7.16) on [2024-11-21 周四 00:22] | RSS