From 5eaa79884cb8d2a551c2efd720f6033d1a5b7915 Mon Sep 17 00:00:00 2001 From: Marco Biasini <marco.biasini@unibas.ch> Date: Sun, 3 Jun 2012 11:10:35 +0200 Subject: [PATCH] prefer versioned python binaries to non-versioned ones On many systems, there are several parallel python installations. There is usually a "python" symlink that points to the currently active python version. That might not be the one we need. Instead of picking up that one, we now search for python$VERSION and as a last resort use python. Also, make sure found python lib/binaries match. This fixes BZDNG-388. --- cmake_support/FindPython.cmake | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/cmake_support/FindPython.cmake b/cmake_support/FindPython.cmake index 66c62f51c..c3b6f7dfd 100644 --- a/cmake_support/FindPython.cmake +++ b/cmake_support/FindPython.cmake @@ -42,7 +42,7 @@ endmacro() macro(_find_python PYTHON_ROOT VERSION) - string(REPLACE "." "" _VERSION_NO_DOTS ${VERSION}) + string(REPLACE "." "" _VERSION_NO_DOTS "${VERSION}") if(PYTHON_ROOT) find_library(PYTHON_LIBRARIES NAMES "python${_VERSION_NO_DOTS}" "python${VERSION}" @@ -69,17 +69,17 @@ macro(_find_python PYTHON_ROOT VERSION) endmacro() macro(_find_python_bin PYTHON_ROOT VERSION) - string(REPLACE "." "" _VERSION_NO_DOTS ${VERSION}) + string(REPLACE "." "" _VERSION_NO_DOTS "${VERSION}") if(PYTHON_ROOT) find_program(PYTHON_BINARY - NAMES "python" "python${_VERSION_NO_DOTS}" "python${VERSION}" + NAMES "python${_VERSION_NO_DOTS}" "python${VERSION}" HINTS "${PYTHON_ROOT}" PATH_SUFFIXES bin NO_SYSTEM_ENVIRONMENT_PATH NO_DEFAULT_PATH ) else() find_program(PYTHON_BINARY - NAMES "python" "python${_VERSION_NO_DOTS}" "python${VERSION}" + NAMES "python${_VERSION_NO_DOTS}" "python${VERSION}" HINTS "${CMAKE_PREFIX_PATH}" PATH_SUFFIXES bin ) @@ -105,6 +105,10 @@ macro(check_for_python_lib) endif() endforeach() endif() + # fallback to non-versioned naming scheme + if (NOT $PYTHON_LIBRARIES) + _find_python("${PYTHON_ROOT}" "") + endif() endmacro() macro(check_for_python_binary) @@ -114,13 +118,19 @@ macro(check_for_python_binary) foreach(_VERSION ${PYTHON_VERSIONS}) if(${PYTHON_MIN_VERSION} VERSION_LESS ${_VERSION}) _find_python_bin("${PYTHON_ROOT}" "${_VERSION}") - if(PYTHON_LIBRARIES) + if(PYTHON_BINARY) set(PYTHON_VERSION "${_VERSION}") + # disallow all versions except for the one we just found. This makes + # sure we don't mismatch the python binary and the libraries. + set(PYTHON_VERSIONS "${_VERSION}") break() endif() endif() endforeach() endif() + if (NOT PYTHON_BINARY) + _find_python("${PYTHON_ROOT}" "") + endif() endmacro() if(NOT PYTHON_ROOT) @@ -134,11 +144,13 @@ if(APPLE AND NOT PYTHON_IGNORE_FRAMEWORKS) check_for_python_framework() endif() +# first check for python binary. +check_for_python_binary() + if(NOT PYTHON_FRAMEWORK_FOUND) check_for_python_lib() endif() -check_for_python_binary() mark_as_advanced( PYTHON_LIBRARIES PYTHON_INCLUDE_PATH @@ -159,6 +171,8 @@ if(PYTHON_LIBRARIES) endif() if (PYTHON_BINARY) + set(PYTHON_VERSION "${PYTHON_VERSION}" + CACHE STRING "Python Version" FORCE) set(PYTHON_BINARY "${PYTHON_BINARY}" CACHE FILEPATH "Python Binary" FORCE) endif() -- GitLab