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

Jeremy Yallop yallop at gmail.com
Mon May 4 14:04:35 BST 2015


On 29 April 2015 at 12:40, Si Beaumont <simon.beaumont at citrix.com> wrote:
> 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]).

Right.

> 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.

It's not trivial, but it's certainly possible.  The async_ssl package
is a useful example to work from; it binds various constants in the
Ffi_bindings.types module:

   https://github.com/janestreet/async_ssl/blob/c58a80b7/bindings/ffi_bindings.ml#L1-L40

and uses ocamlbuild rules to set up the appropriate dependencies

   https://github.com/janestreet/async_ssl/blob/c58a80b7/myocamlbuild.ml

There's also a bit of _oasis set-up to build the executables:

   https://github.com/janestreet/async_ssl/blob/c58a80b7/_oasis#L84-L97

I wonder if it'd be useful for the Ctypes package to include some
reusable ocamlbuild rules to make this kind of thing easier.

> 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?

I've just pushed a branch with an implementation of this proposal.

   https://github.com/ocamllabs/ocaml-ctypes/pull/296

Feedback is very welcome!

Jeremy.


More information about the Ctypes mailing list