FindPython.cmake 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #[=======================================================================[.rst
  2. FindPython
  3. ----------
  4. FindModule for Python import libraries and header on Windows
  5. .. versionchanged:: 3.0
  6. Updated FindModule to CMake standards
  7. Imported Targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 2.0
  10. This module defines the :prop_tgt:`IMPORTED` target ``Python::Python``.
  11. Result Variables
  12. ^^^^^^^^^^^^^^^^
  13. This module sets the following variables:
  14. ``Python_FOUND``
  15. True, if all required components and the core library were found.
  16. ``Python_VERSION``
  17. Detected version of found Python libraries.
  18. ``Python_INCLUDE_DIRS``
  19. Include directories needed for Python.
  20. ``Python_LIBRARIES``
  21. Libraries needed to link to Python.
  22. Cache variables
  23. ^^^^^^^^^^^^^^^
  24. The following cache variables may also be set:
  25. ``Python_LIBRARY``
  26. Path to the library component of Python.
  27. ``Python_INCLUDE_DIR``
  28. Directory containing ``python.h``.
  29. #]=======================================================================]
  30. include(FindPackageHandleStandardArgs)
  31. find_path(Python_INCLUDE_DIR NAMES Python.h PATH_SUFFIXES include include/python DOC "Python include directory")
  32. find_library(Python_LIBRARY NAMES python3 PATHS lib DOC "Python location")
  33. if(EXISTS "${Python_INCLUDE_DIR}/patchlevel.h")
  34. file(STRINGS "${Python_INCLUDE_DIR}/patchlevel.h" _VERSION_STRING REGEX "^.*PY_VERSION[ \t]+\"[0-9\\.]+\"[ \t]*$")
  35. string(REGEX REPLACE ".*PY_VERSION[ \t]+\"([0-9\\.]+)\".*" "\\1" Python_VERSION "${_VERSION_STRING}")
  36. else()
  37. if(NOT Python_FIND_QUIETLY)
  38. message(AUTHOR_WARNING "Failed to find Python version.")
  39. endif()
  40. set(Python_VERSION 0.0.0)
  41. endif()
  42. find_package_handle_standard_args(
  43. Python
  44. REQUIRED_VARS Python_LIBRARY Python_INCLUDE_DIR
  45. VERSION_VAR Python_VERSION
  46. HANDLE_VERSION_RANGE
  47. REASON_FAILURE_MESSAGE "Ensure that obs-deps is provided as part of CMAKE_PREFIX_PATH."
  48. )
  49. mark_as_advanced(Python_INCLUDE_DIR Python_LIBRARY)
  50. if(Python_FOUND)
  51. set(Python_INCLUDE_DIRS ${Python_INCLUDE_DIR})
  52. set(Python_LIBRARIES ${Python_LIBRARY})
  53. set(Python_DEFINITIONS ${PC_Python_CFLAGS_OTHER})
  54. if(NOT TARGET Python::Python)
  55. if(IS_ABSOLUTE "${Python_LIBRARY}")
  56. add_library(Python::Python UNKNOWN IMPORTED)
  57. set_property(TARGET Python::Python PROPERTY IMPORTED_LOCATION "${Python_LIBRARY}")
  58. else()
  59. add_library(Python::Python INTERFACE IMPORTED)
  60. set_property(TARGET Python::Python PROPERTY IMPORTED_LIBNAME "${Python_LIBRARY}")
  61. endif()
  62. set_target_properties(
  63. Python::Python
  64. PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${Python_INCLUDE_DIR}" VERSION ${Python_VERSION}
  65. )
  66. endif()
  67. endif()
  68. include(FeatureSummary)
  69. set_package_properties(
  70. Python
  71. PROPERTIES
  72. URL "https://www.python.org"
  73. DESCRIPTION
  74. "Python is a programming language that lets you work more quickly and integrate your systems more effectively."
  75. )