rules_ada

Bazel rules for building Ada with GNAT.

Install

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

Getting started

  1. Add rules_ada to MODULE.bazel.
  2. Define Ada targets with rules from //ada:defs.bzl.
  3. Build or test with Bazel:
bazel build //:your_target
bazel test //:your_test

Features

  • Ada libraries, binaries, and tests
  • Static and shared library outputs
  • Coverage support via bazel coverage
  • Interop with C/C++/Rust via CcInfo
  • GNAT toolchain registration support

Example

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"],
)

Rule reference

See Rules.

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_info

Providers

Functions

AdaInfo

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

AdaInfo(cc_info, direct_body_alis, direct_objects, direct_spec_alis, transitive_body_alidirs,
        transitive_body_alis, transitive_exported_bodies, transitive_objects, transitive_spec_alidirs,
        transitive_spec_alis, transitive_specs, transitive_srcdirs, units)

Carries the spec/body ALI/object/source closure of an Ada compilation target.

FIELDS

NameDescription
cc_infoCcInfo: Compilation and linking context for cc_common integration.
direct_body_alisdepset[File]: Body ALIs emitted by this target only.
direct_objectsdepset[File]: Body .o files emitted by this target only.
direct_spec_alisdepset[File]: Spec ALIs emitted by this target only.
transitive_body_alidirsdepset[str]: Dirs containing body ALIs.
transitive_body_alisdepset[File]: Body ALIs reachable through deps (binder inputs).
transitive_exported_bodiesdepset[File]: .adb sources from libs marked exports_bodies; compile inputs for cross-lib generic instantiation.
transitive_objectsdepset[File]: Body .o files reachable through deps (link inputs).
transitive_spec_alidirsdepset[str]: Dirs containing spec ALIs.
transitive_spec_alisdepset[File]: Spec ALIs reachable through deps (compile inputs).
transitive_specsdepset[File]: .ads files reachable through deps.
transitive_srcdirsdepset[str]: Dirs to add via -I for .ads source lookup.
unitslist[struct(stem, spec, body, spec_ali, body_ali, object)]: Per-unit compilation info.

merge_ada_infos

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

merge_ada_infos(deps)

Combine AdaInfo from deps into an aggregate view used by action helpers.

PARAMETERS

NameDescriptionDefault Value
deps

-

none

ada_library

Rules

ada_library

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

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

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[]
exports_bodiesIf True, this library's body sources flow into downstream compile inputs. Set this on libraries whose public API exposes generics, since cross-unit generic instantiation requires the generic body at the instantiation site's compile time.BooleanoptionalFalse
linkoptsAdditional linker flags.List of stringsoptional[]
subunitsBody files that are separate subunits, listed explicitly to bypass the hyphen-stem heuristic. Files listed here MUST also appear in srcs. They flow as inputs to their parent unit's compile but get no standalone compile action.List of labelsoptional[]

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, lib_name, 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[]
lib_nameLibrary name for gnatbind -L (unique elaboration namespace). Defaults to the target name.Stringoptional""
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, lib_name, 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[]
lib_nameLibrary name for gnatbind -L (unique elaboration namespace). Defaults to the target name.Stringoptional""
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, target_triple)

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). If not set, the CC toolchain's archiver is used.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[]
target_tripleGCC target triple (e.g., 'aarch64-apple-darwin23.6.0', 'x86_64-pc-linux-gnu').Stringoptional""