CMakeLists.txt 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. cmake_minimum_required(VERSION 3.15)
  2. project(TestPython LANGUAGES C)
  3. include(CTest)
  4. find_package(Python ${Python_REQUESTED_VERSION} REQUIRED COMPONENTS Interpreter Development)
  5. if (NOT Python_FOUND)
  6. message (FATAL_ERROR "Failed to find Python ${Python_REQUESTED_VERSION}")
  7. endif()
  8. if (NOT Python_Development.Module_FOUND)
  9. message (FATAL_ERROR "Failed to find Python ${Python_REQUESTED_VERSION}, COMPONENT 'Development.Module'")
  10. endif()
  11. if (NOT Python_Development.Embed_FOUND)
  12. message (FATAL_ERROR "Failed to find Python ${Python_REQUESTED_VERSION}, COMPOENENT 'Development.Embed'")
  13. endif()
  14. if(NOT TARGET Python::Interpreter)
  15. message(SEND_ERROR "Python::Interpreter not found")
  16. endif()
  17. if(NOT TARGET Python::Python)
  18. message(SEND_ERROR "Python::Python not found")
  19. endif()
  20. if(NOT TARGET Python::Module)
  21. message(SEND_ERROR "Python::Module not found")
  22. endif()
  23. if (Python_REQUESTED_VERSION)
  24. Python_add_library (spam${Python_REQUESTED_VERSION} MODULE ../spam.c)
  25. target_compile_definitions (spam${Python_REQUESTED_VERSION} PRIVATE PYTHON${Python_REQUESTED_VERSION})
  26. add_test (NAME python_spam${Python_REQUESTED_VERSION}
  27. COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam${Python_REQUESTED_VERSION}>"
  28. "${Python_INTERPRETER}" -c "import spam${Python_REQUESTED_VERSION}; spam${Python_REQUESTED_VERSION}.system(\"cd\")")
  29. else()
  30. add_test(NAME findpython_script
  31. COMMAND "${CMAKE_COMMAND}" -DPYTHON_PACKAGE_NAME=Python
  32. -P "${CMAKE_CURRENT_LIST_DIR}/../FindPythonScript.cmake")
  33. endif()
  34. #
  35. # New search with user's prefix
  36. #
  37. foreach(item IN ITEMS FOUND Development_FOUND Development.Module_FOUND Development.Embed_FOUND)
  38. unset(Python_${item})
  39. endforeach()
  40. set(Python_ARTIFACTS_PREFIX "_TEST")
  41. find_package(Python ${Python_REQUESTED_VERSION} REQUIRED COMPONENTS Interpreter Development)
  42. if (NOT Python_TEST_FOUND OR NOT Python_FOUND)
  43. message (FATAL_ERROR "Failed to find Python ${Python_REQUESTED_VERSION} (TEST prefix)")
  44. endif()
  45. if (NOT Python_TEST_Development.Module_FOUND OR NOT Python_Development.Module_FOUND)
  46. message (FATAL_ERROR "Failed to find Python ${Python_REQUESTED_VERSION}, COMPONENT 'Development.Module' (TEST prefix)")
  47. endif()
  48. if (NOT Python_TEST_Development.Embed_FOUND OR NOT Python_Development.Embed_FOUND)
  49. message (FATAL_ERROR "Failed to find Python ${Python_REQUESTED_VERSION}, COMPOENENT 'Development.Embed' (TEST prefix)")
  50. endif()
  51. if(NOT TARGET Python_TEST::Interpreter)
  52. message(SEND_ERROR "Python_TEST::Interpreter not found")
  53. endif()
  54. if(NOT TARGET Python_TEST::Python)
  55. message(SEND_ERROR "Python_TEST::Python not found")
  56. endif()
  57. if(NOT TARGET Python_TEST::Module)
  58. message(SEND_ERROR "Python_TEST::Module not found")
  59. endif()
  60. if (Python_REQUESTED_VERSION)
  61. Python_TEST_add_library (TEST_spam${Python_REQUESTED_VERSION} MODULE ../TEST_spam.c)
  62. target_compile_definitions (TEST_spam${Python_REQUESTED_VERSION} PRIVATE PYTHON${Python_REQUESTED_VERSION})
  63. add_test (NAME python_TEST_spam${Python_REQUESTED_VERSION}
  64. COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:TEST_spam${Python_REQUESTED_VERSION}>"
  65. "${Python_INTERPRETER}" -c "import TEST_spam${Python_REQUESTED_VERSION}; TEST_spam${Python_REQUESTED_VERSION}.system(\"cd\")")
  66. endif()