Skip to content

Commit 3c1b103

Browse files
committed
TST: fix test and reorganize
Rework the dependencies between the Python extension module and the package libraries so that they can be loaded with a single additional RPATH entry for each. The previous dependencies required two RPATH entries to be added to the extension module to find the two libraries installed in two different paths, however setting two RPATH entries is not possible via install_rpath. Meson version 1.6.0 or later is required for insrall_rpath to be present in meson introspection data. Reorganizes the test package to a flatter layout that helps visualizing all the parts involved in the test.
1 parent 37c79fc commit 3c1b103

File tree

12 files changed

+79
-62
lines changed

12 files changed

+79
-62
lines changed

tests/packages/link-against-local-lib/meson.build

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if meson.get_compiler('c').get_id() in ['msvc', 'clang-cl', 'intel-cl']
99
link_args = ['-DEXAMPLE_DLL_IMPORTS']
1010
else
1111
lib_compile_args = []
12-
link_args = ['-Wl,-rpath,custom-rpath']
12+
link_args = ['-Wl,-rpath,rpath-from-linker-arguments']
1313
endif
1414

1515
subdir('lib')
@@ -26,6 +26,7 @@ py.extension_module(
2626
'examplemod.c',
2727
link_with: example_lib,
2828
link_args: link_args,
29+
install_rpath: 'custom-rpath',
2930
install: true,
3031
subdir: 'example',
3132
)

tests/packages/sharedlib-in-package/mypkg/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _append_to_sharedlib_load_path():
4545
# end-literalinclude
4646

4747

48-
from ._example import example_prod, example_sum #noqa: E402
48+
from ._example import prodsum # noqa: E402
4949

5050

51-
__all__ = ['example_prod', 'example_sum']
51+
__all__ = ['prodsum']

tests/packages/sharedlib-in-package/mypkg/_examplemod.c

+6-19
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,23 @@
44

55
#include <Python.h>
66

7-
#include "examplelib.h"
8-
#include "examplelib2.h"
7+
#include "lib.h"
98

10-
static PyObject* example_sum(PyObject* self, PyObject *args)
9+
static PyObject* example_prodsum(PyObject* self, PyObject *args)
1110
{
12-
int a, b;
13-
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
14-
return NULL;
15-
}
11+
int a, b, x;
1612

17-
long result = sum(a, b);
18-
19-
return PyLong_FromLong(result);
20-
}
21-
22-
static PyObject* example_prod(PyObject* self, PyObject *args)
23-
{
24-
int a, b;
25-
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
13+
if (!PyArg_ParseTuple(args, "iii", &a, &b, &x)) {
2614
return NULL;
2715
}
2816

29-
long result = prod(a, b);
17+
long result = prodsum(a, b, x);
3018

3119
return PyLong_FromLong(result);
3220
}
3321

3422
static PyMethodDef methods[] = {
35-
{"example_prod", (PyCFunction)example_prod, METH_VARARGS, NULL},
36-
{"example_sum", (PyCFunction)example_sum, METH_VARARGS, NULL},
23+
{"prodsum", (PyCFunction)example_prodsum, METH_VARARGS, NULL},
3724
{NULL, NULL, 0, NULL},
3825
};
3926

tests/packages/sharedlib-in-package/mypkg/examplelib.c

-9
This file was deleted.

