CMakeLists.txt 2.3 KB

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