View mode: basic / threaded / horizontal-split · Log in · Help
January 21, 2012
binding tool for C libs
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

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

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
January 22, 2012
Re: binding tool for C libs
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.
January 23, 2012
Re: binding tool for C libs
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.

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

-- 
But a person free from all attachment and aversion and able 
to control his senses through regulative principles of 
freedom can obtain the complete mercy of the Lord.

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
January 23, 2012
Re: binding tool for C libs
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."



Sincerely,
Gour

-- 
The senses, the mind and the intelligence are the sitting places 
of this lust. Through them lust covers the real knowledge of the 
living entity and bewilders him.
January 23, 2012
Re: binding tool for C libs
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
January 23, 2012
Re: binding tool for C libs
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

-- 
Those persons who execute their duties according to My injunctions 
and who follow this teaching faithfully, without envy, become free 
from the bondage of fruitive actions.

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
January 23, 2012
Re: binding tool for C libs
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
January 23, 2012
Re: binding tool for C libs
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


-- 
There are principles to regulate attachment and aversion pertaining to 
the senses and their objects. One should not come under the control of 
such attachment and aversion, because they are stumbling blocks on the 
path of self-realization.

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
January 24, 2012
Re: binding tool for C libs
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.
January 25, 2012
Re: binding tool for C libs
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. 

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,

-- 
But those who, out of envy, disregard these teachings and do not 
follow them are to be considered bereft of all knowledge, befooled, 
and ruined in their endeavors for perfection.

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810
Top | Discussion index | About this forum | D home