Skip to content

feat(fqbn): run pre-link hooks and compile the variant folder (#1298 4e) - #2243

Merged
zackees merged 1 commit into
mainfrom
agent/1298-mbed-link
Dec 4, 2026
Merged

zackees merged 1 commit into
mainfrom
agent/1298-mbed-link

Conversation

@zackees

@zackees zackees commented Dec 4, 2026

Copy link
Copy Markdown
Member

Follow-up 4e of #1298: an FQBN build now links a sketch through a vendor core's own pre-link hooks and variant sources, proven end-to-end on the published Seeeduino mbed 2.9.3 core (XIAO nRF52840 Plus).

#1298 stays open — the upload/objcopy half is a separate cell.

What was missing (both found from the real package, not from a fixture)

  1. recipe.hooks.linking.prelink.<n>.pattern. Arduino runs these in numeric order before recipe.c.combine.pattern. The mbed core uses exactly that to compose the linker script into the build directory:

    recipe.hooks.linking.prelink.1.pattern="{compiler.path}{compiler.c.elf.cmd}" -E -P -x c {build.extra_ldflags} "{build.variant.path}/{build.ldscript}" -o "{build.path}/{build.ldscript}"
    recipe.c.combine.pattern="…" "-T{build.path}/{build.ldscript}" …
    

    So the linker's script does not exist until the builder runs the hook — and a plain copy is wrong too, because the script is C-preprocessed (#define FLASH_ORIGIN … collapses to a number).

    fbuild fqbn --link now plans every prelink hook (expanded over the same properties as the link, ordered numerically — prelink.10 after prelink.2), runs them before the linker, and stops the link when a hook exits non-zero, naming the hook key and leaving no image.

  2. The board's variant folder is part of the core archive. Arduino compiles variants/<build.variant>/* into core.a alongside cores/<build.core>/** (its own files only, not subdirectories). Without variant.cpp the archive builds clean and the ELF dies with undefined reference to g_APinDescription / PINCOUNT_fn / getUniqueSerialNumber / _ontouch1200bps_. core_sources now includes those files.

CLI: --link reports link_hooks=N and link_hook.<i>=<command>, and after --run link_hooks_run=N; the JSON gains link.hooks[] and link_outcome.hooks.

Real end-to-end (not a fixture)

Data dir built from the published index: core tarball ArduinoCore-mbed-2.9.3.tar.bz2 (34,722,259 B, sha256 65d9de0b…fd784f, matches the index) and toolchain arm-none-eabi-gcc 7-2017q4 (index sha256 96a029e2ae13…), installed at the canonical <data>/packages/<vendor>/{hardware,tools} layout.

step result
core sources 42 (41 under cores/arduino + variants/SEEED_XIAO_NRF52840_PLUS/variant.cpp)
--archive 42 arm-none-eabi-ar invocations → core.a 27,637,612 B
sketch TU Blink.cpp → 936,772 B
--link link_hooks_run=1, link_exit=0 → Blink.elf 1,732,660 B
readelf -h ELF32, Machine ARM, Type EXEC, entry 0x2fb6d
symbols main (WEAK, the mbed starter), setup, loop
hook output build_mbed/linker_script.ld 4,242 B, preprocessed (FLASH ORIGIN = 0x27000, no #define left)

Gates (real output)

  • soldr cargo test -p fbuild-build-engine --lib → 709 passed, 0 failed, 6 ignored
  • soldr cargo test -p fbuild-cli --bin fbuild → 589 passed, 0 failed
  • soldr cargo clippy -p fbuild-build-engine -p fbuild-cli --all-targets -- -D warnings → 0
  • uv run --no-project python ci/enforce_platform_boundary.py --print-totals → rows=0; dylint_rows=0
  • cargo fmt --all -- --check → clean

RED, twice:

  • disabling the hook loop (take(0)) → a_prelink_hook_runs_before_the_linker_and_blocks_it_when_it_cannot_run fails, 19 passed / 1 failed, exit 101;
  • dropping the variant walk → the_variant_folder_sources_join_the_core_but_not_its_subdirectories fails, 17 passed / 1 failed, exit 101.
  • The real e2e was itself RED before the variant fix: the identical run ended in the undefined reference block above.

Tests added

  • prelink_hooks_are_planned_in_numeric_order_and_name_the_composed_script (1, 2, 10 — a lexical sort would reverse 2 and 10)
  • a_package_without_prelink_hooks_plans_none
  • a_prelink_hook_runs_before_the_linker_and_blocks_it_when_it_cannot_run
  • the_variant_folder_sources_join_the_core_but_not_its_subdirectories
  • the_link_report_names_the_prelink_hooks_in_order (text + JSON)

A vendor core's link is not one template: `Seeeduino:mbed` composes its
linker script with `recipe.hooks.linking.prelink.1.pattern` (`-E -P -x c`
variant -> build.path) and links the composed copy with
`-T{build.path}/{build.ldscript}`, and its pin tables live in
`variants/<board>/variant.cpp`, which Arduino compiles into the core
archive.

- plan every `recipe.hooks.linking.prelink.<n>.pattern` in numeric order
  (prelink.10 after prelink.2) and run them before the linker; a hook
  that exits non-zero stops the link and names its key
- include the variant folder's own sources in `core_sources` (top level
  only, matching Arduino; a variant's subdirectories are not compiled)
- CLI: link_hooks=N / link_hook.<i>= / link_hooks_run=N, and
  link.hooks[] in JSON

Real e2e on the published Seeeduino mbed 2.9.3 core + arm-none-eabi
7-2017q4: 42 core units -> core.a 27,637,612 B, prelink hook ran,
link exit 0 -> Blink.elf 1,732,660 B ELF32 ARM EXEC with main/setup/loop.
@zackees
zackees added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit e3c8538 Dec 4, 2026
61 of 64 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