www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Some guidance on writing a Deimos C library interface

reply Jens Mueller <jens.k.mueller gmx.de> writes:
Hi,

there is some documentation on writing a Deimos interface.
E.g.
http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP12
http://dlang.org/interfaceToC.html

I'd like to get a list of rules that one has to follow.

First you have to convert the name of the header file to a valid D
module name. Therefore, I found/came up with the following rules:
* a slash (/) has to be replaced by a dot (.)
* a dash (-) has to be replaced by a dot (.)
* a D keyword gets a underscore (_) appended

Maybe someone can commented on them. I suppose there are even more.
These are just the ones I encountered.

Some questions on module names I could not find a definite answer for:
1. The D page says they should be lower case. Should Deimos module names
   also be made lower case?
2. Should all interfaces be put in a common package, say deimos?

Translating the header file itself is pretty straightforward following
http://dlang.org/interfaceToC.html. Though some cases are not covered:
* E.g. use core.stdc.stdint for standard C types.
* const T* should be replaced with const(T)*
* How to deal with macros?

Can we write a recipe like guide for writing a Deimos interface?

Jens
Jul 13 2012
next sibling parent reply "dnewbie" <run3 myopera.com> writes:
Hi Jens.

 1. The D page says they should be lower case. Should Deimos 
 module names
    also be made lower case?
Yes.
 2. Should all interfaces be put in a common package, say deimos?
Yes.
 * const T* should be replaced with const(T)*
Yes.
 * How to deal with macros?
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=146873 Also please check this guide http://www.gamedev.net/blog/1140/entry-2254003-binding-d-to-c/
Jul 13 2012
next sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
dnewbie wrote:
2. Should all interfaces be put in a common package, say deimos?
Yes.
Why is this? What is the rational behind it? Because some Deimos projects seem not to follow it.
* How to deal with macros?
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=146873 Also please check this guide http://www.gamedev.net/blog/1140/entry-2254003-binding-d-to-c/
Most of this is covered by http://dlang.org/interfaceToC.html, isn't it? Jens
Jul 14 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, July 15, 2012 08:14:22 Jens Mueller wrote:
 dnewbie wrote:
2. Should all interfaces be put in a common package, say deimos?
Yes.
Why is this? What is the rational behind it? Because some Deimos projects seem not to follow it.
It thought that it was decided that that it was _bad_ idea to have any kind of deimos package when it was discussed previously, but I don't remember what all was discussed about it or when. Presumably, it's in the newsgroup archives somewhere though. - jonathan M Davis
Jul 15 2012
prev sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Jonathan M Davis wrote:
 On Sunday, July 15, 2012 08:14:22 Jens Mueller wrote:
 dnewbie wrote:
2. Should all interfaces be put in a common package, say deimos?
Yes.
Why is this? What is the rational behind it? Because some Deimos projects seem not to follow it.
It thought that it was decided that that it was _bad_ idea to have any kind of deimos package when it was discussed previously, but I don't remember what all was discussed about it or when. Presumably, it's in the newsgroup archives somewhere though.
I found http://www.digitalmars.com/d/archives/digitalmars/D/Deimos_Consistent_structure_149602.html#N149602 But there is no consensus. Jens
Jul 15 2012
prev sibling next sibling parent reply Johannes Pfau <nospam example.com> writes:
Am Sat, 14 Jul 2012 05:24:50 +0200
schrieb Jens Mueller <jens.k.mueller gmx.de>:

 * const T* should be replaced with const(T)*
When it's a function parameter you could even make it const(T*): void hello(const char *name); --> extern(C) void hello(const(char*) name); as the pointer itself is passed by value, there's no way it could be modified by hello. DMD should be able to figure that out and treat const(char)* exactly the same, but last time I checked it didn't.
 * How to deal with macros?
There are different solutions. Some people prefer to make those templates, so the binding can be used as a .di file and doesn't need to be compiled. Others just use functions and hope the optimizer will inline it ;-)
 
 Can we write a recipe like guide for writing a Deimos interface?
 
 Jens
Jul 13 2012
parent Jens Mueller <jens.k.mueller gmx.de> writes:
Johannes Pfau wrote:
 Am Sat, 14 Jul 2012 05:24:50 +0200
 schrieb Jens Mueller <jens.k.mueller gmx.de>:
 
 * const T* should be replaced with const(T)*
