Thread overview
how do I pass --whole-archive and --no-whole-archive to dub?
Aug 06, 2020
rikki cattermole
Aug 06, 2020
rikki cattermole
Aug 07, 2020
Andre Pany
August 06, 2020
So this project has a registration function that associates pointers to functions to functionality architecture.

The current approach is to have a file, say everything.d, that imports all modules in the project and then for each module calls the function (inside that module) that carries the registration.

Of course that makes everything.d a large dependency knot that takes long to compile and also makes incremental/modular/parallel compilation unduly difficult.

This is a classic problem with a classic solution: build a little registry and have each module register itself in the shared static this() module constructor. The code in each module would go:

shared static this() {
    addRegistration(&myRegistrationFunction);
}

That way everything does not depend on anything, and the onus is on each module to register itself. That way modules can be changed (or even added!) literally without a need to touch or recompile anything else in the project. Neat!

Except, the project is a static library. When linking the library with an executable, the linker is motivated to cull unused modules. And for good reason. (You don't want to initialize the fpga library in hello_world, etc.) So the module finds that each of those self-registering modules is not used from the outside (the module constructor is beautifully/damnedly self-referential) and eliminates the entire module. So no more registration and the scheme doesn't work.

This is another classic problem with a classic solution: when linking anything with mylib.a, send the linker the --whole-archive flag just before it and the --no-whole-archive flag right after it. (Other OSs have similar flags.)

How can I do that in dub?
August 07, 2020
In theory it should be as simple as setting it in lflags.

But you will need to test it, as I'm not sure if it'll propagate upwards to where you need it.

If that doesn't work, having dub automatically handle registration functions would be a nice feature request to have.
August 06, 2020
On 8/6/20 2:19 PM, rikki cattermole wrote:
> In theory it should be as simple as setting it in lflags.
> 
> But you will need to test it, as I'm not sure if it'll propagate upwards to where you need it.
> 
> If that doesn't work, having dub automatically handle registration functions would be a nice feature request to have.

I did try this:

lflags "--whole-archive" "/path/to/libmylib.a" "--no-whole-archive"

But that doesn't work, presumably because the command line contains the same library before these flags. So I assume the first occurrence is taken and the second ignored.

As an aside, it is very irritating that dub multiplies the flags for some reason, e.g. -defaultlib=libphobos2.so appears literally 30 times in the same command line. Similar for other flags.
August 07, 2020
https://github.com/dlang/dub/issues/1990
August 07, 2020
On Thursday, 6 August 2020 at 19:31:01 UTC, Andrei Alexandrescu wrote:
> On 8/6/20 2:19 PM, rikki cattermole wrote:
>> In theory it should be as simple as setting it in lflags.
>> 
>> But you will need to test it, as I'm not sure if it'll propagate upwards to where you need it.
>> 
>> If that doesn't work, having dub automatically handle registration functions would be a nice feature request to have.
>
> I did try this:
>
> lflags "--whole-archive" "/path/to/libmylib.a" "--no-whole-archive"
>
> But that doesn't work, presumably because the command line contains the same library before these flags. So I assume the first occurrence is taken and the second ignored.
>
> As an aside, it is very irritating that dub multiplies the flags for some reason, e.g. -defaultlib=libphobos2.so appears literally 30 times in the same command line. Similar for other flags.

I am not an expert in this area, just thinking loud: dub could recognize the `--whole-archive`, `--no-whole-archive` pattern in lflags and add it also to the first occurrence of the lib file in the command line.

This should be a rather simple fix?

Kind regards
Andre