Skip to content

Commit

Permalink
WIP: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Daniele Nicolodi <[email protected]>
  • Loading branch information
rgommers and dnicolodi authored Jan 29, 2025
1 parent e577eac commit c73c25f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docs/how-to-guides/shared-libraries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ the lack of RPATH support:

If an internal shared library is not only used as part of a Python package, but
for example also as a regular shared library in a C/C++ project or as a
standalone library, then the method shown above won't work. The library is`
standalone library, then the method shown above won't work. The library is
then marked for installation into the system default ``libdir`` location.
Actually installing into ``libdir`` isn't possible with wheels, hence
``meson-python`` will instead do the following *on platforms other than
Expand Down Expand Up @@ -125,7 +125,7 @@ RPATH entry - and Meson will not automatically manage RPATH entries for you.
Hence you'll need to add the needed RPATH yourself, for example by adding
``-Wl,rpath=/path/to/dir/sharedlib/is/in`` to ``LDFLAGS`` before starting
the build. In case you run into this problem after a wheel is built and
installed, adding that same path to ``LD_LIBRARY_PATH`` is a quick way of
installed, adding that same path to the ``LD_LIBRARY_PATH`` environment variable is a quick way of
checking if that is indeed the problem.

On Windows, the solution is similar - the shared library can either be
Expand Down
39 changes: 19 additions & 20 deletions tests/packages/sharedlib-in-package/mypkg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@
# start-literalinclude
def _enable_sharedlib_loading():
"""
Ensure the shared libraries in this dir and the ``sub`` subdir can be
loaded on Windows.
One shared library is installed alongside this ``__init__.py`` file.
Windows can load it because it searches for DLLs in the directory where a
``.pyd`` (Python extension module) is located in. Cygwin does not though.
For a shared library in another directory inside the package, Windows also
needs a hint.
This function is Windows-specific due to lack of RPATH support on Windows.
It cannot find shared libraries installed within wheels unless we either
amend the DLL search path or pre-load the DLL.
Note that ``delvewheel`` inserts a similar snippet into the main
``__init__.py`` of a package when it vendors external shared libraries.
Ensure the shared libraries in this package loaded on Windows.
Windows lacks a concept equivalent to RPATH: Python extension modules cannot
find DLLs installed outside the DLL search path. This function ensured that the
location of the shared libraries distributed with this Python wheel is in the DLL
search path of the process.
The Windows DLL search path includes the object depending on it is located:
the DLL search path needs to be augmented only when the Python extension
modules and the DLLs they require are installed in separate directories.
Cygwin does not have the same default library search path: all locations where
the shared libraries are installed need to be added to the search path.
This function is very similar to the snippet inserted into the main ``__init__.py``
of a package by ``delvewheel`` when it vendors external shared libraries.
.. note::
`os.add_dll_directory` is only available for Python >=3.8, and with
the Conda ``python`` packages only works as advertised for >=3.10.
If you require support for older versions, pre-loading the DLL
with `ctypes.WinDLL` may be preferred (the SciPy code base has an
example of this).
`os.add_dll_directory` is only available for Python 3.8 and later, and in
the Conda ``python`` packages it works as advertised only for version
3.10 and later. For older Python versions, pre-loading the DLLs with
`ctypes.WinDLL` may be preferred.
"""
basedir = os.path.dirname(__file__)
subdir = os.path.join(basedir, 'sub')
Expand Down

0 comments on commit c73c25f

Please sign in to comment.