yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
369 stars 95 forks source link

Generated bindings fail to compile after upgrade to 0.17.0 #628

Closed dmbaturin closed 4 years ago

dmbaturin commented 4 years ago

To allow using my OCaml code from Python, I put together a shared library using your examples and my limited understanding of ctypes. It used to work fine, until the 0.17.0 release. Now compilation of generated bindings fails with a type error. When I downgrade to 0.16.0, it works again.

Reproducing procedure:

$ opam pin add vyos1x-config https://github.com/vyos/vyos1x-config.git
$ git clone https://github.com/vyos/libvyosconfig.git
$ cd libvyosconfig
$ make

ocamlc -g -c stub/init.c
mv init.o _build/stub/init.o
ocamlfind opt -c -o _build/lib/bindings.cmx -I _build/generated -I _build/lib -package vyos1x-config,pcre,ctypes.stubs,ctypes.foreign lib/bindings.ml
ocamlfind opt -c -o _build/stub_generator/generate.cmx -I _build/generated -I _build/lib -package vyos1x-config,pcre,ctypes.stubs,ctypes.foreign stub_generator/generate.ml
ocamlfind opt -o _build/generate -I _build/lib -linkpkg -package vyos1x-config,pcre,ctypes.stubs,ctypes.foreign _build/lib/bindings.cmx _build/stub_generator/generate.cmx
_build/generate _build/generated
ocamlfind opt -c -o _build/generated/vyosconfig_bindings.cmx -I _build/generated -I _build/lib -package vyos1x-config,pcre,ctypes.stubs,ctypes.foreign _build/generated/vyosconfig_bindings.ml
File "_build/generated/vyosconfig_bindings.ml", line 39, characters 62-64:
39 |  fun x1 -> let CI.CPointer x4 = f (x3 (CI.make_ptr x2 x1)) in x4))
                                                                   ^^
Error: This expression has type
         ($CPointer option, $1 CI.typ) Ctypes_ptr.Fat.t
       but an expression was expected of type
         ('a, 'b) CI.fatptr = ('a, 'b Ctypes.typ) Ctypes_ptr.Fat.t
       The type constructor $CPointer would escape its scope
       Ctypes_ptr.Fat.t is abstract because no corresponding cmi file was found in path.
make: *** [Makefile:64: _build/generated/vyosconfig_bindings.cmx] Error 2

My guess is that it's related to the "Reflect "managed" status in the types of fat pointers" change. Note that my understanding of the ctypes library or libffi itself is still limites, so I'm ready to do any debug you want me to do, you'll have to tell me what exactly I should do.

yallop commented 4 years ago

Thank you for this clear bug report & reproduction procedure!

My guess is that it's related to the "Reflect "managed" status in the types of fat pointers" change.

Your guess is spot on. There should be a new ctypes release that fixes the libvyosconfig build available soon.

yallop commented 4 years ago

@dmbaturin, could you check that the master branch of ctypes works for you? You can install it by pinning with --dev:

opam pin --dev ctypes
dmbaturin commented 4 years ago

@yallop Thanks, I can confirm that after pinning to the dev version, it builds without errors.

yallop commented 4 years ago

Thanks for the confirmation. There's a new release on its way with the fix: https://github.com/ocaml/opam-repository/pull/15911

yallop commented 4 years ago

I'm closing this, since it's fixed in 0.17.1 (ocaml/opam-repository#15911).