Provides hermetic Bazel C/C++ toolchains based on Buildroot toolchains provided by Bootlin.
To incorporate bazel_bootlin
toolchains into your project, copy the following into your
WORKSPACE
file.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "bazel_bootlin",
# See release page for latest version url and sha.
)
load("@bazel_bootlin//:defs.bzl", "bootlin_toolchain")
bootlin_toolchain(
name = "gcc_12_2",
architecture = "x86-64",
libc_impl = "glibc"
buildroot_version = "bleeding-edge-2022.08-1",
)
register_toolchains(
"@gcc_12_2//:toolchain",
)
architecture
- refers to the architecture string used by Bootlin.libc_impl
- refers to the libc implementation (e.g.glibc
,musl
, oruclibc
).buildroot_version
- refers to the Buildroot version string (e.g.bleeding-edge-2022.08-1
).
Currently bazel_bootlin
only provides simplifyied registration for the toolchains listed in
[toolchain/toolchain_info.bzl]. This list is easily expanded so feel free to add
more as necessary. However it's possible to configure unsupported toolchains manually by passing extra parameters to the bootlin_toolchain
rule, like in the example below:
bootlin_toolchain(
name = "gcc_12_3",
architecture = "x86-64",
libc_impl = "glibc",
buildroot_version = "stable-2023.08-1",
sha256 = "d6eca7f1ea736ef6f868a027a9d0baa875f9513755026aed2badc04a2b9cd7bd",
gcc_version ="12.3.0",
libc_version = "glibc_2.35",
)
bootlin_toolchain(
name = "riscv64_gcc",
architecture = "riscv64-lp64d",
libc_impl = "glibc",
buildroot_version = "stable-2023.08-1",
sha256 = "97d7f95ced5852e3fa0119fd5370356df0b652844a76ad9a1c88caa8dd09f367",
gcc_version ="12.3.0",
libc_version = "glibc_2.37",
)
sha256
refers to the SHA256 hash of the toolchain archive.gcc_version
refers to the GCC version string (e.g.12.3.0
).libc_version
refers to the libc version string (e.g.glibc_2.35
).
These values can be found on the Bootlin toolchains page.
In order to enable toolchain selection, Bazel requires flag
--incompatible_enable_cc_toolchain_resolution
.
Additionally, you may also want to use
--action_env="BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1"
to prevent
accidental use of any local toolchains.
To avoid needing always specify these flags on the command line, you can add
these to your .bazelrc
file:
build --incompatible_enable_cc_toolchain_resolution
build --action_env="BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1"
Toolchains can configured during definition:
# //:WORKSPACE.bazel
bootlin_toolchain(
name = "gcc_12_2",
architecture = "x86-64",
libc_impl = "glibc"
buildroot_version = "bleeding-edge-2022.08-1",
extra_cxx_flags = [
"-std=c++23",
"-fdiagnostics-color=always",
"-Wduplicated-cond",
"-Wduplicated-branches",
"-Wlogical-op",
"-Wuseless-cast",
"-Wshadow=compatible-local",
"-Werror",
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wconversion",
"-Wnon-virtual-dtor",
"-Wold-style-cast",
"-Wcast-align",
"-Wunused",
"-Woverloaded-virtual",
"-Wmisleading-indentation",
"-Wnull-dereference",
"-Wdouble-promotion",
"-Wformat=2",
"-Wimplicit-fallthrough",
],
)
If multiple toolchains are registered, toolchain resolution selects the first
available and compatible toolchain.
--extra_toolchains
can then be used to select a specific toolchain when running Bazel:
# //:WORKSPACE.bazel
register_toolchains(
"@gcc_12_2//:toolchain",
"@gcc_11_5//:toolchain",
"@clang_16_0//:...:"
)
bazel build --extra_toolchains="@gcc_12_2//:toolchain" //...
bootlin_toolchain
uses
unix_cc_tooclahin_config and has same features.
For example:
bazel run <target> --features=asan