www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Chew through those header files with "headerchew"!

reply Larry Hastings <larry hastings.org> writes:
I'm not a D programmer, or at least not yet.  But I've been reading 
about it all week.  I saw this comment in the Derelict forum on dsource.org:
//
	libPNG - forget it. I really wanted to include this, but
	there are just so many conditional compilation oprtions
	that there's no way I can get an accurate representation
	of the headers in D.
//

I thought about that for a while, and inspiration struck.  Y'see, you 
can actually get the C compiler to do most of the work here. 
Specifically, the C preprocessor.

I emailed the Derelict guy with a suggestion along those lines.  But I 
figure source code speaks louder than words.  So I spent a couple of 
hours this morning and hacked together "headerchew":
   http://www.midwinter.com/~lch/programming/headerchew/headerchew.zip
It's more a proof-of-concept than anything else, but perhaps it'll help 
in the good fight to make Windows libraries callable from D.

To use it, first edit headerchew.cpp.  Add your header files, and list 
all the constants you want converted over.  (Instructions on how and 
where are in headerchew.cpp.)

Next you run "nmake".  Nmake will run three commands:
	cl /P headerchew.cpp
	cl headerchew.cpp
	headerchew.exe headerchew.i

The first one is the really interesting one.  /P is a switch for CL.EXE 
that says "don't actually compile the file, just run it through the C 
preprocessor and write the output to <<filename>>.i".  That will boil 
out *all* of the #ifs and #defines, and leave you with straight C 
typedefs and structs and prototypes.

The second command compiles headerchew, gosh.  The *third* command is 
interesting again.  Headerchew *parses its own pre-processed source*, 
looking for a couple special tokens which I have artfully disguised as 
static data declarations.  It uses them to decide when to redirect the 
input to a file, and when to spew out the list of constants you constructed.

Right now headerchew does little processing itself; I did throw in, as a 
proof-of concept, changing "typedef" to "alias".  I realize this is 
probably not what you want.  But it shows you how you could massage the 
data as it passed by.


Anyway, I submit it for your approval.  Perhaps you'll collectively find 
something useful to do with it, perhaps not.  I'd be interested to hear 
about it if you did.

One last note: I apologize for the dirty, awful source code.  I'm sure 
I'll be punished for such willfull preprocessor abuse once I pass beyond 
this veil of tears.


Cheers,

--
/larry/
Apr 15 2005
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Larry Hastings" <larry hastings.org> wrote in message 
news:d3pe7n$1b4s$1 digitaldaemon.com...

The D manual actually mentions running the old header file through the C 
compiler without compiling it, but it pretty much says "now, you're on your 
own, go ahead and convert the rest by hand."  It would be very nice to have 
an _automated_ C->D header convertor :) 
Apr 16 2005