rp_binary_info/
macros.rs

1//! Handy macros for making Binary Info entries
2
3/// Generate a static item containing the given environment variable,
4/// and return its [`EntryAddr`](super::EntryAddr).
5#[macro_export]
6macro_rules! env {
7    ($tag:expr, $id:expr, $env_var_name:expr) => {
8        $crate::str!($tag, $id, {
9            let value = concat!(env!($env_var_name), "\0");
10            // # Safety
11            //
12            // We used `concat!` to null-terminate on the line above.
13            let value_cstr =
14                unsafe { core::ffi::CStr::from_bytes_with_nul_unchecked(value.as_bytes()) };
15            value_cstr
16        })
17    };
18}
19
20/// Generate a static item containing the given string, and return its
21/// [`EntryAddr`](super::EntryAddr).
22///
23/// You must pass a numeric tag, a numeric ID, and `&CStr` (which is always
24/// null-terminated).
25#[macro_export]
26macro_rules! str {
27    ($tag:expr, $id:expr, $str:expr) => {{
28        static ENTRY: $crate::StringEntry = $crate::StringEntry::new($tag, $id, $str);
29        ENTRY.addr()
30    }};
31}
32
33/// Generate a static item containing the given integer, and return its
34/// [`EntryAddr`](super::EntryAddr).
35///
36/// You must pass a numeric tag, a numeric ID, and `&CStr` (which is always
37/// null-terminated).
38#[macro_export]
39macro_rules! int {
40    ($tag:expr, $id:expr, $int:expr) => {{
41        static ENTRY: $crate::IntegerEntry = $crate::IntegerEntry::new($tag, $id, $int);
42        ENTRY.addr()
43    }};
44}
45
46/// Generate a static item containing the given pointer, and return its
47/// [`EntryAddr`](super::EntryAddr).
48///
49/// You must pass a numeric tag, a numeric ID, and a pointer
50#[macro_export]
51macro_rules! pointer {
52    ($tag:expr, $id:expr, $ptr:expr) => {{
53        static ENTRY: $crate::PointerEntry = $crate::PointerEntry::new($tag, $id, $ptr);
54        ENTRY.addr()
55    }};
56}
57
58/// Generate a static item containing the program name, and return its
59/// [`EntryAddr`](super::EntryAddr).
60#[macro_export]
61macro_rules! rp_program_name {
62    ($name:expr) => {
63        $crate::str!(
64            $crate::consts::TAG_RASPBERRY_PI,
65            $crate::consts::ID_RP_PROGRAM_NAME,
66            $name
67        )
68    };
69}
70
71/// Generate a static item containing the `CARGO_BIN_NAME` as the program name,
72/// and return its [`EntryAddr`](super::EntryAddr).
73#[macro_export]
74macro_rules! rp_cargo_bin_name {
75    () => {
76        $crate::env!(
77            $crate::consts::TAG_RASPBERRY_PI,
78            $crate::consts::ID_RP_PROGRAM_NAME,
79            "CARGO_BIN_NAME"
80        )
81    };
82}
83
84/// Generate a static item containing the program version, and return its
85/// [`EntryAddr`](super::EntryAddr).
86#[macro_export]
87macro_rules! rp_program_version {
88    ($version:expr) => {{
89        $crate::str!(
90            $crate::consts::TAG_RASPBERRY_PI,
91            $crate::consts::ID_RP_PROGRAM_VERSION,
92            $version
93        )
94    }};
95}
96
97/// Generate a static item containing the `CARGO_PKG_VERSION` as the program
98/// version, and return its [`EntryAddr`](super::EntryAddr).
99#[macro_export]
100macro_rules! rp_cargo_version {
101    () => {
102        $crate::env!(
103            $crate::consts::TAG_RASPBERRY_PI,
104            $crate::consts::ID_RP_PROGRAM_VERSION_STRING,
105            "CARGO_PKG_VERSION"
106        )
107    };
108}
109
110/// Generate a static item containing the program URL, and return its
111/// [`EntryAddr`](super::EntryAddr).
112#[macro_export]
113macro_rules! rp_program_url {
114    ($url:expr) => {
115        $crate::str!(
116            $crate::consts::TAG_RASPBERRY_PI,
117            $crate::consts::ID_RP_PROGRAM_URL,
118            $url
119        )
120    };
121}
122
123/// Generate a static item containing the `CARGO_PKG_HOMEPAGE` as the program URL,
124/// and return its [`EntryAddr`](super::EntryAddr).
125#[macro_export]
126macro_rules! rp_cargo_homepage_url {
127    () => {
128        $crate::env!(
129            $crate::consts::TAG_RASPBERRY_PI,
130            $crate::consts::ID_RP_PROGRAM_URL,
131            "CARGO_PKG_HOMEPAGE"
132        )
133    };
134}
135
136/// Generate a static item containing the program description, and return its
137/// [`EntryAddr`](super::EntryAddr).
138#[macro_export]
139macro_rules! rp_program_description {
140    ($description:expr) => {
141        $crate::str!(
142            $crate::consts::TAG_RASPBERRY_PI,
143            $crate::consts::ID_RP_PROGRAM_DESCRIPTION,
144            $description
145        )
146    };
147}
148
149/// Generate a static item containing whether this is a debug or a release
150/// build, and return its [`EntryAddr`](super::EntryAddr).
151#[macro_export]
152macro_rules! rp_program_build_attribute {
153    () => {
154        $crate::str!(
155            $crate::consts::TAG_RASPBERRY_PI,
156            $crate::consts::ID_RP_PROGRAM_BUILD_ATTRIBUTE,
157            {
158                if cfg!(debug_assertions) {
159                    c"debug"
160                } else {
161                    c"release"
162                }
163            }
164        )
165    };
166}
167
168/// Generate a static item containing the specific board this program runs on,
169/// and return its [`EntryAddr`](super::EntryAddr).
170#[macro_export]
171macro_rules! rp_pico_board {
172    ($board:expr) => {
173        $crate::str!(
174            $crate::consts::TAG_RASPBERRY_PI,
175            $crate::consts::ID_RP_PICO_BOARD,
176            $board
177        )
178    };
179}
180
181/// Generate a static item containing the binary end address, and return its
182/// [`EntryAddr`](super::EntryAddr). The argument should be a symbol provided
183/// by the linker script that is located at the end of the binary.
184#[macro_export]
185macro_rules! rp_binary_end {
186    ($ptr:ident) => {{
187        $crate::pointer!(
188            $crate::consts::TAG_RASPBERRY_PI,
189            $crate::consts::ID_RP_BINARY_END,
190            // `unsafe` only needed because MSRV does not yet
191            // contain https://github.com/rust-lang/rust/pull/125834
192            unsafe { core::ptr::addr_of!($ptr).cast() }
193        )
194    }};
195}
196
197// End of file