Skip to content

Commit 8942a00

Browse files
committed
[CMake] Extend ROOT_FIND_PYTHON_MODULE to also save version info
Also, fix the comparison of the status code retuned by the Python invokation.
1 parent 7e32769 commit 8942a00

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

cmake/modules/RootMacros.cmake

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,14 +2008,16 @@ endmacro()
20082008
# ROOT_FIND_PYTHON_MODULE(module [REQUIRED] [QUIET])
20092009
# Try importing the python dependency and cache the result in
20102010
# ROOT_TEST_<MODULE> (all upper case).
2011-
# Also set ROOT_<MODULE>_FOUND (all upper case) as well as ROOT_<module>_FOUND
2012-
# (the original spelling of the argument) in the parent scope of this function
2013-
# for convenient testing in subsequent if().
2011+
# Also set ROOT_<MODULE>_FOUND and ROOT_<module>_FOUND (the original spelling)
2012+
# in the parent scope for convenient testing in subsequent if() statements.
2013+
# Additionally, sets ROOT_<MODULE>_VERSION (and ROOT_<module>_VERSION)
2014+
# if the version could be determined.
20142015
#----------------------------------------------------------------------------
20152016
function(ROOT_FIND_PYTHON_MODULE module)
20162017
CMAKE_PARSE_ARGUMENTS(ARG "REQUIRED;QUIET" "" "" ${ARGN})
20172018
string(TOUPPER ${module} module_upper)
20182019
set(CACHE_VAR ROOT_TEST_${module_upper})
2020+
set(CACHE_VAR_VERSION "${CACHE_VAR}_VERSION")
20192021

20202022
if(NOT DEFINED ${CACHE_VAR})
20212023
execute_process(COMMAND "${Python3_EXECUTABLE}" "-c"
@@ -2025,10 +2027,25 @@ function(ROOT_FIND_PYTHON_MODULE module)
20252027
OUTPUT_STRIP_TRAILING_WHITESPACE
20262028
ERROR_QUIET)
20272029

2028-
if(${status} EQUAL 0)
2030+
if(status EQUAL 0)
20292031
set(${CACHE_VAR} ON CACHE BOOL "Enable tests depending on '${module}'")
2032+
# Only cache a non-empty, non-'unknown' version string.
2033+
if(module_version AND NOT module_version STREQUAL "unknown")
2034+
set(${CACHE_VAR_VERSION} "${module_version}" CACHE STRING "Detected version of python module ${module}")
2035+
else()
2036+
# ensure no stale version remains in cache
2037+
if(DEFINED ${CACHE_VAR_VERSION})
2038+
unset(${CACHE_VAR_VERSION} CACHE)
2039+
endif()
2040+
unset(module_version)
2041+
endif()
20302042
else()
20312043
set(${CACHE_VAR} OFF CACHE BOOL "Enable tests depending on '${module}'")
2044+
# ensure version cache entry is removed on failure
2045+
if(DEFINED ${CACHE_VAR_VERSION})
2046+
unset(${CACHE_VAR_VERSION} CACHE)
2047+
endif()
2048+
unset(module_version)
20322049
endif()
20332050

20342051
if(NOT ARG_QUIET)
@@ -2038,12 +2055,25 @@ function(ROOT_FIND_PYTHON_MODULE module)
20382055
message(STATUS "Could NOT find Python module ${module}. Corresponding tests will be disabled.")
20392056
endif()
20402057
endif()
2058+
else()
2059+
# Cache exists; if a cached version string exists, read it into module_version.
2060+
if(DEFINED ${CACHE_VAR_VERSION})
2061+
set(module_version ${${CACHE_VAR_VERSION}})
2062+
endif()
20412063
endif()
20422064

20432065
# Set the ROOT_xxx_FOUND to the (cached) result of the search:
20442066
set(ROOT_${module_upper}_FOUND ${${CACHE_VAR}} PARENT_SCOPE)
20452067
set(ROOT_${module}_FOUND ${${CACHE_VAR}} PARENT_SCOPE)
20462068

2069+
# Expose version only if module was found and a version string is available.
2070+
if(${CACHE_VAR})
2071+
if(DEFINED module_version AND NOT module_version STREQUAL "" AND NOT module_version STREQUAL "unknown")
2072+
set(ROOT_${module_upper}_VERSION "${module_version}" PARENT_SCOPE)
2073+
set(ROOT_${module}_VERSION "${module_version}" PARENT_SCOPE)
2074+
endif()
2075+
endif()
2076+
20472077
if(ARG_REQUIRED AND NOT ${CACHE_VAR})
20482078
message(FATAL_ERROR "Python module ${module} is required.")
20492079
endif()

0 commit comments

Comments
 (0)