Skip to content

feat(fqbn): discover the libraries a sketch includes (#1298) - #2250

Merged
zackees merged 2 commits into
mainfrom
agent/1298-fqbn-libraries
Dec 4, 2026
Merged

zackees merged 2 commits into
mainfrom
agent/1298-fqbn-libraries

Conversation

@zackees

@zackees zackees commented Dec 4, 2026

Copy link
Copy Markdown
Member

What

fbuild fqbn could compile a package's core and a sketch, but nothing resolved the sketch's own #include lines to libraries — the step Arduino's builder does before it compiles anything. #include <FastLED.h> therefore had no source.

New crates/fbuild-build-engine/src/fqbn_libraries.rs:

  • Ownership: a header belongs to the library that declares it (library.properties includes=), else to the library whose folder name is its stem (Wire/Wire.h), else to the one library holding a file of that name. The earliest search root wins, as Arduino's include order does; two libraries inside the same root claiming one header are refused by name (compiling the wrong utility/twi.h only shows up at link).
  • Roots: the package's bundled libraries/, then the data directory's libraries/, then each --library-root.
  • Layouts: a 1.5 library is src/-rooted (sources recursive, src/ the only include dir); a 1.0 library compiles from its root plus utility/, and never from examples//extras/.
  • Core first: a header the core or the board variant provides is never a library header.
  • Transitive: library headers are scanned for further includes, so a library including another library's header selects both.
  • Unresolved: headers the sketch includes that nothing provides are reported (the compiler stays the authority — stdint.h is the toolchain's). A library's own guarded platform includes (FastLED names every ESP32/nRF52/STM32 header it knows behind #ifdef) are not reported; the preprocessor decides whether they are read.

CLI surface on the existing fbuild fqbn:

fbuild fqbn arduino:avr:uno --arduino-data-dir <dir> \
  --sketch <dir>/Blink/Blink.ino [--library-root DIR] [--json]

prints library_count, library.<i>, library_root.<i>, library_include.<i>.<j>, library_source.<i>.<j>, unresolved_header.<i>; JSON carries the same under libraries. A --compile of the sketch gains the selected include directories, which is the wiring the report would otherwise only describe.

Real e2e (no fixtures)

arduino:[email protected] installed from the published index (checksums verified) plus a real FastLED 3.10.10 checkout as --library-root:

library_count=5
library.0=EEPROM   library.1=FastLED (36 sources)
library.2=SPI      library.3=SoftwareSerial   library.4=Wire (2 sources)
unresolved_header_count=0

then the sketch TU through the package's own recipe.cpp.o.pattern:

compile_recipe=recipe.cpp.o.pattern
compile_command="…/avr-g++" -c -g -Os … -mmcu=atmega328p … -I<core> -I<variant> -I<sketch>
  -I<build>/sketch -I…/libraries/EEPROM/src -I<FastLED>/src -I…/SPI/src
  -I…/SoftwareSerial/src -I…/Wire/src "<build>/sketch/Blink.ino.cpp" -o "<build>/Blink.ino.o"
compile_exit=0
compile_object_bytes=160096

A deliberately-missing <NotInstalledHere.h> was reported as unresolved_header.0 and refused by avr-g++ with that exact name, then removed to make the run green.

Gates

  • soldr cargo test -p fbuild-build-engine --lib: 742 passed / 0 failed / 6 ignored (12 new library tests)
  • soldr cargo test -p fbuild-cli: 598 + 12 passed / 0 failed (4 new CLI tests: text, JSON, include-wiring, in-band library_error)
  • soldr cargo clippy -p fbuild-build-engine -p fbuild-cli --all-targets -- -D warnings: exit 0
  • ci/enforce_platform_boundary.py --print-totals: rows=0; dylint_rows=0; platform_boundary_research.py --check: exit 0
  • cargo fmt --all --check: clean
  • New test files split (fqbn_cmd_library_tests.rs) so every .rs stays under the 1000-line gate.

RED proven twice: removing the core-header check fails the core/unresolved test; removing the include-append in plan_compile fails the include-wiring test.

#1298 stays open — the remaining cell is fbuild build --fqbn.

Arduino's builder resolves a sketch's `#include` lines to libraries before it
compiles anything; the FQBN path compiled the core and the sketch but had no
library step, so `#include <FastLED.h>` had nowhere to come from.

New `fqbn_libraries` module: a header is owned by the library that declares it
(`library.properties` `includes=`), else by the folder whose name is its stem,
else by the one library holding a file of that name; the earliest search root
wins, exactly as Arduino's include order does, and two libraries in the SAME
root claiming one header are refused by name. Roots are the package's bundled
`libraries/`, then the data directory's, then `--library-root`. A 1.5 library
is `src/`-rooted (sources recursively, one include dir); a 1.0 library compiles
from its root plus `utility/` and never from `examples/`/`extras/`. Core and
variant headers are never library headers, and selection is transitive through
library headers.

CLI: `fbuild fqbn <FQBN> --sketch <dir>/<name>.ino [--library-root DIR]` reports
`library_count`, `library.<i>`, `library_root.<i>`, `library_include.<i>.<j>`,
`library_source.<i>.<j>` and `unresolved_header.<i>` (JSON: `libraries`), and a
`--compile` of the sketch gains the selected include directories.

Real e2e on `arduino:[email protected]` from the published index plus a real FastLED
3.10.10 checkout: 5 libraries (FastLED 36 sources, Wire 2, SPI 1,
SoftwareSerial 1, EEPROM header-only), 0 unresolved, and the sketch TU compiled
through the package's own `recipe.cpp.o.pattern` -- avr-g++ exit 0,
`Blink.ino.o` 160,096 B, with all five library `-I` directories on the command.
`ban_std_pathbuf` denies `std::path::PathBuf` in new code, including a
`Vec<PathBuf>` used only to dedup roots and to sort directory entries. Carry
`NormalizedPath` through both instead; no behaviour change.
@zackees
zackees added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit 66c05e0 Dec 4, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant