Skip to content
systemcoding.dev
Embedded

Build Embedded Ada With Alire and the GNAT ARM Toolchain

Install Alire, select the gnat_arm_elf toolchain, create a crate, add a board support crate and run alr build to produce an ARM Cortex-M firmware ELF.

6 min read
Summarize in AI
Flat illustration of a microcontroller wired to an LED and a sensor, with the Ada, Arm and GNU logos.

Ada has a reputation for needing an expensive compiler and a week of setup. For microcontrollers that reputation is out of date. Alire, the Ada package manager, installs a GNAT compiler for bare-metal Arm, resolves board support packages from a public index, and builds the result with one command. This post walks through that flow on Linux or macOS with Alire 2.x and the gnat_arm_elf toolchain crate: install, select the compiler, create a project, add dependencies, build, and read what came out. It stops at the ELF file; flashing depends on the board and is covered by the board crate's documentation. It is the Ada entry in the TinyGo, Zig and Ada guide, and it assumes you have used a command line but not necessarily written any Ada.

The authority for every command is the Alire documentation. The alr tool changes between minor versions, so run alr --help if a flag below is not recognized.

The pieces and what each does

Four names appear in every embedded Ada project, and confusing them costs an afternoon.

GNAT is the Ada compiler, part of GCC. The bare-metal Arm build of it, gnat_arm_elf, produces code for Cortex-M and Cortex-R cores with the prefix arm-eabi- on its tools, for example arm-eabi-gcc and arm-eabi-gnatls.

GPRbuild is the build tool for Ada. It reads project files with the extension .gpr, which list source directories, the target and the compiler switches, and runs the compiler and linker in the right order.

Alire, command alr, is the package manager. A package is a crate, described by an alire.toml file with its name, version and dependencies. Alire downloads crates, installs toolchains, and invokes GPRbuild with the right environment.

The runtime is the part of the Ada library that the compiler links to implement language features such as tasking and exceptions. Embedded runtimes come in sizes. A zero-footprint runtime provides almost nothing; a light runtime adds a little more; a Ravenscar runtime adds tasks and protected objects. A board crate normally selects the runtime for you.

Install Alire

Download the release for your system from the Alire releases page and put alr on your path. On macOS with Homebrew, brew install alire works. Check:

Shell
alr --version
Text
alr 2.1.0

The version printed will match what you installed. The first time you run a command that needs a toolchain, Alire asks where to keep its configuration and offers to install a compiler. Everything it downloads lives under your user directory, so removing it later is deleting one folder.

Select the ARM toolchain

Alire installs toolchains as ordinary crates. Select the native compiler for building tools and the bare-metal Arm compiler for firmware:

Shell
alr toolchain --select

The command shows a menu. Pick a recent gnat_native and gprbuild, then the gnat_arm_elf entry. If you prefer to avoid the menu, list what exists:

Shell
alr search --list gnat
Text
NAME            STATUS  VERSION  DESCRIPTION
gnat_arm_elf            14.2.1   The GNAT Ada compiler - ARM ELF cross-compiler
gnat_native             14.2.1   The GNAT Ada compiler - Native
...

The version numbers and the exact columns depend on your Alire release and on what the index holds today. The point is that gnat_arm_elf is a crate, and a firmware project depends on it like any other dependency. That is what makes the build reproducible: the compiler version is recorded in the project, not in whatever happens to be on your machine.

Verify the cross compiler is reachable inside the project's environment:

Shell
alr exec -- arm-eabi-gcc --version
Text
arm-eabi-gcc (GCC) 14.2.1 20250301 (for GNAT FSF 14.2.1 20250301)

The date and version vary. alr exec runs a command with the toolchain on its path, so you never edit your shell's PATH yourself.

Create the project

Shell
alr init --bin blinky
cd blinky

Alire asks a few questions: a description, a licence, a maintainer. The result is a folder with alire.toml, a GPR file and a src directory. The crate manifest lists dependencies:

alire.tomlTOML
name = "blinky"
version = "0.1.0-dev"
description = "Blink an LED on a Cortex-M board"
licenses = "MIT OR Apache-2.0 WITH LLVM-exception"

