Use bindgen to generate for a target platform?

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?

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.

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?

Hmm, that might work.

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

It should work out-of-the-box if you use it from
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.

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