Conditional compile and user defined attributes


#1

I want to use conditional compilation, and I imagined from the document,

#[cfg(foo)]
const A: i32 = 1;
#[cfg(not(foo)]
const A: i32 = 2;

How can I define “foo”? I could not understand the former “foo” is built-in or user-defined.


#2

Configuration tokens are defined via --cfg [SPEC] argument for rustc. In case of cargo, you need to use build script: http://doc.crates.io/build-script.html#outputs-of-the-build-script
In particular, cargo:rust-cfg=[SPEC].

UPD
https://github.com/target-san/context-rs/tree/rewrite
Contains example build script and its use in code. In particular, contains custom config flag stack_grows_up


#3

I see.
So there are no ways to define configuration tokens in source code.
Is my understanding right?

Thank you for telling me information!


#4

Yes. You can only define them outside. Pretty much logical to me. Otherwise you’d happen directly in C defines hell.


#5

Thanks!