From 4467663baf420cf30ae6b202e5658299040938d1 Mon Sep 17 00:00:00 2001
From: Ting-Wei Lan <lantw@src.gnome.org>
Date: Thu, 4 Oct 2018 23:03:30 +0800
Subject: [PATCH] backends: Use raw_link_args to check for the need of RPATH

Function rpaths_for_bundled_shared_libraries assumes it needs RPATH when
linking arguments of an external dependency has exactly one argument and
the only argument is an absolute path to a library file. This was mostly
fine because almost all .pc files use a -L -l pair instead of the full
path of the library, which means pkg-config dependencies almost always
have at least two arguments. However, there are patches landed in the
meson 0.47 cycle which convert -L -l pair returned by pkg-config to the
absolute path of library. If the output of pkg-config includes exactly
one -L argument and one -l argument, it will be converted to exactly one
absolute path by meson and rpaths_for_bundled_shared_libraries will
assume it needs RPATH. Since meson passes both -rpath and -rpath-link to
the linker and -rpath-link has precedence over LD_LIBRARY_PATH, it
changes the search order of dependent libraries in an unexpected way and
it causes a lot of linking troubles in JHBuild environments on FreeBSD.

To make the method behave like the old way of using -L -l pairs and
avoid library path order problems, we use raw_link_args instead of
link_args here. raw_link_args stores the unmodified output of pkg-config
and it is much less likely to accidentally match the rule currently used
by the method.

Works around https://github.com/mesonbuild/meson/issues/4270.
---
 mesonbuild/backend/backends.py | 2 +-
 run_unittests.py               | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py
index 1e5a13e7325f..09f84f12d4e6 100644
--- a/mesonbuild/backend/backends.py
+++ b/mesonbuild/backend/backends.py
@@ -611,7 +611,7 @@ def rpaths_for_bundled_shared_libraries(self, target, exclude_system=True):
         for dep in target.external_deps:
             if not isinstance(dep, (dependencies.ExternalLibrary, dependencies.PkgConfigDependency)):
                 continue
-            la = dep.link_args
+            la = dep.get_link_args(raw=True)
             if len(la) != 1 or not os.path.isabs(la[0]):
                 continue
             # The only link argument is an absolute path to a library file.
diff --git a/run_unittests.py b/run_unittests.py
index 0b18ef55b402..591800db6cb4 100755
--- a/run_unittests.py
+++ b/run_unittests.py
@@ -7858,7 +7858,7 @@ def test_static_link(self):
         # Test that installed libraries works
         self.new_builddir()
         self.prefix = oldprefix
-        meson_args = [f'-Dc_link_args=-L{libdir}',
+        meson_args = [f'-Dc_link_args=-L{libdir} -Wl,-rpath,{libdir}',
                       '--fatal-meson-warnings']
         testdir = os.path.join(self.unit_test_dir, '67 static link')
         env = {'PKG_CONFIG_LIBDIR': os.path.join(libdir, 'pkgconfig')}