| 1 |
# Find if a Python module is installed |
| 2 |
# Found at http://www.cmake.org/pipermail/cmake/2011-January/041666.html |
| 3 |
# To use do: find_python_module(PyQt4 REQUIRED) |
| 4 |
|
| 5 |
# Keep filename as is |
| 6 |
# lint_cmake: -convention/filename, -package/stdargs |
| 7 |
|
| 8 |
include(FindPackageHandleStandardArgs) |
| 9 |
|
| 10 |
function(find_python_module module) |
| 11 |
string(TOUPPER ${module} module_upper) |
| 12 |
if(NOT PY_${module_upper}) |
| 13 |
if(ARGC GREATER 1 AND ARGV1 STREQUAL "REQUIRED") |
| 14 |
set(PY_${module}_FIND_REQUIRED TRUE) |
| 15 |
endif() |
| 16 |
if($ENV{SSG_USE_PIP_PACKAGES}) |
| 17 |
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" |
| 18 |
"import platform; print(''.join('python'+platform.python_version()[:-2]))" |
| 19 |
RESULT_VARIABLE _python_version_status |
| 20 |
OUTPUT_VARIABLE _python_version |
| 21 |
ERROR_QUIET |
| 22 |
OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 23 |
if(NOT ${_python_version_status}) |
| 24 |
set(ENV{PYTHONPATH} "/usr/local/lib/${_python_version}/site-packages:/usr/local/lib64/${_python_version}/site-packages") |
| 25 |
endif() |
| 26 |
endif() |
| 27 |
# A module's location is usually a directory, but for binary modules |
| 28 |
# it's a .so file. |
| 29 |
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" |
| 30 |
"import re, ${module}; print(re.compile('/__init__.py.*').sub('',${module}.__file__))" |
| 31 |
RESULT_VARIABLE _${module}_status |
| 32 |
OUTPUT_VARIABLE _${module}_location |
| 33 |
ERROR_QUIET |
| 34 |
OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 35 |
if(NOT _${module}_status) |
| 36 |
set(PY_${module_upper} ${_${module}_location} CACHE STRING |
| 37 |
"Location of Python module ${module}") |
| 38 |
endif() |
| 39 |
endif() |
| 40 |
find_package_handle_standard_args(PY_${module} DEFAULT_MSG PY_${module_upper}) |
| 41 |
endfunction() |