Skip to content

Commit 299a1b1

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 299a1b1

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

cmake/modules/RootMacros.cmake

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,42 +2008,79 @@ 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})
2017-
string(TOUPPER ${module} module_upper)
2018-
set(CACHE_VAR ROOT_TEST_${module_upper})
2018+
2019+
string(TOUPPER "${module}" module_upper)
2020+
set(CACHE_VAR "ROOT_TEST_${module_upper}")
2021+
set(CACHE_VAR_VERSION "${CACHE_VAR}_VERSION")
20192022

20202023
if(NOT DEFINED ${CACHE_VAR})
2021-
execute_process(COMMAND "${Python3_EXECUTABLE}" "-c"
2022-
"import ${module}; print(getattr(${module}, '__version__', 'unknown'))"
2024+
execute_process(
2025+
COMMAND "${Python3_EXECUTABLE}" "-c"
2026+
"import ${module}; print(getattr(${module}, '__version__', 'unknown'))"
20232027
RESULT_VARIABLE status
20242028
OUTPUT_VARIABLE module_version
20252029
OUTPUT_STRIP_TRAILING_WHITESPACE
2026-
ERROR_QUIET)
2030+
ERROR_QUIET
2031+
)
20272032

2028-
if(${status} EQUAL 0)
2033+
if(status EQUAL 0)
20292034
set(${CACHE_VAR} ON CACHE BOOL "Enable tests depending on '${module}'")
2035+
# Only cache a non-empty, non-'unknown' version string.
2036+
if(module_version AND NOT module_version STREQUAL "unknown")
2037+
set(${CACHE_VAR_VERSION} "${module_version}" CACHE STRING "Detected version of python module ${module}")
2038+
else()
2039+
# ensure no stale version remains in cache
2040+
if(DEFINED ${CACHE_VAR_VERSION})
2041+
unset(${CACHE_VAR_VERSION} CACHE)
2042+
endif()
2043+
unset(module_version)
2044+
endif()
20302045
else()
20312046
set(${CACHE_VAR} OFF CACHE BOOL "Enable tests depending on '${module}'")
2047+
# ensure version cache entry is removed on failure
2048+
if(DEFINED ${CACHE_VAR_VERSION})
2049+
unset(${CACHE_VAR_VERSION} CACHE)
2050+
endif()
2051+
unset(module_version)
20322052
endif()
20332053

20342054
if(NOT ARG_QUIET)
20352055
if(${CACHE_VAR})
2036-
message(STATUS "Found Python module ${module} (found version \"${module_version}\")")
2056+
if(DEFINED module_version)
2057+
message(STATUS "Found Python module ${module} (version \"${module_version}\")")
2058+
else()
2059+
message(STATUS "Found Python module ${module} (version unknown)")
2060+
endif()
20372061
else()
20382062
message(STATUS "Could NOT find Python module ${module}. Corresponding tests will be disabled.")
20392063
endif()
20402064
endif()
2065+
else()
2066+
# Cache exists; if a cached version string exists, read it into module_version.
2067+
if(DEFINED ${CACHE_VAR_VERSION})
2068+
set(module_version ${${CACHE_VAR_VERSION}})
2069+
endif()
20412070
endif()
20422071

2043-
# Set the ROOT_xxx_FOUND to the (cached) result of the search:
2072+
# Expose FOUND variables in parent scope (both upper-case and original).
20442073
set(ROOT_${module_upper}_FOUND ${${CACHE_VAR}} PARENT_SCOPE)
20452074
set(ROOT_${module}_FOUND ${${CACHE_VAR}} PARENT_SCOPE)
20462075

2076+
# Expose version only if module was found and a version string is available.
2077+
if(${CACHE_VAR})
2078+
if(DEFINED module_version AND NOT module_version STREQUAL "" AND NOT module_version STREQUAL "unknown")
2079+
set(ROOT_${module_upper}_VERSION "${module_version}" PARENT_SCOPE)
2080+
set(ROOT_${module}_VERSION "${module_version}" PARENT_SCOPE)
2081+
endif()
2082+
endif()
2083+
20472084
if(ARG_REQUIRED AND NOT ${CACHE_VAR})
20482085
message(FATAL_ERROR "Python module ${module} is required.")
20492086
endif()

0 commit comments

Comments
 (0)