www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - libffi-d: D binding to libffi

reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
Hi folks,

Since I needed a way to call arbitrary C functions dynamically (while 
knowing their pointer + signature), I decided to write a binding to 
libffi for D.

https://github.com/lycus/libffi-d

There is heavy focus on simplicity: It is composed of 3 enums (FFIType, 
FFIStatus, FFIInterface), one alias (FFIFunction), and one function 
(ffiCall). The API is fairly self-describing, so I haven't really spent 
a whole lot of time on docs. For examples, see the tests sub-directory 
(please also read 'info libffi').

Known limitations:

* No access to the closure API.
* No access to the 'raw' API (which is undocumented in libffi anyway).

Known issues:

* Detection of soft float ABIs is not currently done (due to limitations 
in most D compilers).
* ABI overriding for some rare Unix ABIs is not currently possible.
* Currently no good way to actually use the library on Windows. Anyone 
who knows their stuff about DMD and DLLs on Windows, please do 
contribute. ;)

If you run into any issues, please throw a test case at the GitHub issue 
tracker and I'll look into it. Enjoy!

- Alex
Dec 08 2011
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
There's a few DLL's floating around for libffi, however none of them
seem to have the symbol "ffi_prep_cif_var" which seems to be needed
for variadic functions.

I can version variadic support out and compile your bindings, the
tests will work fine on win32.
I've also tried compiling libffi on my own but had no success.

I can make a pull with the import lib, the DLL (it's only 99KB), and
the versioned out sections, if you'd like that.
Dec 09 2011
parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
Hi Andrej,

Sorry for the late response (for some reason, my news reader had marked 
this thread as read).

 There's a few DLL's floating around for libffi, however none of them
 seem to have the symbol "ffi_prep_cif_var" which seems to be needed
 for variadic functions.

 I can version variadic support out and compile your bindings, the
 tests will work fine on win32.
 I've also tried compiling libffi on my own but had no success.
ffi_prep_cif_var doesn't seem to be available in any Windows builds (Cygwin, MinGW, or otherwise) of libffi for whatever reason. I'll version it out on Windows for now, as you suggested. I haven't managed to get a MinGW build of libffi working so far, but I did manage to get one working under Cygwin. Build libffi like this: $ ./configure --enable-static=yes --enable-shared=no $ make Now you should have i686-pc-cygwin/.libs/libffi.a. You'll also need your Cygwin installation's libgcc.a: $ cd i686-pc-cygwin/.libs $ ar -r libffi.a /lib/gcc/i686-pc-cygwin/4.5.3/libgcc.a Now you need to convert the .a to an OMF .lib. You'll need objconv.zip from this page: http://www.agner.org/optimize/ $ objconv -fomf -nu libffi.a libffi.lib libffi.lib should now be linkable with DMD. (I long for the day DMD doesn't use OMF.)
 I can make a pull with the import lib, the DLL (it's only 99KB), and
 the versioned out sections, if you'd like that.
I prefer keeping the binaries out of Git for various portability, licensing, etc issues. That being said, I think we could upload .libs/.dlls as downloads on the GitHub page. The question is whether we should put .libs or .dlls there? Personally, I think .libs are easier to work with when using DMD. Thanks for the input! - Alex
Dec 15 2011
parent =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 15-12-2011 09:14, Alex Rønne Petersen wrote:
 Hi Andrej,

 Sorry for the late response (for some reason, my news reader had marked
 this thread as read).

  > There's a few DLL's floating around for libffi, however none of them
  > seem to have the symbol "ffi_prep_cif_var" which seems to be needed
  > for variadic functions.
  >
  > I can version variadic support out and compile your bindings, the
  > tests will work fine on win32.
  > I've also tried compiling libffi on my own but had no success.

 ffi_prep_cif_var doesn't seem to be available in any Windows builds
 (Cygwin, MinGW, or otherwise) of libffi for whatever reason. I'll
 version it out on Windows for now, as you suggested.

 I haven't managed to get a MinGW build of libffi working so far, but I
 did manage to get one working under Cygwin. Build libffi like this:

 $ ./configure --enable-static=yes --enable-shared=no
 $ make

 Now you should have i686-pc-cygwin/.libs/libffi.a.

 You'll also need your Cygwin installation's libgcc.a:

 $ cd i686-pc-cygwin/.libs
 $ ar -r libffi.a /lib/gcc/i686-pc-cygwin/4.5.3/libgcc.a

 Now you need to convert the .a to an OMF .lib. You'll need objconv.zip
 from this page: http://www.agner.org/optimize/

 $ objconv -fomf -nu libffi.a libffi.lib

 libffi.lib should now be linkable with DMD.

 (I long for the day DMD doesn't use OMF.)

  > I can make a pull with the import lib, the DLL (it's only 99KB), and
  > the versioned out sections, if you'd like that.

 I prefer keeping the binaries out of Git for various portability,
 licensing, etc issues. That being said, I think we could upload
 .libs/.dlls as downloads on the GitHub page. The question is whether we
 should put .libs or .dlls there? Personally, I think .libs are easier to
 work with when using DMD.

 Thanks for the input!

 - Alex
Correction: It seems as though that function has disappeared on my Linux system! It has most probably been deprecated entirely, so I have just removed it from libffi-d. - Alex
Dec 15 2011