www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Importing version identifiers from another file?

reply KytoDragon <kytodragon e.mail.de> writes:
I am currently maintaining a port of a c++ library that uses 
conditional compilation to integrate into the users program. e.g.:

config.h (edited by the user of the library)
```
#define USE_MY_ASSERT
void MY_ASSERT(bool expr) {...}
```

library.c
```
include "config.h"
#ifndef USE_MY_ASSERT
     void MY_ASSERT(bool expr) {...} // default implementation
#end
```

Given that static if at top-level has been broken for some time 
(https://issues.dlang.org/show_bug.cgi?id=17883, 
https://issues.dlang.org/show_bug.cgi?id=20905, 
https://issues.dlang.org/show_bug.cgi?id=21171), I tried to use 
version identifiers for this in D:

config.d
```
version = USE_MY_ASSERT;
void MY_ASSERT(bool expr) {...}
```

library.d
```
import config;
version (USE_MY_ASSERT) {} else {
     void MY_ASSERT(bool expr) {...}
}
```

Sadly this results in an identifier conflict, as the version set 
in config.d does not seem to affect library.d. Is there any way 
to import version specifiers from a separate file? I don't want 
to pollute the users build files (dub.json etc.) with dozens of 
versions they need to pass to the compiler.
Apr 11 2022
next sibling parent wjoe <invalid example.com> writes:
On Monday, 11 April 2022 at 08:57:12 UTC, KytoDragon wrote:
 [...]
 Sadly this results in an identifier conflict, as the version 
 set in config.d does not seem to affect library.d. Is there any 
 way to import version specifiers from a separate file? I don't 
 want to pollute the users build files (dub.json etc.) with 
 dozens of versions they need to pass to the compiler.
No, version identifiers don't escape module scope. See 24.1.4 https://dlang.org/spec/version.html for reference.
Apr 11 2022
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 4/11/22 01:57, KytoDragon wrote:

 Is there any way to import
 version specifiers from a separate file?
Have you tried mixing in the versions? Have one file with the versions in it: // file: 'versions' version = x; Then mix it in: mixin (import ("versions")); version (x) { /* ... */ } Seems to work for me. Ali
Apr 12 2022
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Apr 12, 2022 at 10:07:15AM -0700, Ali Çehreli via Digitalmars-d-learn
wrote:
 On 4/11/22 01:57, KytoDragon wrote:
 
 Is there any way to import version specifiers from a separate file?
Have you tried mixing in the versions? Have one file with the versions in it: // file: 'versions' version = x; Then mix it in: mixin (import ("versions")); version (x) { /* ... */ } Seems to work for me.
[...] If you need globally-defined versions, I'd just use the command-line option `-version=MyVersionA -version=MyVersionB ...`. Or, if you absolutely have to define versions in a source file, why not just use enums + static if instead: // versions.d module versions; enum MyVersionA = true; enum MyVersionB = true; // program.d import versions; static if (MyVersionA) { ... } else { ... } // ... and so on T -- An elephant: A mouse built to government specifications. -- Robert Heinlein
Apr 12 2022
prev sibling parent cc <cc nevernet.com> writes:
Another option for this was suggested here:

https://forum.dlang.org/post/qbvgboihhwcuqglygzec forum.dlang.org

On Wednesday, 12 February 2020 at 09:28:15 UTC, Simen Kjærås 
wrote:
 So, you could have a file called 'versions' containing this:


 -version=Compress




 and feed it to dmd like so:

     dmd -w -wi -g  versions -main foo.d
Apr 17 2022