Prologue

In this blog, I'll show you how to compile Emacs on GNU/Linux Debian 12. But the procedure is of course easily adaptable to any GNU/Linux OS, since we'll only be installing a few packages using their package manager. To make everything tutti, we will also add a native compiler and tree-sitter.

Native compilation

…in Emacs is the process of translating and converting Emacs Lisp source code to machine code, which allows Emacs Lisp to run more efficiently and faster each time it is run.
Native compilation in Emacs is not a new feature; it was introduced in version 28.1.
The result of native compilation is a binary blob that (can) be subsequently loaded when Emacs starts.

Tree-sitter

Tree-sitter is a syntax tree analysis library used to parse code as it is manipulated. It is a tool that allows you to analyze the syntax structure of a programming language and provides an interface to perform various operations such as code refactoring, dependency visualization, or documentation generation. Tree-sitter supports different programming languages and allows users to define their own "recipes". Normally, when working with code (meaning highlighting it, folding it, moving it to definitions,…) some kind of parser working with regular expressions was used. Tree-sitter (supposedly) provides better parsing than a regular expression parser. Regular expressions work with characters or a group of characters, and these must appear in the displayed text in a defined order. However, a tree-sitter is capable of parsing and recognizing more complex patterns of structured language, including syntax. The regular expression parser requires that the entire input text be read and processed at once, which is memory and computing power intensive when dealing with large files. Tree-sitter works incrementally and performs parsing without the need to load the entire text at once. The regular expression parser only works with the linear structure of the text and cannot capture its hierarchy. Tree-sitter is able to understand the hierarchical structure of the text and provide information about different levels of structure - such as nested code blocks and so on.

Installation


Installing the necessary tools and libraries

To successfully compile GNU Emacs, we will need some additional tools and libraries that may not be present in a normal OS installation. These are:
| git              | tool for storing, managing and tracking changes to project source code                                |
| autoconf         | (simplified) used to generate a Makefile file for compilation                                         |
| texinfo          | tool for working with and converting documentation                                                    |
| gnutls-bin       | libraries for using SSL/TLS protocols (if we want to use Emacs as a mail client)                      |
| libgccjit-12-dev | library containing the language independent JIT (Just-In-Time) compiler needed for native compilation |
| gcc              | is probably not necessary to introduce the C compiler                                                 |
| libgtk2.0-dev    | GTK libraries if Emacs is to run in its own rendered window and not in the terminal                   |
| libgnutls28-dev  | libraries for compiling for gnutls-bin                                                                |
| libxpm-dev       | libraries to support xpm graphics filesknižnice pre podporu xpm grafických súborov                    |
| libgif-dev       | … and detto for gif                                                                                   |
| libtinfo-dev     | transition package for libncurses                                                                     |

Without hesitation or second thoughts 😏, we copy the command:
  sudo apt install git autoconf make texinfo gnutls-bin libgccjit-12-dev gcc libgtk2.0-dev libgnutls28-dev lib libxpm-dev libgif-dev


Tree-sitter

According to the tutorial on Mastering Emacs: How to Get Started with Tree-Sitter:
Download the latest version of tree-sitter:
  git clone https://github.com/tree-sitter/tree-sitter.git

And compile the program using the familiar three-line magic:
  cd tree-sitter/
  make
  sudo make install

And (if we haven't already) if we define one more variable so that Emacs doesn't fiddle during starting up:
  export LD_LIBRARY_PATH=/usr/local/lib/

GNU Emacs 30

From the savannah repositories we'll download the Emacs source code:
  git clone -b master git://git.sv.gnu.org/emacs.git

Next up:
  cd emacs

Run autogen.sh, which is used to generate configuration files and scripts for compilation and installation
  ./autogen.sh

And then we run configure, with parameters for tree-sitter support and native compilation.
  ./configure --with-tree-sitter --with-native-compilation --with-mailutils --with-pop
(--with-pop - if we also require POP3 protocol when downloading emails)
Followed by the classic:
  make
  sudo make install

Tree-sitter setting


Somewhere in the initialization file, we put a list of addresses from where the rules for that language can be downloaded:
  (setq treesit-language-source-alist
    '((bash "https://github.com/tree-sitter/tree-sitter-bash"))
     (cmake "https://github.com/uyha/tree-sitter-cmake")
     (c "https://github.com/tree-sitter/tree-sitter-c")
     (css "https://github.com/tree-sitter/tree-sitter-css")
     (elisp "https://github.com/Wilfred/tree-sitter-elisp")
     (go "https://github.com/tree-sitter/tree-sitter-go")
     (html "https://github.com/tree-sitter/tree-sitter-html")
     (javascript "https://github.com/tree-sitter/tree-sitter-javascript" "master" "src")
     (json "https://github.com/tree-sitter/tree-sitter-json")
     (make "https://github.com/alemuller/tree-sitter-make")
     (markdown "https://github.com/ikatyang/tree-sitter-markdown")
     (python "https://github.com/tree-sitter/tree-sitter-python")
     (toml "https://github.com/tree-sitter/tree-sitter-toml")
     (tsx "https://github.com/tree-sitter/tree-sitter-typescript" "master" "tsx/src")
     (typescript "https://github.com/tree-sitter/tree-sitter-typescript" "master" "typescript/src")
     (yaml "https://github.com/ikatyang/tree-sitter-yaml")))

And run the
  M-x treesit-install-language-grammar
command.

Well, we have compiled Emacs in version… well, last version, for me it was testing version 30. But I haven't deployed it to full operation yet.


A couple of pictures at the end:

Fig. 1: Installed version 30
Fig. 1: Installed version 30


Fig. 2: ts-c-mode
Fig. 2: ts-c-mode