meson icon indicating copy to clipboard operation
meson copied to clipboard

Add option to link with a dependency even if no symbols are used

Open kathoum opened this issue 1 year ago • 4 comments

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.

kathoum avatar Sep 10 '24 11:09 kathoum

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']
)

dcbaker avatar Sep 10 '24 16:09 dcbaker

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.

kathoum avatar Sep 10 '24 16:09 kathoum

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.

eli-schwartz avatar Sep 10 '24 16:09 eli-schwartz

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).

dcbaker avatar Sep 10 '24 17:09 dcbaker

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.

manipuladordedados avatar Jan 09 '25 14:01 manipuladordedados