rust/kernel/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
Add a new if_cfg! macro to simplify conditional compilation using
cfg attributes. This macro expands to paired #[cfg(cond)] and
#[cfg(not(cond))] blocks, allowing compile-time selection between
code branches in both expression and statement contexts.
Suggested-by: Benno Lossin <lossin@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1183
Signed-off-by: Areej Hamid <areejhamid8560@gmail.com>
---
rust/kernel/lib.rs | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index ed53169e795c..47e73949392d 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -294,6 +294,42 @@ macro_rules! asm {
};
}
+/// Conditionally compiles and executes code based on a `#[cfg]` condition.
+///
+/// Expands to `#[cfg(cond)] { ... }` and `#[cfg(not(cond))] { ... }`,
+/// allowing conditional compilation in both expression and statement positions.
+///
+/// This macro is useful when both branches must be valid Rust code and the
+/// selection between them is done at compile time via a config option.
+/// # Examples
+/// ```
+/// # use kernel::if_cfg;
+/// // Select a value depending on CONFIG_64BIT.
+/// let x = if_cfg!(if CONFIG_64BIT {
+/// 64
+/// } else {
+/// 32
+/// });
+///
+/// // `x` will be 64 if CONFIG_64BIT is enabled, otherwise 32.
+/// assert!(x == 64 || x == 32);
+/// ```
+#[macro_export]
+macro_rules! if_cfg {
+ (if $cond:tt { $($then:tt)* } else { $($else:tt)* }) => {{
+ #[cfg($cond)]
+ { $($then)* }
+ #[cfg(not($cond))]
+ { $($else)* }
+ }};
+ (if $cond:tt { $($then:tt)* }) => {{
+ #[cfg($cond)]
+ { $($then)* }
+ #[cfg(not($cond))]
+ { () }
+ }};
+}
+
/// Gets the C string file name of a [`Location`].
///
/// If `file_with_nul()` is not available, returns a string that warns about it.
@@ -337,3 +373,4 @@ pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::f
c"<Location::file_with_nul() not supported>"
}
}
+
--
2.43.0
On Thu, Aug 14, 2025 at 01:38:26AM +0500, Areej wrote: > Add a new if_cfg! macro to simplify conditional compilation using > cfg attributes. This macro expands to paired #[cfg(cond)] and > #[cfg(not(cond))] blocks, allowing compile-time selection between > code branches in both expression and statement contexts. Nit, trailing whitespace in your text :( > > Suggested-by: Benno Lossin <lossin@kernel.org> > Link: https://github.com/Rust-for-Linux/linux/issues/1183 > Signed-off-by: Areej Hamid <areejhamid8560@gmail.com> From: line does not match up with this text :( thanks, greg k-h
On 2025-08-14 01:38:26+0500, Areej wrote: > Add a new if_cfg! macro to simplify conditional compilation using > cfg attributes. This macro expands to paired #[cfg(cond)] and > #[cfg(not(cond))] blocks, allowing compile-time selection between > code branches in both expression and statement contexts. > > Suggested-by: Benno Lossin <lossin@kernel.org> > Link: https://github.com/Rust-for-Linux/linux/issues/1183 > Signed-off-by: Areej Hamid <areejhamid8560@gmail.com> > --- > rust/kernel/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ > 1 file changed, 37 insertions(+) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index ed53169e795c..47e73949392d 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -294,6 +294,42 @@ macro_rules! asm { > }; > } > > +/// Conditionally compiles and executes code based on a `#[cfg]` condition. > +/// > +/// Expands to `#[cfg(cond)] { ... }` and `#[cfg(not(cond))] { ... }`, > +/// allowing conditional compilation in both expression and statement positions. > +/// > +/// This macro is useful when both branches must be valid Rust code and the > +/// selection between them is done at compile time via a config option. > +/// # Examples > +/// ``` > +/// # use kernel::if_cfg; > +/// // Select a value depending on CONFIG_64BIT. > +/// let x = if_cfg!(if CONFIG_64BIT { > +/// 64 > +/// } else { > +/// 32 > +/// }); Isn't this the same as cfg!()? if cfg!(CONFIG_64BIT) { 64 } else { 32 } https://doc.rust-lang.org/std/macro.cfg.html > +/// > +/// // `x` will be 64 if CONFIG_64BIT is enabled, otherwise 32. > +/// assert!(x == 64 || x == 32); > +/// ``` > +#[macro_export] > +macro_rules! if_cfg { > + (if $cond:tt { $($then:tt)* } else { $($else:tt)* }) => {{ > + #[cfg($cond)] > + { $($then)* } > + #[cfg(not($cond))] > + { $($else)* } > + }}; > + (if $cond:tt { $($then:tt)* }) => {{ > + #[cfg($cond)] > + { $($then)* } > + #[cfg(not($cond))] > + { () } > + }}; > +} > + > /// Gets the C string file name of a [`Location`]. > /// > /// If `file_with_nul()` is not available, returns a string that warns about it. > @@ -337,3 +373,4 @@ pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::f > c"<Location::file_with_nul() not supported>" > } > } > + Spurous whitespace change.
© 2016 - 2025 Red Hat, Inc.