CMakeLists.txt 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. cmake_minimum_required (VERSION 3.18...3.19)
  2. project (TestVersionRange LANGUAGES C)
  3. find_package (${Python} ${Python_REQUESTED_VERSION} EXACT COMPONENTS Interpreter)
  4. if (NOT ${Python}_FOUND)
  5. message (FATAL_ERROR "Failed to find ${Python} ${Python_REQUESTED_VERSION}")
  6. endif()
  7. if (Python_REQUESTED_VERSION VERSION_LESS 3.0)
  8. set (IN_VERSION_RANGE 2.0...<3.0)
  9. set (OUT_VERSION_RANGE 2.0...<${${Python}_VERSION})
  10. else()
  11. set (IN_VERSION_RANGE 3.0...<4.0)
  12. set (OUT_VERSION_RANGE 3.0...<${${Python}_VERSION})
  13. endif()
  14. function (FIND_PYTHON EXPECTED_VERSION RANGE)
  15. macro (FIND_PYTHON_PACKAGE)
  16. unset (_${Python}_EXECUTABLE CACHE)
  17. unset (_${Python}_LIBRARY_RELEASE CACHE)
  18. unset (_${Python}_INCLUDE_DIR CACHE)
  19. unset (${Python}_FOUND)
  20. find_package (${Python} ${ARGV})
  21. endmacro()
  22. find_python_package(${RANGE} ${ARGN})
  23. if (EXPECTED_VERSION STREQUAL "NONE")
  24. while (${Python}_FOUND AND ${Python}_VERSION VERSION_GREATER ${Python_REQUESTED_VERSION})
  25. # Possible if multiple versions are installed
  26. # Try with a different range
  27. find_python_package(${Python_REQUESTED_VERSION}.0...<${${Python}_VERSION} ${ARGN})
  28. endwhile()
  29. if (${Python}_FOUND)
  30. message (SEND_ERROR "Unexpectedly found version: ${${Python}_VERSION} for '${Python} ${Python_REQUESTED_VERSION}.0...<${${Python}_VERSION} ${ARGN}'")
  31. endif()
  32. return()
  33. endif()
  34. if (NOT ${Python}_FOUND)
  35. message (SEND_ERROR "Not found: ${Python} ${RANGE} ${ARGN}")
  36. elseif (NOT ${Python}_VERSION VERSION_EQUAL EXPECTED_VERSION)
  37. message (SEND_ERROR "Wrong version: ${${Python}_VERSION} for '${Python} ${RANGE} ${ARGN}'")
  38. endif()
  39. endfunction()
  40. find_python (${${Python}_VERSION} ${IN_VERSION_RANGE} COMPONENTS Interpreter)
  41. if (${Python}_FIND_IMPLEMENTATIONS STREQUAL "IronPython")
  42. find_python (${${Python}_VERSION} ${IN_VERSION_RANGE} COMPONENTS Compiler)
  43. else()
  44. find_python (${${Python}_VERSION} ${IN_VERSION_RANGE} COMPONENTS Development)
  45. endif()
  46. find_python ("NONE" ${OUT_VERSION_RANGE} COMPONENTS Interpreter)
  47. if (${Python}_FIND_IMPLEMENTATIONS STREQUAL "IronPython")
  48. find_python ("NONE" ${OUT_VERSION_RANGE} COMPONENTS Compiler)
  49. else()
  50. find_python ("NONE" ${OUT_VERSION_RANGE} COMPONENTS Development)
  51. endif()
  52. find_python ("NONE" 5...6 COMPONENTS Interpreter)