www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to set constant value to environment variable at compile time?

reply Narxa <fbf99eQrXpHcjP8L gmail.com> writes:
Hello, people!

I would like to have a constant with the value of some 
environment variable that is defined at compile time.

In FreePascal, it can be done by defining it in source as:

VALUE_OF_SOMETHING = {$I %SOMETHING%};

And I can call the compiler with (bash):

SOMETHING='Anything' export SOMETHING; <compiler> <flags>

And it will automatically assign VALUE_OF_SOMETHING to 'Anything'.


In GCC C compiler, the solution I found was more complicated but 
it worked.

I had to explicitly define the environment variable when calling 
the compiler with:

gcc <flags> -DSOMETHING=\""Anything"\" -o <output> <source_file>


Now, I would like to do that with the 'dmd' compiler.

I know I could possibly use 'gdc' to achieve that but I want to 
use the 'dmd' compiler.

Is it possible to do that such a thing or through source or any 
other means?


Thank you!
Dec 10 2018
next sibling parent reply Neia Neutuladh <neia ikeran.org> writes:
On Mon, 10 Dec 2018 11:08:23 +0000, Narxa wrote:
 Hello, people!
 
 I would like to have a constant with the value of some environment
 variable that is defined at compile time.
In your build script, echo the environment variable into a file. Then import() that file and use the value.
 I know I could possibly use 'gdc' to achieve that but I want to use the
 'dmd' compiler.
Defining variables like that is a language-level feature. gdc supports exactly the same options as dmd.
Dec 10 2018
parent Narxa <fbf99eQrXpHcjP8L gmail.com> writes:
On Monday, 10 December 2018 at 17:47:37 UTC, Neia Neutuladh wrote:
 On Mon, 10 Dec 2018 11:08:23 +0000, Narxa wrote:
 Hello, people!
 
 I would like to have a constant with the value of some 
 environment variable that is defined at compile time.
In your build script, echo the environment variable into a file. Then import() that file and use the value.
 I know I could possibly use 'gdc' to achieve that but I want 
 to use the 'dmd' compiler.
Defining variables like that is a language-level feature. gdc supports exactly the same options as dmd.
It worked! Thank you very much!
Dec 10 2018
prev sibling parent reply aliak <something something.com> writes:
On Monday, 10 December 2018 at 11:08:23 UTC, Narxa wrote:
 Hello, people!

 I would like to have a constant with the value of some 
 environment variable that is defined at compile time.

 In FreePascal, it can be done by defining it in source as:

 VALUE_OF_SOMETHING = {$I %SOMETHING%};

 And I can call the compiler with (bash):

 SOMETHING='Anything' export SOMETHING; <compiler> <flags>

 And it will automatically assign VALUE_OF_SOMETHING to 
 'Anything'.


 In GCC C compiler, the solution I found was more complicated 
 but it worked.

 I had to explicitly define the environment variable when 
 calling the compiler with:

 gcc <flags> -DSOMETHING=\""Anything"\" -o <output> <source_file>


 Now, I would like to do that with the 'dmd' compiler.

 I know I could possibly use 'gdc' to achieve that but I want to 
 use the 'dmd' compiler.

 Is it possible to do that such a thing or through source or any 
 other means?


 Thank you!
I don't know if it's possible but one way to do it would be to use the -J switch and give it a config file that has contents: VAR1=Value1 VAR2=Value2 And then in source code: immutable config = import("config"); mixin(parseConfig); string parseConfig(string str) { string ret; foreach (line; str.split("\n")) { auto parts = line.split("="); ret ~= `string ` ~ parts[0] ~ ` = "` parts[2] `";`; } return ret; } You can also echo out the config file with bash or something Cheers, - Ali
Dec 10 2018
parent Neia Neutuladh <neia ikeran.org> writes:
On Mon, 10 Dec 2018 17:58:32 +0000, aliak wrote:
 string parseConfig(string str) {
    string ret;
    foreach (line; str.split("\n")) {
      auto parts = line.split("=");
      ret ~= `string ` ~ parts[0] ~ ` = "` parts[2] `";`;
    }
    return ret;
 }
That works as long as none of the environment variable values contain a double quote or a newline character.
Dec 10 2018