caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Creating wrappers for C libraries
@ 2006-12-08  4:54 Erik de Castro Lopo
  2006-12-08  6:36 ` [Caml-list] " Chris King
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Erik de Castro Lopo @ 2006-12-08  4:54 UTC (permalink / raw)
  To: caml-list

Hi all,

I've had a look at chapter 12 of the O'Rielly book as well as the
wrapper for ocaml-fftw3 and cairo-ocaml and it all seems a little
daunting.

I was wondering if there was any semi-automated too for creating
Ocaml bindings for C libraries.

Any pointers appreciated.

Cheers,
Erik
-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo
+-----------------------------------------------------------+
"Lumping configuration data, security data, kernel tuning parameters,
etc. into one monstrous fragile binary data structure is really dumb."
- David F. Skoll


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Creating wrappers for C libraries
  2006-12-08  4:54 Creating wrappers for C libraries Erik de Castro Lopo
@ 2006-12-08  6:36 ` Chris King
  2006-12-08  6:45 ` Sébastien Hinderer
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Chris King @ 2006-12-08  6:36 UTC (permalink / raw)
  To: Erik de Castro Lopo; +Cc: caml-list

On 12/7/06, Erik de Castro Lopo <mle+ocaml@mega-nerd.com> wrote:
> I was wondering if there was any semi-automated too for creating
> Ocaml bindings for C libraries.

I recently discovered that SWIG (http://www.swig.org/) supports O'Caml
(though I haven't yet used it myself).  If you're looking for speed
though it may not be the best choice since all C types are mapped to
one variant type in O'Caml.  You can find O'Caml-specific docs here:
http://www.swig.org/Doc1.3/Ocaml.html

I presume you're looking to wrap libsndfile...  I've wrapped a few
similar libraries manually with very few problems.  The libsndfile API
looks to me like it should be fairly straightforward to wrap thanks to
O'Caml's Bigarrays (plus you'll have lots of cut & paste).

Start by writing a few simple integer, string, or array functions for
O'Caml in C to get a feel for the API without having to worry about
custom blocks and such.  The curses wrapper in chapter 18 of the
O'Caml manual is also a very good starting point.  The custom block
from that example can be used nearly verbatim for many C structures
(such as SNDFILE *).  I can provide you with the source of a couple
other small wrappers if you like.

Hope this helps,
Chris


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Creating wrappers for C libraries
  2006-12-08  4:54 Creating wrappers for C libraries Erik de Castro Lopo
  2006-12-08  6:36 ` [Caml-list] " Chris King
@ 2006-12-08  6:45 ` Sébastien Hinderer
  2006-12-08  7:08 ` [Caml-list] " Jon Harrop
  2006-12-08 11:41 ` [Caml-list] " Richard Jones
  3 siblings, 0 replies; 8+ messages in thread
From: Sébastien Hinderer @ 2006-12-08  6:45 UTC (permalink / raw)
  To: caml-list, caml-list

Hi,

Erik de Castro Lopo :
> I was wondering if there was any semi-automated too for creating
> Ocaml bindings for C libraries.

You may have a look to swig.
No idea how convenient the generated bindings are.

Cheers,
Sébastien.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Creating wrappers for C libraries
  2006-12-08  4:54 Creating wrappers for C libraries Erik de Castro Lopo
  2006-12-08  6:36 ` [Caml-list] " Chris King
  2006-12-08  6:45 ` Sébastien Hinderer
@ 2006-12-08  7:08 ` Jon Harrop
  2006-12-08 10:18   ` David Baelde
  2006-12-08 11:41 ` [Caml-list] " Richard Jones
  3 siblings, 1 reply; 8+ messages in thread
From: Jon Harrop @ 2006-12-08  7:08 UTC (permalink / raw)
  To: caml-list

On Friday 08 December 2006 04:54, Erik de Castro Lopo wrote:
> I was wondering if there was any semi-automated too for creating
> Ocaml bindings for C libraries.