executables = ["blinky"]

[[depends-on]]
gnat_arm_elf = "*"

gnat_arm_elf = "*" says any version of the toolchain crate is acceptable. For a reproducible build, pin it with a range such as ^14.2. Alire writes the resolved versions to a lock file, so a second machine gets the same versions as the first.

Add a board support crate

A bare-metal program needs more than the compiler: it needs start-up code, a linker script, and drivers that know the chip's registers. Those come from board support crates in the Alire index. Search for your board by name:

Shell
alr search --crates pico
Text
NAME       STATUS  VERSION  DESCRIPTION
pico_bsp           2.0.0    Board support package for the Raspberry Pi Pico
rp2040_hal         2.0.0    Hardware abstraction layer for the RP2040

Your index may list different versions and more crates. Add the one for your board:

Shell
alr with pico_bsp
Text
Requested pico_bsp=2.0.0
Solving dependencies...
Dependencies automatically updated as follows:
   +  pico_bsp 2.0.0 (new)
   +  rp2040_hal 2.0.0 (new)
   +  gnat_arm_elf 14.2.1 (new)

alr with edits alire.toml, resolves the dependency tree, and fetches the sources. The board crate brings its own HAL, and the HAL brings the runtime and the linker script, so one with is enough. Read the crate's README, linked from its page in the index, for the exact names of the packages it provides and for any configuration settings, because those names belong to that crate and not to Alire.

Write the program and build it

src/blinky.adbada
with RP.Clock;
with RP.Device;
with RP.GPIO;
with Pico;

procedure Blinky is
   use RP.GPIO;
begin
   RP.Clock.Initialize (Pico.XOSC_Frequency);
   RP.Device.Timer.Enable;
   Pico.LED.Configure (Output);

   loop
      Pico.LED.Set;
      RP.Device.Timer.Delay_Milliseconds (500);
      Pico.LED.Clear;
      RP.Device.Timer.Delay_Milliseconds (500);
   end loop;
end Blinky;

The with lines name the packages the program uses, and use RP.GPIO makes Output visible without a prefix. The procedure initializes the clock tree, enables the timer, configures the LED pin and toggles it. The loop never ends, as embedded main programs do not return.

Build:

Shell
alr build
Text
Building blinky=0.1.0-dev/blinky.gpr (1 of 3)...
Compile
   [Ada]          blinky.adb
Bind
   [gprbind]      blinky.bexch
   [Ada]          blinky.ali
Link
   [link]         blinky.adb
✓ Build finished successfully in 4.21 seconds.

The timings and the number of files vary. alr build runs three steps: compile each source file, bind (check that all packages are consistent and decide the elaboration order, which is the order in which package bodies initialize), and link. The result is an ELF file in bin/. Check its size with the toolchain's size tool:

Shell
alr exec -- arm-eabi-size bin/blinky
Text
   text	   data	    bss	    dec	    hex	filename
   9236	    112	   1396	  10744	   29f8	bin/blinky

The numbers are illustrative; the columns are flash code, initialized data and zeroed data, in bytes. Convert the ELF to the format your flashing tool wants: arm-eabi-objcopy -O binary makes a raw image, and board crates for the Pico document how to produce a UF2 file.

Build profiles

alr build takes a profile. alr build --development is the default and enables run-time checks and debug information. alr build --release optimizes and may disable some checks. alr build --validation turns on the strictest warnings and style checks. Embedded code usually needs --release once it works, because the development profile can be too large for a small flash, but keep the checks on while you are still finding bugs.

Typical failures

SymptomCauseFix
alr cannot find arm-eabi-gccThe toolchain crate is not selected or not a dependencyAdd gnat_arm_elf to [[depends-on]] and run alr update
Dependency resolution failsTwo crates need incompatible versionsRead the message, relax a version range, or pick a compatible board crate version
Link error about a missing runtimeWrong runtime for the chipLet the board crate choose the runtime; do not override Runtime in the GPR file
Build succeeds but the board is silentClock not initialized, or the wrong pinFollow the crate's example for the initialization order
Program too large for flashDevelopment profileBuild with alr build --release
Also inToolchains
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.