Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 27, 2013 getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
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- |
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ravn | 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__); } --- |
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to nazriel | 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-
|
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ravn | 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.
|
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lemonfiend | 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 |
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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-
|
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ravn | > 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 |
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ravn | 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 Ç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- 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. -- Marco |
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | 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 |
December 27, 2013 Re: getting __DIR__ and __TIME__ of compilation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | 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?
|
Copyright © 1999-2021 by the D Language Foundation