FindPythonModule.cmake 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Find python module and version
  2. #
  3. # It sets the variables (given module called MODULE)
  4. # unless MODULE is __FUTURE__. In that case
  5. # it only checks if it has been found.
  6. #
  7. # MODULE_FOUND - has the module been found?
  8. # MODULE_VERSION - module version as a string
  9. # MODULE_VERSION_MAJOR - major version number
  10. # MODULE_VERSION_MINOR - minor version number
  11. # MODULE_VERSION_PATCH - patch version number
  12. # MODULE_VERSION_DECIMAL - e.g. version 1.6.1 is 10601
  13. #
  14. function(find_python_module module)
  15. # Write module in upper and lower case
  16. string(TOUPPER ${module} module_upper)
  17. string(TOLOWER ${module} module_lower)
  18. unset(${module_upper}_VERSION)
  19. #
  20. # if(ARGC GREATER 1 AND ARGV1 STREQUAL "REQUIRED")
  21. # set(${module_upper}_FIND_REQUIRED TRUE)
  22. # endif()
  23. if(PYTHONINTERP_FOUND)
  24. if (NOT ${module} STREQUAL "__future__")
  25. execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
  26. "import ${module_lower} as n; print(n.__version__);"
  27. RESULT_VARIABLE __result
  28. OUTPUT_VARIABLE __output
  29. ERROR_QUIET
  30. OUTPUT_STRIP_TRAILING_WHITESPACE)
  31. if(__result MATCHES 0)
  32. string(REGEX REPLACE ";" "\\\\;" __values ${__output})
  33. string(REGEX REPLACE "\r?\n" ";" __values ${__values})
  34. list(GET __values 0 ${module_upper}_VERSION)
  35. string(REGEX MATCH "^([0-9])+\\.([0-9])+\\.([0-9])+" __ver_check "${${module_upper}_VERSION}")
  36. if(NOT "${__ver_check}" STREQUAL "")
  37. set(${module_upper}_VERSION_MAJOR ${CMAKE_MATCH_1})
  38. set(${module_upper}_VERSION_MINOR ${CMAKE_MATCH_2})
  39. set(${module_upper}_VERSION_PATCH ${CMAKE_MATCH_3})
  40. math(EXPR ${module_upper}_VERSION_DECIMAL
  41. "(${CMAKE_MATCH_1} * 10000) + (${CMAKE_MATCH_2} * 100) + ${CMAKE_MATCH_3}")
  42. else()
  43. unset(${module_upper}_VERSION)
  44. message(STATUS "Requested ${module_lower} version, but got instead:\n${__output}\n")
  45. endif()
  46. find_package_handle_standard_args(${module_upper}
  47. FOUND_VAR ${module_upper}_FOUND
  48. REQUIRED_VARS ${module_upper}_VERSION
  49. VERSION_VAR ${module_upper}_VERSION)
  50. endif()
  51. else()
  52. execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
  53. "import ${module_lower} as n"
  54. RESULT_VARIABLE __result
  55. OUTPUT_VARIABLE __output
  56. ERROR_QUIET
  57. OUTPUT_STRIP_TRAILING_WHITESPACE)
  58. if(NOT __result)
  59. set(${module_upper}_FOUND ON)
  60. endif()
  61. message(STATUS "Found Python __FUTURE__")
  62. endif()
  63. else()
  64. message(STATUS "Python interpreter not found. To find ${module} you need the Python interpreter.")
  65. endif()
  66. # Set variables in parent scope
  67. if(${module_upper}_FOUND)
  68. set(${module_upper}_FOUND ON PARENT_SCOPE)
  69. if (NOT ${module} STREQUAL "__future__")
  70. set(${module_upper}_VERSION ${${module_upper}_VERSION} PARENT_SCOPE)
  71. set(${module_upper}_VERSION_MAJOR ${${module_upper}_VERSION_MAJOR} PARENT_SCOPE)
  72. set(${module_upper}_VERSION_MINOR ${${module_upper}_VERSION_MINOR} PARENT_SCOPE)
  73. set(${module_upper}_VERSION_PATCH ${${module_upper}_VERSION_PATCH} PARENT_SCOPE)
  74. set(${module_upper}_VERSION_DECIMAL ${${module_upper}_VERSION_DECIMAL} PARENT_SCOPE)
  75. endif()
  76. endif()
  77. # Clear variables
  78. osqp_clear_vars(__result __output __values __ver_check)
  79. endfunction(find_python_module)