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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Dependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo). | List of labels | optional | [] |
| srcs | Ada source files (.ads specs and .adb bodies). | List of labels | optional | [] |
| data | Additional files needed at runtime. | List of labels | optional | [] |
| compile_data | Additional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate'). | List of labels | optional | [] |
| copts | Additional compiler flags for Ada compilation (e.g., -gnatwa, -O2). | List of strings | optional | [] |
| linkopts | Additional linker flags. | List of strings | optional | [] |
| linkstatic | Prefer static linking for dependencies. | Boolean | optional | True |
| main | Main Ada source file containing the entry point procedure. If not set, the first .adb file in srcs is used. | Label | optional | None |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Dependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo). | List of labels | optional | [] |
| srcs | Ada source files (.ads specs and .adb bodies). | List of labels | optional | [] |
| data | Additional files needed at runtime. | List of labels | optional | [] |
| compile_data | Additional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate'). | List of labels | optional | [] |
| copts | Additional compiler flags for Ada compilation (e.g., -gnatwa, -O2). | List of strings | optional | [] |
| linkopts | Additional linker flags. | List of strings | optional | [] |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Dependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo). | List of labels | optional | [] |
| srcs | Ada source files (.ads specs and .adb bodies). | List of labels | optional | [] |
| data | Additional files needed at runtime. | List of labels | optional | [] |
| compile_data | Additional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate'). | List of labels | optional | [] |
| copts | Additional compiler flags for Ada compilation (e.g., -gnatwa, -O2). | List of strings | optional | [] |
| linkopts | Additional linker flags. | List of strings | optional | [] |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Dependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo). | List of labels | optional | [] |
| srcs | Ada source files (.ads specs and .adb bodies). | List of labels | optional | [] |
| data | Additional files needed at runtime. | List of labels | optional | [] |
| compile_data | Additional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate'). | List of labels | optional | [] |
| copts | Additional compiler flags for Ada compilation (e.g., -gnatwa, -O2). | List of strings | optional | [] |
| linkopts | Additional linker flags. | List of strings | optional | [] |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Dependencies. Can be Ada targets (AdaInfo) or C/C++ targets (CcInfo). | List of labels | optional | [] |
| srcs | Ada source files (.ads specs and .adb bodies). | List of labels | optional | [] |
| data | Additional files needed at runtime. | List of labels | optional | [] |
| compile_data | Additional Ada source files needed during compilation but not compiled independently (e.g., subunit bodies referenced via 'separate'). | List of labels | optional | [] |
| copts | Additional compiler flags for Ada compilation (e.g., -gnatwa, -O2). | List of strings | optional | [] |
| env | Environment variables to set when running the test. | Dictionary: String -> String | optional | {} |
| env_inherit | Environment variables to inherit from the host environment. | List of strings | optional | [] |
| linkopts | Additional linker flags. | List of strings | optional | [] |
| linkstatic | Prefer static linking for dependencies. | Boolean | optional | True |
| main | Main Ada source file containing the entry point procedure. If not set, the first .adb file in srcs is used. | Label | optional | None |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| ada_std | The Ada standard library (adalib and adainclude). | Label | optional | None |
| ar | The archiver executable (ar or gcc-ar). | Label | optional | None |
| bind_flags | Additional flags for gnatbind. | List of strings | optional | [] |
| binder | The gnatbind executable for elaboration ordering and consistency checking. | Label | optional | None |
| compile_flags | Additional compiler flags for Ada compilation. | List of strings | optional | [] |
| compiler | The Ada compiler executable (gcc with GNAT support). | Label | optional | None |
| compiler_id | Identifier for the Ada compiler. Currently only 'gnat' is supported. | String | optional | "gnat" |
| compiler_lib | GCC support files (backends, shared libs, runtime libs like libgcc.a, libatomic.a). | Label | optional | None |
| gcov | The gcov executable for coverage support. | Label | optional | None |
| link_flags | Additional linker flags (e.g., -lgnat, -lgnarl). | List of strings | optional | [] |