[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250814162211.566168-2-areejhamid8560@gmail.com>
Date: Thu, 14 Aug 2025 21:22:11 +0500
From: Areej Hamid <areejhamid8560@...il.com>
To: rust-for-linux@...r.kernel.org
Cc: linux-kernel@...r.kernel.org,
ojeda@...nel.org,
alex.gaynor@...il.com,
boqun.feng@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
lossin@...nel.org,
a.hindborg@...aro.org,
aliceryhl@...gle.com,
tmgross@...ch.edu,
dakr@...nel.org,
viresh.kumar@...aro.org,
tamird@...il.com,
dingxiangfei2009@...il.com,
gregkh@...uxfoundation.org,
thomas.weissschuh@...ain.com,
Areej Hamid <areejhamid8560@...il.com>
Subject: [PATCH v2] rust: lib: add if_cfg! macro for conditional compilation
Add the `if_cfg!` macro to simplify conditional compilation using `cfg`
attributes. The macro expands to paired `#[cfg(cond)]` and
`#[cfg(not(cond))]` blocks, allowing compile-time selection between
code branches in both expression and statement contexts.
Note: Previous documentation incorrectly stated that both branches
must be valid Rust code. In reality, `if_cfg!()` compiles only the
active branch, allowing the inactive branch to reference items that
may not exist under certain configurations.
Suggested-by: Benno Lossin <lossin@...nel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1183
Signed-off-by: Areej Hamid <areejhamid8560@...il.com>
---
rust/kernel/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index ed53169e795c..6bcce24600e3 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -294,6 +294,57 @@ 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.
+///
+/// # Key difference from `cfg!()`
+/// - `cfg!()` evaluates a configuration flag at compile time, but both branches must be valid Rust code
+/// - `if_cfg!()` compiles only the active branch, so the inactive branch can reference
+/// functions, types, or constants that may not exist under certain configurations
+///
+/// # Examples
+///
+/// Demonstrates the difference between `if_cfg!()` and `cfg!()`:
+/// ```ignore
+/// # use kernel::if_cfg;
+/// // FOR CONFIG_64BIT
+/// // Only the active branch is compiled - inactive branch can be invalid
+/// let x = if_cfg!(if CONFIG_64BIT {
+/// 42 // valid code
+/// } else {
+/// undefined_function() // invalid, but completely ignored by compiler
+/// });
+/// assert_eq!(x, 42);
+/// ```
+///
+/// Using `cfg!()` instead would fail compilation:
+/// ```ignore
+/// // This fails because Rust must validate both branches
+/// let x = if cfg!(CONFIG_64BIT) {
+/// 42
+/// } else {
+/// undefined_function() // compilation error - function doesn't exist
+/// };
+/// assert_eq!(x, 42);
+/// ```
+#[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.
--
2.43.0
Powered by blists - more mailing lists