www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it legal to have physical package structure not corresponding to

reply Max Samukha <maxsamukha gmail.com> writes:
Please consider a project:

src/p2/
     |---a.d
     |---b.d

in which it is desirable that the logical package tree does not 
fully correspond to the filesystem subtree:

src/p2/a.d:
module p1.p2.a;
...

src/p2/b.d:
module p1.p2.b;
import p1.p2.a;
...

dmd compiles the above structure successfully, even though the 
spec explicitly states that "The packages correspond to directory 
names in the source file path" 
(https://dlang.org/spec/module.html#module_declaration, item 2):

dmd -lib src/p2/a.d src/p2/b.d // ok

Is the spec wrong? Can the current dmd behavior be relied on?


If yes, how does one create an import tree from the structure 
like that?

'dmd -H -o- -Hdimport -op src/p1/a.d src/p2/b.d' is close, but it 
places the .di files into 'import/src/p1' directory (which is 
useless for automatic importing) instead of the expected 
'import/p1/p2'. Is it possible to achieve that without making the 
build process unreasonably complex?
Sep 16 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 16 September 2019 at 07:02:04 UTC, Max Samukha wrote:
 Is the spec wrong?
Well, sort of, your code is perfectly legal and has worked since the beginning of D. The thing in the spec is referring to the assumption the compiler uses to find files not explicitly referenced on the build command. import p1.p2.a; assumes p1/p2/a.di or p1/p2/a.d. but if you reference the module, `dmd a.d whatever/b.d`, it will not look up based on this assumption anymore.
 If yes, how does one create an import tree from the structure 
 like that?
I don't think dmd will create them automatically though, dmd -H kinda sucks, it used to just slap them down flat, now I think it copies the src structure, but it doesn't consider the module names (but it should!). You'll have to sort them yourself. Also note the -mv option to dmd -mv=package.module = Use path/filename as the source file for package.module. This is used when the source file path and names are not the same as the package and module hierarchy. The rightmost components of the path/filename and package.module can be omitted if they are the same. which explicitly overrides the automatic assumption above, so import can look up any file.
Sep 16 2019
parent Max Samukha <maxsamukha gmail.com> writes:
On Monday, 16 September 2019 at 13:13:50 UTC, Adam D. Ruppe wrote:
 On Monday, 16 September 2019 at 07:02:04 UTC, Max Samukha wrote:
 [...]
Well, sort of, your code is perfectly legal and has worked since the beginning of D. The thing in the spec is referring to the assumption the compiler uses to find files not explicitly referenced on the build command. [...]
Thank you, Adam.
Sep 16 2019