backends: fix source dir stripping from rpaths

Commit e88887be4a ("Only remove substring if it is part of string")
removed the source dir from the rpath when the following check succeeds:

  if absdir.startswith(self.environment.get_source_dir()):
      rel_to_src = absdir[len(self.environment.get_source_dir()) + 1:]

For example, absdir '/myproject/foo' starts with source dir
'/myproject', so we want to generate the relative path 'foo'.

This code doesn't work with absdir '/myproject-libs/foo' though, because
we'll incorrectly turn it into a relative path 'libs/foo' after
stripping away '/myproject-'.

Use os.path.commonpath() instead of str.startswith() so path components
are correctly handled.

Cc: Niklas Claesson <niklas.claesson@cosylab.com>
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
pull/11390/merge
Stefan Hajnoczi 2 years ago committed by Xavier Claessens
parent 61d525cc16
commit c849031513
  1. 11
      mesonbuild/backend/backends.py

@ -733,6 +733,7 @@ class Backend:
@lru_cache(maxsize=None)
def rpaths_for_non_system_absolute_shared_libraries(self, target: build.BuildTarget, exclude_system: bool = True) -> 'ImmutableListProtocol[str]':
paths: OrderedSet[str] = OrderedSet()
srcdir = self.environment.get_source_dir()
for dep in target.external_deps:
if not isinstance(dep, (dependencies.ExternalLibrary, dependencies.PkgConfigDependency)):
continue
@ -751,8 +752,14 @@ class Backend:
# emulate rpaths by setting PATH, so also accept DLLs here
if os.path.splitext(libpath)[1] not in ['.dll', '.lib', '.so', '.dylib']:
continue
if libdir.startswith(self.environment.get_source_dir()):
rel_to_src = libdir[len(self.environment.get_source_dir()) + 1:]
try:
commonpath = os.path.commonpath((libdir, srcdir))
except ValueError: # when paths are on different drives on Windows
commonpath = ''
if commonpath == srcdir:
rel_to_src = libdir[len(srcdir) + 1:]
assert not os.path.isabs(rel_to_src), f'rel_to_src: {rel_to_src} is absolute'
paths.add(os.path.join(self.build_to_src, rel_to_src))
else:

Loading…
Cancel
Save