www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - getting __DIR__ and __TIME__ of compilation?

reply "Ravn" <ravndust gmail.com> writes:
Hi, I'm trying get the directory path and time at which the
compilation was made (not when the program is run), something
similar like this example in Haxe
http://haxe.org/manual/macros#macro-functions

Is it possible to do so in D?
Something like __DIR__ and __DATE__ or __TIME__ in D traits maybe?
( http://dlang.org/traits.html#specialkeywords )

Thanks in advance
-Ravn-
Dec 26 2013
parent reply "nazriel" <spam dzfl.pl> writes:
On Friday, 27 December 2013 at 06:39:54 UTC, Ravn wrote:
 Hi, I'm trying get the directory path and time at which the
 compilation was made (not when the program is run), something
 similar like this example in Haxe
 http://haxe.org/manual/macros#macro-functions

 Is it possible to do so in D?
 Something like __DIR__ and __DATE__ or __TIME__ in D traits 
 maybe?
 ( http://dlang.org/traits.html#specialkeywords )

 Thanks in advance
 -Ravn-
Hello. Maybe this will work for you? http://dpaste.dzfl.pl/3ad4aa3a --- void main() { import std.path: dirName; pragma(msg, dirName(__FILE__) ~ " " ~ __DATE__ ~ " " ~ __TIME__); } ---
Dec 26 2013
parent reply "Ravn" <ravndust gmail.com> writes:
On Friday, 27 December 2013 at 07:31:19 UTC, nazriel wrote:
 On Friday, 27 December 2013 at 06:39:54 UTC, Ravn wrote:
 Hi, I'm trying get the directory path and time at which the
 compilation was made (not when the program is run), something
 similar like this example in Haxe
 http://haxe.org/manual/macros#macro-functions

 Is it possible to do so in D?
 Something like __DIR__ and __DATE__ or __TIME__ in D traits 
 maybe?
 ( http://dlang.org/traits.html#specialkeywords )

 Thanks in advance
 -Ravn-
Hello. Maybe this will work for you? http://dpaste.dzfl.pl/3ad4aa3a --- void main() { import std.path: dirName; pragma(msg, dirName(__FILE__) ~ " " ~ __DATE__ ~ " " ~ __TIME__); } ---
Hi, thanks for the answer, I've tried your solution, but I need to store the path and time to be used for various stuffs later, not just during errors, maybe something closer to this, Example: file located at D:\misc\hello.d if there's a code like this // inside hello.d void main() { string path = getAbsolutePath(); string date_and_time = getDateAndTime(); } I would like the compiler to specifically process the return value of getAbsolutePath() as well as getDateAndTime during compile time. So that whenever I run the program, its as if the code above is written and compiled as the following code, // inside hello.d void main() { string path = "D:\misc\"; string date_and_time = "Dec 2x 20xx hh:mm:ss"; } Would that be possible? -Ravn-
Dec 27 2013
parent reply "Lemonfiend" <lemon fie.nd> writes:
On Friday, 27 December 2013 at 08:57:02 UTC, Ravn wrote:
 On Friday, 27 December 2013 at 07:31:19 UTC, nazriel wrote:
 On Friday, 27 December 2013 at 06:39:54 UTC, Ravn wrote:
 Hi, I'm trying get the directory path and time at which the
 compilation was made (not when the program is run), something
 similar like this example in Haxe
 http://haxe.org/manual/macros#macro-functions

 Is it possible to do so in D?
 Something like __DIR__ and __DATE__ or __TIME__ in D traits 
 maybe?
 ( http://dlang.org/traits.html#specialkeywords )

 Thanks in advance
 -Ravn-
Hello. Maybe this will work for you? http://dpaste.dzfl.pl/3ad4aa3a --- void main() { import std.path: dirName; pragma(msg, dirName(__FILE__) ~ " " ~ __DATE__ ~ " " ~ __TIME__); } ---
Hi, thanks for the answer, I've tried your solution, but I need to store the path and time to be used for various stuffs later, not just during errors, maybe something closer to this, Example: file located at D:\misc\hello.d if there's a code like this // inside hello.d void main() { string path = getAbsolutePath(); string date_and_time = getDateAndTime(); } I would like the compiler to specifically process the return value of getAbsolutePath() as well as getDateAndTime during compile time. So that whenever I run the program, its as if the code above is written and compiled as the following code, // inside hello.d void main() { string path = "D:\misc\"; string date_and_time = "Dec 2x 20xx hh:mm:ss"; } Would that be possible? -Ravn-
module main; import std.stdio; enum file = __FILE__; enum time = __TIME__; void main() { writeln(file); writeln(time); } This works for me.
Dec 27 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/27/2013 03:51 AM, Lemonfiend wrote:

 module main;

 import std.stdio;

 enum file = __FILE__;
 enum time = __TIME__;

 void main()
 {
      writeln(file);
      writeln(time);
 }

 This works for me.
And the reason is D's CTFE: Anything that needs to be and can to be evaluated at compile time is evaluated at compile time. Since manifest constants and the initial values of static constants must be known at compile time both enum and 'static const' will work. However, __FILE__ happens to be the current source file that is being compiled but I think the OP wants the current compilation directory. Being a C library file, getcwd() does not work at compile time: import std.stdio; void main() { import std.path: dirName; enum compiledFile = __FILE__; writeln(compiledFile); static const compileTime = __DATE__ ~ " " ~ __TIME__; writeln(compileTime); /* import std.file: getcwd; static const compilationDir = getcwd(); writeln(compilationDir); Error: getcwd cannot be interpreted at compile time, because it has no available source code */ } Ali
Dec 27 2013
parent reply "Ravn" <ravndust gmail.com> writes:
On Friday, 27 December 2013 at 11:56:08 UTC, Ali Çehreli wrote:
 However, __FILE__ happens to be the current source file that is 
 being compiled but I think the OP wants the current compilation 
 directory. Being a C library file, getcwd() does not work at 
 compile time:

 import std.stdio;

 void main()
 {
     import std.path: dirName;

     enum compiledFile = __FILE__;
     writeln(compiledFile);

     static const compileTime = __DATE__ ~ " " ~ __TIME__;
     writeln(compileTime);

     /*
     import std.file: getcwd;
     static const compilationDir = getcwd();
     writeln(compilationDir);

     Error: getcwd cannot be interpreted at compile time, 
 because it has no available source code
     */
 }

 Ali
Yes, just like what Ali said above, __FILE__, __DATE__ and __TIME__ do work for their respective usages, but I'm also looking for a way to get the current compilation directory during compile time, and getcwd() doesn't seem to be working. Isn't there something like __DIR__ or __PATH__ that I can use to get that information? -Ravn-
Dec 27 2013
next sibling parent reply Jacob Carlborg <doob me.com> writes:
 Yes, just like what Ali said above,

 __FILE__, __DATE__ and __TIME__ do work for their respective usages,
 but I'm also looking for a way to get the current compilation directory
 during compile time, and getcwd() doesn't seem to be working.

 Isn't there something like __DIR__ or __PATH__ that I can use to get
 that information?
You can use this ugly hack: Create a shell script with the following content. #!/bin/bash echo `pwd` > dir.txt dmd main.d -J. And the D source code: module main; enum compilePath = import("dir.txt"); pragma(msg, compilePath); Run the shell script to compile the D code. -- /Jacob Carlborg
Dec 27 2013
parent "Ravn" <ravndust gmail.com> writes:
On Friday, 27 December 2013 at 15:15:31 UTC, Jacob Carlborg wrote:
 You can use this ugly hack:

 Create a shell script with the following content.

 #!/bin/bash
 echo `pwd` > dir.txt
 dmd main.d -J.

 And the D source code:

 module main;

 enum compilePath = import("dir.txt");
 pragma(msg, compilePath);

 Run the shell script to compile the D code.
I end up using something similar to your 'hack', made a tiny wrapper in D that accepts the parameters that is supposed to go to dmd, get the absolute path of the compiled sourcecode using std.path.absolutePath(), save it inside a new <stuffs>.d file, the wrapper then calls the dmd compiler with <stuffs>.d added to the list of compiled files. Bit of a stretch, but it'll do for now. -Ravn-
Dec 27 2013
prev sibling parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Fri, 27 Dec 2013 12:43:15 +0000
schrieb "Ravn" <ravndust gmail.com>:

 On Friday, 27 December 2013 at 11:56:08 UTC, Ali =C3=87ehreli wrote:
 However, __FILE__ happens to be the current source file that is=20
 being compiled but I think the OP wants the current compilation=20
 directory. Being a C library file, getcwd() does not work at=20
 compile time:

 import std.stdio;

 void main()
 {
     import std.path: dirName;

     enum compiledFile =3D __FILE__;
     writeln(compiledFile);

     static const compileTime =3D __DATE__ ~ " " ~ __TIME__;
     writeln(compileTime);

     /*
     import std.file: getcwd;
     static const compilationDir =3D getcwd();
     writeln(compilationDir);

     Error: getcwd cannot be interpreted at compile time,=20
 because it has no available source code
     */
 }

 Ali
=20 Yes, just like what Ali said above, =20 __FILE__, __DATE__ and __TIME__ do work for their respective=20 usages, but I'm also looking for a way to get the current compilation=20 directory during compile time, and getcwd() doesn't seem to be=20 working. =20 Isn't there something like __DIR__ or __PATH__ that I can use to=20 get that information? =20 -Ravn-
No, but if you just want the path where your sources are you could use __FILE__ for any module and cut off the part of it that belongs to the module path. A lot of Phobos works at compile time, so you might be able to write a one-liner for that. --=20 Marco
Dec 27 2013
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-12-27 16:23, Marco Leise wrote:

 No, but if you just want the path where your sources are you
 could use __FILE__ for any module and cut off the part of
 it that belongs to the module path. A lot of Phobos works at
 compile time, so you might be able to write a one-liner for
 that.
That might not be the same as where the compilation is performed. -- /Jacob Carlborg
Dec 27 2013
prev sibling parent reply "Ravn" <ravndust gmail.com> writes:
On Friday, 27 December 2013 at 15:23:37 UTC, Marco Leise wrote:
 No, but if you just want the path where your sources are you
 could use __FILE__ for any module and cut off the part of
 it that belongs to the module path. A lot of Phobos works at
 compile time, so you might be able to write a one-liner for
 that.
I need the absolute path __FILE__ during compile time, so far the only way I know to get absolute paths is by using std.path.absolutePath (which uses getcwd() as its base) and getcwd() itself (which doesn't seem to work during compile time). Is there any alternative on how to get the absolute path for a file during compile time besides using these functions?
Dec 27 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-12-27 19:14, Ravn wrote:

 I need the absolute path __FILE__ during compile time,
 so far the only way I know to get absolute paths is by using
 std.path.absolutePath (which uses getcwd() as its base) and getcwd()
 itself (which doesn't seem to work during compile time).

 Is there any alternative on how to get the absolute path for a file
 during compile time besides using these functions?
__FILE__ will return the full path (absolute path) of the file currently compiling. But that is not the same thing as getting the path to where the compilation was made. I would guess you could use __FILE__ with dirName, but that currently does not compile. -- /Jacob Carlborg
Dec 27 2013
parent reply "Ravn" <ravndust gmail.com> writes:
On Friday, 27 December 2013 at 19:35:23 UTC, Jacob Carlborg wrote:
 On 2013-12-27 19:14, Ravn wrote:
 __FILE__ will return the full path (absolute path) of the file 
 currently compiling. But that is not the same thing as getting 
 the path to where the compilation was made.
Eh, it does? :-? It prints a relative path when I used writeln(__FILE__), I'm on windows 7. On Friday, 27 December 2013 at 19:35:23 UTC, Jacob Carlborg wrote:
 On 2013-12-27 19:14, Ravn wrote:
 I would guess you could use __FILE__ with dirName, but that 
 currently does not compile.
Tried enum path = dirName(__FILE__), compiles normally, no error from the compiler, but it still returns a relative path instead of a fullpath in my machine.
Dec 27 2013
next sibling parent Marco Leise <Marco.Leise gmx.de> writes:
Am Fri, 27 Dec 2013 20:14:00 +0000
schrieb "Ravn" <ravndust gmail.com>:

 Tried enum path = dirName(__FILE__), compiles normally, no error 
 from the compiler, but it still returns a relative path instead 
 of a fullpath in my machine.
Too bad :-/ I hoped it would use absolute paths. -- Marco
Dec 27 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-12-27 21:14, Ravn wrote:

 Eh, it does? :-?
 It prints a relative path when I used writeln(__FILE__), I'm on windows 7.
Hmm, ok. It prints the full path when I run it via my text editor. But it prints the relative path when I run it via the command line.
 Tried enum path = dirName(__FILE__), compiles normally, no error from
 the compiler, but it still returns a relative path instead of a fullpath
 in my machine.
Yes, same here, don't know why it didn't compile the first time I tried it. Perhaps this deserves an enhancement request. I really though __FILE__ would give the full path. -- /Jacob Carlborg
Dec 28 2013