I'd like to use Rust's conditional compilation, e.g.:
#[cfg(platform="value")]
I'm wondering if there's a way to define a constant VALUE = "value", such that this can be written as
#[cfg(platform=VALUE)]
As I understand it, I'd need these defined during my build script exec (before compiling the module), but still accessible within cfg macros within these files.
Is there a way to do this?
Alternatively, any thoughts on an approach that's cleaner than hard-coding the same string repeatedly (and risking typos)?
This is not possible.
However, the Rust developers are aware that
cfgs are error-prone, and have developed an accepted (and implemented) RFC to help prevent that. With this RFC, the compiler warns about unknowncfgvalues (or names).Cargo also has unstable support for this feature. To use it, you need to enable the
-Zcheck-cfgoption in the command line invocation of Cargo. It accepts four values:featuresfor checking of featurescfgs.namesfor checking of well-knowncfgnames.valuesfor checking of well-knowncfgvalues.outputfor checking of customcfgs from build scripts.To use all checks, pass
-Zcheck-cfg=features,names,values,outputin the command line.If you have a build scripts outputting custom
cfgs, you need to enable-Zcheck-cfg=outputas well as adjust your build scripts to outputcargo:rustc-check-cfginstructions. They work like the following: