|
| 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 _append_to_sharedlib_load_path(): |
| 11 | + """ |
| 12 | + Ensure the shared libraries in this package can be loaded on Windows. |
| 13 | +
|
| 14 | + Windows lacks a concept equivalent to RPATH: Python extension modules |
| 15 | + cannot find DLLs installed outside the DLL search path. This function |
| 16 | + ensures that the location of the shared libraries distributed inside this |
| 17 | + Python package is in the DLL search path of the process. |
| 18 | +
|
| 19 | + The Windows DLL search path includes the object depending on it is located: |
| 20 | + the DLL search path needs to be augmented only when the Python extension |
| 21 | + modules and the DLLs they require are installed in separate directories. |
| 22 | + Cygwin does not have the same default library search path: all locations |
| 23 | + where the shared libraries are installed need to be added to the search |
| 24 | + path. |
| 25 | +
|
| 26 | + This function is very similar to the snippet inserted into the main |
| 27 | + ``__init__.py`` of a package by ``delvewheel`` when it vendors external |
| 28 | + shared libraries. |
| 29 | +
|
| 30 | + .. note:: |
| 31 | +
|
| 32 | + `os.add_dll_directory` is only available for Python 3.8 and later, and |
| 33 | + in the Conda ``python`` packages it works as advertised only for |
| 34 | + version 3.10 and later. For older Python versions, pre-loading the DLLs |
| 35 | + with `ctypes.WinDLL` may be preferred. |
| 36 | + """ |
| 37 | + basedir = os.path.dirname(__file__) |
| 38 | + subdir = os.path.join(basedir, 'sub') |
| 39 | + if os.name == 'nt': |
| 40 | + os.add_dll_directory(subdir) |
| 41 | + elif sys.platform == 'cygwin': |
| 42 | + os.environ['PATH'] = os.pathsep.join((os.environ['PATH'], basedir, subdir)) |
| 43 | + |
| 44 | + |
| 45 | +_append_to_sharedlib_load_path() |
| 46 | +# end-literalinclude |
| 47 | + |
| 48 | + |
| 49 | +from ._example import example_prod, example_sum #noqa: E402 |
| 50 | + |
| 51 | + |
| 52 | +__all__ = ['example_prod', 'example_sum'] |
0 commit comments