|
| 1 | +# SPDX-FileCopyrightText: 2024 The meson-python developers |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +# start-literalinclude |
| 10 | +def _enable_sharedlib_loading(): |
| 11 | + """ |
| 12 | + Ensure the shared libraries in this dir and the ``sub`` subdir can be |
| 13 | + loaded on Windows. |
| 14 | +
|
| 15 | + One shared library is installed alongside this ``__init__.py`` file. |
| 16 | + Windows can load it because it searches for DLLs in the directory where a |
| 17 | + ``.pyd`` (Python extension module) is located in. Cygwin does not though. |
| 18 | + For a shared library in another directory inside the package, Windows also |
| 19 | + needs a hint. |
| 20 | +
|
| 21 | + This function is Windows-specific due to lack of RPATH support on Windows. |
| 22 | + It cannot find shared libraries installed within wheels unless we either |
| 23 | + amend the DLL search path or pre-load the DLL. |
| 24 | +
|
| 25 | + Note that ``delvewheel`` inserts a similar snippet into the main |
| 26 | + ``__init__.py`` of a package when it vendors external shared libraries. |
| 27 | +
|
| 28 | + .. note:: |
| 29 | +
|
| 30 | + `os.add_dll_directory` is only available for Python >=3.8, and with |
| 31 | + the Conda ``python`` packages only works as advertised for >=3.10. |
| 32 | + If you require support for older versions, pre-loading the DLL |
| 33 | + with `ctypes.WinDLL` may be preferred (the SciPy code base has an |
| 34 | + example of this). |
| 35 | + """ |
| 36 | + basedir = os.path.dirname(__file__) |
| 37 | + subdir = os.path.join(basedir, 'sub') |
| 38 | + if os.name == 'nt': |
| 39 | + os.add_dll_directory(subdir) |
| 40 | + elif sys.platform == 'cygwin': |
| 41 | + os.environ['PATH'] = f'os.environ["PATH"]:{basedir}:{subdir}' |
| 42 | + |
| 43 | + |
| 44 | +_enable_sharedlib_loading() |
| 45 | +# end-literalinclude |
| 46 | + |
| 47 | + |
| 48 | +from ._example import example_prod, example_sum #noqa: E402 |
| 49 | + |
| 50 | + |
| 51 | +__all__ = ['example_prod', 'example_sum'] |
0 commit comments