Evan Pratten
Evan Pratten
Software Developer

The GitHub repository for everything in this post can be found here

When writing software for an Arduino, or any other AVR-based device, there are generally three main options. You can use the Arduino IDE with arduino-cli, which is in my opinion, a clunky system that is great for high levels of abstraction and teaching people how to program, but lacks any kind of easy customization I am interested in. If you are looking for something more advanced (and works in your favorite IDE), you might look at PlatformIO. Finally, you can just program without any Hardware Abstraction Library at all, and use avr-libc along with avr-gcc and avrdude.

This final option is my favorite by far, as it both forces me to think about how the system I am building is actually working "behind the scenes", and lets me do everything exactly the way I want. Unfortunately, when working directly with the AVR system libraries, the only buildsystem / tool that is available (without a lot of extra work) is Make. As somebody who spends 90% of his time working with higher-level buildsystems like Gradle and Bazel, I don't really like needing to deal with Makefiles, and manually handle dependency loading. This got me thinking. I have spent a lot of time working in Bazel, and cross-compiling for the armv7l platform via the FRC Toolchain. How hard can it be to add AVR Toolchain support to Bazel?

The answer: Its pretty easy.

The Bazel buildsystem allows users to define custom toolchains via the toolchain rule. I am going to assume you have a decent understanding of the Starlark DSL, or at least Python3 (which Starlark is syntactically based on). To get started setting up a Bazel toolchain, I create empty WORKSPACE and BUILD files, along with a new bazel package named toolchain that has a bazel file inside for the toolchain settings, a .bazelrc file, and a package to store my test program.

    |   |
    |   +-BUILD
    |   +-main.cc
    |   |
    |   +-BUILD
    |   +-avr.bzl

I only learned about this recently, but you can use a .bazelrc file to define constant arguments to be passed to the buildsystem per-project. For this project, I am adding the following arguments to the config file to define which toolchain to use for which target:

# .bazelrc

# Use our custom-configured c++ toolchain.
build:avr_config --crosstool_top=//toolchain:avr_suite
build:avr_config --cpu=avr

# Use the default Bazel C++ toolchain to build the tools used during the
# build.
build:avr_config --host_crosstool_top=@bazel_tools//tools/cpp:toolchain

This config will default all builds to use a custom toolchain named avr_suite, and compile to target the avr CPU architecture. But, the final line will make sure to use the host's toolchain for compiling tools needed for Bazel itself (since we can't run AVR code on the host machine). With this, we now have everything needed to tell Bazel what to use when building, but we have not actually defined the toolcahin in the first place. This step comes in two parts. First, we need to define a toolchain implementation (this happens in avr.bzl). This implementation will define things like, where to find every tool on the host, which libc version to use, and what types of tools are provided by avr-gcc in the first place. We can start out by adding some load statements to the file to tell Bazel what functions we need to use.

# toolchain/avr.bzl

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")

Once this is done, we need to define everything that this toolchain implementation can do. In this case avr-gcc can link executables, link dynamic libraries, and link a "nodeps" dynamic library.

# ...

all_link_actions = [

We also need to tell Bazel where to find every tool. This may vary platform-to-platform, but with a standard avr-gcc install on Linux, the following should work just fine. Experienced Bazel users may wish to make use of Bazel's config_setting and select rules to allow the buildsystem to run on any type of host via a CLI flag.

# ...

tool_paths = [
        name = "gcc",
        path = "/usr/bin/avr-gcc",
        name = "ld",
        path = "/usr/bin/avr-ld",
        name = "ar",
        path = "/usr/bin/avr-ar",
        name = "cpp",
        path = "/usr/bin/avr-g++",
        name = "gcov",
        path = "/usr/bin/avr-gcov",
        name = "nm",
        path = "/usr/bin/avr-nm",
        name = "objdump",
        path = "/usr/bin/avr-objdump",
        name = "strip",
        path = "/usr/bin/avr-strip",

Finally, we need to define the actual avr-toolchain implementation. This can be done via a simple function, and the creation of a new custom rule:

# ...

def _avr_impl(ctx):
    features = [
            name = "default_linker_flags",
            enabled = True,
            flag_sets = [
                    actions = all_link_actions,
                    flag_groups = ([
                            flags = [

    return cc_common.create_cc_toolchain_config_info(
        ctx = ctx,
        toolchain_identifier = "avr-toolchain",
        host_system_name = "local",
        target_system_name = "local",
        target_cpu = "avr",
        target_libc = "unknown",
        compiler = "avr-g++",
        abi_version = "unknown",
        abi_libc_version = "unknown",
        tool_paths = tool_paths,
        cxx_builtin_include_directories = [

cc_toolchain_config = rule(
    attrs = {},
    provides = [CcToolchainConfigInfo],
    implementation = _avr_impl,

The cxx_builtin_include_directories argument is very important. This tells the compiler where to find the libc headers. Both paths are required, as the headers are split between two directories on Linux for some reason. We are now done with the avr.bzl file, and can add the following to the toolchain package's BUILD file to register our custom toolcahin as an official CC toolchain for Bazel to use:

# toolchain/BUILD

load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
load(":avr.bzl", "cc_toolchain_config")

cc_toolchain_config(name = "avr_toolchain_config")

    name = "avr_suite",
    toolchains = {
        "avr": ":avr_toolchain",

filegroup(name = "empty")

    name = "avr_toolchain",
    all_files = ":empty",
    compiler_files = ":empty",
    dwp_files = ":empty",
    linker_files = ":empty",
    objcopy_files = ":empty",
    strip_files = ":empty",
    supports_param_files = 0,
    toolchain_config = ":avr_toolchain_config",
    toolchain_identifier = "avr-toolchain",

Thats it. Now, if we wanted to compile a simple blink program in AVR-C, we can add the following to main.cc:

#ifndef F_CPU
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void)
  DDRC = 0xFF;
  while(1) {
    PORTC = 0xFF; 
    PORTC= 0x00; 

To compile this, just define a cc_binary in the example BUILD file just like any normal Bazel program.

# example/BUILD

load("@rules_cc//cc:defs.bzl", "cc_binary")

    name = "example",
    srcs = ["main.cc"],
    # Add any needed cc options here for your specific platform

This can be compiled with bazel build //example --config=avr_config, and the output binary will be in the bazel-bin directory. You can run avr-objcopy and avrdude manually just like with a normal program.

Importantly, every normal Bazel function will still work. Want to include EigenArduino in your project? Just import the rules_foreign_cc ruleset and load the Eigen library like normal. You can also run unit tests through Bazel's regular testing rules. If you are a masochist, you could even try loading the pybind11 rules and embedding a Python interpreter in your code.