Python.cmake 3.2 KB

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