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