Meson: Uniform way to link whole libraries

Created on 12 Feb 2018  路  10Comments  路  Source: mesonbuild/meson

Use case

Let's say you have 3 libraries and executable

  • global register, it defines some storage for plugins that automatically register themselves
  • plugin that registers on initialization, can be via __attribute__((constructor)) or static initialization
  • user of the register, also links with plugin
  • executable that interacts with user but does not know about plugin directly.

There are 3 problems in meson that make it hard to deal with this

  1. It is not possible to specify link_whole as part of declare_dependency() call and make executable link with plugin if it is static library.
  2. It is not possible to use shared library with link_whole. So if you use library() and let user specify default_library option, you can't consistently use result.
  3. Shared libraries are not linked without specifying '-Wl,--no-as-needed'. But this option is not really needed for the whole project, most likely for few plugins only.

Example to demonstrate this behavior.

Proposal

  • Add link_whole to declare_dependency(). Semantics is identical to link_with.
  • Allow use of shared libraries in link_whole and add '-Wl,--no-as-needed' if shared library is used.
  • Or add another option link_always, let it accept both static and shared libraries with behavior described above, and add it to declare_dependency() as well.

Caveat

I did not take any other systems or compilers but Linux/GCC/Clang into consideration. Looking for input.

compilers dependencies enhancement help wanted

Most helpful comment

I think of the 3 problems listed in the original report, the first one is resolved, the other two are still pending. I tried to solve them in #3785 first, but then realized that #3865 is probably better. I'm waiting for feedback on the latter at the moment.

All 10 comments

Add link_whole to declare_dependency(). Semantics is identical to link_with

Makes sense.

Allow use of shared libraries in link_whole and add '-Wl,--no-as-needed' if shared library is used.

I see, that makes sense too. "link whole" for static libraries will mean "whole archive" (use all symbols) and for shared will mean "force link" (use all symbols).

I think link_whole: needs to do this only with GNU ld, since other platforms don't support as-needed.

So I guess I go ahead with implementing

  • Add link_whole to declare_dependency(). Semantics is identical to link_with.
  • Allow use of shared libraries in link_whole and add '-Wl,--no-as-needed' if shared library is used.

Before you finish the implementation, you may want to write a test and open a PR so we know how all the platforms behave w.r.t. linking against shared libraries from which no symbols are being used.

Do you mean to just trigger all appveyor/travis before going too far implementing this?

Exactly.

Related #2478

_Just passing by to say that we would also love to have a nice way to link with dynamic libraries using -Wl,--no-as-needed, for the exact same use case you described above (self-registering plugins). I'm very happy to see that you are already working on it, and am looking forward to it being merged! Kudos to you, @sarum9in!_

Is this actually still open? I ended up here and then realized I was on an old version of meson. After I upgraded, linking static libraries together appears to work.

I think of the 3 problems listed in the original report, the first one is resolved, the other two are still pending. I tried to solve them in #3785 first, but then realized that #3865 is probably better. I'm waiting for feedback on the latter at the moment.

Can I obtain the list of libraries to link with from a dependency object? The purpose would be to use it as a link_whole argument when creating a executable or library, and not the other way as it was suggested (and later implemented) here. The dependency object surely has this information but i can't find a way to retrieve it directly in the reference manual.

Was this page helpful?
0 / 5 - 0 ratings