When it's a function parameter you could even make it const(T*): void hello(const char *name); --> extern(C) void hello(const(char*) name); as the pointer itself is passed by value, there's no way it could be modified by hello. DMD should be able to figure that out and treat const(char)* exactly the same, but last time I checked it didn't.
Very true. But just to be on the safe side I think replacing it with const(T)* is better.
 * How to deal with macros?
There are different solutions. Some people prefer to make those templates, so the binding can be used as a .di file and doesn't need to be compiled. Others just use functions and hope the optimizer will inline it ;-)
I see. Jens
Jul 15 2012
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-07-14 05:24, Jens Mueller wrote:
 Hi,

 there is some documentation on writing a Deimos interface.
 E.g.
 http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP12
 http://dlang.org/interfaceToC.html

 I'd like to get a list of rules that one has to follow.

 First you have to convert the name of the header file to a valid D
 module name. Therefore, I found/came up with the following rules:
 * a slash (/) has to be replaced by a dot (.)
 * a dash (-) has to be replaced by a dot (.)
 * a D keyword gets a underscore (_) appended

 Maybe someone can commented on them. I suppose there are even more.
 These are just the ones I encountered.

 Some questions on module names I could not find a definite answer for:
 1. The D page says they should be lower case. Should Deimos module names
     also be made lower case?
 2. Should all interfaces be put in a common package, say deimos?

 Translating the header file itself is pretty straightforward following
 http://dlang.org/interfaceToC.html. Though some cases are not covered:
 * E.g. use core.stdc.stdint for standard C types.
I would say that when a C typedef has a corresponding type in D, use that instead of an alias. For example: int8_t -> use byte int32_t -> use int But one need to make sure that the typedef resolves to the same D type on all platforms.
 * const T* should be replaced with const(T)*
 * How to deal with macros?

 Can we write a recipe like guide for writing a Deimos interface?
You can have a look at dstep for automatically translate C/Objective-C headers: https://github.com/jacob-carlborg/dstep -- /Jacob Carlborg
Jul 14 2012
parent reply Jens Mueller <jens.k.mueller gmx.de> writes:
Jacob Carlborg wrote:
 On 2012-07-14 05:24, Jens Mueller wrote:
Hi,

there is some documentation on writing a Deimos interface.
E.g.
http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP12
http://dlang.org/interfaceToC.html

I'd like to get a list of rules that one has to follow.

First you have to convert the name of the header file to a valid D
module name. Therefore, I found/came up with the following rules:
* a slash (/) has to be replaced by a dot (.)
* a dash (-) has to be replaced by a dot (.)
* a D keyword gets a underscore (_) appended

Maybe someone can commented on them. I suppose there are even more.
These are just the ones I encountered.

Some questions on module names I could not find a definite answer for:
1. The D page says they should be lower case. Should Deimos module names
    also be made lower case?
2. Should all interfaces be put in a common package, say deimos?

Translating the header file itself is pretty straightforward following
http://dlang.org/interfaceToC.html. Though some cases are not covered:
* E.g. use core.stdc.stdint for standard C types.
I would say that when a C typedef has a corresponding type in D, use that instead of an alias. For example: int8_t -> use byte int32_t -> use int But one need to make sure that the typedef resolves to the same D type on all platforms.
By using core.stdc.stdint I'm safe, right? I mean int8_t is just an alias in core.stdc.stdint. I'd like to change the header file as little as possible.
* const T* should be replaced with const(T)*
* How to deal with macros?

Can we write a recipe like guide for writing a Deimos interface?
You can have a look at dstep for automatically translate C/Objective-C headers: https://github.com/jacob-carlborg/dstep
Usually, the changes are straightforward. It's just these little things where is no clear rule that cost time. Jens
Jul 15 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-07-16 06:10, Jens Mueller wrote:

 By using core.stdc.stdint I'm safe, right? I mean int8_t is just an
 alias in core.stdc.stdint. I'd like to change the header file as little
 as possible.
Sure it's safe. Fair enough but I think it's pointless. -- /Jacob Carlborg
Jul 16 2012