Home

Building a bare-metal GNAT cross-compiler

I spent quite a few afternoons attempting to build a bare-metal GNAT cross-compiler for arm-none-eabi, but the exact process which ended up working wasn’t written down anywhere. crosstool-ng has failed me, so here I am, writing this post, in case I never get around to submitting a patch to the issue I just linked.1

There is an article on the OS Dev wiki, which my build process is based on, however the native compiler build omits building GNAT2.

First, we need a native compiler of the same version as our cross-compiler-to-be3:

# Use separate build directories for the native-and the cross-compilers
cd /path/to/native-build
/path/to/gcc/source/configure \
    --prefix=/path/to/your/prefix \
    --enable-languages='c,c++,ada' \
    --disable-bootstrap \
    --disable-nls \
    --disable-shared \
    --without-headers
make -j$(nproc) all-gcc
make -j$(nproc) all-target-libgcc
make -j$(nproc) all-gnattools
make -j$(nproc) install-gcc install-gnattools install-target-libgcc

Some flags—such as --disable-bootstrap, --disable-nls, disable-shared—may not be necessary, however I’m happy with this configuration. Refer to the GCC configuration page for information on what these flags do.

With our Ada-capable native compiler, we can build the cross-compiler:

cd /path/to/cross-compiler-build
# Use the new compiler
export PATH="/path/to/your/prefix/bin:$PATH"
/path/to/gcc/source/configure --target=arm-none-eabi \
    --prefix=/path/to/your/prefix \
    --enable-languages=ada \
    --disable-libada \
    --disable-nls \
    --disable-shared \
    --without-headers
make -j$(nproc) all-gcc
make -j$(nproc) all-target-libgcc
make make -C gcc cross-gnattools ada.all.cross
make -k install

4

And you’ll end up with an arm-none-eabi GNAT cross-compiler (adapt to your target arch) without an Ada runtime, so trying to compile anything will yield issues about missing packages like Ada.System. The OS Dev wiki has an article on setting up an Ada runtime for x86 bare metal which you could adapt to your needs, or you could also grab one of the existing zero-footprint runtimes, or even write your own from scratch5.

Until later!


  1. Not to imply that I’m a busy person, but I just picked up DOOM (2016) on Friday, and I’ve been having a blast. And also exams I guess… ↩︎

  2. Initially, I just wanted to correct that article, however I kept failing the questions on the registration page… ↩︎

  3. Refer to the GNAT section ↩︎

  4. While make -k install works just fine, admittedly it would be nice to know the exact install-* targets to avoid unnecessary operations. However, I’ll leave that as an exercise to you, dear reader :) ↩︎

  5. One day I might even attempt such a feat for a simpler chip. ↩︎