From bf6ff44306a7f4c31ca604120bae3143efba183f Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Thu, 24 Oct 2024 17:52:03 +0200 Subject: [PATCH 1/2] Fix loading of Python bindings on Windows when installed in arbitrary directory --- bindings/python/CMakeLists.txt | 26 ++++++++++++++++++++++++++ bindings/yarp.i | 9 +++++++++ 2 files changed, 35 insertions(+) diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index a98282717f..0814451d88 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -99,6 +99,32 @@ if(YARP_PYTHON_PIP_METADATA_INSTALL) DESTINATION ${CMAKE_INSTALL_PYTHON3DIR}/yarp-${YARP_VERSION_SHORT}.dist-info) endif() +# We generate some preable for the yarp.py library so that the dll are correctly loaded on Windows +set(swig_python_windows_preable_file ${CMAKE_CURRENT_BINARY_DIR}/swig_python_windows_preable.i) +file(WRITE ${swig_python_windows_preable_file} "") +# If we are on Windows and BUILD_SHARED_LIBS is ON, handle the fact that +# the Python interpreter does not look into PATH to find dll (see https://docs.python.org/3.8/library/os.html#os.add_dll_directory) +if(WIN32 AND BUILD_SHARED_LIBS) + if(IS_ABSOLUTE ${CMAKE_INSTALL_PYTHON3DIR}) + set(PYTHON_FULL_INSTDIR "${CMAKE_INSTALL_PYTHON3DIR}") + else() + set(PYTHON_FULL_INSTDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_PYTHON3DIR}") + endif() + file(RELATIVE_PATH RELATIVE_PATH_BETWEEN_INIT_PY_AND_DLL_DIRECTORY ${PYTHON_FULL_INSTDIR} ${CMAKE_INSTALL_FULL_BINDIR}) + file(APPEND ${swig_python_windows_preable_file} "%pythonbegin %{\n") + file(APPEND ${swig_python_windows_preable_file} "import os\n") + file(APPEND ${swig_python_windows_preable_file} "library_dll_path = os.path.join(os.path.dirname(__file__),'${RELATIVE_PATH_BETWEEN_INIT_PY_AND_DLL_DIRECTORY}')\n") + file(APPEND ${swig_python_windows_preable_file} "# Avoid to call add_dll_directory if not necessary,\n") + file(APPEND ${swig_python_windows_preable_file} "# for example if the library to find are already found in the proper location in a conda environment\n") + file(APPEND ${swig_python_windows_preable_file} "if(library_dll_path != os.path.join(os.environ.get('CONDA_PREFIX', ''),'Library','bin') and library_dll_path != os.path.join(os.environ.get('CONDA_PREFIX', ''),'bin')):\n") + file(APPEND ${swig_python_windows_preable_file} " if(os.path.exists(library_dll_path)):\n") + file(APPEND ${swig_python_windows_preable_file} " os.add_dll_directory(library_dll_path)\n") + file(APPEND ${swig_python_windows_preable_file} "%}\n") +endif() +# Make sure that the created file can be found by swig while parsing the main .i file +set_property(TARGET ${SWIG_MODULE_yarp_python_REAL_NAME} APPEND PROPERTY SWIG_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_BINARY_DIR}) + + if(YARP_COMPILE_TESTS) add_subdirectory(tests) endif() diff --git a/bindings/yarp.i b/bindings/yarp.i index 72b58a0d83..5f834d2451 100644 --- a/bindings/yarp.i +++ b/bindings/yarp.i @@ -2003,3 +2003,12 @@ public: return result; } } + +////////////////////////////////////////////////////////////////////////// +// Just in Python add some code to automatically call +// add_dll_directory as necessary +// See https://github.com/robotology/robotology-superbuild/issues/1268 +// for more details +#ifdef SWIGPYTHON +%include +#endif From 62cfa4bc7aea4f6ff9235430af94c93b3cd40289 Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Thu, 24 Oct 2024 17:55:50 +0200 Subject: [PATCH 2/2] Document fix of Python bindings on Windows --- doc/release/master.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/release/master.md b/doc/release/master.md index 6c4d043b47..3d19525dcd 100644 --- a/doc/release/master.md +++ b/doc/release/master.md @@ -118,3 +118,9 @@ New Features * Improvements to serialization class `yarp::sig::Sound` (breaking change) * yarp_sig can now use IDL thrift to generate custom data types. * The following data types have been migrated from `yarp_dev` to `yarp_sig` library: `AudioPlayerStatus, AudioRecorderStatus, AudioBufferSize, AudioBufferSizeData, LaserMeasurementData, LaserScan2D`. + +### Bindings + +#### Python + +* Fix loading of Python bindings on Windows when installed in arbitrary directory.