On Fri, Jul 12, 2013 at 2:54 PM, Walter Bright <span dir="ltr"><<a href="mailto:newshound2@digitalmars.com" target="_blank">newshound2@digitalmars.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On 7/12/2013 1:42 PM, Tofu Ninja wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
So I had an idea recently, wouldn't it be cool to have the ability to call an<br>
executable at compile time and capture its output. Something like the string<br>
imports but instead of opening and reading a text file, it run an executable,<br>
waits for it to finish, and grabs its output.<br>
<br>
It would get really cool if you could pass this executable some args and then<br>
mix in its out put into your own code. It could be used similarly to how CTFE<br>
are used but with out the overhead of trying to compile that function and what<br>
not and with out the limitations on what it can do.<br>
<br>
I could imagine all sorts of things that would be possible with this that is<br>
currently not.<br>
<br>
Not sure if this is something that could be implemented easily, but seems like<br>
something that could be done and something that would be really cool.<br>
</blockquote>
<br></div></div>
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.<br>

<br>
It is a very powerful technique.<br>
</blockquote></div><br><div>I think the OP was refering to something different:</div><div>ability to call an arbitrary executable / shell command during compile time of a D function, whereas optabgen is during compiling dmd itself:</div>
<div><br></div><div>what we want to have is this:</div><div><br></div><div>----</div><div>import("some_source_file.txt"); //currently this is our only interaction with outside world during compile time</div><div>
import std.process; //or std.ctfeprocess ? </div><div><br></div><div>string getStringAtCompileTime(string command){</div><div>  if(!__ctfe) assert(0);</div><div>  //below is currently impossible, but would be great to have</div>
<div>  Tuple!(int,string,string) result=systemCaptureStdinStdout(command); //likewise without redirecting stdin/stdout</div><div>  assert(!result[0]);</div><div>  return result[1];</div><div>}</div><div><br></div><div>void main(){</div>
<div>  enum host = getStringAtCompileTime("env | grep HOST");</div><div>}</div><div><div>----</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>