-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPythonModule.cmake
More file actions
100 lines (86 loc) · 4.45 KB
/
FindPythonModule.cmake
File metadata and controls
100 lines (86 loc) · 4.45 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Search for a Python module.
#
# Usage :
#
# find_python_module(mpi4py REQUIRED)
# find_python_module(sphinx)
# find_python_module(mpi4py INCLUDES)
#
# Warning : use ${PYTHON_EXECUTABLE} as python interpreter
#
# If INCLUDES options is provided, it means that the function
# is supposed to check for the existence of <path-to-module>/include
# and set ${module}_INCLUDE_DIR cache variable.
include(FindPackageHandleStandardArgs)
function(find_python_module module)
message("include dir for module ${module}: ${${module}_INCLUDE_DIR}")
if(${module}_INCLUDE_DIR)
if(NOT TARGET Python::${module})
add_library(Python::${module} IMPORTED INTERFACE)
set_target_properties(Python::${module} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${${module}_INCLUDE_DIR}")
endif()
target_link_libraries(${COMPONENT} PRIVATE Python::${module})
else()
set(options REQUIRED INCLUDES) # If INCLUDES options is provided
set(oneValueArgs VERSION)
set (ENV{VIRTUAL_ENV} ${PYTHON_VENV})
message("Search python module : ${module}")
message("PYTHON_EXECUTABLE : ${PYTHON_EXECUTABLE}")
message("PYTHON_VENV : ${PYTHON_VENV}")
cmake_parse_arguments(${module}_FIND "${options}" "${oneValueArgs}" "" ${ARGN} )
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import sys; import ${module} as name; print(name.__file__)"
RESULT_VARIABLE ${module}_FIND_RESULT # Return code from command above
OUTPUT_VARIABLE ${module}_FIND_OUTPUT # Standard output form command above
ERROR_QUIET # Ignores quietly standard error
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT ${module}_FIND_RESULT) # Return code == 0 means that things have gone well
set(${module}_file ${${module}_FIND_OUTPUT} CACHE STRING "Python ${module} module file.")
else()
set(${module}_file ${module}-NOTFOUND)
endif()
# Save version
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import sys; import ${module} as name; print(name.__version__)"
RESULT_VARIABLE ${module}_FIND_RESULT # Return code from command above
OUTPUT_VARIABLE ${module}_VERSION # Standard output form command above
ERROR_QUIET # Ignores quietly standard error
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(${module}_FIND_RESULT)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import sys; import ${module} as name; print(name.VERSION)"
RESULT_VARIABLE ${module}_FIND_RESULT # Return code from command above
OUTPUT_VARIABLE ${module}_VERSION # Standard output form command above
ERROR_QUIET # Ignores quietly standard error
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
set(${module}_REQUIRED_VARS ${module}_file ${module}_PYTHONPATH)
if(${module}_file)
message("${module} file: ${${module}_file}")
# module directory
get_filename_component(${module}_path ${${module}_file} DIRECTORY)
# path to the module (i.e. what must be added to PYTHONPATH or sys.path)
get_filename_component(${module}_PYTHONPATH ${${module}_path} DIRECTORY)
if(${module}_FIND_INCLUDES)
find_file(${module}_INCLUDE_DIR include PATHS ${${module}_path} NO_DEFAULT_PATH)
list(APPEND ${module}_REQUIRED_VARS ${module}_INCLUDE_DIR)
message("${module} include: ${${module}_INCLUDE_DIR}")
endif()
endif()
find_package_handle_standard_args(${module}
REQUIRED_VARS ${${module}_REQUIRED_VARS}
VERSION_VAR ${module}_VERSION)
if(${module}_FOUND)
message("-- Found python package ${${module}_file}, version ${${module}_VERSION}")
if(${module}_INCLUDE_DIR)
if(NOT TARGET Python::${module})
add_library(Python::${module} IMPORTED INTERFACE)
set_target_properties(Python::${module} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${${module}_INCLUDE_DIR}")
endif()
target_link_libraries(${COMPONENT} PRIVATE Python::${module})
endif()
endif()
endif()
endfunction(find_python_module)