Add option to link with a dependency even if no symbols are used
Feature request I think it is useful to specify that a dependency should be linked, even if no symbols from that dependency are used, without disabling 'b_asneeded' globally for the current target.
I have found only one way to override 'as-needed' for a single dependency, and it requires adding -Wl,--push-state,--no-as-needed $link_options -Wl,--pop-state to link_args, but this is toolchain-specific.
For macos frameworks the option becomes -needed_framework $name.
It would be nice if there was a parameter to dependency(), e.g. needed: true, that worked uniformly in multiple situations.
You can override builtin options on a per-target basis using the override_options keyword argument, which is compiler/linker/platform agnostic:
library(
...
override_options : ['b_asneeded=false']
)
I know I can set the option per-target, but I would like to set it per-dependency, that is, link to all the other dependencies only if they are used, but make sure that this specific dependency is linked in.
By overriding b_asneeded we would end up in a situation where a target links to 4 libraries, 2 of which are used and one (the third) of which the author wishes to unconditionally link to, and the linker doesn't drop any of the libraries, even though the author intends to drop one of them (the fourth).
I understand the use case, and it's not really possible today. Unsure what to do about it, though.
Indeed, I misunderstood the initial post as saying "there's only turn off b_asneeded universally, or manually pass link arguments to turn it off by target."
I think I'd prefer not to have a needed keyword argument, and would prefer something like a force_link() method that returns a specially annotated version, since you may want that for an internal dependency as well, but it's a consumer choice not a builder choice (for subprojects).
Hi, I am a contributor to the Emilua project, and the issue described here directly affects our work.
In Emilua, we use shared libraries that override standard libc functions (this is a common practice in many projects, such as jemalloc and others).
To ensure these overrides are correctly linked, it is necessary to pass the linker options -Wl,--push-state,--no-as-needed $link_options -Wl,--pop-state.
For example, we use the following linker arguments in meson.build:
link_args : [
'-Wl,--push-state,--no-as-needed',
'libemilua-libc-service.so.0.11.0',
'-Wl,--pop-state',
]
Unfortunately, Meson does not respect the order of link_args when using declare_dependency(), making it impossible to enforce the correct linking sequence. Without this, the linker may ignore our custom library.