aboutsummaryrefslogtreecommitdiffstats
path: root/host/examples/rfnoc-example/python/CMakeLists.txt
blob: 3290611f4fe6a76f2bf07c9203e41ecbc378d07b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
# Copyright 2024 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

###############################################################################
# Build Python wrapper module for C++ -> Python bindings
###############################################################################
# Global Python API constants
set(PYMODULE_NAME rfnoc_example)
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")
# convert binary directory to native format to use in SETUP_PY file.
file(TO_NATIVE_PATH ${CMAKE_CURRENT_BINARY_DIR} NATIVE_CURRENT_BINARY_DIR)
configure_file(${SETUP_PY_IN} ${SETUP_PY})


###############################################################################
# Build Python wrapper module for C++ -> Python bindings
###############################################################################
pybind11_add_module(${PYMODULE_NAME}_python
  MODULE
  pyrfnoc-example.cpp
)
target_include_directories(${PYMODULE_NAME}_python
  PUBLIC
  ${CMAKE_SOURCE_DIR}/lib
  ${CMAKE_SOURCE_DIR}/include
)
target_link_libraries(
  ${PYMODULE_NAME}_python
  PRIVATE
  pybind11::pybind11
  uhd
)

# Copy pybind bindings library to the staging directory (it will get copied to
# its final destination further down)
add_custom_command(TARGET ${PYMODULE_NAME}_python
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PYMODULE_NAME}_python> ${CMAKE_CURRENT_BINARY_DIR}/${PYMODULE_NAME}/$<TARGET_FILE_NAME:${PYMODULE_NAME}_python>)


# List of Python files that are part of the module but don't get
# generated during build time.
# Note: When adding Python files into the module, they don't get added to the
# dependency list until CMake is re-run.
file(GLOB_RECURSE PYMODULE_FILE
    ${CMAKE_CURRENT_SOURCE_DIR}/${PYMODULE_NAME}/*.py
)

# If we're not in a virtual environment, then we need to figure out where to
# install the Python module.
if(NOT DEFINED UHD_PYTHON_DIR)
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
        # Avoid the posix_local install scheme
        "import os,sysconfig;\
        install_scheme = 'posix_prefix';\
        platlib = sysconfig.get_path('platlib', scheme=install_scheme);\
        prefix = sysconfig.get_config_var('prefix');\
        print(os.path.relpath(platlib, prefix));"
        OUTPUT_VARIABLE UHD_PYTHON_DIR OUTPUT_STRIP_TRAILING_WHITESPACE
    )
endif(NOT DEFINED UHD_PYTHON_DIR)

# This copies the contents of the Python module 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}/${PYMODULE_NAME} ${CMAKE_CURRENT_BINARY_DIR}/${PYMODULE_NAME}
  COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} -q build
  COMMAND ${CMAKE_COMMAND} -E touch ${TIMESTAMP_FILE}
  DEPENDS ${PYMODULE_FILE})

add_custom_target(pymodule_library
    ALL DEPENDS ${TIMESTAMP_FILE} ${PYMODULE_NAME}_python)

# Now install the Python module from the build directory to the final destination.
PYTHON_INSTALL_MODULE(
    MODULE "${PYMODULE_NAME}"
    LIBTARGET "${PYMODULE_NAME}_python"
)