rules_ada

Bazel rules for building Ada programs using the GNAT compiler.

Setup

bazel_dep(name = "rules_ada", version = "{version}")

Overview

rules_ada provides Bazel rules for compiling Ada source files (.ads specs and .adb bodies) into libraries, shared libraries, binaries, and test executables. It integrates with the GNAT toolchain (the GCC-based Ada compiler) and Bazel's C/C++ toolchain infrastructure via rules_cc.

Features

  • Compile Ada source files with full dependency tracking
  • Static and shared library support
  • Binary and test executable targets
  • Code coverage via bazel coverage
  • Interoperability with C, C++, and Rust targets through CcInfo
  • Automatic GNAT toolchain detection and registration

Quick Start

load("//ada:defs.bzl", "ada_binary", "ada_library", "ada_test")

ada_library(
    name = "math_utils",
    srcs = [
        "math_utils.ads",
        "math_utils.adb",
    ],
)

ada_binary(
    name = "calculator",
    srcs = ["main.adb"],
    deps = [":math_utils"],
)

ada_test(
    name = "math_test",
    srcs = ["math_test.adb"],
    deps = [":math_utils"],
)

Rules

ada_binary

Rules

ada_binary

load("@rules_ada//ada:ada_binary.bzl", "ada_binary")

ada_binary(name, deps, srcs, data, compile_data, copts, linkopts, linkstatic, main)

Compiles Ada source files, binds them, and links into an executable.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsDependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo).List of labelsoptional[]
srcsAda source files (.ads specs and .adb bodies).List of labelsoptional[]
dataAdditional files needed at runtime.List of labelsoptional[]
compile_dataAdditional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate').List of labelsoptional[]
coptsAdditional compiler flags for Ada compilation (e.g., -gnatwa, -O2).List of stringsoptional[]
linkoptsAdditional linker flags.List of stringsoptional[]
linkstaticPrefer static linking for dependencies.BooleanoptionalTrue
mainMain Ada source file containing the entry point procedure. If not set, the first .adb file in srcs is used.LabeloptionalNone

ada_library

Rules

ada_library

load("@rules_ada//ada:ada_library.bzl", "ada_library")

ada_library(name, deps, srcs, data, compile_data, copts, linkopts)

Compiles Ada source files into a library. This is the primary rule for Ada-to-Ada dependencies and cross-language interop via CcInfo. Use ada_static_library or ada_shared_library when you need to produce a specific artifact type for external consumption.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsDependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo).List of labelsoptional[]
srcsAda source files (.ads specs and .adb bodies).List of labelsoptional[]
dataAdditional files needed at runtime.List of labelsoptional[]
compile_dataAdditional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate').List of labelsoptional[]
coptsAdditional compiler flags for Ada compilation (e.g., -gnatwa, -O2).List of stringsoptional[]
linkoptsAdditional linker flags.List of stringsoptional[]

ada_shared_library

Rules

ada_shared_library

load("@rules_ada//ada:ada_shared_library.bzl", "ada_shared_library")

ada_shared_library(name, deps, srcs, data, compile_data, copts, linkopts)

Compiles Ada source files into a shared (dynamic) library.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsDependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo).List of labelsoptional[]
srcsAda source files (.ads specs and .adb bodies).List of labelsoptional[]
dataAdditional files needed at runtime.List of labelsoptional[]
compile_dataAdditional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate').List of labelsoptional[]
coptsAdditional compiler flags for Ada compilation (e.g., -gnatwa, -O2).List of stringsoptional[]
linkoptsAdditional linker flags.List of stringsoptional[]

ada_static_library

Rules

ada_static_library

load("@rules_ada//ada:ada_static_library.bzl", "ada_static_library")

ada_static_library(name, deps, srcs, data, compile_data, copts, linkopts)

Compiles Ada source files into a static archive (.a) for external consumption.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsDependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo).List of labelsoptional[]
srcsAda source files (.ads specs and .adb bodies).List of labelsoptional[]
dataAdditional files needed at runtime.List of labelsoptional[]
compile_dataAdditional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate').List of labelsoptional[]
coptsAdditional compiler flags for Ada compilation (e.g., -gnatwa, -O2).List of stringsoptional[]
linkoptsAdditional linker flags.List of stringsoptional[]

ada_test

Rules

ada_test

load("@rules_ada//ada:ada_test.bzl", "ada_test")

ada_test(name, deps, srcs, data, compile_data, copts, env, env_inherit, linkopts, linkstatic, main)

Compiles Ada source files, binds them, and links into a test executable. Run with bazel test.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsDependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo).List of labelsoptional[]
srcsAda source files (.ads specs and .adb bodies).List of labelsoptional[]
dataAdditional files needed at runtime.List of labelsoptional[]
compile_dataAdditional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate').List of labelsoptional[]
coptsAdditional compiler flags for Ada compilation (e.g., -gnatwa, -O2).List of stringsoptional[]
envEnvironment variables to set when running the test.Dictionary: String -> Stringoptional{}
env_inheritEnvironment variables to inherit from the host environment.List of stringsoptional[]
linkoptsAdditional linker flags.List of stringsoptional[]
linkstaticPrefer static linking for dependencies.BooleanoptionalTrue
mainMain Ada source file containing the entry point procedure. If not set, the first .adb file in srcs is used.LabeloptionalNone

ada_toolchain

Rules

ada_toolchain

load("@rules_ada//ada:ada_toolchain.bzl", "ada_toolchain")

ada_toolchain(name, ada_std, ar, bind_flags, binder, compile_flags, compiler, compiler_id,
              compiler_lib, gcov, link_flags)

Defines an Ada toolchain providing the GNAT compiler (gcc), binder (gnatbind), archiver (ar), and compilation/linking flags.

Example:

load("@rules_ada//ada:ada_toolchain.bzl", "ada_toolchain")

ada_toolchain(
    name = "gnat_toolchain",
    compiler = "@gnat//:bin/gcc",
    binder = "@gnat//:bin/gnatbind",
    ar = "@gnat//:bin/ar",
    ada_std = "@gnat//:ada_std",
    compiler_lib = "@gnat//:compiler_lib",
    gcov = "@gnat//:bin/gcov",
    compile_flags = ["-O2"],
    link_flags = ["-lgnat"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
ada_stdThe Ada standard library (adalib and adainclude).LabeloptionalNone
arThe archiver executable (ar or gcc-ar).LabeloptionalNone
bind_flagsAdditional flags for gnatbind.List of stringsoptional[]
binderThe gnatbind executable for elaboration ordering and consistency checking.LabeloptionalNone
compile_flagsAdditional compiler flags for Ada compilation.List of stringsoptional[]
compilerThe Ada compiler executable (gcc with GNAT support).LabeloptionalNone
compiler_idIdentifier for the Ada compiler. Currently only 'gnat' is supported.Stringoptional"gnat"
compiler_libGCC support files (backends, shared libs, runtime libs like libgcc.a, libatomic.a).LabeloptionalNone
gcovThe gcov executable for coverage support.LabeloptionalNone
link_flagsAdditional linker flags (e.g., -lgnat, -lgnarl).List of stringsoptional[]