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 string, 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 program name, and return its
47/// [`EntryAddr`](super::EntryAddr).
48#[macro_export]
49macro_rules! rp_program_name {
50    ($name:expr) => {
51        $crate::str!(
52            $crate::consts::TAG_RASPBERRY_PI,
53            $crate::consts::ID_RP_PROGRAM_NAME,
54            $name
55        )
56    };
57}
58
59/// Generate a static item containing the `CARGO_BIN_NAME` as the program name,
60/// and return its [`EntryAddr`](super::EntryAddr).
61#[macro_export]
62macro_rules! rp_cargo_bin_name {
63    () => {
64        $crate::env!(
65            $crate::consts::TAG_RASPBERRY_PI,
66            $crate::consts::ID_RP_PROGRAM_NAME,
67            "CARGO_BIN_NAME"
68        )
69    };
70}
71
72/// Generate a static item containing the program version, and return its
73/// [`EntryAddr`](super::EntryAddr).
74#[macro_export]
75macro_rules! rp_program_version {
76    ($version:expr) => {{
77        $crate::str!(
78            $crate::consts::TAG_RASPBERRY_PI,
79            $crate::consts::ID_RP_PROGRAM_VERSION,
80            $version
81        )
82    }};
83}
84
85/// Generate a static item containing the `CARGO_PKG_VERSION` as the program
86/// version, and return its [`EntryAddr`](super::EntryAddr).
87#[macro_export]
88macro_rules! rp_cargo_version {
89    () => {
90        $crate::env!(
91            $crate::consts::TAG_RASPBERRY_PI,
92            $crate::consts::ID_RP_PROGRAM_VERSION_STRING,
93            "CARGO_PKG_VERSION"
94        )
95    };
96}
97
98/// Generate a static item containing the program URL, and return its
99/// [`EntryAddr`](super::EntryAddr).
100#[macro_export]
101macro_rules! rp_program_url {
102    ($url:expr) => {
103        $crate::str!(
104            $crate::consts::TAG_RASPBERRY_PI,
105            $crate::consts::ID_RP_PROGRAM_URL,
106            $url
107        )
108    };
109}
110
111/// Generate a static item containing the `CARGO_PKG_HOMEPAGE` as the program URL,
112/// and return its [`EntryAddr`](super::EntryAddr).
113#[macro_export]
114macro_rules! rp_cargo_homepage_url {
115    () => {
116        $crate::env!(
117            $crate::consts::TAG_RASPBERRY_PI,
118            $crate::consts::ID_RP_PROGRAM_URL,
119            "CARGO_PKG_HOMEPAGE"
120        )
121    };
122}
123
124/// Generate a static item containing the program description, and return its
125/// [`EntryAddr`](super::EntryAddr).
126#[macro_export]
127macro_rules! rp_program_description {
128    ($description:expr) => {
129        $crate::str!(
130            $crate::consts::TAG_RASPBERRY_PI,
131            $crate::consts::ID_RP_PROGRAM_DESCRIPTION,
132            $description
133        )
134    };
135}
136
137/// Generate a static item containing whether this is a debug or a release
138/// build, and return its [`EntryAddr`](super::EntryAddr).
139#[macro_export]
140macro_rules! rp_program_build_attribute {
141    () => {
142        $crate::str!(
143            $crate::consts::TAG_RASPBERRY_PI,
144            $crate::consts::ID_RP_PROGRAM_BUILD_ATTRIBUTE,
145            {
146                if cfg!(debug_assertions) {
147                    c"debug"
148                } else {
149                    c"release"
150                }
151            }
152        )
153    };
154}
155
156/// Generate a static item containing the specific board this program runs on,
157/// and return its [`EntryAddr`](super::EntryAddr).
158#[macro_export]
159macro_rules! rp_pico_board {
160    ($board:expr) => {
161        $crate::str!(
162            $crate::consts::TAG_RASPBERRY_PI,
163            $crate::consts::ID_RP_PICO_BOARD,
164            $board
165        )
166    };
167}
168
169// End of file