4 ms·
Panama FFI has a tool called jextract that parses C headers and writes the Java bindings for you.
by mike_hearn 3y ago
Panama FFI has a tool called jextract that parses C headers and writes the Java bindings for you.
- marwis 3y agoBut can it generate bindings for multiple abi variants? This would seem extremely complicated to automate.
- mike_hearn 3y agoIt can understand preprocessor macros, and the FFI's CLinker class understands different platform ABIs. So I guess you could generate several different packages with variant ABIs, abstract over them and then use whichever one seems to be available.
- marwis 3y agoAbstract over them meaning writing wrappers by hand?
- grishka 3y agoThe ABI stuff is taken care of at runtime.
- marwis 3y agoWhat do you mean?
- grishka 3y agoYou only specify the function signatures and structs. The JVM then figures out how to put arguments and return values into registers/stack/whatever at runtime, depending on which OS and CPU it's running on. That's what the `CLinker` class is responsible for. It's like regular dynamic linking, but taken a bit further.
- marwis 3y agoThe problem is structs and function signatures of standard APIs can have differences for each os/version combination.
- grishka 3y agoThen you have to take care of making sure you call the right thing. But I suppose this doesn't happen very often because OS APIs usually maintain backwards compatibility. Also a well-designed API would take complex structs by pointer, thus allowing the addition of fields at the end without breaking the ABI.