From 5eec9da8fe4386c4ae027dc4e9340e105ebb5391 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 19 Jul 2022 13:28:43 -0400 Subject: [PATCH] make search for python libraries consistent with search for python interpreter - apply same semantics of selecting the interpreter than the main cmake script - make certain that we search for the interpreter first - when searching for the library find the version matching the interpreter - error out when library version and interpreter version does not match --- cmake/Modules/Packages/MDI.cmake | 13 +++++++++++++ cmake/Modules/Packages/PYTHON.cmake | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/cmake/Modules/Packages/MDI.cmake b/cmake/Modules/Packages/MDI.cmake index 1a14d5273a..d873c8f6d1 100644 --- a/cmake/Modules/Packages/MDI.cmake +++ b/cmake/Modules/Packages/MDI.cmake @@ -26,8 +26,21 @@ if(DOWNLOAD_MDI) # detect if we have python development support and thus can enable python plugins set(MDI_USE_PYTHON_PLUGINS OFF) if(CMAKE_VERSION VERSION_LESS 3.12) + if(NOT PYTHON_VERSION_STRING) + set(Python_ADDITIONAL_VERSIONS 3.12 3.11 3.10 3.9 3.8 3.7 3.6) + # search for interpreter first, so we have a consistent library + find_package(PythonInterp) # Deprecated since version 3.12 + if(PYTHONINTERP_FOUND) + set(Python_EXECUTABLE ${PYTHON_EXECUTABLE}) + endif() + endif() + # search for the library matching the selected interpreter + set(Python_ADDITIONAL_VERSIONS ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}) find_package(PythonLibs QUIET) # Deprecated since version 3.12 if(PYTHONLIBS_FOUND) + if(NOT (PYTHON_VERSION_STRING STREQUAL PYTHONLIBS_VERSION_STRING)) + message(FATAL_ERROR "Python Library version ${PYTHONLIBS_VERSION_STRING} does not match Interpreter version ${PYTHON_VERSION_STRING}") + endif() set(MDI_USE_PYTHON_PLUGINS ON) endif() else() diff --git a/cmake/Modules/Packages/PYTHON.cmake b/cmake/Modules/Packages/PYTHON.cmake index c94db88073..4a2925fe31 100644 --- a/cmake/Modules/Packages/PYTHON.cmake +++ b/cmake/Modules/Packages/PYTHON.cmake @@ -1,8 +1,28 @@ if(CMAKE_VERSION VERSION_LESS 3.12) + if(NOT PYTHON_VERSION_STRING) + set(Python_ADDITIONAL_VERSIONS 3.12 3.11 3.10 3.9 3.8 3.7 3.6) + # search for interpreter first, so we have a consistent library + find_package(PythonInterp) # Deprecated since version 3.12 + if(PYTHONINTERP_FOUND) + set(Python_EXECUTABLE ${PYTHON_EXECUTABLE}) + endif() + endif() + # search for the library matching the selected interpreter + set(Python_ADDITIONAL_VERSIONS ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}) find_package(PythonLibs REQUIRED) # Deprecated since version 3.12 + if(NOT (PYTHON_VERSION_STRING STREQUAL PYTHONLIBS_VERSION_STRING)) + message(FATAL_ERROR "Python Library version ${PYTHONLIBS_VERSION_STRING} does not match Interpreter version ${PYTHON_VERSION_STRING}") + endif() target_include_directories(lammps PRIVATE ${PYTHON_INCLUDE_DIRS}) target_link_libraries(lammps PRIVATE ${PYTHON_LIBRARIES}) else() + if(NOT Python_INTERPRETER) + # backward compatibility + if(PYTHON_EXECUTABLE) + set(Python_EXECUTABLE ${PYTHON_EXECUTABLE}) + endif() + find_package(Python COMPONENTS Interpreter) + endif() find_package(Python REQUIRED COMPONENTS Interpreter Development) target_link_libraries(lammps PRIVATE Python::Python) endif()