shebang launcher for D programs

Anders F Björklund afb at algonet.se
Sat Mar 24 13:32:01 PDT 2007


Andrei Alexandrescu (See Website For Email) wrote:

>> When doing more complex such "scripts", this seems to work OK:
>>
>> #!/usr/bin/env rdmd --compiler=rebuild
>>
>> i.e. just using rebuild as a drop-in replacement for gdmd/dmd
>>
>> This caches the resulting program in /tmp (or $TEMP on Windows)
>> and passes any additional arguments over to the compiled program:
...
>> Should also work OK with Tango.
> 
> This won't work if your script includes one of your own modules.

I'm not sure what you mean. If I import foo and bar, then rebuild
will include foo.o and bar.o when compiling the main D program...

Had I used gdmd, then I *would* have needed to include foo.o/bar.o
(or a library containing those), but rebuild picks dependencies up:

run.d:
======
#!/usr/bin/env rdmd -v --verbose --compiler=rebuild

import std.stdio;

import foo;
import bar;

void main(char[][] args)
{
   writefln("%s %s", foo.foo, bar.bar);
}

foo.d:
======
module foo;

char[] foo()
{
   return "Hello";
}

bar.d:
======
module bar;

char[] bar()
{
   return "World";
}

And if we run this program with full verbosity, it will show us:

$ ./run.d
running: rebuild -quiet -v ./run.d 
-of/tmp/run-501-234881028-13386453-A1689B41B25F1EA1A08CED3004FB04D6 -od/tmp/
parse     run
meta      run
import    std.stdio     (/usr/bin/../include/d/4.0.1/std/stdio.d)
import    std.c.stdio   (/usr/bin/../include/d/4.0.1/std/c/stdio.d)
import    std.stdint    (/usr/bin/../include/d/4.0.1/std/stdint.d)
import    gcc.builtins  (/usr/bin/../include/d/4.0.1/gcc/builtins.d)
import    std.c.stddef  (/usr/bin/../include/d/4.0.1/std/c/stddef.d)
import    std.c.stdarg  (/usr/bin/../include/d/4.0.1/std/c/stdarg.d)
import    gcc.config 
(/usr/bin/../include/d/4.0.1/i686-apple-darwin8/gcc/config.d)
import    gcc.configext (/usr/bin/../include/d/4.0.1/gcc/configext.d)
import    std.c.darwin.ldblcompat 
(/usr/bin/../include/d/4.0.1/std/c/darwin/ldblcompat.d)
import    std.format    (/usr/bin/../include/d/4.0.1/std/format.d)
import    std.stdarg    (/usr/bin/../include/d/4.0.1/std/stdarg.d)
import    std.utf       (/usr/bin/../include/d/4.0.1/std/utf.d)
import    std.c.stdlib  (/usr/bin/../include/d/4.0.1/std/c/stdlib.d)
import    std.c.string  (/usr/bin/../include/d/4.0.1/std/c/string.d)
import    std.string    (/usr/bin/../include/d/4.0.1/std/string.d)
import    std.uni       (/usr/bin/../include/d/4.0.1/std/uni.d)
import    std.array     (/usr/bin/../include/d/4.0.1/std/array.d)
import    std.ctype     (/usr/bin/../include/d/4.0.1/std/ctype.d)
import    foo   (foo.d)
import    bar   (bar.d)
meta      stdio
meta      stdio
meta      stdint
meta      builtins
meta      stddef
meta      stdarg
meta      config
meta      configext
meta      ldblcompat
meta      format
meta      stdarg
meta      utf
meta      stdlib
meta      string
meta      string
meta      uni
meta      array
meta      ctype
meta      foo
meta      bar
code      run
code      stdio
code      stdio
code      stdint
code      builtins
code      stddef
code      stdarg
code      config
code      configext
code      ldblcompat
code      format
code      stdarg
code      utf
code      stdlib
code      string
code      string
code      uni
code      array
code      ctype
code      foo
code      bar
compile   gdmd -version=Posix -c ./run.d foo.d bar.d  -quiet  -od/tmp/
link      gdmd /tmp/run.o /tmp/foo.o /tmp/bar.o 
-of/tmp/run-501-234881028-13386453-A1689B41B25F1EA1A08CED3004FB04D6
running: /tmp/run-501-234881028-13386453-A1689B41B25F1EA1A08CED3004FB04D6
Hello World

I'm not sure it caches the object files (like C/C++'s "ccache"* does),
but it does cache the resulting program which works fine for "scripts".
If you have the same module name in different levels, you can use the
-oq flag that writes object files using fully-qualified module names.

It also picks up needed system modules, such as the one Tango uses...
(but above it decided correctly that all std modules were in gphobos)
Think you can get it to pick up external libraries using the library
build/lib pragmas, or by adding the linker flags to the shebang line.


If I want to change between dmd or gdc, or between Phobos and Tango,
then all I need is to change the "default" setting of rebuild.conf/.

--anders

* See http://ccache.samba.org/



More information about the Digitalmars-d mailing list