aboutsummaryrefslogtreecommitdiffstats
path: root/host/cmake/Modules/UHDPython.cmake
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2019-04-11 09:10:53 -0700
committerMartin Braun <martin.braun@ettus.com>2019-05-24 14:17:13 -0700
commitf83faf28b3424ba60c3bdc40d408011c9c619c8a (patch)
tree9f3750f097feabe8c9493b8b5b9cc99bab3e7bd2 /host/cmake/Modules/UHDPython.cmake
parent6563c53743617215a18542db7d7050a04a0d409d (diff)
downloaduhd-f83faf28b3424ba60c3bdc40d408011c9c619c8a.tar.gz
uhd-f83faf28b3424ba60c3bdc40d408011c9c619c8a.tar.bz2
uhd-f83faf28b3424ba60c3bdc40d408011c9c619c8a.zip
cmake: Remove ENABLE_PYTHON3 flag and simplify Python detection
- Makes use of more modern find_package(Python2/3) if available - Moves almost all Python-related code to UHDPython.cmake - ENABLE_PYTHON3 is no longer necessary
Diffstat (limited to 'host/cmake/Modules/UHDPython.cmake')
-rw-r--r--host/cmake/Modules/UHDPython.cmake180
1 files changed, 148 insertions, 32 deletions
diff --git a/host/cmake/Modules/UHDPython.cmake b/host/cmake/Modules/UHDPython.cmake
index 5ac2d991b..a93761131 100644
--- a/host/cmake/Modules/UHDPython.cmake
+++ b/host/cmake/Modules/UHDPython.cmake
@@ -1,6 +1,7 @@
#
# Copyright 2010-2011 Ettus Research LLC
# Copyright 2018 Ettus Research, a National Instruments Company
+# Copyright 2019 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
@@ -9,62 +10,121 @@ if(NOT DEFINED INCLUDED_UHD_PYTHON_CMAKE)
set(INCLUDED_UHD_PYTHON_CMAKE TRUE)
########################################################################
-# Setup Python
+# Setup Python Part 1: Find the interpreters
########################################################################
message(STATUS "")
-message(STATUS "Configuring the python interpreter...")
+message(STATUS "Configuring the Python interpreter...")
#this allows the user to override PYTHON_EXECUTABLE
if(PYTHON_EXECUTABLE)
-
set(PYTHONINTERP_FOUND TRUE)
+endif(PYTHON_EXECUTABLE)
-#otherwise if not set, try to automatically find it
-else(PYTHON_EXECUTABLE)
-
- #use the built-in find script
- if(ENABLE_PYTHON3)
- find_package(PythonInterp 3.0)
- else(ENABLE_PYTHON3)
- find_package(PythonInterp 2.0)
- endif(ENABLE_PYTHON3)
-
- #and if that fails use the find program routine
- if(NOT PYTHONINTERP_FOUND)
- if(ENABLE_PYTHON3)
- find_program(PYTHON_EXECUTABLE NAMES python3 python3.5 python3.6)
- else(ENABLE_PYTHON3)
- find_program(PYTHON_EXECUTABLE NAMES python2 python2.7)
- endif(ENABLE_PYTHON3)
-
- if(PYTHON_EXECUTABLE)
- set(PYTHONINTERP_FOUND TRUE)
- endif(PYTHON_EXECUTABLE)
- endif(NOT PYTHONINTERP_FOUND)
+# We always try to find Py3k first. Once we only support Py3k we can remove
+# most of this.
+if(NOT PYTHONINTERP_FOUND)
+ find_package(Python3 ${PYTHON3_MIN_VERSION} QUIET)
+ if(Python3_Interpreter_FOUND)
+ set(PYTHON_MIN_VERSION ${PYTHON3_MIN_VERSION})
+ set(PYTHON_VERSION ${Python3_VERSION})
+ set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
+ set(PYTHONINTERP_FOUND TRUE)
+ endif(Python3_Interpreter_FOUND)
+endif(NOT PYTHONINTERP_FOUND)
-endif(PYTHON_EXECUTABLE)
+if(NOT PYTHONINTERP_FOUND)
+ find_package(PythonInterp ${PYTHON3_MIN_VERSION} QUIET)
+ if(PYTHONINTERP_FOUND)
+ set(PYTHON_MIN_VERSION ${PYTHON3_MIN_VERSION})
+ set(PYTHON_VERSION ${PYTHON_VERSION_STRING})
+ endif(PYTHONINTERP_FOUND)
+endif(NOT PYTHONINTERP_FOUND)
-#make the path to the executable appear in the cmake gui
-set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE} CACHE FILEPATH
- "python buildtime interpreter")
+# Next, try and find Py2k.
+if(NOT PYTHONINTERP_FOUND)
+ find_package(Python2 ${PYTHON_MIN_VERSION} QUIET)
+ if(Python2_Interpreter_FOUND)
+ set(PYTHON_VERSION ${Python2_VERSION})
+ set(PYTHON_EXECUTABLE ${Python2_EXECUTABLE})
+ set(PYTHONINTERP_FOUND TRUE)
+ endif(Python2_Interpreter_FOUND)
+endif(NOT PYTHONINTERP_FOUND)
-message(STATUS "Python interpreter: ${PYTHON_EXECUTABLE}")
-message(STATUS "Override with: -DPYTHON_EXECUTABLE=<path-to-python>")
+if(NOT PYTHONINTERP_FOUND)
+ find_package(PythonInterp ${PYTHON_MIN_VERSION} QUIET)
+ set(PYTHON_VERSION ${PYTHON_VERSION_STRING})
+endif(NOT PYTHONINTERP_FOUND)
+# If that fails, try using the build-in find program routine.
+if(NOT PYTHONINTERP_FOUND)
+ message(STATUS "Attempting to find Python without CMake...")
+ find_program(PYTHON_EXECUTABLE NAMES python3 python3.5 python3.6 python3.7 python3.8)
+ if(PYTHON_EXECUTABLE)
+ set(PYTHONINTERP_FOUND TRUE)
+ set(PYTHON_MIN_VERSION ${PYTHON3_MIN_VERSION})
+ endif(PYTHON_EXECUTABLE)
+endif(NOT PYTHONINTERP_FOUND)
+
+if(NOT PYTHONINTERP_FOUND)
+ message(STATUS "Attempting to find Python without CMake...")
+ find_program(PYTHON_EXECUTABLE NAMES python2 python2.7)
+ if(PYTHON_EXECUTABLE)
+ set(PYTHONINTERP_FOUND TRUE)
+ endif(PYTHON_EXECUTABLE)
+endif(NOT PYTHONINTERP_FOUND)
+
+if(NOT PYTHON_VERSION)
+ message(STATUS "Manually determining build Python version...")
+ execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "
+from __future__ import print_function
+import sys
+print('{}.{}.{}'.format(
+ sys.version_info.major,
+ sys.version_info.minor,
+ sys.version_info.micro))"
+ OUTPUT_VARIABLE PYTHON_VERSION
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+endif(NOT PYTHON_VERSION)
+
+# If we still haven't found a Python interpreter, then we're done.
if(NOT PYTHONINTERP_FOUND)
message(FATAL_ERROR "Error: Python interpreter required by the build system.")
endif(NOT PYTHONINTERP_FOUND)
+if(NOT PYTHON_EXECUTABLE)
+ message(FATAL_ERROR "Error: Python interpreter required by the build system.")
+endif(NOT PYTHON_EXECUTABLE)
+
+#make the path to the executable appear in the cmake gui
+set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE} CACHE FILEPATH
+ "python buildtime interpreter")
+
+message(STATUS "Python interpreter: ${PYTHON_EXECUTABLE} Version: ${PYTHON_VERSION}")
+message(STATUS "Override with: -DPYTHON_EXECUTABLE=<path-to-python>")
#this allows the user to override RUNTIME_PYTHON_EXECUTABLE
if(NOT RUNTIME_PYTHON_EXECUTABLE)
#default to the buildtime interpreter
set(RUNTIME_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
+ set(RUNTIME_PYTHON_VERSION ${PYTHON_VERSION})
endif(NOT RUNTIME_PYTHON_EXECUTABLE)
+if(NOT RUNTIME_PYTHON_VERSION)
+ message(STATUS "Manually determining runtime Python version...")
+ execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "
+from __future__ import print_function
+import sys
+print('{}.{}.{}'.format(
+ sys.version_info.major,
+ sys.version_info.minor,
+ sys.version_info.micro))"
+ OUTPUT_VARIABLE RUNTIME_PYTHON_VERSION
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+endif(NOT RUNTIME_PYTHON_VERSION)
+
#make the path to the executable appear in the cmake gui
set(RUNTIME_PYTHON_EXECUTABLE ${RUNTIME_PYTHON_EXECUTABLE} CACHE FILEPATH
"python runtime interpreter")
-message(STATUS "Python runtime interpreter: ${RUNTIME_PYTHON_EXECUTABLE}")
+message(STATUS "Python runtime interpreter: ${RUNTIME_PYTHON_EXECUTABLE} Version: ${RUNTIME_PYTHON_VERSION}")
message(STATUS "Override with: -DRUNTIME_PYTHON_EXECUTABLE=<path-to-python>")
macro(PYTHON_CHECK_MODULE desc mod cmd have)
@@ -97,4 +157,60 @@ exit(0)
endif()
endmacro(PYTHON_CHECK_MODULE)
+###############################################################################
+# Part 2: Python Libraries
+###############################################################################
+# The libraries must match the RUNTIME_PYTHON_EXECUTABLE's version.
+# - Figure out version
+# - See if Python3_LIBRARIES is already set (or Python2_LIBRARIES)
+if(NOT PYTHON_LIBRARIES OR NOT PYTHON_INCLUDE_DIRS)
+ message(STATUS "Finding Python Libraries...")
+ find_package(PythonLibs ${RUNTIME_PYTHON_VERSION} EXACT QUIET)
+ if(RUNTIME_PYTHON_VERSION VERSION_LESS 3)
+ if(NOT PYTHON_LIBRARIES OR NOT PYTHON_INCLUDE_DIRS)
+ find_package(Python3 ${RUNTIME_PYTHON_VERSION}
+ EXACT
+ QUIET
+ COMPONENTS Interpreter Development)
+ if(Python3_Development_FOUND)
+ set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
+ set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
+ endif(Python3_Development_FOUND)
+ endif(NOT PYTHON_LIBRARIES OR NOT PYTHON_INCLUDE_DIRS)
+ else(RUNTIME_PYTHON_VERSION VERSION_LESS 3)
+ if(NOT PYTHON_LIBRARIES OR NOT PYTHON_INCLUDE_DIRS)
+ find_package(Python2 ${RUNTIME_PYTHON_VERSION}
+ EXACT
+ QUIET
+ COMPONENTS Interpreter Development)
+ if(Python2_Development_FOUND)
+ set(PYTHON_LIBRARIES ${Python2_LIBRARIES})
+ set(PYTHON_INCLUDE_DIRS ${Python2_INCLUDE_DIRS})
+ endif(Python2_Development_FOUND)
+ endif(NOT PYTHON_LIBRARIES OR NOT PYTHON_INCLUDE_DIRS)
+ endif(RUNTIME_PYTHON_VERSION VERSION_LESS 3)
+ if(NOT PYTHON_LIBRARIES OR NOT PYTHON_INCLUDE_DIRS)
+ message(STATUS "Could not find Python Libraries.")
+ endif(NOT PYTHON_LIBRARIES OR NOT PYTHON_INCLUDE_DIRS)
+endif(NOT PYTHON_LIBRARIES OR NOT PYTHON_INCLUDE_DIRS)
+
+if(PYTHON_LIBRARIES AND PYTHON_INCLUDE_DIRS)
+ set(HAVE_PYTHON_LIBS TRUE)
+ message(STATUS "Python Libraries: ${PYTHON_LIBRARIES}")
+ message(STATUS "Python include directories: ${PYTHON_INCLUDE_DIRS}")
+else(PYTHON_LIBRARIES AND PYTHON_INCLUDE_DIRS)
+ set(HAVE_PYTHON_LIBS FALSE)
+endif(PYTHON_LIBRARIES AND PYTHON_INCLUDE_DIRS)
+
+if(NOT PYTHON_LIBRARY)
+ set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE FILEPATH
+ "Python libraries")
+ mark_as_advanced(PYTHON_LIBRARIES)
+endif(NOT PYTHON_LIBRARY)
+if(NOT PYTHON_INCLUDE_DIR)
+ set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE FILEPATH
+ "Python include dirs")
+ mark_as_advanced(PYTHON_INCLUDE_DIRS)
+endif(NOT PYTHON_INCLUDE_DIR)
+
endif(NOT DEFINED INCLUDED_UHD_PYTHON_CMAKE)