FindPythonModule.cmake 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Find a Python module
  2. macro(find_python_module module)
  3. # Fail if Python interpreter not known
  4. if(NOT PYTHON_EXECUTABLE)
  5. message(FATAL_ERROR "Use find_package(PythonInterp) first!")
  6. endif()
  7. string(TOLOWER ${module} _module_lower)
  8. if(NOT ${_module_lower})
  9. if(ARGC GREATER 1 AND ARGV1 STREQUAL "REQUIRED")
  10. set(${module}_FIND_REQUIRED TRUE)
  11. endif()
  12. # Find module location
  13. execute_process(
  14. COMMAND
  15. ${PYTHON_EXECUTABLE} "-c" "import re, ${_module_lower}; print(re.compile('/__init__.py.*').sub('',${_module_lower}.__file__))"
  16. RESULT_VARIABLE ${module}_STATUS
  17. OUTPUT_VARIABLE ${module}_LOCATION
  18. ERROR_QUIET
  19. OUTPUT_STRIP_TRAILING_WHITESPACE
  20. )
  21. if(NOT ${module}_STATUS)
  22. set(${module} ${${module}_LOCATION} CACHE STRING "Location of Python module ${module}")
  23. endif()
  24. # Find module version
  25. execute_process(
  26. COMMAND
  27. ${PYTHON_EXECUTABLE} "-c" "import re, ${_module_lower}; print(re.compile('/__init__.py.*').sub('',${_module_lower}.__version__))"
  28. OUTPUT_VARIABLE ${module}_VERSION
  29. ERROR_QUIET
  30. OUTPUT_STRIP_TRAILING_WHITESPACE
  31. )
  32. endif()
  33. include(FindPackageHandleStandardArgs)
  34. find_package_handle_standard_args(${module}
  35. FOUND_VAR
  36. ${module}_FOUND
  37. REQUIRED_VARS
  38. ${module}_LOCATION
  39. VERSION_VAR
  40. ${module}_VERSION
  41. )
  42. endmacro()