Convert simple C #define's into Rust constants

2019-08-16 00:12发布

I have a proprietary library with a long story and 30KLoC header. I'd like to generate bindings for Rust to it. And I've used bindgen crate almost successfully. Except bindgen can't transform macro constants into normal constants. Because those constants are defined in a fuzzy way, like

#define CONSTANT ((const_type)SOME_OTHER_CONSTANT)

So, is there some way to translate such half-constants into normal Rust ones:

const Type name = value;

UPDATE

Apparently, crate bindgen uses clang as its backend. And clang is able to deal with macro definitions and other such stuff, if right flag is provided to parser. So, can anyone advice good tutorial on dealing with macro definitions using libclang API?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-16 00:36

Running the headers through gcc -E (i.e. the preprocessor) will remove the #defines. It may remove more than you want though.

If you want to selectively preprocess, there used to be a utility called scpp that did that. I can't immediately find a reference to it other than to some tar files to download, but you may have more luck.

查看更多
登录 后发表回答