tests/packages/sharedlib-in-package/mypkg/examplelib.h

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-FileCopyrightText: 2022 The meson-python developers
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include "lib.h"
6+
#include "sublib.h"
7+
8+
int prodsum(int a, int b, int x) {
9+
return prod(a, x) + b;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-FileCopyrightText: 2022 The meson-python developers
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#if defined(MYPKG_DLL_EXPORTS)
6+
#define EXPORT __declspec(dllexport)
7+
#elif defined(MYPKG_DLL_IMPORTS)
8+
#define EXPORT __declspec(dllimport)
9+
#else
10+
#define EXPORT
11+
#endif
12+
13+
EXPORT int prodsum(int a, int b, int x);

tests/packages/sharedlib-in-package/mypkg/meson.build

+22-9
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,42 @@ else
1010
import_dll_args = []
1111
endif
1212

13-
example_lib = shared_library(
14-
'examplelib',
15-
'examplelib.c',
13+
sublib = shared_library(
14+
'sublib',
15+
'sublib.c',
1616
c_args: export_dll_args,
1717
install: true,
18-
install_dir: py.get_install_dir() / 'mypkg',
18+
install_dir: py.get_install_dir() / 'mypkg/sub',
1919
)
2020

21-
example_lib_dep = declare_dependency(
21+
sublib_dep = declare_dependency(
2222
compile_args: import_dll_args,
23-
link_with: example_lib,
23+
link_with: sublib,
24+
)
25+
26+
lib = shared_library(
27+
'lib',
28+
'lib.c',
29+
dependencies: sublib_dep,
30+
c_args: export_dll_args,
31+
install_rpath: '$ORIGIN/sub',
32+
install: true,
33+
install_dir: py.get_install_dir() / 'mypkg',
2434
)
2535

26-
subdir('sub')
36+
lib_dep = declare_dependency(
37+
compile_args: import_dll_args,
38+
link_with: lib,
39+
)
2740

2841
py.extension_module(
2942
'_example',
3043
'_examplemod.c',
31-
dependencies: [example_lib_dep, example_lib2_dep],
44+
dependencies: lib_dep,
3245
include_directories: 'sub',
3346
install: true,
3447
subdir: 'mypkg',
35-
install_rpath: '$ORIGIN',
48+
install_rpath: build_machine.system() == 'darwin' ? '@loader_path' : '$ORIGIN',
3649
)
3750

3851
py.install_sources(

tests/packages/sharedlib-in-package/mypkg/sub/examplelib2.h

-7
This file was deleted.

tests/packages/sharedlib-in-package/mypkg/sub/examplelib2.c tests/packages/sharedlib-in-package/mypkg/sublib.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5-
#include "mypkg_dll.h"
5+
#include "sublib.h"
66

7-
MYPKG_DLL int prod(int a, int b) {
7+
int prod(int a, int b) {
88
return a * b;
99
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-FileCopyrightText: 2022 The meson-python developers
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#if defined(MYPKG_DLL_EXPORTS)
6+
#define EXPORT __declspec(dllexport)
7+
#elif defined(MYPKG_DLL_IMPORTS)
8+
#define EXPORT __declspec(dllimport)
9+
#else
10+
#define EXPORT
11+
#endif
12+
13+
EXPORT int prod(int a, int b);

tests/test_wheel.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,14 @@ def test_local_lib(venv, wheel_link_against_local_lib):
178178
assert int(output) == 3
179179

180180

181+
@pytest.mark.skipif(MESON_VERSION < (1, 6, 0), reason='meson too old')
181182
def test_sharedlib_in_package(venv, wheel_sharedlib_in_package):
182183
venv.pip('install', wheel_sharedlib_in_package)
183-
output = venv.python('-c', 'import mypkg; print(mypkg.example_sum(2, 5))')
184-
assert int(output) == 7
185-
output = venv.python('-c', 'import mypkg; print(mypkg.example_prod(6, 7))')
186-
assert int(output) == 42
184+
output = venv.python('-c', 'import mypkg; print(mypkg.prodsum(2, 3, 4))')
185+
assert int(output) == 11
187186

188187

189-
@pytest.mark.skipif(MESON_VERSION < (1, 3, 0), reason='Meson version too old')
188+
@pytest.mark.skipif(MESON_VERSION < (1, 3, 0), reason='meson too old')
190189
def test_link_library_in_subproject(venv, wheel_link_library_in_subproject):
191190
venv.pip('install', wheel_link_library_in_subproject)
192191
output = venv.python('-c', 'import foo; print(foo.example_sum(3, 6))')
@@ -199,7 +198,11 @@ def test_rpath(wheel_link_against_local_lib, tmp_path):
199198
artifact.extractall(tmp_path)
200199

201200
origin = '@loader_path' if sys.platform == 'darwin' else '$ORIGIN'
202-
expected = {f'{origin}/../.link_against_local_lib.mesonpy.libs', 'custom-rpath',}
201+
expected = {f'{origin}/../.link_against_local_lib.mesonpy.libs', 'rpath-from-linker-arguments',}
202+
203+
# ``install_rpath`` is supported starting with meson 1.6.0
204+
if MESON_VERSION >= (1, 6, 0):
205+
expected.add('custom-rpath')
203206

204207
rpath = set(mesonpy._rpath._get_rpath(tmp_path / 'example' / f'_example{EXT_SUFFIX}'))
205208
# Verify that rpath is a superset of the expected one: linking to

0 commit comments

Comments
 (0)