FindPython.cmake 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. # cmake-format: off
  31. # cmake-lint: disable=C0103
  32. # cmake-lint: disable=C0301
  33. # cmake-format: on
  34. include(FindPackageHandleStandardArgs)
  35. find_path(
  36. Python_INCLUDE_DIR
  37. NAMES Python.h
  38. PATH_SUFFIXES include include/python
  39. DOC "Python include directory")
  40. find_library(
  41. Python_LIBRARY
  42. NAMES python3
  43. PATHS lib
  44. DOC "Python location")
  45. if(EXISTS "${Python_INCLUDE_DIR}/patchlevel.h")
  46. file(STRINGS "${Python_INCLUDE_DIR}/patchlevel.h" _VERSION_STRING REGEX "^.*PY_VERSION[ \t]+\"[0-9\\.]+\"[ \t]*$")
  47. string(REGEX REPLACE ".*PY_VERSION[ \t]+\"([0-9\\.]+)\".*" "\\1" Python_VERSION "${_VERSION_STRING}")
  48. else()
  49. if(NOT Python_FIND_QUIETLY)
  50. message(AUTHOR_WARNING "Failed to find Python version.")
  51. endif()
  52. set(Python_VERSION 0.0.0)
  53. endif()
  54. find_package_handle_standard_args(
  55. Python
  56. REQUIRED_VARS Python_LIBRARY Python_INCLUDE_DIR
  57. VERSION_VAR Python_VERSION HANDLE_VERSION_RANGE REASON_FAILURE_MESSAGE
  58. "Ensure that obs-deps is provided as part of CMAKE_PREFIX_PATH.")
  59. mark_as_advanced(Python_INCLUDE_DIR Python_LIBRARY)
  60. if(Python_FOUND)
  61. set(Python_INCLUDE_DIRS ${Python_INCLUDE_DIR})
  62. set(Python_LIBRARIES ${Python_LIBRARY})
  63. set(Python_DEFINITIONS ${PC_Python_CFLAGS_OTHER})
  64. if(NOT TARGET Python::Python)
  65. if(IS_ABSOLUTE "${Python_LIBRARY}")
  66. add_library(Python::Python UNKNOWN IMPORTED)
  67. set_property(TARGET Python::Python PROPERTY IMPORTED_LOCATION "${Python_LIBRARY}")
  68. else()
  69. add_library(Python::Python INTERFACE IMPORTED)
  70. set_property(TARGET Python::Python PROPERTY IMPORTED_LIBNAME "${Python_LIBRARY}")
  71. endif()
  72. set_target_properties(Python::Python PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${Python_INCLUDE_DIR}"
  73. VERSION ${Python_VERSION})
  74. endif()
  75. endif()
  76. include(FeatureSummary)
  77. set_package_properties(
  78. Python PROPERTIES
  79. URL "https://www.python.org"
  80. DESCRIPTION
  81. "Python is a programming language that lets you work more quickly and integrate your systems more effectively.")