www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do you declare manifest constants?

reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
I want to embed some info from build system - version info, for 
example. I can easily do this with C++ compiler by 
`-DVERSION="1.2.3"` but there is no such an option in dmd. So I'm 
wondering how do people workaround this?
I see only one way: generate a file and add it to the build (file 
might be `.d` source or something included by import expression).
Nov 04 2021
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 11/4/21 12:43 PM, Andrey Zherikov wrote:
 I want to embed some info from build system - version info, for example. 
 I can easily do this with C++ compiler by `-DVERSION="1.2.3"` but there 
 is no such an option in dmd. So I'm wondering how do people workaround 
 this?
 I see only one way: generate a file and add it to the build (file might 
 be `.d` source or something included by import expression).
D doesn't have any equivalent for this. The closest you can get is to turn on `version` identifiers. There is also a quirky `-version=123` which is IMO, a completely useless feature. Your best bet is to do what you are doing -- generate a d file, and then include it. Note, there is at least one dub package which does this for you: https://code.dlang.org/packages/gen-package-version I use it in my build and it works OK. You just have to remember to attribute your tag in git. -Steve
Nov 04 2021
parent reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
On Thursday, 4 November 2021 at 17:09:31 UTC, Steven 
Schveighoffer wrote:
 D doesn't have any equivalent for this.
Is it possible to add this feature having `-C VERSION="1.2.3"` (`-D` is already used) to be equal to `enum VERSION="1.2.3"` in global namespace?
 The closest you can get is to turn on `version` identifiers.

 There is also a quirky `-version=123` which is IMO, a 
 completely useless feature.
I agree - this is useless. `-version myversion=123` would be much more useful.
 Note, there is at least one dub package which does this for 
 you: https://code.dlang.org/packages/gen-package-version
Unfortunately git/hg tags is not the only possible source of manifest constants.
Nov 04 2021
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Nov 04, 2021 at 05:24:44PM +0000, Andrey Zherikov via
Digitalmars-d-learn wrote:
 On Thursday, 4 November 2021 at 17:09:31 UTC, Steven Schveighoffer wrote:
 D doesn't have any equivalent for this.
Is it possible to add this feature having `-C VERSION="1.2.3"` (`-D` is already used) to be equal to `enum VERSION="1.2.3"` in global namespace?
 The closest you can get is to turn on `version` identifiers.
 
 There is also a quirky `-version=123` which is IMO, a completely
 useless feature.
I agree - this is useless. `-version myversion=123` would be much more useful.
Here's a hack that uses dmd's stdin feature to inject D code into a compile command: // main.d import __stdin : myversion; void main() { import std; writeln(myversion); } Compile command: echo 'enum myversion = "1.2.3";' | dmd - -run main.d Output: 1.2.3 You can change the version number just by changing the echo command. And of course, it doesn't have to be a string, it can be any valid D type, and obviously you can declare more than one variable that can then be read by importing from __stdin. T -- Don't throw out the baby with the bathwater. Use your hands...
Nov 04 2021
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/4/21 10:36 AM, H. S. Teoh wrote:

 	import __stdin : myversion;
Where can we learn more of that magic? :) Ali
Nov 04 2021
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Nov 04, 2021 at 01:17:02PM -0700, Ali Çehreli via Digitalmars-d-learn
wrote:
 On 11/4/21 10:36 AM, H. S. Teoh wrote:
 
 	import __stdin : myversion;
Where can we learn more of that magic? :)
[...] I kinda cheated, because I was the one who implemented dmd's stdin feature, so I knew that dmd implicitly creates a specially-named module to contain the code read from stdin. I had actually forgotten what the name of this module was, but that was no problem since it was easily found by: echo 'pragma(msg, __MODULE__);' | dmd -c - which revealed the module name to be `__stdin`. Next, thanks to D's module system, is the realization that you could import this implicit module from somewhere else and pull symbols from it. From there, the everything else followed. :-) T -- There is no gravity. The earth sucks.
Nov 04 2021