CMakeLists.txt 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. cmake_minimum_required(VERSION 3.15)
  2. project(TestPython2 LANGUAGES C)
  3. include(CTest)
  4. find_package(Python2 3 QUIET)
  5. if (Python2_FOUND)
  6. message (FATAL_ERROR "Wrong python version found: ${Python2_VERSION}")
  7. endif()
  8. find_package(Python2 REQUIRED COMPONENTS Interpreter Development)
  9. if (NOT Python2_FOUND)
  10. message (FATAL_ERROR "Failed to find Python 2")
  11. endif()
  12. if (NOT Python2_Development_FOUND)
  13. message (FATAL_ERROR "Failed to find Python 2 'Development' component")
  14. endif()
  15. if (NOT Python2_Development.Module_FOUND)
  16. message (FATAL_ERROR "Failed to find Python 2 'Development.Module' component")
  17. endif()
  18. if (NOT Python2_Development.Embed_FOUND)
  19. message (FATAL_ERROR "Failed to find Python 2 'Development.Embed' component")
  20. endif()
  21. if(NOT TARGET Python2::Interpreter)
  22. message(SEND_ERROR "Python2::Interpreter not found")
  23. endif()
  24. if(NOT TARGET Python2::Python)
  25. message(SEND_ERROR "Python2::Python not found")
  26. endif()
  27. if(NOT TARGET Python2::Module)
  28. message(SEND_ERROR "Python2::Module not found")
  29. endif()
  30. Python2_add_library (spam2 MODULE ../spam.c)
  31. target_compile_definitions (spam2 PRIVATE PYTHON2)
  32. add_test (NAME python2_spam2
  33. COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam2>"
  34. "${Python2_INTERPRETER}" -c "import spam2; spam2.system(\"cd\")")
  35. add_test(NAME findpython2_script
  36. COMMAND "${CMAKE_COMMAND}" -DPYTHON_PACKAGE_NAME=Python2
  37. -DPython2_FIND_STRATEGY=${Python2_FIND_STRATEGY}
  38. -P "${CMAKE_CURRENT_LIST_DIR}/../FindPythonScript.cmake")
  39. #
  40. # New search with user's prefix
  41. #
  42. foreach(item IN ITEMS FOUND Development_FOUND Development.Module_FOUND Development.Embed_FOUND)
  43. unset(Python2_${item})
  44. endforeach()
  45. set(Python2_ARTIFACTS_PREFIX "_TEST")
  46. find_package(Python2 REQUIRED COMPONENTS Interpreter Development)
  47. if (NOT Python2_TEST_FOUND OR NOT Python2_FOUND)
  48. message (FATAL_ERROR "Failed to find Python 2 (TEST prefix)")
  49. endif()
  50. if (NOT Python2_TEST_Development_FOUND OR NOT Python2_Development_FOUND)
  51. message (FATAL_ERROR "Failed to find Python 2 'Development' component (TEST prefix)")
  52. endif()
  53. if (NOT Python2_TEST_Development.Module_FOUND OR NOT Python2_Development.Module_FOUND)
  54. message (FATAL_ERROR "Failed to find Python 2 'Development.Module' component (TEST prefix)")
  55. endif()
  56. if (NOT Python2_TEST_Development.Embed_FOUND OR NOT Python2_Development.Embed_FOUND)
  57. message (FATAL_ERROR "Failed to find Python 2 'Development.Embed' component (TEST prefix)")
  58. endif()
  59. if(NOT TARGET Python2_TEST::Interpreter)
  60. message(SEND_ERROR "Python2_TEST::Interpreter not found")
  61. endif()
  62. if(NOT TARGET Python2_TEST::Python)
  63. message(SEND_ERROR "Python2_TEST::Python not found")
  64. endif()
  65. if(NOT TARGET Python2_TEST::Module)
  66. message(SEND_ERROR "Python2_TEST::Module not found")
  67. endif()
  68. Python2_TEST_add_library (TEST_spam2 MODULE ../TEST_spam.c)
  69. target_compile_definitions (TEST_spam2 PRIVATE PYTHON2)
  70. add_test (NAME python2_TEST_spam2
  71. COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:TEST_spam2>"
  72. "${Python2_INTERPRETER}" -c "import TEST_spam2; TEST_spam2.system(\"cd\")")