CMakeLists.txt 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. cmake_minimum_required(VERSION 3.1)
  2. project(TestPython C)
  3. include(CTest)
  4. find_package(Python ${Python_REQUESTED_VERSION} REQUIRED COMPONENTS Interpreter Development)
  5. if (NOT Python_FOUND)
  6. message (FATAL_ERROR "Fail to found Python ${Python_REQUESTED_VERSION}")
  7. endif()
  8. if (NOT Python_Development.Module_FOUND)
  9. message (FATAL_ERROR "Fail to found Python ${Python_REQUESTED_VERSION}, COMPONENT 'Development.Module'")
  10. endif()
  11. if (NOT Python_Development.Embed_FOUND)
  12. message (FATAL_ERROR "Fail to found 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_EXECUTABLE}" -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()