rules_dtc: Device Trees as Bazel Build Artifacts
rules_dtc packages the Device Tree Compiler (dtc) and companion
libfdt tools (fdtget, fdtput, fdtdump) as hermetic Bazel rules.
The rules compile .dts source files into binary .dtb blobs, assemble and
apply overlays, and decompile binaries back into source text.
Critically, the module allows developers to assert device tree properties
directly through Bazel test targets.
This module generates the device tree that the Cocoapuffs board passes
to OpenSBI and the Fuchsia Zircon kernel.
What it does
The module provides its public interface in @rules_dtc//:defs.bzl:
dtc_compile(name, src, deps)compiles a.dtsfile into a.dtbblob, supporting both native/include/directives and C-preprocessor includes.dts_library(name, srcs)packages reusable.dtsifragments for sharing across multiple build targets.dtbo_compileandfdt_overlaybuild devicetree overlays and merge them into a base blob, whiledtb_decompileconverts.dtbfiles back into.dts.dts_test(name, dtb, properties)asserts that a compiled blob contains expected properties using apath|propertykey format, validating board configurations in continuous integration.
The module provides a hermetic dtc toolchain built from source, eliminating
dependencies on host utilities.
In cocoapuffs-fpga
The NOEL-V board definition resides in boards/noelv/board.dts with model
“HDLFactory Cocoapuffs” and compatible string hdlf,cocoapuffs.
The dtc_compile rule compiles this source into :board_dtb, while dts_test
ensures that the model and compatible strings remain consistent.
That compiled blob serves as the fdt input for OpenSBI firmware images.
Downstream build rules invoke fdtput from the same toolchain to insert
/chosen ramdisk properties and /memory nodes for the ZBI boot path.
Hermetic compilation provides an important optimization for simulation.
Because Bazel produces a verified DTB during the build, OpenSBI can trust the
structure without runtime verification.
The firmware build sets FDT_ASSUME_MASK to bypass per-access sanity checks.
This change reduces the instruction count substantially, making RTL simulation
of the boot flow practical.
A devicetree typo can cause a silent boot failure, as described in the
Cocoapuffs post.
The dts_test target detects these discrepancies before simulation or synthesis
begins.
Prior art and references
- Upstream dgibson/dtc provides the compiler and
libfdtimplementation. - rules_foreign_cc builds make-based tools inside Bazel;
rules_dtcinstead defines a native Bazel toolchain. - The Zephyr project incorporates devicetree compilation as a primary CMake build phase.
- bazel_rules_hdl offers related hardware description tooling in Bazel.
This post is part of the build system series behind cocoapuffs-fpga,
following rules_vivado, grlib, and rules_fusesoc.
Source code is available at filmil/bazel_rules_dtc and distributed via
my Bazel registry.