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