Bindgen with #define and platform specific feature gates?

I'm generating a sys crate using bindgen. This is binding a stable ABI but, there are some #define features in the source: opt-in features, operating system/build system differences, etc.

I'd love not to have to require the generation every build, but I'm not sure if/how I can generate bindings for the various features and Os specific things? I guess I could just create a variety of different wrapper.h style files with the various #define defines and then use rust's feature gates to selectively include the generated code? but.. features are additive right? Also, I'm not sure how I do the builds for other platforms.. _WIN32, _MSC_VER etc

I assume there is a document about this somewhere but I haven't found it..

Thanks!

Hopefully things like _MSC_VER don't matter for the ABI. C usually does a lot of ifdefs to work around bugs in C compilers and guess which system headers to #include, just to end up with the same thing on every platform.

For features, such as #ifdef ENABLE_FOO_FEATURE defs, it'd be awesome to translate these to #[cfg(feature = "foo")], but bindgen can't do it automatically.

one example is the definition of this macro:

#if defined(_MSC_VER) && !defined(_LANGUAGE_C_PLUS_PLUS) \
    && !defined(__cplusplus)
#define EXTERN_STRUCT extern struct
#else
#define EXTERN_STRUCT struct
#endif

which is later used .. EXTERN_STRUCT _array; etc..

Maybe in practice this doesn't matter to the rust FFI, especially considering we're not building with the microsoft compiler? I guess I've now convinced myself of that one :slight_smile: