nros_rmw_cffi/section.rs
1//! RMW backend self-registration (`.init_array` ctor).
2//!
3//! Every `nros-rmw-<name>` crate (or C/C++ static lib) self-registers
4//! its vtable with the cffi registry. The trigger differs by tier:
5//!
6//! - **Hosted (Rust + C/C++):** the backend emits a `#[used]` ctor
7//! function pointer into the platform loader's pre-`main` init
8//! section (`.init_array` on ELF). The loader fires every ctor before `main`, so each
9//! backend's [`crate::nros_rmw_cffi_register_named`] call has already
10//! run by the time the runtime opens a session. No runtime walk, no
11//! `linkme` distributed slice.
12//!
13//! - **Embedded (`target_os = "none"`):** the ctor expands to nothing.
14//! Bare-metal firmware has no loader that walks `.init_array` in the
15//! shape the registry needs, so the board / typed carrier calls
16//! `nros_rmw_<x>::register()` EXPLICITLY. The RTOS
17//! targets (NuttX / Zephyr / ESP-IDF / VxWorks) keep that explicit
18//! call too; the ctor there is harmless (register is idempotent).
19//!
20//! The native app keeps its `#[used] __FORCE_LINK_*` anchor: the anchor
21//! defeats dead-code elimination so the backend closure AND its ctor
22//! survive into the final binary — it is NOT a registration call.
23//!
24//! This replaces the earlier `linkme` distributed-slice walker
25//! (~~`RMW_INIT_ENTRIES`~~ + ~~`nros_rmw_cffi_walk_init_section`~~), since
26//! removed.
27//!
28//! # C / C++ backends
29//!
30//! Static-lib backends emit their entry via the
31//! [`NROS_RMW_REGISTER_BACKEND`] macro in `<nros/rmw_vtable.h>`; the
32//! cmake `nano_ros_link_rmw` strong stub handles the C/C++-via-cmake
33//! path.
34//!
35//! [`NROS_RMW_REGISTER_BACKEND`]: ../../include/nros/rmw_vtable.h
36
37/// Macro emitted by backend crates to self-register their vtable.
38///
39/// On hosted targets (`not(target_os = "none")`) it expands to a
40/// `#[used]` ctor function pointer landed in the loader's pre-`main`
41/// init section (`.init_array` on ELF). The loader invokes the ctor before `main`; the ctor body is
42/// `$body`, which calls the backend's `register()`. On embedded
43/// (`target_os = "none"`) it expands to nothing — the board calls
44/// `register()` explicitly.
45///
46/// Usage (inside the backend crate):
47///
48/// ```ignore
49/// nros_rmw_cffi::nros_rmw_register_backend! {
50/// fn() { let _ = nros_rmw_zenoh_register(); }
51/// }
52/// ```
53#[macro_export]
54macro_rules! nros_rmw_register_backend {
55 (fn() $body:block) => {
56 // Hosted only. Bare-metal (`target_os = "none"`) has no loader
57 // that fires `.init_array` for the registry, so the carrier
58 // calls `register()` explicitly and the macro emits nothing.
59 #[cfg(not(target_os = "none"))]
60 const _: () = {
61 unsafe extern "C" fn __nros_rmw_backend_auto_register() $body
62
63 // The loader walks this section before transferring control
64 // to `main`, invoking every fn pointer found there.
65 // `#[used]` keeps the symbol against `--gc-sections`.
66 // macOS/Apple dropped (phase-260) — ELF `.init_array` only.
67 #[used]
68 #[unsafe(link_section = ".init_array")]
69 static __NROS_RMW_BACKEND_AUTO_REGISTER_CTOR: unsafe extern "C" fn() =
70 __nros_rmw_backend_auto_register;
71 };
72 };
73}