www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get all modules in a package at CT?

reply Domain <dont_email empty.com> writes:
I have a package named command, and many modules inside it, such 
as command.build, command.pack, command.help...
I want to get all these modules at compile time so that I know 
what command is available.
Nov 24 2018
next sibling parent Anonymouse <asdf asdf.net> writes:
On Saturday, 24 November 2018 at 08:44:19 UTC, Domain wrote:
 I have a package named command, and many modules inside it, 
 such as command.build, command.pack, command.help...
 I want to get all these modules at compile time so that I know 
 what command is available.
If you just want static if expressions of whether *known* modules are available or not, then test if __traits(identifier, package.module) compiles. --- // Two-step workaround for https://issues.dlang.org/show_bug.cgi?id=19409 enum hasBuild = __traits(compiles, __traits(identifier, command.build)); enum hasPack = __traits(compiles, __traits(identifier, command.pack)); enum hasHelp = __traits(compiles, __traits(identifier, command.help)); static if (hasBuild) { /* ... */ } static if (hasPack) { /* ... */ } static if (hasHelp) { /* ... */ } --- __traits(compiles, { import package.module; }) mostly works, but I ran into problems when the module was available and merely did not compile. If you want to iterate the package for modules imported in it, I'm not sure. __traits(allMembers, package) will list names of imported packages but not which modules.
Nov 24 2018
prev sibling next sibling parent Neia Neutuladh <neia ikeran.org> writes:
Easiest way is to put this in your build script:

find path/to/package -name '*.d' | \
   xargs grep '^module ' | \
   sed 's,^module,import,' \
   > data/modules.d

Add `-J data` to your DMD command line, or add `"stringImportPaths":
["data"]` to dub.json.

Then in your file:

mixin(import("modules.d"));
Nov 24 2018
prev sibling parent Andre Pany <andre s-e-a-p.de> writes:
On Saturday, 24 November 2018 at 08:44:19 UTC, Domain wrote:
 I have a package named command, and many modules inside it, 
 such as command.build, command.pack, command.help...
 I want to get all these modules at compile time so that I know 
 what command is available.
As far as I understand there is s.th. called separate compilations. Your program may is compiled in parts. Your logic which loops over the modules only knows which modules are available at the specific point in time. I do not know whether there is some functionality which tells you all modules available at compile time. But if this functionality exists, you (the developer) has to be aware of separate compilation and should have a plan how to mitigate. Either do not use separate compilation or always force recompilation of all source code files... In would really like to have such functionality and also the possibility to express in code: always recompile this part of code (the loop over the modules) even in separate compilation. I do not know whether that is technically possible... Kind regards Andre
Nov 25 2018