-
Notifications
You must be signed in to change notification settings - Fork 71
/
FindPythonModule.cmake
70 lines (62 loc) · 2.46 KB
/
FindPythonModule.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Find a specific Python module.
#
# Will set:
# PYTHON${NAME_UPPER}_FOUND if found;
# PYTHON${NAME_UPPER}_VERSION if module __version__ is defined;
# where ${NAME_UPPER} is the capitalized module name.
#
# find_python_module(${MODULE}) will emulate find_package()'s 'quiet' behaviour
# if Python${MODULE}_FIND_QUIETLY is set to a true value, and similarly
# will emulate 'required' behaviour if Python${MODULE}_FIND_REQUIRED
# is set to a true value. (MODULE is case-sensitive.)
macro(cache_test_and_set KEY MSG FOUND)
if("${MSG}" STREQUAL "${MESSAGE_CACHE_BY_${KEY}}")
set("${FOUND}" 1)
else()
set("${FOUND}" 0)
set("MESSAGE_CACHE_BY_${KEY}" "${MSG}" CACHE INTERNAL "cached reporting message")
endif()
endmacro()
# Don't repeat a message; check by looking up key in cache
macro(fail_message key msg)
if(REQUIRED)
message(FATAL_ERROR "${msg}")
elseif (NOT QUIET)
cache_test_and_set("${key}" "${msg}" FOUND)
if(NOT FOUND)
message(STATUS "${msg}")
endif()
endif()
endmacro()
macro(success_message key msg)
if(NOT QUIET)
cache_test_and_set("${key}" "${msg}" FOUND)
if(NOT FOUND)
message(STATUS "${msg}")
endif()
endif()
endmacro()
function(find_python_module MODULENAME)
string(TOUPPER ${MODULENAME} NAME_UPPER)
set(QUIET ${Python${MODULENAME}_FIND_QUIETLY})
set(REQUIRED ${Python${MODULENAME}_FIND_REQUIRED})
find_package(PythonInterp ${QUIET} ${REQUIRED})
if(NOT PYTHON_EXECUTABLE)
fail_message(PYTHON_${MODULENAME} "No python interpreter found")
set(PYTHON${NAME_UPPER}_FOUND 0)
else()
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
"import ${MODULENAME} as x; print x.__version__ if hasattr(x,'__version__') else ''"
ERROR_QUIET
RESULT_VARIABLE RV
OUTPUT_VARIABLE MODVER
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT RV)
set(PYTHON${NAME_UPPER}_FOUND 1 CACHE INTERNAL "Python module ${MODULENAME} found")
set(PYTHON${NAME_UPPER}_VERSION "${MODVER}" CACHE INTERNAL "Python module ${MODULENAME} version")
success_message(PYTHON${MODULENAME} "Found python module ${MODULENAME}: (found version \"${MODVER}\")")
else()
fail_message(PYTHON${MODULENAME} "Could NOT find python module ${MODULENAME}")
endif()
endif()
endfunction()