FindPython: ensure reproductibility between CMake calls

Ensures, when CMake is re-launched with some artifacts specified by the user,
that the multiple runs of CMake give always the same result.

Fixes: #26778
This commit is contained in:
Marc Chevrier 2025-03-18 17:25:15 +01:00
parent 1c07011300
commit f08b218a0d

View File

@ -79,6 +79,53 @@ function (_PYTHON_MARK_AS_INTERNAL)
endfunction()
function (_PYTHON_RESET_ARTIFACTS comp)
set(components ${comp})
if (comp STREQUAL "Interpreter")
list (APPEND components Compiler Development NumPy)
endif()
if (comp STREQUAL "Development")
list (APPEND components NumPy)
endif()
set(find_components ${${_PYTHON_BASE}_FIND_COMPONENTS})
list(TRANSFORM find_components REPLACE "^Development.*" "Development")
list(REMOVE_DUPLICATES find_components)
foreach (component IN LISTS components)
if (NOT component IN_LIST find_components)
continue()
endif()
if (component STREQUAL "Interpreter")
unset(_${_PYTHON_PREFIX}_EXECUTABLE CACHE)
endif()
if (component STREQUAL "Compiler"
AND "IronPython" IN_LIST _${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS)
unset(_${_PYTHON_PREFIX}_COMPILER CACHE)
endif()
if (component STREQUAL "Development")
set(artifacts ${ARGN})
if (NOT artifacts)
set(artifacts LIBRARY SABI_LIBRARY INCLUDE_DIR)
endif()
unset(_${_PYTHON_PREFIX}_CONFIG)
foreach (artifact IN LISTS artifacts)
if (artifact IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_ARTIFACTS)
if (artifact MATCHES "LIBRARY")
unset(_${_PYTHON_PREFIX}_${artifact}_RELEASE CACHE)
unset(_${_PYTHON_PREFIX}_${artifact}_DEBUG CACHE)
elseif(arifact STREQUAL "INCLUDE_DIR")
unset(_${_PYTHON_PREFIX}_${artifact} CACHE)
endif()
endif()
endforeach()
endif()
if (component STREQUAL "NumPy")
unset(_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR CACHE)
endif()
endforeach()
endfunction()
macro (_PYTHON_SELECT_LIBRARY_CONFIGURATIONS _PYTHON_BASENAME)
if(NOT DEFINED ${_PYTHON_BASENAME}_LIBRARY_RELEASE)
set(${_PYTHON_BASENAME}_LIBRARY_RELEASE "${_PYTHON_BASENAME}_LIBRARY_RELEASE-NOTFOUND")
@ -1915,6 +1962,8 @@ if ("Interpreter" IN_LIST ${_PYTHON_BASE}_FIND_COMPONENTS)
if (NOT ${_PYTHON_PREFIX}_EXECUTABLE STREQUAL _${_PYTHON_PREFIX}_EXECUTABLE)
# invalidate cache properties
unset (_${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES CACHE)
# invalidate the previous results for any other requested components
_python_reset_artifacts(Interpreter)
endif()
set (_${_PYTHON_PREFIX}_EXECUTABLE "${${_PYTHON_PREFIX}_EXECUTABLE}" CACHE INTERNAL "")
unset (_${_PYTHON_PREFIX}_EXECUTABLE_DEBUG CACHE)
@ -2821,18 +2870,17 @@ if (("Development.Module" IN_LIST ${_PYTHON_BASE}_FIND_COMPONENTS
if (DEFINED ${_PYTHON_PREFIX}_LIBRARY
AND IS_ABSOLUTE "${${_PYTHON_PREFIX}_LIBRARY}")
_python_reset_artifacts(Development)
set (_${_PYTHON_PREFIX}_LIBRARY_RELEASE "${${_PYTHON_PREFIX}_LIBRARY}" CACHE INTERNAL "")
unset (_${_PYTHON_PREFIX}_LIBRARY_DEBUG CACHE)
unset (_${_PYTHON_PREFIX}_INCLUDE_DIR CACHE)
endif()
if (DEFINED ${_PYTHON_PREFIX}_SABI_LIBRARY
AND IS_ABSOLUTE "${${_PYTHON_PREFIX}_SABI_LIBRARY}")
_python_reset_artifacts(Development SABI_LIBRARY INCLUDE_DIR)
set (_${_PYTHON_PREFIX}_SABI_LIBRARY_RELEASE "${${_PYTHON_PREFIX}_SABI_LIBRARY}" CACHE INTERNAL "")
unset (_${_PYTHON_PREFIX}_SABI_LIBRARY_DEBUG CACHE)
unset (_${_PYTHON_PREFIX}_INCLUDE_DIR CACHE)
endif()
if (DEFINED ${_PYTHON_PREFIX}_INCLUDE_DIR
AND IS_ABSOLUTE "${${_PYTHON_PREFIX}_INCLUDE_DIR}")
_python_reset_artifacts(Development INCLUDE_DIR)
set (_${_PYTHON_PREFIX}_INCLUDE_DIR "${${_PYTHON_PREFIX}_INCLUDE_DIR}" CACHE INTERNAL "")
endif()