Skip to content

Commit

Permalink
Use the modern FindPython instead of FindPython3 (#2881)
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino committed May 24, 2024
1 parent 81ddd0e commit 71bf299
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions cmake/ExecuteFindPython/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
# This is called from PythonHelper.cmake in a subprocess, to allow multiple Python versions to be
# searched for in the project without CACHE variable hackery.
project(ExecuteFindPython LANGUAGES C)
find_package(Python3 COMPONENTS ${Python3_COMPONENTS})
message(STATUS "Python3_INCLUDE_DIRS=${Python3_INCLUDE_DIRS}")
message(STATUS "Python3_LIBRARIES=${Python3_LIBRARIES}")
message(STATUS "Python3_VERSION=${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}")
find_package(Python COMPONENTS ${Python_COMPONENTS})
message(STATUS "Python_INCLUDE_DIRS=${Python_INCLUDE_DIRS}")
message(STATUS "Python_LIBRARIES=${Python_LIBRARIES}")
message(STATUS "Python_VERSION=${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
22 changes: 11 additions & 11 deletions cmake/PythonHelper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function(nrn_find_python)
message(FATAL_ERROR "${opt_KEYWORDS_MISSING_VALUES} values are required")
endif()
if(NOT IS_ABSOLUTE "${opt_NAME}")
# Find the full path to ${opt_NAME} as Python3_EXECUTABLE does not accept relative paths.
# Find the full path to ${opt_NAME} as Python_EXECUTABLE does not accept relative paths.
find_program(
"${opt_NAME}_full" "${opt_NAME}"
PATHS ENV PATH
Expand All @@ -87,14 +87,14 @@ function(nrn_find_python)
endif()
# Only bother finding version/include/library information if NRN_ENABLE_PYTHON is set.
if(NRN_ENABLE_PYTHON)
# Run find_package(Python3 ...) in a subprocess, so there is no pollution of CMakeCache.txt and
# Run find_package(Python ...) in a subprocess, so there is no pollution of CMakeCache.txt and
# so on. Our desire to include multiple Python versions in one build means we have to handle
# lists of versions/libraries/... manually. Unfortunately one cannot safely use find_package in
# CMake script mode, so we configure an extra project.
string(SHA1 pyexe_hash "${opt_NAME}")
string(SUBSTRING "${pyexe_hash}" 0 6 pyexe_hash)
# Which attributes we're trying to learn about this Python
set(python_vars Python3_INCLUDE_DIRS Python3_VERSION)
set(python_vars Python_INCLUDE_DIRS Python_VERSION)
if(NRN_ENABLE_PYTHON_DYNAMIC AND NOT NRN_LINK_AGAINST_PYTHON)
# Do not link against Python, so we don't need the library -- just as well, it's not available
# in manylinux
Expand All @@ -105,15 +105,15 @@ function(nrn_find_python)
)
endif()
set(dev_component "Development.Module")
set(Python3_LIBRARIES "do-not-link-against-libpython-in-dynamic-python-builds")
set(Python_LIBRARIES "do-not-link-against-libpython-in-dynamic-python-builds")
else()
set(dev_component "Development")
list(APPEND python_vars Python3_LIBRARIES)
list(APPEND python_vars Python_LIBRARIES)
endif()
execute_process(
COMMAND
${CMAKE_COMMAND} "-DPython3_EXECUTABLE:STRING=${opt_NAME}"
"-DPython3_COMPONENTS=${dev_component};Interpreter" -S
${CMAKE_COMMAND} "-DPython_EXECUTABLE:STRING=${opt_NAME}"
"-DPython_COMPONENTS=${dev_component};Interpreter" -S
${CMAKE_SOURCE_DIR}/cmake/ExecuteFindPython -B
${CMAKE_BINARY_DIR}/ExecuteFindPython_${pyexe_hash}
RESULT_VARIABLE result
Expand All @@ -132,13 +132,13 @@ function(nrn_find_python)
set(${var} "${CMAKE_MATCH_1}")
endforeach()
set("${opt_PREFIX}_INCLUDES"
"${Python3_INCLUDE_DIRS}"
"${Python_INCLUDE_DIRS}"
PARENT_SCOPE)
set("${opt_PREFIX}_LIBRARIES"
"${Python3_LIBRARIES}"
"${Python_LIBRARIES}"
PARENT_SCOPE)
set("${opt_PREFIX}_VERSION"
"${Python3_VERSION}"
"${Python_VERSION}"
PARENT_SCOPE)
endif()
# Finally do our special treatment for macOS + sanitizers
Expand Down Expand Up @@ -202,7 +202,7 @@ foreach(pyexe ${python_executables})
message(FATAL_ERROR "Cannot handle multiple Python include dirs: ${nrnpy_INCLUDES}")
endif()
if(NOT num_lib_dirs EQUAL 1)
message(FATAL_ERROR "Cannot handle multiple Python libraries: ${Python3_LIBRARIES}")
message(FATAL_ERROR "Cannot handle multiple Python libraries: ${Python_LIBRARIES}")
endif()
if(nrnpy_VERSION IN_LIST NRN_PYTHON_VERSIONS)
# We cannot build against multiple copies of the same pythonX.Y version.
Expand Down

0 comments on commit 71bf299

Please sign in to comment.