Python2.cmake 3.0 KB

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