diff --git a/projects/rocprofiler-sdk/source/lib/python/utilities.cmake b/projects/rocprofiler-sdk/source/lib/python/utilities.cmake index ed7cfa3613f9..033420a49989 100644 --- a/projects/rocprofiler-sdk/source/lib/python/utilities.cmake +++ b/projects/rocprofiler-sdk/source/lib/python/utilities.cmake @@ -39,9 +39,52 @@ if(ROCPROFILER_MEMCHECK) set(ROCPROFILER_BUILD_Find_Python3_COMPONENTS "Interpreter" "Development") endif() +# sets _VAR to "." of the python executable _EXE, or empty on failure +function(rocprofiler_get_python_executable_version _EXE _VAR) + execute_process( + COMMAND "${_EXE}" -c + "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" + OUTPUT_VARIABLE _EXE_VER + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE _EXE_RESULT + ERROR_QUIET) + + if(NOT _EXE_RESULT EQUAL 0) + set(_EXE_VER "") + endif() + + set(${_VAR} + "${_EXE_VER}" + PARENT_SCOPE) +endfunction() + macro(rocprofiler_find_python3 _VERSION) rocprofiler_reset_python3_cache() + # If an explicit executable list was provided (e.g. from TheRock via + # -DROCPROFILER_PYTHON_EXECUTABLES), pre-seed Python3_EXECUTABLE for this version + # before calling find_package. This is necessary for Python versions that CMake's + # FindPython3 does not auto-discover (e.g. Python 3.14+ on manylinux where an older + # CMake has no knowledge of the new install path). + if(DEFINED ROCPROFILER_PYTHON_EXECUTABLES) + string(REGEX MATCH "^[0-9]+\\.[0-9]+" _rocprofiler_requested_major_minor + "${_VERSION}") + foreach(_rocprofiler_candidate_exe IN LISTS ROCPROFILER_PYTHON_EXECUTABLES) + rocprofiler_get_python_executable_version("${_rocprofiler_candidate_exe}" + _rocprofiler_candidate_ver) + if(_rocprofiler_candidate_ver AND "${_rocprofiler_candidate_ver}" STREQUAL + "${_rocprofiler_requested_major_minor}") + set(Python3_EXECUTABLE + "${_rocprofiler_candidate_exe}" + CACHE FILEPATH "" FORCE) + break() + endif() + endforeach() + unset(_rocprofiler_candidate_exe) + unset(_rocprofiler_candidate_ver) + unset(_rocprofiler_requested_major_minor) + endif() + if("${_VERSION}" MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$") find_package(Python3 ${_VERSION} EXACT ${ARGN} REQUIRED MODULE COMPONENTS ${ROCPROFILER_BUILD_Find_Python3_COMPONENTS}) @@ -75,6 +118,19 @@ function(get_default_python_versions _VAR) endif() endforeach() + # If find package doesn't find the version, but PYTHON_EXECUTABLES is passed in, add + # it to version list + if(DEFINED ROCPROFILER_PYTHON_EXECUTABLES) + foreach(_EXE IN LISTS ROCPROFILER_PYTHON_EXECUTABLES) + rocprofiler_get_python_executable_version("${_EXE}" _EXE_VER) + if(_EXE_VER AND NOT "${_EXE_VER}" IN_LIST _PYTHON_FOUND_VERSIONS) + list(APPEND _PYTHON_FOUND_VERSIONS "${_EXE_VER}") + endif() + endforeach() + unset(_EXE) + unset(_EXE_VER) + endif() + # If none found, do one last check for 3.6 (no EXACT) if(NOT _PYTHON_FOUND_VERSIONS) find_package(Python3 3.6 COMPONENTS ${ROCPROFILER_BUILD_Find_Python3_COMPONENTS})