www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - binding tool for C libs

reply Gour <gour atmarama.net> writes:
Hello!

We have a need to bind smaller to medium-size C library and would like
to provide more D-ish or higher-level bindings than just wrapper over C
code.

E.g. the C function looks like:=20

int swe_calc_ut ( double tjd_ut, int ipl, int iflag, double* xx, char* serr)

and we would like to e.g. use exception to handle errors instead of
checking return value and fill 'char* serr' or store the result of
6-field 'double* xx' array in some data structure etc.

Afaics, htod is more suitable for just wrapping C code, so I wonder if
there is something else you can recommend besides SWIG?


Sincerely,
Gour

--=20
The working senses are superior to dull matter; mind is higher=20
than the senses; intelligence is still higher than the mind;=20
and he [the soul] is even higher than the intelligence.

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
Jan 21 2012
parent reply Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
21.01.2012 19:24, Gour пишет:
 Hello!

 We have a need to bind smaller to medium-size C library and would like
 to provide more D-ish or higher-level bindings than just wrapper over C
 code.

 E.g. the C function looks like:

 int swe_calc_ut ( double tjd_ut, int ipl, int iflag, double* xx, char* serr)

 and we would like to e.g. use exception to handle errors instead of
 checking return value and fill 'char* serr' or store the result of
 6-field 'double* xx' array in some data structure etc.

 Afaics, htod is more suitable for just wrapping C code, so I wonder if
 there is something else you can recommend besides SWIG?


 Sincerely,
 Gour
I'm finishing a fast (I just don't see a faster way) and garbage-free CTFE-capable wrapper. But it requires IDL to be written for every C function. It already performs C-arrays (with length or end element) to D arrays conversion and UTF-8/16/32 conversion (usable for WinAPI *W functions). It will soon wrap functions like `GetCurrentDirectoryW` where it will call C function twice (if it will not guess with buffer size at first time). Sources: https://bitbucket.org/denis_sh/cwrap/src Docs (just a draft): http://deoma-cmd.ru/d/docs/src/cwrap.html For easy understanding of how does it work look at `examples/c.d` - this is a bunch of test C sources. D sources obtained from it are in `out` directory.
Jan 22 2012
next sibling parent Gour <gour atmarama.net> writes:
On Sun, 22 Jan 2012 22:03:08 +0400
Denis Shelomovskij <verylonglogin.reg gmail.com> wrote:

 I'm finishing a fast (I just don't see a faster way) and garbage-free=20
 CTFE-capable wrapper. But it requires IDL to be written for every C=20
 function.
Interesting...otoh, I found out that htod tool is out being available for Windows only. :-( Will take a look at your tool as well as at SWIG. Our Bindings needs for C are quite modest... Sincerely, Gour --=20 But a person free from all attachment and aversion and able=20 to control his senses through regulative principles of=20 freedom can obtain the complete mercy of the Lord. http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
Jan 23 2012
prev sibling parent reply Gour <gour atmarama.net> writes:
On Sun, 22 Jan 2012 22:03:08 +0400
Denis Shelomovskij <verylonglogin.reg gmail.com> wrote:

 I'm finishing a fast (I just don't see a faster way) and garbage-free=20
 CTFE-capable wrapper. But it requires IDL to be written for every C=20
 function.
I'm curious if you have read the following paper: http://www.cse.unsw.edu.au/~chak/papers/Cha99b.html describing C2Hs tool used to provide bindings for C libraries for Haskell? =46rom the abstract: "The tool obtains information about the C data type definitions and function signatures by analysing the C header files of the library. It uses this information to compute the missing details in the template of a Haskell module that implements a Haskell binding to the C library. Hooks embedded in the binding file signal where, which, and how C objects are accessed from Haskell. The Haskell code in the binding file determines Haskell type signatures and marshaling details. The approach is lightweight and does not require an extra interface description language." Sincerely, Gour --=20 The senses, the mind and the intelligence are the sitting places=20 of this lust. Through them lust covers the real knowledge of the=20 living entity and bewilders him.
Jan 23 2012
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-01-23 14:15, Gour wrote:
 On Sun, 22 Jan 2012 22:03:08 +0400
 Denis Shelomovskij<verylonglogin.reg gmail.com>  wrote:

 I'm finishing a fast (I just don't see a faster way) and garbage-free
 CTFE-capable wrapper. But it requires IDL to be written for every C
 function.
