python: Add windows dll import logic

In python3.8, a change was made to the DLL search path, which requires using os.add_dll_directory to add a non-default path.
This commit is contained in:
Grant Meyerhoff 2024-07-22 16:24:06 -05:00 committed by joergho
parent fcac1a5692
commit 09f125f631
5 changed files with 62 additions and 25 deletions

View File

@ -196,11 +196,17 @@ if(ENABLE_SIM)
set(PYUHD_FILES ${PYUHD_FILES} copy_mpm_packages)
endif(ENABLE_SIM)
# This copies the contents of host/python/uhd into the build directory. We will
# use that as a staging ground for installing the final module to the system.
# This sets the UHD_INSTALL_PATH variable in the __init__.py file so that the UHD python module can find the UHD library.
set(UHD_INSTALL_PATH ${CMAKE_INSTALL_PREFIX})
set(UHD_INIT_IN "${CMAKE_CURRENT_SOURCE_DIR}/uhd/__init__.py.in")
set(UHD_INIT "${CMAKE_CURRENT_BINARY_DIR}/uhd/__init__.py")
configure_file(${UHD_INIT_IN} ${UHD_INIT} @ONLY)
# This copies the *.py and *.mako files of host/python/uhd into the build directory.
# We will use that as a staging ground for installing the final module to the system.
# We make sure that we always have an up-to-date copy in here.
add_custom_command(OUTPUT ${TIMESTAMP_FILE}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/uhd ${CMAKE_CURRENT_BINARY_DIR}/uhd
COMMAND ${CMAKE_COMMAND} -DSOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}" -DBINARY_DIR="${CMAKE_CURRENT_BINARY_DIR}" -P ${CMAKE_CURRENT_SOURCE_DIR}/copy_python_module.cmake
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} -q build
COMMAND ${CMAKE_COMMAND} -E touch ${TIMESTAMP_FILE}
DEPENDS ${PYUHD_FILES})

View File

@ -0,0 +1,11 @@
#
# Copyright 2024 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
########################################################################
set(BINARY_DIR "" CACHE STRING "")
set(SOURCE_DIR "" CACHE STRING "")
file(COPY "${SOURCE_DIR}/uhd/" DESTINATION "${BINARY_DIR}/uhd"
FILES_MATCHING PATTERN "*.py" PATTERN "*.mako")

View File

@ -1,22 +0,0 @@
#
# Copyright 2017-2018 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
"""
UHD Python API module
"""
from . import types
from . import usrp
from . import usrpctl
from . import filters
from . import rfnoc
from . import dsp
from . import chdr
from .libpyuhd.paths import *
from .libpyuhd import find
from .libpyuhd import get_version_string, get_abi_string, get_component
from .property_tree import PropertyTree
__version__ = get_version_string()

View File

@ -0,0 +1,35 @@
#
# Copyright 2017-2018 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
"""UHD Python API module."""
import os
# As of python3.8, python stopped using PATH to search for DLLs on Windows, and
# required the use of os.add_dll_directory to add paths to the DLL search.
if os.name == "nt" and hasattr(os, "add_dll_directory"):
# The UHD_PKG_PATH will be set if UHD was installed with a UHD NSIS installer, use this path to
# find the uhd.dll and add it to the DLL search.
uhd_pkg_dir = os.environ.get("UHD_PKG_PATH")
if uhd_pkg_dir is not None and os.path.exists(os.path.join(uhd_pkg_dir, "bin", "uhd.dll")):
os.add_dll_directory(os.path.join(uhd_pkg_dir, "bin"))
else:
# If UHD_PKG_PATH is not set, the UHD may have been built and installed from source.
# CMake will generate the UHD_INSTALL_PATH variable in the config_vars.py file using the
# CMAKE_INSTALL_PREFIX, try to find the uhd.dll with this path.
UHD_INSTALL_PATH = "@UHD_INSTALL_PATH@"
if os.path.exists(os.path.join(UHD_INSTALL_PATH, "bin", "uhd.dll")):
os.add_dll_directory(os.path.join(UHD_INSTALL_PATH, "bin"))
else:
print(
"Failed to add uhd.dll path to DLL search, module may not import properly, use os.add_dll_directory to add the correct path manually."
)
from . import chdr, dsp, filters, rfnoc, types, usrp, usrpctl
from .libpyuhd import find, get_abi_string, get_component, get_version_string
from .libpyuhd.paths import * # noqa: F403
from .property_tree import PropertyTree
__version__ = get_version_string()

View File

@ -1,3 +1,10 @@
#
# Copyright 2017 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
########################################################################
set(BINARY_DIR "" CACHE STRING "")
set(SOURCE_DIR "" CACHE STRING "")
file(COPY "${SOURCE_DIR}/usrp_mpm/" DESTINATION ${BINARY_DIR}/usrp_mpm