FindPythonModule.cmake 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Find a Python module
  2. # Found at http://www.cmake.org/pipermail/cmake/2011-January/041666.html
  3. # To use do: find_python_module(NumPy REQUIRED)
  4. # Reports also version of package, but you can't currently enforce a specific version to be
  5. # searched for...
  6. macro(find_python_module module)
  7. # Fail if Python interpreter not known
  8. if(NOT PYTHON_EXECUTABLE)
  9. message(FATAL_ERROR "Use find_package(PythonInterp) first!")
  10. endif()
  11. string(TOLOWER ${module} _module_lower)
  12. if(NOT ${_module_lower})
  13. if(ARGC GREATER 1 AND ARGV1 STREQUAL "REQUIRED")
  14. set(${module}_FIND_REQUIRED TRUE)
  15. endif()
  16. # Find module location
  17. execute_process(
  18. COMMAND
  19. ${PYTHON_EXECUTABLE} "-c" "import re, ${_module_lower}; print(re.compile('/__init__.py.*').sub('',${_module_lower}.__file__))"
  20. RESULT_VARIABLE ${module}_STATUS
  21. OUTPUT_VARIABLE ${module}_LOCATION
  22. ERROR_QUIET
  23. OUTPUT_STRIP_TRAILING_WHITESPACE
  24. )
  25. if(NOT ${module}_STATUS)
  26. set(${module} ${${module}_LOCATION} CACHE STRING "Location of Python module ${module}")
  27. endif()
  28. # Find module version
  29. execute_process(
  30. COMMAND
  31. ${PYTHON_EXECUTABLE} "-c" "import re, ${_module_lower}; print(re.compile('/__init__.py.*').sub('',${_module_lower}.__version__))"
  32. OUTPUT_VARIABLE ${module}_VERSION
  33. ERROR_QUIET
  34. OUTPUT_STRIP_TRAILING_WHITESPACE
  35. )
  36. endif()
  37. include(FindPackageHandleStandardArgs)
  38. find_package_handle_standard_args(${module}
  39. FOUND_VAR
  40. ${module}_FOUND
  41. REQUIRED_VARS
  42. ${module}_LOCATION
  43. VERSION_VAR
  44. ${module}_VERSION
  45. )
  46. endmacro()