You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature Request] Handle if + cfg! in const context #1018
Since the addition of if in const context, the usage of if + cfg! macro became a thing, since it reduces the need of replication of docs and definition.
Currently, cbindgen ignores this kind of construction, but ideally, supporting that would make easier for maintaining C-exported constants that differ on targets.
In const context, an if must always have an else, so it is also common to have panicking macros, like panic!, unimplemented!, todo! and unreacheble! in the else clause. That causes an compiler error with the panic message if it reaches the panic. I think this could generate an #error directive with the message.
Example
pubconstMYCONST:i32 = ifcfg!(target_os = "linux"){10}elseifcfg!(windows){5}else{panic!("We only support linux and windows for now!")}
#if defined(⟨USER_DEFINED_LINUX_FLAG_HERE⟩)
#defineMYCONST 10
#elsif defined(⟨USER_DEFINED_WINDOWS_FLAG_HERE⟩)
#defineMYCONST 5
#else#error "We only support linux and windows for now!"
#endif
Activity
GrayJack commentedon Oct 27, 2024
Its unclear to me how this would work with the macro expansion feature. I'm not using the expasion feature in none of my projects that uses cbindgen
GrayJack commentedon Oct 27, 2024
I currently have a working version for C/C++, but I have no idea how would be the equivalent for Cython to turn panics->
#errorchrysn commentedon Feb 3, 2025
Is there an established workaround for this?
Right now I'm tempted to just place a manual copy of the cfg tree into the
header, but then I'll also have to do the include guards manually.lakers-c: Manually reconstruct the buffer size selection for C
lakers-c: Manually reconstruct the buffer size selection for C
lakers-c: Manually reconstruct the buffer size selection for C
GrayJack commentedon Feb 9, 2025
I think the only workaround for this is to create a bunch of
#[cfg(...)]with the variable name for your const