Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 36 ยท Systems and Tools

GNAT, GPRbuild and Alire

A tour of the Ada toolchain: the GNAT compiler, gprbuild project files, the Alire package manager, style and warning switches, GNATprove, GNATtest and GNATcoverage.

Writing Ada is only half of the work: you also need a compiler, a build system and a way to get libraries. The mainstream open-source toolchain is GNAT, part of GCC, plus GPRbuild for projects and Alire (alr) as the package manager. This lesson explains how they fit together and which switches you will use most.

None of these tools run on this site, so every command and file below is read-only, and we do not show tool output unless we are certain of it. The runnable stand-ins at the end are small C programs that model the build ideas (dependency ordering and incremental rebuilds).

Compiling one file

The simplest case needs no project file:

TEXT
gnatmake hello.adb

gnatmake (or gprbuild hello.adb) finds the packages hello.adb depends on, compiles whatever is out of date and links the executable hello. Ada's with clauses give the compiler everything it needs to know the dependencies, so you do not write a makefile with a list of headers.

File names follow a convention: a package Utils.Strings has its specification in utils-strings.ads and body in utils-strings.adb (the dot becomes a hyphen, all lowercase). Compilation order is derived from the with clauses, and the binder checks that all units are consistent before linking, catching mismatches that a C linker would miss.

Project files

A GNAT project file (*.gpr) describes a build in Ada-like syntax:

ADA
project My_App is

   for Source_Dirs use ("src");
   for Object_Dir  use "obj";
   for Exec_Dir    use "bin";
   for Main        use ("main.adb");

   package Compiler is
      for Default_Switches ("Ada") use
        ("-gnat2022", "-gnatwa", "-gnaty", "-g", "-O2");
   end Compiler;

end My_App;

Build with gprbuild -P my_app.gpr. Useful switches:

Switch Meaning
-gnat2022 Ada 2022 language rules
-gnata enable assertions (contracts checked at run time)
-gnatwa all the common warnings
-gnatwe treat warnings as errors
-gnaty check coding style
-gnatp suppress run-time checks (use only after proof!)
-fstack-check detect stack overflow

A good development profile uses -gnata -gnatwa -gnaty -g -O0, and a release profile uses -O2 with checks kept unless they were proved absent.

The toolchain

gnat the compiler (GCC)
gprbuild builds .gpr projects
Alire crates and toolchains
GNATprove SPARK proofs

All free and open source.

1 / 2

Alire: packages and toolchains

Alire is to Ada what Cargo is to Rust or npm to JavaScript. It manages crates (libraries and programs), downloads the right GNAT toolchain, and builds your project:

TEXT
alr init --bin hello_world       # new executable project
cd hello_world
alr with gnat_native             # make sure a native compiler is selected
alr with ada_toml                # add a dependency (crate)
alr build                        # compile with the right settings
alr run                          # build and run
alr search json                  # find crates

alr init creates a folder with alire.toml (name, version, dependencies, executable list), a src/ directory and a .gpr file. Dependencies are declared in alire.toml:

TOML
name = "hello_world"
version = "0.1.0"
executables = ["hello_world"]

[[depends-on]]
ada_toml = "^0.3"

Version ranges use semantic versioning (^0.3 means compatible with 0.3.x). Alire pins resolved versions so everyone gets the same build. It is the recommended way to start a new Ada project today, and also how to install GNAT itself without system packages.

Other tools worth knowing

  • GNATprove: the SPARK prover from the SPARK chapter. Run it from the project with gnatprove -P my_app.gpr --level=2, or from Alire with alr gnatprove.
  • GNATformat / GNATpp: automatic source formatters.
  • GNATtest: generates unit-test skeletons from your specifications; AUnit is the xUnit-style testing framework for Ada.
  • GNATcoverage: coverage analysis (statement, decision, MC/DC for certification).
  • GNATstack and GNATcheck: stack usage and coding-standard checking.
  • GNAT Studio and the Ada Language Server: IDE support in GNAT Studio, VS Code and others.
  • gdb: the debugger works with Ada code and understands Ada types.

Stand-in: ordering by dependencies (C, runnable)

The binder and gprbuild compute a build order from with relationships. This C program does the same on a tiny graph using a depth-first topological sort: each unit is compiled only after the units it withs.

C C stand-in: compilation order from with clauses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The order is types, utils, log, config, main: every unit comes after the ones it depends on.

Stand-in: incremental rebuilds (C, runnable)

Tools rebuild only what changed. The model below keeps a version number per unit and marks a unit stale if its own source changed or any unit it withs was rebuilt, which is why changing a specification triggers recompiling its clients.

C C stand-in: what needs recompiling?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Editing utils recompiles utils and main (which withs it), while types and log stay up to date.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is Alire?

    Alire (alr) manages crates, dependencies and toolchains, like Cargo for Rust.
  2. 2. Which GNAT switch enables run-time checking of Pre and Post contracts?

    -gnata enables assertions; -gnatp suppresses run-time checks.
  3. 3. How do Ada tools know the build dependencies without a makefile?

    with clauses declare dependencies explicitly.
  4. 4. What is the file name for the body of package Utils.Strings?

    GNAT uses lowercase and replaces dots with hyphens: utils-strings.adb.

Summary

  • GNAT is the free Ada compiler (in GCC); gprbuild builds from .gpr project files.
  • Dependencies come from with clauses; the binder checks consistency across units.
  • Common switches: -gnata (assertions), -gnatwa (warnings), -gnaty (style), -gnat2022.
  • Alire (alr) creates projects, fetches crates and installs toolchains.
  • GNATprove, GNATtest/AUnit, GNATcoverage and the Ada Language Server round out the toolset.

Next: Embedded and Low-Level Ada