2017-02-09 19:16:16 +00:00
|
|
|
#
|
2017-05-02 18:10:05 +00:00
|
|
|
# Copyright 2017-2018 Ettus Research, a National Instruments Company
|
2017-02-09 19:16:16 +00:00
|
|
|
#
|
2017-05-02 18:10:05 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2017-02-09 19:16:16 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
# This file included, use CMake directory variables
|
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
|
|
PYTHON_CHECK_MODULE(
|
2017-05-02 18:10:05 +00:00
|
|
|
"virtualenv"
|
|
|
|
|
"sys" "hasattr(sys, 'real_prefix')"
|
|
|
|
|
HAVE_PYTHON_VIRTUALENV
|
|
|
|
|
)
|
|
|
|
|
|
2017-02-09 19:16:16 +00:00
|
|
|
# Get include dirs
|
2018-11-14 05:53:22 +00:00
|
|
|
include_directories(${PYTHON_INCLUDE_DIRS})
|
2019-02-04 15:00:16 +00:00
|
|
|
set(PYBIND11_INCLUDE_DIR
|
|
|
|
|
"${CMAKE_SOURCE_DIR}/lib/deps/pybind11/include"
|
|
|
|
|
CACHE
|
|
|
|
|
STRING
|
|
|
|
|
"Location of PyBind11 includes"
|
|
|
|
|
)
|
|
|
|
|
include_directories(${PYBIND11_INCLUDE_DIR})
|
2018-11-14 05:53:22 +00:00
|
|
|
execute_process(
|
2017-02-09 19:16:16 +00:00
|
|
|
COMMAND "${PYTHON_EXECUTABLE}" -c
|
2020-05-07 16:43:15 +00:00
|
|
|
"try:\n import numpy\n import os\n inc_path = numpy.get_include()\n if os.path.exists(os.path.join(inc_path, 'numpy', 'arrayobject.h')):\n print(inc_path, end='')\nexcept:\n pass"
|
2017-02-09 19:16:16 +00:00
|
|
|
OUTPUT_VARIABLE PYTHON_NUMPY_INCLUDE_DIR)
|
|
|
|
|
|
2017-05-02 18:10:05 +00:00
|
|
|
# Build pyuhd library
|
2018-11-14 05:53:22 +00:00
|
|
|
add_library(pyuhd SHARED pyuhd.cpp)
|
2019-11-01 14:32:24 +00:00
|
|
|
# python expects extension modules with a particular suffix
|
|
|
|
|
if(WIN32)
|
|
|
|
|
set_target_properties(pyuhd PROPERTIES PREFIX "lib" SUFFIX ".pyd")
|
|
|
|
|
else()
|
2019-11-20 21:10:05 +00:00
|
|
|
execute_process(
|
|
|
|
|
COMMAND "${PYTHON_EXECUTABLE}" -c
|
|
|
|
|
"from distutils.sysconfig import get_config_var; print(get_config_var('EXT_SUFFIX'))"
|
|
|
|
|
OUTPUT_VARIABLE PYTHON_EXTENSION_SUFFIX
|
|
|
|
|
)
|
|
|
|
|
string(STRIP ${PYTHON_EXTENSION_SUFFIX} PYTHON_EXTENSION_SUFFIX)
|
|
|
|
|
if(${PYTHON_EXTENSION_SUFFIX} STREQUAL "None")
|
|
|
|
|
set(PYTHON_EXTENSION_SUFFIX ${CMAKE_SHARED_MODULE_SUFFIX})
|
|
|
|
|
endif()
|
python: Fix RPATH for the Python library
On UNIX systems, CMake will set the RPATH of the Python library to the
build directory, which is very helpful because it allows unit and other
tests to be executed from within the build directory.
On installation, the RPATH is removed, but only if install(TARGETS) is
used, which we were not, thus resulting in incorrect RPATHs.
This would surface as a bug, too. Calling uhd.get_lib_path() would
always point to the build directory, thus resulting in no FPGA images
being found automatically, e.g. when running a B200 through the Python
API.
This change installs the Python .so file separately, using the correct
CMake mechanisms.
2020-05-01 17:53:00 +00:00
|
|
|
set_target_properties(pyuhd
|
|
|
|
|
PROPERTIES
|
|
|
|
|
PREFIX "lib"
|
|
|
|
|
SUFFIX ${PYTHON_EXTENSION_SUFFIX}
|
|
|
|
|
)
|
2019-11-01 14:32:24 +00:00
|
|
|
endif(WIN32)
|
2018-11-14 05:53:22 +00:00
|
|
|
target_include_directories(pyuhd PUBLIC
|
2017-02-09 19:16:16 +00:00
|
|
|
${PYTHON_NUMPY_INCLUDE_DIR}
|
|
|
|
|
${CMAKE_SOURCE_DIR}/lib
|
2019-02-04 15:00:16 +00:00
|
|
|
${PYBIND11_INCLUDE_DIR}
|
2017-02-09 19:16:16 +00:00
|
|
|
)
|
2018-07-26 01:23:31 +00:00
|
|
|
|
2019-10-31 21:02:07 +00:00
|
|
|
if(WIN32)
|
|
|
|
|
target_link_libraries(pyuhd ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} uhd)
|
|
|
|
|
else()
|
|
|
|
|
# for extension module, proper to NOT link against python library and instead
|
|
|
|
|
# add dynamic lookup link option on OSX
|
|
|
|
|
target_link_libraries(pyuhd ${Boost_LIBRARIES} uhd)
|
|
|
|
|
if(APPLE)
|
|
|
|
|
target_link_options(pyuhd PRIVATE "LINKER:-undefined,dynamic_lookup")
|
|
|
|
|
endif(APPLE)
|
|
|
|
|
endif(WIN32)
|
2017-02-09 19:16:16 +00:00
|
|
|
# Copy pyuhd library to the staging directory
|
2018-11-14 05:53:22 +00:00
|
|
|
add_custom_command(TARGET pyuhd
|
2019-11-01 14:32:24 +00:00
|
|
|
POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pyuhd> ${CMAKE_CURRENT_BINARY_DIR}/uhd/$<TARGET_FILE_NAME:pyuhd>)
|
2017-02-09 19:16:16 +00:00
|
|
|
|
2020-03-09 21:43:10 +00:00
|
|
|
# List of Python files that are part of the module but don't get
|
|
|
|
|
# generated during build time.
|
|
|
|
|
# Note: When adding Python files into uhd/, they don't get added to the
|
|
|
|
|
# dependency list until CMake is re-run.
|
|
|
|
|
file(GLOB_RECURSE PYUHD_FILES
|
|
|
|
|
${CMAKE_CURRENT_SOURCE_DIR}/uhd/*.py
|
2017-02-09 19:16:16 +00:00
|
|
|
)
|
|
|
|
|
|
2018-11-14 05:53:22 +00:00
|
|
|
set(SETUP_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in")
|
|
|
|
|
set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
|
|
|
|
|
set(TIMESTAMP_FILE "${CMAKE_CURRENT_BINARY_DIR}/build/timestamp")
|
2019-02-25 06:54:25 +00:00
|
|
|
# convert binary directory to native format to use in SETUP_PY file.
|
|
|
|
|
file(TO_NATIVE_PATH ${CMAKE_CURRENT_BINARY_DIR} NATIVE_CURRENT_BINARY_DIR)
|
2018-11-14 05:53:22 +00:00
|
|
|
configure_file(${SETUP_PY_IN} ${SETUP_PY})
|
2017-02-09 19:16:16 +00:00
|
|
|
|
2018-11-14 05:53:22 +00:00
|
|
|
add_custom_command(OUTPUT ${TIMESTAMP_FILE}
|
2020-03-09 21:43:10 +00:00
|
|
|
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/uhd ${CMAKE_CURRENT_BINARY_DIR}/uhd
|
2018-07-27 14:47:31 +00:00
|
|
|
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} -q build
|
2017-05-02 18:10:05 +00:00
|
|
|
COMMAND ${CMAKE_COMMAND} -E touch ${TIMESTAMP_FILE}
|
|
|
|
|
DEPENDS ${PYUHD_FILES})
|
2017-02-09 19:16:16 +00:00
|
|
|
|
2018-11-14 05:53:22 +00:00
|
|
|
add_custom_target(pyuhd_library ALL DEPENDS ${TIMESTAMP_FILE} pyuhd)
|
|
|
|
|
if(HAVE_PYTHON_VIRTUALENV)
|
2018-07-26 17:53:17 +00:00
|
|
|
# In virtualenvs, let setuptools do its thing
|
2018-11-14 05:53:22 +00:00
|
|
|
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} -q install --force)")
|
|
|
|
|
else()
|
2018-07-26 17:53:17 +00:00
|
|
|
# Otherwise, use distutils to determine the correct relative path for Python
|
|
|
|
|
# packages, and install to our prefix
|
2018-11-14 05:53:22 +00:00
|
|
|
if(NOT DEFINED UHD_PYTHON_DIR)
|
2020-08-26 17:20:42 +00:00
|
|
|
if(WIN32)
|
|
|
|
|
# CPack with NSIS generates an error when using install()
|
|
|
|
|
# with a DESTINATION that is an absolute path. Thus, specify
|
|
|
|
|
# a blank prefix, which returns only the Python library relative
|
|
|
|
|
# path, and use that in the install step below.
|
|
|
|
|
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
|
|
|
|
|
"from distutils import sysconfig;\
|
|
|
|
|
print(sysconfig.get_python_lib(plat_specific=True, prefix=''));"
|
|
|
|
|
OUTPUT_VARIABLE UHD_PYTHON_DIR OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
|
|
|
)
|
|
|
|
|
else()
|
|
|
|
|
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
|
|
|
|
|
"from distutils import sysconfig;\
|
|
|
|
|
print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'));"
|
|
|
|
|
OUTPUT_VARIABLE UHD_PYTHON_DIR OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
|
|
|
)
|
|
|
|
|
endif(WIN32)
|
2018-11-14 05:53:22 +00:00
|
|
|
endif(NOT DEFINED UHD_PYTHON_DIR)
|
2018-07-26 17:53:17 +00:00
|
|
|
file(TO_CMAKE_PATH ${UHD_PYTHON_DIR} UHD_PYTHON_DIR)
|
|
|
|
|
|
2018-11-14 05:53:22 +00:00
|
|
|
message(STATUS "Utilizing the python install directory: ${UHD_PYTHON_DIR}")
|
python: Fix RPATH for the Python library
On UNIX systems, CMake will set the RPATH of the Python library to the
build directory, which is very helpful because it allows unit and other
tests to be executed from within the build directory.
On installation, the RPATH is removed, but only if install(TARGETS) is
used, which we were not, thus resulting in incorrect RPATHs.
This would surface as a bug, too. Calling uhd.get_lib_path() would
always point to the build directory, thus resulting in no FPGA images
being found automatically, e.g. when running a B200 through the Python
API.
This change installs the Python .so file separately, using the correct
CMake mechanisms.
2020-05-01 17:53:00 +00:00
|
|
|
# CMake will create an up-to-date copy of the entire Python module within
|
|
|
|
|
# the build directory. Instead of using setuptools, we use distutils (above)
|
|
|
|
|
# to figure out the destination path, and then we simply copy this module
|
|
|
|
|
# recursively into its final destination.
|
|
|
|
|
install(DIRECTORY
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/uhd
|
|
|
|
|
DESTINATION ${UHD_PYTHON_DIR}
|
|
|
|
|
COMPONENT pythonapi
|
|
|
|
|
)
|
|
|
|
|
# On Linux/Unix systems, we must properly install the library file, though.
|
|
|
|
|
# install(DIRECTORY) will treat the .so file like any other file, which
|
|
|
|
|
# means it won't update its RPATH, and thus the RPATH would be stuck to the
|
|
|
|
|
# build directory.
|
|
|
|
|
if(UNIX)
|
|
|
|
|
install(TARGETS pyuhd
|
|
|
|
|
DESTINATION ${UHD_PYTHON_DIR}/uhd
|
|
|
|
|
)
|
|
|
|
|
endif(UNIX)
|
2018-11-14 05:53:22 +00:00
|
|
|
endif(HAVE_PYTHON_VIRTUALENV)
|