From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id C43A4BBAF for ; Sun, 1 Nov 2009 10:31:22 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkMCAF7k7EpIDtyYimdsb2JhbACRaYIshn4/AQEBCgkMBxEFrgqBOYUdiGgBAwMFhDQEgWKBCQ X-IronPort-AV: E=Sophos;i="4.44,661,1249250400"; d="scan'208";a="49589140" Received: from fg-out-1718.google.com ([72.14.220.152]) by mail4-smtp-sop.national.inria.fr with ESMTP; 01 Nov 2009 10:31:22 +0100 Received: by fg-out-1718.google.com with SMTP id d23so217584fga.3 for ; Sun, 01 Nov 2009 01:31:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=BkukWTLDPIMF80iuw+h77dm97kErkRh+INL0O2bQxMY=; b=uBv8TEUyZM1htc2rZi2gDIXavCRqs3T3q/lzR/GkGUTL/h68nv64J+M6FKMTqKP+TL WhnG74U4lpJGnwN7O/qyvd6dVG27k6tMuQWyuxK5XeKA4MLigaR5437M4KZGd9KvZeyB 51o3e8CsqZvQHKMxy0QTU0V3jMIOAuwxBdhTU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=QYLIyonxRB9alqDT00q6lQKE61j6GwIHZmmYnVa6udy7htaJSX9hIfEhnCpQB3Du0I DwfxwNzHyvMNB6CDETXt7tgxUm8yQtShN9OpXEsMThaK1F53mq4gKFxakUw6TOegPn8I /wGX3IY45aiZTHw6yKQKUmhfiVxDXYKW4NT/8= MIME-Version: 1.0 Received: by 10.103.122.23 with SMTP id z23mr1531636mum.32.1257067881598; Sun, 01 Nov 2009 01:31:21 -0800 (PST) In-Reply-To: <4AEAB347.3060008@inria.fr> References: <666572260910281507q74cb1c1at157c9e3c86070c69@mail.gmail.com> <4AE8C987.8020100@starynkevitch.net> <666572260910291418p585b0aa9laadc14f6589c9772@mail.gmail.com> <4AEAB347.3060008@inria.fr> Date: Sun, 1 Nov 2009 10:31:21 +0100 Message-ID: <666572260911010131x1b3c778bgc1c87879ca96a62d@mail.gmail.com> Subject: Re: [Caml-list] Binding C libraries which use variable arguments (stdarg.h) From: Adrien To: Caml Mailing List Content-Type: text/plain; charset=ISO-8859-1 X-Spam: no; 0.00; ocaml:01 sizeof:01 bindings:01 synthesize:01 statically:01 userland:01 debian's:01 4.3:98 invoke:01 wrote:01 caml-list:01 functions:01 portability:02 argument:02 supported:02 On 30/10/2009, Xavier Leroy wrote: > [Invoking a variadic C argument from OCaml] > >>> Assuming a Linux system, you could lazily generate the glue code and >>> invoke dynamic linker on it. So the general case would be to call the >>> code generator. >> >> On linux and x86, I could probably use an array where I would move by >> sizeof(some_type). I really want portability however (I want to use >> these bindings at least on windows too). > > It might be worth investigating the "libffi" library. If I guess > correctly, it lets you synthesize calls to native C functions when the > number and type of arguments are statically unknown. I had checked libffi but its readme (from gcc-4.3) stated variable arguments weren't supported. After some time I had doubts about the relevance of the documentation (the last change mentionned was from 98) and tried anyway. It turned out it worked on linux x86_32, linux x86_64, netbsd sparc64 with 32-bit userland, debian's kfreebsd amd64 and ppc32. There's windows which I haven't checked yet though. I posted my test code on vpaste.net[1]. I think it's good enough to actually check libffi works with variable arguments but it could probably be made better. Any comment welcome. I also asked on the libffi mailing-list and was told by someone it should work almost everywhere except on really exotic architectures. That means I'll probably go the libffi way. :-) [1] http://vpaste.net/6vPst? --- Adrien Nader