meson: rpath doesn't get set with find_library('foo', dirs:'/path/to/lib')

Hi Jussi,

If you build a shared object with meson, the rpath get sets correctly (or so the docs say), but when I use find_library() and give it a search path, the rpath doesn’t get set. This is causing me lot of pain.

I’m currently doing stuff like this:

thirdparty_root = '/usr/local/thirdparty-1.6.0'
thirdparty_lib = [thirdparty_root + '/lib/linux-x64-all',
    thirdparty_root + '/lib/linux-x64-gcc4.8'
]
thirdparty_rpath = thirdparty_lib[0] + ':' + thirdparty_lib[1]

thirdparty_dep = declare_dependency(
    include_directories: thirdparty_inc,
    dependencies: [
        find_library('lib1', dirs: thirdparty_lib),
        find_library('lib2', dirs: thirdparty_lib),
        find_library('lib3', dirs: thirdparty_lib)
    ]
)

Then doing this:

executable('foo', 'foo.cpp',
   ...,
    link_args: '-Wl,-rpath,@0@:@1@'.format(thirdparty_rpath, boost_rpath),
)

Am I doing it wrong?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 56 (36 by maintainers)

Commits related to this issue

Most upvoted comments

I don’t think this is fixed.

Here is a reproduction scenario:

wget http://downloads.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.xz
tar xf zlib-1.2.8.tar.xz
cd zlib-1.2.8
./configure --prefix=/tmp/mesonfail
make
make install

cd ..

cat > fail.c <<EOF
#include <zlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
   printf("%s = %s\n", zlibVersion(), ZLIB_VERSION);
   return !!strcmp(zlibVersion(), ZLIB_VERSION);
}

EOF

cat > meson.build <<EOF
project('mesonfail', ['c'])
zdep = dependency('zlib', version : '=1.2.8')
exe = executable('fail', 'fail.c', dependencies: zdep, install: true)
EOF

rm -rf build
mkdir build
cd build
PKG_CONFIG_LIBDIR=/tmp/mesonfail/lib/pkgconfig meson --prefix=/tmp/mesonfail ..
ninja -v install
./fail && echo OK # OK
/tmp/mesonfail/bin/fail || echo FAIL # FAIL unless zlib 1.2.8 is installed in /usr/lib

@NickeZ This is a bug. It’s specific to platforms that use ELF (which doesn’t include Darwin). Solving it has no security repercussions whatsoever: if you believe that it does, you should also wipe rpath in Mach-O binaries, so it’s a bug either way.

https://github.com/mesonbuild/meson/blob/db34a3a7017d0096faa8d3f020efd078ad8a65e1/mesonbuild/scripts/depfixer.py#L287-L318

The problem here is that this overwrites existing rpath instead of appending install_rpath to it. (Moreover, it seems that if I set install_rpath to be long enough, this will fail).

This should not be a post-build fixup as it currently is: flags should be passed to the compiler to append to existing rpath. Also it doesn’t make sense that install_rpath doesn’t do anything on Darwin as Mach-O has @rpath. Sorry, but overwriting ELF headers is a dirty hack.

No other build system has this problem, because no other build system touches ELF headers. If build system ignores compile/linker flags, it doesn’t do its job properly. Remember SCons? 😃

FWIW, on nixpkgs we just patch meson now and everything works as desired https://github.com/NixOS/nixpkgs/commit/8f95aef531de13e6fffc62a31b4106f745d647ec

That’s what this issue is about. cmake automatically adds the rpath for you - at least it used to when I first opened this issue

We changed our behaviour to do the same just this last release. The commit to do that is this one.

That is only applied inside the build dir, it is stripped on install like the other ones.

Based solely on description CMAKE_INSTALL_RPATH_USE_LINK_PATH seems to be the behavior that @rhd is interested in when they opened this ticket. I would also like to see an equivalent to CMAKE_INSTALL_RPATH which gives the builder full control over the final rpath.

I’m not sure how you can use rpath in meson. But I would say that the use case should be strictly limited to libraries that are compiled within the same project. Meson shouldn’t support public dependencies in non-standard locations because this is not related to building a project. Definitely not through the build file syntax.

I think package managers that needs this funcitonality should run some post-build command to add rpaths.

Anyways this isn’t supported on windows binaries so it would give meson a platform dependent feeling.