DStep - Bindings Generator 0.0.1

Jonathan Andrew jonfandrew at gmail.com
Sun Jul 8 14:22:36 PDT 2012


On Saturday, 7 July 2012 at 14:47:49 UTC, Jacob Carlborg wrote:
> DStep is a tool for translating C and Objective-C headers to D 
> modules. It uses libclang for lexing/parsing and AST traversal. 
> This means it handles everything that Clang itself can handle, 
> although this doesn't mean it will correctly translate 
> everything.
>
> I would consider this release alpha or beta. I'm releasing this 
> now in hope I get some feedback on what language features the 
> tool can't handle.
>
> The tool is available at github:
> https://github.com/jacob-carlborg/dstep
>
> Binaries are available for Mac OS X and Ubuntu 11.10 32bit:
> https://github.com/jacob-carlborg/dstep/downloads
>
> Unfortunately I haven't been able to successfully compile it on 
> Windows due to Optlink not cooperating. I'll most likely 
> provide Linux binaries with better compatibility later.
>
> Build instructions are available at github.
>
> Usage:
>
> dstep <input-file.h> -o output_file.d
>
> For Objective-C
>
> dstep <input-file.h> -o output_file.d -ObjC
>
> Tests:
>
> DStep uses Cucumber and Aruba (Ruby tools) to run its tests. It 
> will basically run the tool on all *.h files in the 
> "test_files" directory and compare the results to the 
> corresponding *.d files.
>
> Known issues/missing functionality:
>
> * Multiple input files
> * Framework as input file
> * Add module declaration
> * Option for specifying before and after code
> * Option for specifying package
> * Windows support
>
> C:
>     * Self includes
>     * Out of order typedefs of structs
>     * Bitfields
>     * Non-standard extensions
>     * Preprocessor
>     * Arrays with no size marked as "extern".
>
> Objective-C:
>     * Protocols
>     * Properties
>     * Blocks
>     * Categories
>     * Actions
>     * Outlets
>     * Selectors
>
> This is basically what's on the todo list:
>
> https://raw.github.com/jacob-carlborg/dstep/master/todo.taskpaper
>
> There's no point in reporting issues which are listed above.

Jacob,

   I just used your tool to try and convert the GTK 3.0 include 
files into D stubs, and wanted to say that it worked pretty darn 
well. As a matter of practice, the single file limitation wasn't 
too much of a problem - I hacked together a script to just go 
through all the .h files and put the outputs in the right place. 
D has become my new favorite scripting language, BTW!

For anybody who cares - here's how I went about it. This is 
probably the most ignorant and least efficient method possible, 
but it worked for me to at least display a little window (yay.).

#!/usr/bin/rdmd

import std.stdio;
import std.string;
import std.process;

int main(string[] args)
{	
	foreach(string word; args[1..$])
	{
string cmd = format("dstep /home/jon/devel/gtk-include/%s -o 
/home/jon/devel/gtk-include/d/%s.d -v -I./ -I./glib-2.0/ -I./gdk/ 
-I./gdk-pixbuf/ -I/usr/lib/gcc/i686-linux-gnu/4.6/include/ 
-I/usr/include/pango-1.0/ -I/usr/include/cairo/ 
-I/usr/include/atk-1.0/", word, chomp(word,".h"));
		writefln("%s",cmd);
		system(cmd);
	}
	return 0;
}

Run with ./convert.d folder/*.h

The only disadvantage to the single-file limitation is that in 
the case of GTK at least, it has preprocessor directives to keep 
you from just #include-ing the single file you want to convert, 
so I just used sed to strip out all the #error directives that 
come up and force it to do my bidding. I understand DStep doesn't 
deal with preprocessor yet, but as far as the CLang front-end it 
uses goes, it might be helpful to find a way to turn off #error-s.

sed -i 's/#error/\/\//g' *.h

The next step was to rename all the D reserved words that GTK 
used as function arguments - in, out, function, and align are the 
only ones I can think of off the top of my head. Easy fix for the 
user (by no means am I complaining), but if you want to 
streamline the conversion, automatically renaming these kinds of 
arguments might be a helpful option.

Then, renaming all the duplicate empty struct{} entries in some 
of the files. You already know about this, but it was probably 
the most time-consuming part of the process for converting GTK, 
at least. I couldn't think of an easy way to automate this on my 
end, because some of the empty structs were necessary to get it 
to compile.

Finally, putting import statements in all the .d files after I 
was done. Still a long way to go on this (500 files).

Sorry for the long post, this is probably obvious stuff to 
everybody else, but I was really impressed with DStep - thank you 
for creating it!

-Jon


More information about the Digitalmars-d-announce mailing list