Invoking the compiler during runtime

cy dlang at
Wed Aug 5 06:02:58 UTC 2020

D's compile-time-execution is fantastic, but there are some times 
when I'd like to examine the generated code, or produce code that 
needs to pass through earlier phases before CTFE, or do AST 
stuff. Sometimes I simply don't want to generate the code with 
every compilation, so saving the generated code in a file some 
would be really neat, if I could then invoke the compiler during 
runtime to build it. Plus backtraces wouldn't just be 

I'm a big supporter of compiling from source and not using third 
party binaries, so I don't mind designing software that only 
works with runtime access to a D compiler. But I'm not sure how 
best to generate the code, or to invoke the compiler, or how to 
use what it compiles. In C I write a utility program in awful C 
to print out C code, have a cmake custom command run that awful C 
as needed, and assume the existence of that generated source in 
my main program.

So is that the best way to do it? Have a side program that writes 
D source to a file, and then the main program simply imports the 
file? Or is there some AST syntax I should be generating instead? 
Some way to import the compiler itself, instead of calling it in 
a subprocess? Is there a way in dub to specify that you run a D 
program contained in X.d during the build process, to build Y.d, 
that Z.d imports?

More information about the Digitalmars-d-learn mailing list