Skip to content

feat(fqbn): compile an Arduino sketch by preprocess-reusing SourceScanner (#1298) - #2242

Merged
zackees merged 1 commit into
mainfrom
agent/1298-ino-preprocess
Dec 3, 2026
Merged

zackees merged 1 commit into
mainfrom
agent/1298-ino-preprocess

Conversation

@zackees

@zackees zackees commented Dec 3, 2026

Copy link
Copy Markdown
Member

What

fbuild fqbn <FQBN> --compile Blink.ino now compiles an Arduino sketch. A
sketch has no recipe of its own; the Arduino builder preprocesses it into one
C++ translation unit first, and this is that stage:

  • the sketch directory's .ino tabs are concatenated (primary tab first, each
    with its own #line 1 "<tab>"),
  • #include <Arduino.h> is prepended when the core provides it, plus the
    auto-generated prototype block,
  • the result is written to build.path/sketch/<name>.ino.cpp and compiled
    with recipe.cpp.o.pattern.

The preprocessing is reused, not re-derived: it goes through
SourceScanner::scan_sketch_sources_filtered_with_include_roots, the same code
that preprocesses a PlatformIO sketch, so tab order, hoisted includes, line
mapping and prototypes cannot diverge between the two build paths. The sketch
directory is kept on the include path because the generated unit lives under
build.path/sketch while a quoted #include "foo.h" must resolve next to its
tab (Arduino copies sketch headers into build.path/sketch; naming the sketch
directory reaches the same files without copying).

A sketch directory that carries its own main.cpp is refused by name: the
scanner does not generate a unit there, and compiling one anyway would
duplicate the entry point.

Refs #1298 (follow-up 4d). #1298 stays open.

Real end-to-end evidence

Installed from the vendors' own package indexes (fbuild fqbn --install),
nothing hand-assembled:

arduino:avr:uno — a full Blink build. 24/24 core units compiled, archive
340,294 B, link exit 0:

compile_source=build/sketch/Blink.ino.cpp
compile_recipe=recipe.cpp.o.pattern
compile_exit=0   compile_object_bytes=4544
avr-readelf -h build/Blink.elf: ELF32 EXEC, Machine: Atmel AVR 8-bit microcontroller
avr-size: text 956  data 0  bss 9
avr-nm: 000002b2 T main

setup/loop/blink_once are inlined into main by the core's own
-flto -Os link recipe (hence local symbols only).

Seeeduino:mbed:xiaonRF52840Plus — the motivating slice. Real
Seeeduino:[email protected] (34,722,259 B) + arm-none-eabi-gcc@7-2017q4 (99.8 MB);
the preprocessed unit compiles through the native path:

compile_include.0=…/cores/arduino
compile_include.1=…/variants/SEEED_XIAO_NRF52840_PLUS
compile_include.2=sketch      compile_include.3=build-seeed/sketch
compile_command="…/arm-none-eabi-g++" -c -g3 -nostdlib "@…/defines.txt" "@…/cxxflags.txt" \
  -DARDUINO_ARCH_NRF52840 -mcpu=cortex-m4 -mfloat-abi=softfp -mfpu=fpv4-sp-d16 …
compile_exit=0   compile_object_bytes=936144
arm-none-eabi-readelf -h: ELF32 REL, Machine: ARM

Remaining cell (deliberately not attempted here): linking the full mbed core —
hundreds of TUs plus the mbed-os framework tree.

Gates

  • soldr cargo test -p fbuild-build-engine --lib: 705 passed, 0 failed, 6 ignored
  • soldr cargo test -p fbuild-cli --bin fbuild: 588 passed, 0 failed
  • soldr cargo clippy -p fbuild-build-engine -p fbuild-cli --all-targets -- -D warnings: clean
  • soldr cargo fmt --all -- --check: clean
  • ci/enforce_platform_boundary.py --print-totals: rows=0; dylint_rows=0
  • RED proved: with the is_sketch_source branch removed the new tests fail
    (13 passed / 4 failed, exit 101).

New tests: plans_a_sketch_by_preprocessing_it_into_one_translation_unit,
a_sketch_keeps_its_own_directory_on_the_include_path,
every_tab_is_concatenated_under_its_own_line_map,
a_sketch_with_its_own_main_cpp_is_refused_rather_than_double_compiled
(engine) and a_sketch_is_planned_through_its_preprocessed_unit (CLI).

…nner

`fbuild fqbn --compile <sketch>.ino` now plans the unit Arduino's builder
would compile: the sketch directory's `.ino` tabs are concatenated with
`#include <Arduino.h>` and the auto-generated prototype block into
`build.path/sketch/<name>.ino.cpp`, which `recipe.cpp.o.pattern` compiles.

The preprocessing is not re-derived: it goes through `SourceScanner`, the
same code that preprocesses a PlatformIO sketch, so tab order, hoisted
includes, `#line` mapping and prototypes cannot drift between the two build
paths. The sketch directory stays on the include path because the generated
unit lives under `build.path/sketch` and a quoted include must still resolve
next to its tab.

Refs #1298
@zackees
zackees added this pull request to the merge queue Dec 3, 2026
Merged via the queue into main with commit c9aa1e6 Dec 3, 2026
38 checks passed
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant