UnversionedNames.cmake 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # check if it is possible to find python with a generic name
  2. find_program(UNVERSIONED_Python3 NAMES python3)
  3. if (NOT UNVERSIONED_Python3)
  4. # no generic name available
  5. # test cannot be done
  6. return()
  7. endif()
  8. # search with default configuration
  9. find_package(Python3 REQUIRED COMPONENTS Interpreter)
  10. if (Python3_EXECUTABLE STREQUAL UNVERSIONED_Python3)
  11. # default configuration pick-up the generic name
  12. # test cannot be completed
  13. return()
  14. endif()
  15. unset(Python3_EXECUTABLE)
  16. # Force now to search first for generic name
  17. set(Python3_FIND_UNVERSIONED_NAMES FIRST)
  18. set(Python3_FIND_FRAMEWORK NEVER)
  19. find_package(Python3 REQUIRED COMPONENTS Interpreter)
  20. if (NOT Python3_EXECUTABLE STREQUAL UNVERSIONED_Python3)
  21. message(SEND_ERROR "Found unexpected interpreter ${Python3_EXECUTABLE} instead of ${UNVERSIONED_Python3}")
  22. endif()
  23. # To check value 'NEVER", creates directory holding a symlink to the generic name
  24. file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/bin")
  25. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
  26. file(CREATE_LINK "${UNVERSIONED_Python3}" "${CMAKE_CURRENT_BINARY_DIR}/bin/python3" SYMBOLIC)
  27. unset(Python3_EXECUTABLE)
  28. set(Python3_FIND_UNVERSIONED_NAMES FIRST)
  29. set(Python3_ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  30. # First search: generic name must be found
  31. find_package(Python3 REQUIRED COMPONENTS Interpreter)
  32. if (NOT Python3_EXECUTABLE STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/bin/python3")
  33. message(FATAL_ERROR "Found unexpected interpreter ${Python3_EXECUTABLE} instead of ${CMAKE_CURRENT_BINARY_DIR}/bin/python3")
  34. endif()
  35. unset(Python3_EXECUTABLE)
  36. set(Python3_FIND_UNVERSIONED_NAMES LAST)
  37. # Second search: generic name must be found
  38. find_package(Python3 REQUIRED COMPONENTS Interpreter)
  39. if (NOT Python3_EXECUTABLE STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/bin/python3")
  40. message(FATAL_ERROR "Found unexpected interpreter ${Python3_EXECUTABLE} instead of ${CMAKE_CURRENT_BINARY_DIR}/bin/python3")
  41. endif()
  42. unset(Python3_EXECUTABLE)
  43. set(Python3_FIND_UNVERSIONED_NAMES NEVER)
  44. # Third search: generic name must NOT be found
  45. find_package(Python3 REQUIRED COMPONENTS Interpreter)
  46. if (Python3_EXECUTABLE STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/bin/python3")
  47. message(FATAL_ERROR "Found unexpected interpreter ${Python3_EXECUTABLE}")
  48. endif()