On Fri, Jul 12, 2013 at 2:54 PM, Walter Bright <newshound2@digitalmars.com> wrote:
On 7/12/2013 1:42 PM, Tofu Ninja wrote:
So I had an idea recently, wouldn't it be cool to have the ability to call an
executable at compile time and capture its output. Something like the string
imports but instead of opening and reading a text file, it run an executable,
waits for it to finish, and grabs its output.

It would get really cool if you could pass this executable some args and then
mix in its out put into your own code. It could be used similarly to how CTFE
are used but with out the overhead of trying to compile that function and what
not and with out the limitations on what it can do.

I could imagine all sorts of things that would be possible with this that is
currently not.

Not sure if this is something that could be implemented easily, but seems like
something that could be done and something that would be really cool.

This is actually done in the makefile that builds DMD. A program (optabgen) is compiled and then run. Optabgen's output is several .c files which are then compiled into DMD's binary. The programs impcnvgen and idgen do similar things.

It is a very powerful technique.

I think the OP was refering to something different:
ability to call an arbitrary executable / shell command during compile time of a D function, whereas optabgen is during compiling dmd itself:

what we want to have is this:

----
import("some_source_file.txt"); //currently this is our only interaction with outside world during compile time
import std.process; //or std.ctfeprocess ? 

string getStringAtCompileTime(string command){
  if(!__ctfe) assert(0);
  //below is currently impossible, but would be great to have
  Tuple!(int,string,string) result=systemCaptureStdinStdout(command); //likewise without redirecting stdin/stdout
  assert(!result[0]);
  return result[1];
}

void main(){
  enum host = getStringAtCompileTime("env | grep HOST");
}
----