camlidl.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Creating wrappers for C libraries
  2006-12-08  7:08 ` [Caml-list] " Jon Harrop
@ 2006-12-08 10:18   ` David Baelde
  2006-12-08 11:08     ` Sébastien Hinderer
  0 siblings, 1 reply; 8+ messages in thread
From: David Baelde @ 2006-12-08 10:18 UTC (permalink / raw)
  To: caml-list

Hi,

One problem with such automated tools is that they have no clue of
which C function takes time and which returns almost instantly. And
these things matter in OCaml, as soon as you use your wrapper in
multithreaded programs: the time-consuming functions should be wrapped
around enter/leave_blocking_section() (and the ocaml data copied to
places untouched by the Gc) so that other caml threads can run while
the function is performed.

However, for libsndfile, most functions are very fast, so I guess you
can try automatic tools.

Cheers.
-- 
David


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Creating wrappers for C libraries
  2006-12-08 10:18   ` David Baelde
@ 2006-12-08 11:08     ` Sébastien Hinderer
  0 siblings, 0 replies; 8+ messages in thread
From: Sébastien Hinderer @ 2006-12-08 11:08 UTC (permalink / raw)
  To: david.baelde; +Cc: caml-list

Hi.

> One problem with such automated tools is that they have no clue of
> which C function takes time and which returns almost instantly.

Thanks a lot for this remark.

I'm also wondering how good automatic tools are for taking advantage of
Caml's rich typing system and of Caml features like exceptions.
An example will probably make my question more clear.
How would an automatic tool bind the open(2) function that returns either a
file descriptor (on success), or -1 to report failures, the precise reason
for the failure being contained in the errno variable. Would it be possible
to specify, with an automatic tool, that the returned valid is an int
unless open returns -1, in which case an error should be
reported through an exception ?

Or would an automatic tool bind open to a function returning an int in any
case, the task of testing the returned value having then to be performed
by the programmer, but in Caml ?

Sébastien.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Creating wrappers for C libraries
  2006-12-08  4:54 Creating wrappers for C libraries Erik de Castro Lopo
                   ` (2 preceding siblings ...)
  2006-12-08  7:08 ` [Caml-list] " Jon Harrop
@ 2006-12-08 11:41 ` Richard Jones
  2006-12-08 12:00   ` Daniel Bünzli
  3 siblings, 1 reply; 8+ messages in thread
From: Richard Jones @ 2006-12-08 11:41 UTC (permalink / raw)
  To: Erik de Castro Lopo; +Cc: caml-list

On Fri, Dec 08, 2006 at 03:54:37PM +1100, Erik de Castro Lopo wrote:
> I've had a look at chapter 12 of the O'Rielly book as well as the
> wrapper for ocaml-fftw3 and cairo-ocaml and it all seems a little
> daunting.
> 
> I was wondering if there was any semi-automated too for creating
> Ocaml bindings for C libraries.

Obviously there are some semi-automated ways, such as SWIG and camlidl
mentioned already.  But I would definitely urge you to look at doing
the wrapping by hand.  It's really not that hard, although you should
pay careful attention to ch18 of the manual.

Rich.

ch18: http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com - NEW!


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Creating wrappers for C libraries
  2006-12-08 11:41 ` [Caml-list] " Richard Jones
@ 2006-12-08 12:00   ` Daniel Bünzli
  0 siblings, 0 replies; 8+ messages in thread
From: Daniel Bünzli @ 2006-12-08 12:00 UTC (permalink / raw)
  To: Erik de Castro Lopo; +Cc: caml-list

Another way is to meta program the bindings using C macros,  
especially if many stubs have the same structure, have a look at the  
file ml_gl.h in lablgl [1].

Daniel

[1] <http://wwwfun.kurims.kyoto-u.ac.jp/soft/lsl/lablgl.html>


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-12-08 11:58 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-08  4:54 Creating wrappers for C libraries Erik de Castro Lopo
2006-12-08  6:36 ` [Caml-list] " Chris King
2006-12-08  6:45 ` Sébastien Hinderer
2006-12-08  7:08 ` [Caml-list] " Jon Harrop
2006-12-08 10:18   ` David Baelde
2006-12-08 11:08     ` Sébastien Hinderer
2006-12-08 11:41 ` [Caml-list] " Richard Jones
2006-12-08 12:00   ` Daniel Bünzli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).