The goal of the project is to make cross compilation toolchains readily available (and customizable) for bazel developers.
If this project was useful to you, give it a ⭐️ and I'll keep improving it!
- MODULE and WORKSPACE support
- Direct access to gcc tools
- Custom toolchain support
- Select GCC version
- Examples
- Remote execution support
- Linux, MacOS, Windows
And this to your .bazelrc
# .bazelrc
# Build using platforms by default
build --incompatible_enable_cc_toolchain_resolution
bazel_dep(name = "toolchains_riscv_gnu", version = "<module_version>")
riscv_toolchain = use_extension("@toolchains_riscv_gnu//:extensions.bzl", "riscv_toolchain")
riscv_toolchain.riscv_none_elf()
use_repo(riscv_toolchain, "riscv_none_elf")
register_toolchains("@riscv_none_elf//toolchain:all")
Add this git repository to your WORKSPACE to use the compiler
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_cc",
sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
strip_prefix = "rules_cc-0.0.9",
urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz"],
)
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "toolchains_riscv_gnu",
remote = "https://github.com/hexdae/toolchains_riscv_gnu",
branch = "main",
)
load("@toolchains_riscv_gnu//:deps.bzl", "riscv_none_elf_deps")
riscv_none_elf_deps()
register_toolchains("@riscv_none_elf//toolchain:all")
Now Bazel will automatically use riscv-none-elf-gcc
as a compiler.
If you want to bake certain compiler flags in to your toolchain, you can define a custom toolchain in your repo.
In a BUILD file:
# path/to/toolchains/BUILD
load("@riscv_none_elf//toolchain:toolchain.bzl", "riscv_none_elf_toolchain")
riscv_none_elf_toolchain(
name = "custom_toolchain",
target_compatible_with = [
"<your additional constraints>",
],
copts = [
"<your additional copts>",
],
linkopts = [
"<your additional linkopts>",
],
)
And in your WORKSPACE / MODULE file:
register_toolchains("//path/to/toolchains:all")
Be careful about registering the default toolchains when using a custom one
If you need direct access to gcc
tools, they are available as @riscv_none_elf//:<tool>
. For example, the following genrules
could be used to produce .bin
and .hex
artifacts from a generic .out
target.
cc_binary(
name = "target.out"
srcs = [...],
deps = [...],
copts = [...],
...
)
genrule(
name = "bin",
srcs = [":target.out"],
outs = ["target.bin"],
cmd = "$(execpath @riscv_none_elf//:objcopy) -O binary $< $@",
tools = ["@riscv_none_elf//:objcopy"],
)
genrule(
name = "hex",
srcs = [":target.out"],
outs = ["target.hex"],
cmd = "$(execpath @riscv_none_elf//:objcopy) -O ihex $< $@",
tools = ["@riscv_none_elf//:objcopy"],
)
This toolchain is compatible with remote execution, see remote.yml