I'm curious if you have read the following paper: http://www.cse.unsw.edu.au/~chak/papers/Cha99b.html describing C2Hs tool used to provide bindings for C libraries for Haskell? From the abstract: "The tool obtains information about the C data type definitions and function signatures by analysing the C header files of the library. It uses this information to compute the missing details in the template of a Haskell module that implements a Haskell binding to the C library. Hooks embedded in the binding file signal where, which, and how C objects are accessed from Haskell. The Haskell code in the binding file determines Haskell type signatures and marshaling details. The approach is lightweight and does not require an extra interface description language."
I think the best idea is to create a tool that generates bindings using Clang. That's also what I'm slowly working on. -- /Jacob Carlborg
Jan 23 2012
parent reply Gour <gour atmarama.net> writes:
On Mon, 23 Jan 2012 15:32:10 +0100
Jacob Carlborg <doob me.com> wrote:

 I think the best idea is to create a tool that generates bindings
 using Clang. That's also what I'm slowly working on.
Could Clang be used for both C & C++ bindings? Sincerely, Gour --=20 Those persons who execute their duties according to My injunctions=20 and who follow this teaching faithfully, without envy, become free=20 from the bondage of fruitive actions. http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
Jan 23 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-01-23 15:46, Gour wrote:
 On Mon, 23 Jan 2012 15:32:10 +0100
 Jacob Carlborg<doob me.com>  wrote:

 I think the best idea is to create a tool that generates bindings
 using Clang. That's also what I'm slowly working on.
Could Clang be used for both C& C++ bindings?
Yes, Clang handles C, C++, Objective-C and Objective-C++. -- /Jacob Carlborg
Jan 23 2012
parent Gour <gour atmarama.net> writes:
On Mon, 23 Jan 2012 20:49:09 +0100
Jacob Carlborg <doob me.com> wrote:

 Yes, Clang handles C, C++, Objective-C and Objective-C++.
That would be life-saver for language-bindings developers!! Sincerely, Gour --=20 There are principles to regulate attachment and aversion pertaining to=20 the senses and their objects. One should not come under the control of=20 such attachment and aversion, because they are stumbling blocks on the=20 path of self-realization. http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
Jan 23 2012
prev sibling parent reply Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
23.01.2012 17:15, Gour пишет:
 On Sun, 22 Jan 2012 22:03:08 +0400
 Denis Shelomovskij<verylonglogin.reg gmail.com>  wrote:

 I'm finishing a fast (I just don't see a faster way) and garbage-free
 CTFE-capable wrapper. But it requires IDL to be written for every C
 function.
I'm curious if you have read the following paper: http://www.cse.unsw.edu.au/~chak/papers/Cha99b.html describing C2Hs tool used to provide bindings for C libraries for Haskell?
No. Will have a look. Anyway, looks like that isn't the problem I'm trying to solve. I don't accenting on parsing C code (it's done in Function.__ctor and is obvious). The problem is to generate fast wrapper when we already know full function signature. It's done in Function.toString and I just don't understand how does it work, I just carefully add a new functionality watching not to change existing testcase results (nearly impossible without VCS). The wrapper (the one I want to write) shouldn't leave garbage or do unnecessary memory allocations/readings/writings. One shouldn't be able to rewrite it's output to create a faster version for bottleneck, e.g.
Jan 24 2012
parent Gour <gour atmarama.net> writes:
On Wed, 25 Jan 2012 02:27:24 +0400
Denis Shelomovskij <verylonglogin.reg gmail.com> wrote:

 Anyway, looks like that isn't the problem I'm trying to solve. I
 don't accenting on parsing C code (it's done in Function.__ctor and
 is obvious). The problem is to generate fast wrapper when we already
 know full function signature.=20
OK. C2Hs wants to solve problem of writing bindings for C libraries by avoiding to learn some new IDL and provide all the tools in hosted langauge (Haskell) by offering some hooks to tailor higher-level API for bindings taking advantage of hosted language (e.g. GC instead of doing manual memory allocation). I also believe that just wrapping C library and using C API is not enough if we want to take advantage of e.g. SafeD's features. Sincerely, --=20 But those who, out of envy, disregard these teachings and do not=20 follow them are to be considered bereft of all knowledge, befooled,=20 and ruined in their endeavors for perfection. http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
Jan 24 2012