Python3.cmake 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. enable_language(C)
  2. include(CTest)
  3. find_package(Python3 2 QUIET)
  4. if (Python3_FOUND)
  5. message (FATAL_ERROR "Wrong python version found: ${Python3_VERSION}")
  6. endif()
  7. find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
  8. if (NOT Python3_FOUND)
  9. message (FATAL_ERROR "Failed to find Python 3")
  10. endif()
  11. if (NOT Python3_Development_FOUND)
  12. message (FATAL_ERROR "Failed to find Python 3 'Development' component")
  13. endif()
  14. if (NOT Python3_Development.Module_FOUND)
  15. message (FATAL_ERROR "Failed to find Python 3 'Development.Module' component")
  16. endif()
  17. if (NOT Python3_Development.Embed_FOUND)
  18. message (FATAL_ERROR "Failed to find Python 3 'Development.Embed' component")
  19. endif()
  20. if(NOT TARGET Python3::Interpreter)
  21. message(SEND_ERROR "Python3::Interpreter not found")
  22. endif()
  23. if(NOT TARGET Python3::Python)
  24. message(SEND_ERROR "Python3::Python not found")
  25. endif()
  26. if(NOT TARGET Python3::Module)
  27. message(SEND_ERROR "Python3::Module not found")
  28. endif()
  29. Python3_add_library (spam3 MODULE spam.c)
  30. target_compile_definitions (spam3 PRIVATE PYTHON3)
  31. add_test (NAME python3_spam3
  32. COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam3>"
  33. "${Python3_INTERPRETER}" -c "import spam3; spam3.system(\"cd\")")
  34. add_test(NAME findpython3_script
  35. COMMAND "${CMAKE_COMMAND}" -DPYTHON_PACKAGE_NAME=Python3
  36. -DPython3_FIND_STRATEGY=${Python3_FIND_STRATEGY}
  37. -P "${CMAKE_CURRENT_LIST_DIR}/FindPythonScript.cmake")
  38. #
  39. # New search with user's prefix
  40. #
  41. foreach(item IN ITEMS FOUND Development_FOUND Development.Module_FOUND Development.Embed_FOUND)
  42. unset(Python3_${item})
  43. endforeach()
  44. set(Python3_ARTIFACTS_PREFIX "_TEST")
  45. find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
  46. if (NOT Python3_TEST_FOUND OR NOT Python3_FOUND)
  47. message (FATAL_ERROR "Failed to find Python 3 (TEST prefix)")
  48. endif()
  49. if (NOT Python3_TEST_Development_FOUND OR NOT Python3_Development_FOUND)
  50. message (FATAL_ERROR "Failed to find Python 3 'Development' component (TEST prefix)")
  51. endif()
  52. if (NOT Python3_TEST_Development.Module_FOUND OR NOT Python3_Development.Module_FOUND)
  53. message (FATAL_ERROR "Failed to find Python 3 'Development.Module' component (TEST prefix)")
  54. endif()
  55. if (NOT Python3_TEST_Development.Embed_FOUND OR NOT Python3_Development.Embed_FOUND)
  56. message (FATAL_ERROR "Failed to find Python 3 'Development.Embed' component (TEST prefix)")
  57. endif()
  58. if(NOT TARGET Python3_TEST::Interpreter)
  59. message(SEND_ERROR "Python3_TEST::Interpreter not found")
  60. endif()
  61. if(NOT TARGET Python3_TEST::Python)
  62. message(SEND_ERROR "Python3_TEST::Python not found")
  63. endif()
  64. if(NOT TARGET Python3_TEST::Module)
  65. message(SEND_ERROR "Python3_TEST::Module not found")
  66. endif()
  67. Python3_TEST_add_library (TEST_spam3 MODULE TEST_spam.c)
  68. target_compile_definitions (TEST_spam3 PRIVATE PYTHON3)
  69. add_test (NAME python3_TEST_spam3
  70. COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:TEST_spam3>"
  71. "${Python3_INTERPRETER}" -c "import TEST_spam3; TEST_spam3.system(\"cd\")")