Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: link_hidden argument in library() #14015

Open
na-trium-144 opened this issue Dec 16, 2024 · 1 comment
Open

Feature request: link_hidden argument in library() #14015

na-trium-144 opened this issue Dec 16, 2024 · 1 comment

Comments

@na-trium-144
Copy link

na-trium-144 commented Dec 16, 2024

When building a shared library which uses external static library (say libA here) internally, I don't want to expose the libA's symbol from my shared library.
One way to achieve this is to set gnu_symbol_visibility: 'inlineshidden' on libA.
But there is also a way to do this without modifying libA: -Wl,--exclude-libs,libA.a in addition to -lA (on Linux) and -Wl,-hidden-lA instead of -lA (on MacOS). This works no matter what the visibility option of libA is.
Unfortunately, doing this in meson will be something like this and a little complicated:

a_proj = subproject(...)
a_dep = a_proj.get_variable('a_dep')
a_lib = a_proj.get_variable('a_lib')
link_depends = []
if host_machine.system() == 'linux'
  a_hidden_dep = declare_dependency(
    dependencies: [a_dep],
    link_args: ['-Wl,--exclude-libs,' + fs.name(a_lib.full_path())],
  )
elif host_machine.system() == 'darwin'
  link_depends += a_lib
  a_hidden_dep = declare_dependency(
    dependencies: [a_dep.partial_dependency(compile_args: true, includes: true)],
    link_args: [
      '-L' + fs.parent(a_lib.full_path()),
      '-Wl,-hidden-l' + a_lib.name(),
    ],
  )
else
  # On Windows just linking normally is ok for me.
  # I don't know about other platform...
  a_hidden_dep = a_dep
endif

# then link it from my library
my_lib = library(
  ...,
  # on MacOS I need to put a_lib into link_depends to avoid linking with -lA,
  # and declare_dependency do not have link_depends
  link_depends: link_depends,
  dependencies: a_hidden_dep,
)
# In reality I want to do this only when my_lib is shared and a_lib is static, and otherwise it should be linked normally.
# so there will be more if statements than this.

So I thought it would be useful if meson implemented an argument like link_hidden: a_lib.

@eli-schwartz
Copy link
Member

Hmm. Kind of lame that Apple ld64 doesn't support --exclude-libs. :(

Note that you can also achieve a comparable effect using an ld version script, which is likewise linker-dependent but doesn't need hacking around partial dependencies in order to erase -lA from the link command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants