Use bindgen to generate for a target platform?


#1

I’m running bindgen on my Mac, which results in it generating symbols for Darwin. However, I’m compiling for a different platform (Fuchsia). Is there a way that I can instruct bindgen to generate symbols for a different target than the host it’s running on?


#2

I don’t think so. Bindgen gets information from LLVM, so you’d at least need LLVM aware of the target platform, and have system headers for that platform.

However, for C FFI most of the time it doesn’t matter. #[repr(C)] and std::os::raw::c_* types do majority of the work. If you’re lucky and bindgen didn’t have to correct any edge cases, then the generated FFI might just work fine across platforms.

In simple cases if you disable/delete bindgen’s layout tests, then even bindings generated for 64-bit platforms may work on 32-bit platforms correctly.


#3

Assuming my platform is aware in that way, would it be sufficient to simply tell clang to cross compile? I.e., bindgen -- --clang-flag-that-requests-cross-compilation?


#4

Hmm, that might work.


#5

Well, it seems to do something different. Still not sure whether it does the right thing, but fingers crossed.


#6

It should work out-of-the-box if you use it from build.rs.
For example, if I build for android(arm cpu) from linux(amd64): cargo build --target=arm-linux-androideabi,
then bindgen detect right target and generate code for this target.
Of course I setup proper include paths for bindgen, so it can find include files
for Android.


#7

Unfortunately I’m not using build.rs, but invoking it from the command line. But that’s good to know!