Skip to content

Commit

Permalink
BUG: fix all RPATHs that start with $ORIGIN
Browse files Browse the repository at this point in the history
Some library may contain RPATH like '$ORIGIN/../src' and should be fixed as well
Fixes: #509
  • Loading branch information
metab0t committed Oct 4, 2023
1 parent 36e8205 commit 98d3544
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
20 changes: 14 additions & 6 deletions mesonpy/_rpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def _set_rpath(filepath: Path, rpath: Iterable[str]) -> None:

def fix_rpath(filepath: Path, libs_relative_path: str) -> None:
rpath = _get_rpath(filepath)
if '$ORIGIN/' in rpath:
rpath = [('$ORIGIN/' + libs_relative_path if path == '$ORIGIN/' else path) for path in rpath]
if any(path.startswith('$ORIGIN/') for path in rpath):
rpath = ['$ORIGIN/' + libs_relative_path] + [path for path in rpath if not path.startswith('$ORIGIN/')]
_set_rpath(filepath, rpath)


Expand All @@ -46,13 +46,21 @@ def _get_rpath(filepath: Path) -> List[str]:
rpath_tag = False
return rpath

def _replace_rpath(filepath: Path, old: str, new: str) -> None:
subprocess.run(['install_name_tool', '-rpath', old, new, os.fspath(filepath)], check=True)
def _replace_rpath(filepath: Path, old_rpaths: Iterable[str], new_rpaths: Iterable[str]) -> None:
command = ['install_name_tool']
for old in old_rpaths:
command.extend(['-delete_rpath', old])
for new in new_rpaths:
command.extend(['-add_rpath', new])
command.append(os.fspath(filepath))
subprocess.run(command, check=True)

def fix_rpath(filepath: Path, libs_relative_path: str) -> None:
rpath = _get_rpath(filepath)
if '@loader_path/' in rpath:
_replace_rpath(filepath, '@loader_path/', '@loader_path/' + libs_relative_path)
if any(path.startswith('@loader_path/') for path in rpath):
old_rpath = [path for path in rpath if path.startswith('@loader_path/')]
new_rpath = ['@loader_path/' + libs_relative_path]
_replace_rpath(filepath, old_rpath, new_rpath)

else:

Expand Down
2 changes: 1 addition & 1 deletion tests/packages/link-against-local-lib/examplemod.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <Python.h>

#include "examplelib.h"
#include "lib/examplelib.h"

static PyObject* example_sum(PyObject* self, PyObject *args)
{
Expand Down
9 changes: 9 additions & 0 deletions tests/packages/link-against-local-lib/lib/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-FileCopyrightText: 2022 The meson-python developers
#
# SPDX-License-Identifier: MIT

example_lib = shared_library(
'example',
'examplelib.c',
install: true,
)
6 changes: 1 addition & 5 deletions tests/packages/link-against-local-lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

project('link-against-local-lib', 'c', version: '1.0.0')

example_lib = shared_library(
'example',
'examplelib.c',
install: true,
)
subdir('lib')

py = import('python').find_installation()

Expand Down

0 comments on commit 98d3544

Please sign in to comment.