[Feature] add dtgen device tree compile-time code generator#40
[Feature] add dtgen device tree compile-time code generator#40LuisRuisinger wants to merge 3 commits intomainfrom
Conversation
LCOV of commit
|
There was a problem hiding this comment.
Pull request overview
Adds a new dtgen xtask crate that compiles a .dts into a .dtb, parses the DTB into an IR, and generates a static Rust source file exposing topology/peripheral data plus a small query API for use via include!().
Changes:
- Introduce
dtgenlibrary + CLI, including a DTB parser and Rust code generator. - Add IR types (
DeviceTree,Node,PropValue) and generated query helpers (peripheral lookups, aliases). - Add documentation and workspace lockfile updates for the new crate/dependencies.
Reviewed changes
Copilot reviewed 7 out of 9 changed files in this pull request and generated 12 comments.
Show a summary per file
| File | Description |
|---|---|
| xtasks/crates/dtgen/src/parser.rs | Runs cpp/dtc and walks the DTB via fdt into an in-memory DeviceTree. |
| xtasks/crates/dtgen/src/ir.rs | Defines the IR and convenience query helpers like model() / stdout_compat(). |
| xtasks/crates/dtgen/src/codegen.rs | Emits Rust source for static node/peripheral arrays and query functions. |
| xtasks/crates/dtgen/src/lib.rs | Provides the dtgen::run() entrypoint. |
| xtasks/crates/dtgen/src/main.rs | Adds a clap-based CLI wrapper around dtgen::run(). |
| xtasks/crates/dtgen/README.md | Documents generated types and query API usage. |
| xtasks/crates/dtgen/Cargo.toml | Defines the new crate and its dependencies. |
| xtasks/crates/dtgen/Cargo.lock | Adds a per-crate lockfile for dtgen’s dependencies. |
| Cargo.lock | Adds dtgen and fdt to the workspace lockfile. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| fn emit_header(out: &mut String) { | ||
| out.push_str( | ||
| r#"// ================================ | ||
| // GENERATED BY dtgen — DO NOT EDIT | ||
| // ================================ | ||
|
|
||
| #![allow(dead_code)] | ||
|
|
||
| "#, |
| r#"/// Find the first enabled peripheral whose compatible list exactly matches `c`. | ||
| pub fn peripheral_by_compatible(c: &str) -> Option<&'static Peripheral> { | ||
| PERIPHERALS.iter().find(|p| p.is_compatible(c) && p.is_enabled()) | ||
| } | ||
|
|
||
| /// Iterate all enabled peripherals whose compatible list exactly matches `c`. |
| let path = path.split(':').next()?; | ||
| // match by last path component | ||
| let name = path.split('/').last()?; |
| [package] | ||
| name = "dtgen" | ||
| version = "0.1.0" | ||
| edition = "2021" |
| let preprocessed_path = dts_path.with_extension("preprocessed.dts"); | ||
| let dtb_path = dts_path.with_extension("dtb"); | ||
|
|
| .chunks(4) | ||
| .map(|b| u32::from_be_bytes(b.try_into().unwrap())) |
| if let Some(ph) = phandle { | ||
| tree.by_phandle.insert(ph, idx); | ||
| } | ||
| tree.by_name.insert(name, idx); |
| ## `chosen` submodule | ||
|
|
||
| ```rust | ||
| // resolves /chosen stdout-path to the target Peripheral | ||
| chosen::stdout_path() // Option<&'static Peripheral> | ||
| ``` | ||
|
|
There was a problem hiding this comment.
This is an inconsistency between intended design and actual design. Further commits will address the intended design to access chosen-node attributes through the chosen module.
Adds dtgen, a code generator for that parses device tree (.dts) files and emits a typed, queryable rust file to be used during building.
dtgen has been added to the xtasks, however further usage and integration must be discussed.
xtasks/crates/dtgen/README.md provides deeper insights in usage, ir, types.