@@ -2006,14 +2006,16 @@ endmacro()
20062006# ROOT_FIND_PYTHON_MODULE(module [REQUIRED] [QUIET])
20072007# Try importing the python dependency and cache the result in
20082008# ROOT_TEST_<MODULE> (all upper case).
2009- # Also set ROOT_<MODULE>_FOUND (all upper case) as well as ROOT_<module>_FOUND
2010- # (the original spelling of the argument) in the parent scope of this function
2011- # for convenient testing in subsequent if().
2009+ # Also set ROOT_<MODULE>_FOUND and ROOT_<module>_FOUND (the original spelling)
2010+ # in the parent scope for convenient testing in subsequent if() statements.
2011+ # Additionally, sets ROOT_<MODULE>_VERSION (and ROOT_<module>_VERSION)
2012+ # if the version could be determined.
20122013#----------------------------------------------------------------------------
20132014function (ROOT_FIND_PYTHON_MODULE module)
20142015 CMAKE_PARSE_ARGUMENTS (ARG "REQUIRED;QUIET" "" "" ${ARGN} )
20152016 string (TOUPPER ${module} module_upper)
20162017 set (CACHE_VAR ROOT_TEST_${module_upper} )
2018+ set (CACHE_VAR_VERSION "${CACHE_VAR} _VERSION" )
20172019
20182020 if (NOT DEFINED ${CACHE_VAR} )
20192021 execute_process (COMMAND "${Python3_EXECUTABLE} " "-c"
@@ -2025,8 +2027,23 @@ function(ROOT_FIND_PYTHON_MODULE module)
20252027
20262028 if (${status} EQUAL 0)
20272029 set (${CACHE_VAR} ON CACHE BOOL "Enable tests depending on '${module} '" )
2030+ # Only cache a non-empty, non-'unknown' version string.
2031+ if (module_version AND NOT module_version STREQUAL "unknown" )
2032+ set (${CACHE_VAR_VERSION} "${module_version} " CACHE STRING "Detected version of python module ${module} " )
2033+ else ()
2034+ # ensure no stale version remains in cache
2035+ if (DEFINED ${CACHE_VAR_VERSION} )
2036+ unset (${CACHE_VAR_VERSION} CACHE )
2037+ endif ()
2038+ unset (module_version)
2039+ endif ()
20282040 else ()
20292041 set (${CACHE_VAR} OFF CACHE BOOL "Enable tests depending on '${module} '" )
2042+ # ensure version cache entry is removed on failure
2043+ if (DEFINED ${CACHE_VAR_VERSION} )
2044+ unset (${CACHE_VAR_VERSION} CACHE )
2045+ endif ()
2046+ unset (module_version)
20302047 endif ()
20312048
20322049 if (NOT ARG_QUIET)
@@ -2036,12 +2053,25 @@ function(ROOT_FIND_PYTHON_MODULE module)
20362053 message (STATUS "Could NOT find Python module ${module} . Corresponding tests will be disabled." )
20372054 endif ()
20382055 endif ()
2056+ else ()
2057+ # Cache exists: if a cached version string exists, read it into module_version.
2058+ if (DEFINED ${CACHE_VAR_VERSION} )
2059+ set (module_version ${${CACHE_VAR_VERSION} })
2060+ endif ()
20392061 endif ()
20402062
20412063 # Set the ROOT_xxx_FOUND to the (cached) result of the search:
20422064 set (ROOT_${module_upper} _FOUND ${${CACHE_VAR} } PARENT_SCOPE)
20432065 set (ROOT_${module} _FOUND ${${CACHE_VAR} } PARENT_SCOPE)
20442066
2067+ # Expose version only if module was found and a version string is available.
2068+ if (${CACHE_VAR} )
2069+ if (DEFINED module_version AND NOT module_version STREQUAL "" AND NOT module_version STREQUAL "unknown" )
2070+ set (ROOT_${module_upper} _VERSION "${module_version} " PARENT_SCOPE)
2071+ set (ROOT_${module} _VERSION "${module_version} " PARENT_SCOPE)
2072+ endif ()
2073+ endif ()
2074+
20452075 if (ARG_REQUIRED AND NOT ${CACHE_VAR} )
20462076 message (FATAL_ERROR "Python module ${module} is required." )
20472077 endif ()
0 commit comments