diff --git a/tests/packages/sharedlib-in-package/meson.build b/tests/packages/sharedlib-in-package/meson.build new file mode 100644 index 00000000..71921cfe --- /dev/null +++ b/tests/packages/sharedlib-in-package/meson.build @@ -0,0 +1,9 @@ +# SPDX-FileCopyrightText: 2022 The meson-python developers +# +# SPDX-License-Identifier: MIT + +project('sharedlib-in-package', 'c', version: '1.0.0') + +py = import('python').find_installation(pure: false) + +subdir('mypkg') diff --git a/tests/packages/sharedlib-in-package/mypkg/__init__.py b/tests/packages/sharedlib-in-package/mypkg/__init__.py new file mode 100644 index 00000000..e01fac67 --- /dev/null +++ b/tests/packages/sharedlib-in-package/mypkg/__init__.py @@ -0,0 +1,22 @@ +# SPDX-FileCopyrightText: 2024 The meson-python developers +# +# SPDX-License-Identifier: MIT + +def _load_sharedlib(): + """Load the `example_lib.dll` shared library on Windows + + This shared library is installed alongside this __init__.py file. Due to + lack of rpath support, Windows cannot find shared libraries installed + within wheels. So pre-load it. + """ + if os.name == "nt": + from ctypes import WinDLL + basedir = os.path.dirname(__file__) + dll_path = os.path.join(basedir, "example_lib.dll") + WinDLL(dll_path) + + +_load_sharedlib() + + +from ._example import example_sum diff --git a/tests/packages/sharedlib-in-package/mypkg/_examplemod.c b/tests/packages/sharedlib-in-package/mypkg/_examplemod.c new file mode 100644 index 00000000..d6955699 --- /dev/null +++ b/tests/packages/sharedlib-in-package/mypkg/_examplemod.c @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2022 The meson-python developers +// +// SPDX-License-Identifier: MIT + +#include + +#include "examplelib.h" + +static PyObject* example_sum(PyObject* self, PyObject *args) +{ + int a, b; + if (!PyArg_ParseTuple(args, "ii", &a, &b)) { + return NULL; + } + + long result = sum(a, b); + + return PyLong_FromLong(result); +} + +static PyMethodDef methods[] = { + {"example_sum", (PyCFunction)example_sum, METH_VARARGS, NULL}, + {NULL, NULL, 0, NULL}, +}; + +static struct PyModuleDef module = { + PyModuleDef_HEAD_INIT, + "_example", + NULL, + -1, + methods, +}; + +PyMODINIT_FUNC PyInit__example(void) +{ + return PyModule_Create(&module); +} diff --git a/tests/packages/sharedlib-in-package/mypkg/examplelib.c b/tests/packages/sharedlib-in-package/mypkg/examplelib.c new file mode 100644 index 00000000..9ba359db --- /dev/null +++ b/tests/packages/sharedlib-in-package/mypkg/examplelib.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2022 The meson-python developers +// +// SPDX-License-Identifier: MIT + +int sum(int a, int b) { + return a + b; +} diff --git a/tests/packages/sharedlib-in-package/mypkg/examplelib.h b/tests/packages/sharedlib-in-package/mypkg/examplelib.h new file mode 100644 index 00000000..74c8645b --- /dev/null +++ b/tests/packages/sharedlib-in-package/mypkg/examplelib.h @@ -0,0 +1,5 @@ +// SPDX-FileCopyrightText: 2022 The meson-python developers +// +// SPDX-License-Identifier: MIT + +int sum(int a, int b); diff --git a/tests/packages/sharedlib-in-package/mypkg/meson.build b/tests/packages/sharedlib-in-package/mypkg/meson.build new file mode 100644 index 00000000..2b575acf --- /dev/null +++ b/tests/packages/sharedlib-in-package/mypkg/meson.build @@ -0,0 +1,24 @@ +# SPDX-FileCopyrightText: 2022 The meson-python developers +# +# SPDX-License-Identifier: MIT + +example_lib = shared_library( + 'examplelib', + 'examplelib.c', + install: true, + install_dir: py.get_install_dir() / 'mypkg', +) + +py.extension_module( + '_example', + '_examplemod.c', + link_with: example_lib, + install: true, + subdir: 'mypkg', + install_rpath: '$ORIGIN', +) + +py.install_sources( + ['__init__.py'], + subdir: 'mypkg', +) diff --git a/tests/packages/sharedlib-in-package/pyproject.toml b/tests/packages/sharedlib-in-package/pyproject.toml new file mode 100644 index 00000000..2542e439 --- /dev/null +++ b/tests/packages/sharedlib-in-package/pyproject.toml @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: 2022 The meson-python developers +# +# SPDX-License-Identifier: MIT + +[build-system] +build-backend = 'mesonpy' +requires = ['meson-python'] diff --git a/tests/test_wheel.py b/tests/test_wheel.py index 70702248..62e6c5b6 100644 --- a/tests/test_wheel.py +++ b/tests/test_wheel.py @@ -161,6 +161,12 @@ def test_local_lib(venv, wheel_link_against_local_lib): assert int(output) == 3 +def test_sharedlib_in_package(venv, wheel_sharedlib_in_package): + venv.pip('install', wheel_sharedlib_in_package) + output = venv.python('-c', 'import mypkg; print(mypkg.example_sum(2, 5))') + assert int(output) == 7 + + @pytest.mark.skipif(sys.platform not in {'linux', 'darwin'}, reason='Not supported on this platform') def test_rpath(wheel_link_against_local_lib, tmp_path): artifact = wheel.wheelfile.WheelFile(wheel_link_against_local_lib)