[ocaml-ctypes] Using Cstubs.Types.TYPE and Cstubs.FOREIGN coherently

Si Beaumont simon.beaumont at citrix.com
Wed Apr 29 12:40:13 BST 2015


Hi,

I've been looking to use Ctypes to create bindings to libpci and use
stub-generation. I was hoping to generate stubs for both the foreign function
bindings and also for the struct layouts (including inlining of `#define`d
constants from the library).

I successfully got stub generation working for the functions and for the
structures by creating a functor taking a module with signature Cstubs.FOREIGN
as per the examples I've seen in the Ctypes repo[1] and even got that working
with OASIS by creating an executable using the Cstubs.write_{c,ml} functions
and using this as a "BuildTool" following some other examples I found
(ocaml-gsasl[2] and onanomsg[3]).

However, I wanted to also be able to get the struct layout and constant
definitions only available to the C preprocesor without me having to transcribe
them into my library (libpci like many other libraries `#define`s a whole bunch
of values for flags that need to be passed into functions). FWICT to get this
to work I need to create a second functor taking a module with signature
Cstubs.Types.TYPE and then use Cstubs.Type.write_c. This will generate
a C program which needs to be compiled. This compiled C program can then be run
to generate an OCaml module which can be used with my original Bindings
functor (gleanings from a few days of hacking and from ctypes
mailing-list[4][5]).

It seems this pipeline is going to be quite difficult to integrate into a build
system like OASIS since I need to compile an auto-generated C program half way
through. My final bit of research landed me on an open Bountysource issue[6] on
which it looks like there's a plan to smooth this out to make the workflow more
like the stub generation for foreign function bindings:

> We can address both these concerns with an alternative workflow for
> Cstubs_structs.TYPE that follows the same pattern as stub generation for
> functions: generate C and ML directly from the functor containing your
> bindings, then link the generated C and ML into your program.

This sounds fantastic! I was wondering if there is any branch of Ctypes with
this functionality yet that I could try out or if there is any expectation to
add this in the near-term?

For now, I think I'll have to use just the stub generation for the functions
(and for the structures that I've transcribed) and have to hardcode the
`#define`d constants and enums.

TIA,

- Si
[1]: https://github.com/ocamllabs/ocaml-ctypes/blob/f7d8f63d/examples/ncurses/stub-generation/bindings/ncurses_bindings.ml
[2]: https://github.com/nojb/ocaml-gsasl/blob/a96c7b94/_oasis
[3]: https://github.com/rgrinberg/onanomsg/blob/4d09e396/_oasis
[4]: http://lists.ocaml.org/pipermail/ctypes/2015-January/000143.html
[5]: http://lists.ocaml.org/pipermail/ctypes/2015-February/000153.html
[6]: https://www.bountysource.com/issues/8206607-new-constant-function-is-hard-to-use


More information about the Ctypes mailing list