/Tests/FindPython/Python3/CMakeLists.txt

https://github.com/dlrdave/CMake · CMake · 99 lines · 81 code · 13 blank · 5 comment · 13 complexity · 41818bb058fbf15822d0615cea5afef5 MD5 · raw file

  1. cmake_minimum_required(VERSION 3.1)
  2. project(TestPython3 LANGUAGES C)
  3. include(CTest)
  4. find_package(Python3 2 QUIET)
  5. if (Python3_FOUND)
  6. message (FATAL_ERROR "Wrong python version found: ${Python3_VERSION}")
  7. endif()
  8. find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
  9. if (NOT Python3_FOUND)
  10. message (FATAL_ERROR "Failed to find Python 3")
  11. endif()
  12. if (NOT Python3_Development_FOUND)
  13. message (FATAL_ERROR "Failed to find Python 3 'Development' component")
  14. endif()
  15. if (NOT Python3_Development.Module_FOUND)
  16. message (FATAL_ERROR "Failed to find Python 3 'Development.Module' component")
  17. endif()
  18. if (NOT Python3_Development.Embed_FOUND)
  19. message (FATAL_ERROR "Failed to find Python 3 'Development.Embed' component")
  20. endif()
  21. if(NOT TARGET Python3::Interpreter)
  22. message(SEND_ERROR "Python3::Interpreter not found")
  23. endif()
  24. if(NOT TARGET Python3::Python)
  25. message(SEND_ERROR "Python3::Python not found")
  26. endif()
  27. if(NOT TARGET Python3::Module)
  28. message(SEND_ERROR "Python3::Module not found")
  29. endif()
  30. Python3_add_library (spam3 MODULE ../spam.c)
  31. target_compile_definitions (spam3 PRIVATE PYTHON3)
  32. add_test (NAME python3_spam3
  33. COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam3>"
  34. "${Python3_EXECUTABLE}" -c "import spam3; spam3.system(\"cd\")")
  35. add_test(NAME findpython3_script
  36. COMMAND "${CMAKE_COMMAND}" -DPYTHON_PACKAGE_NAME=Python3
  37. -DPython3_FIND_STRATEGY=${Python3_FIND_STRATEGY}
  38. -P "${CMAKE_CURRENT_LIST_DIR}/../FindPythonScript.cmake")
  39. ## Try a new search specifying only expected ABI
  40. # retrieve ABI of python interpreter
  41. execute_process (COMMAND "${Python3_EXECUTABLE}" -c
  42. "import sys; sys.stdout.write(sys.abiflags)"
  43. RESULT_VARIABLE result
  44. OUTPUT_VARIABLE abi
  45. ERROR_QUIET
  46. OUTPUT_STRIP_TRAILING_WHITESPACE)
  47. if (result)
  48. # assume ABI is not supported
  49. set (abi "")
  50. endif()
  51. # define FIND_ABI variable
  52. if (abi MATCHES "d")
  53. set (Python3_VALID_ABI "ON")
  54. else()
  55. set (Python3_VALID_ABI "OFF")
  56. endif()
  57. if (abi MATCHES "m")
  58. list (APPEND Python3_VALID_ABI "ON")
  59. else()
  60. list (APPEND Python3_VALID_ABI "OFF")
  61. endif()
  62. if (abi MATCHES "u")
  63. list (APPEND Python3_VALID_ABI "ON")
  64. else()
  65. list (APPEND Python3_VALID_ABI "OFF")
  66. endif()
  67. # build an invalid pattern for ABI
  68. set (Python3_INVALID_ABI)
  69. foreach (abi IN LISTS Python3_VALID_ABI)
  70. if (abi)
  71. list (APPEND Python3_INVALID_ABI "OFF")
  72. else()
  73. list (APPEND Python3_INVALID_ABI "ON")
  74. endif()
  75. endforeach()
  76. add_test(NAME python3_find_valid_abi
  77. COMMAND "${CMAKE_COMMAND}" -DPYTHON_PACKAGE_NAME=Python3
  78. -DPython3_FIND_STRATEGY=${Python3_FIND_STRATEGY}
  79. "-DPython3_FIND_ABI=${Python3_VALID_ABI}"
  80. -P "${CMAKE_CURRENT_LIST_DIR}/../FindPythonScript.cmake")
  81. add_test(NAME python3_find_invalid_abi
  82. COMMAND "${CMAKE_COMMAND}" -DPYTHON_PACKAGE_NAME=Python3
  83. -DPYTHON_MUST_NOT_BE_FOUND=ON
  84. -DPython3_FIND_STRATEGY=${Python3_FIND_STRATEGY}
  85. "-DPython3_FIND_ABI=${Python3_INVALID_ABI}"
  86. -P "${CMAKE_CURRENT_LIST_DIR}/../FindPythonScript.cmake")