digitalmars.D.learn - How to set constant value to environment variable at compile time?
- Narxa (19/19) Dec 10 2018 Hello, people!
- Neia Neutuladh (5/11) Dec 10 2018 In your build script, echo the environment variable into a file. Then
- Narxa (3/14) Dec 10 2018 It worked!
- aliak (19/39) Dec 10 2018 I don't know if it's possible but one way to do it would be to
- Neia Neutuladh (3/11) Dec 10 2018 That works as long as none of the environment variable values contain a
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
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
On Monday, 10 December 2018 at 17:47:37 UTC, Neia Neutuladh wrote:On Mon, 10 Dec 2018 11:08:23 +0000, Narxa wrote:It worked! Thank you very much!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
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
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