pybind11Config.cmake.in 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #[=============================================================================[.rst:
  2. pybind11Config.cmake
  3. ####################
  4. Exported variables
  5. ==================
  6. This module sets the following variables in your project:
  7. ``pybind11_FOUND``
  8. true if pybind11 and all required components found on the system
  9. ``pybind11_VERSION``
  10. pybind11 version in format Major.Minor.Release
  11. ``pybind11_VERSION_TYPE``
  12. pybind11 version type (dev, release)
  13. ``pybind11_INCLUDE_DIRS``
  14. Directories where pybind11 and python headers are located.
  15. ``pybind11_INCLUDE_DIR``
  16. Directory where pybind11 headers are located.
  17. ``pybind11_DEFINITIONS``
  18. Definitions necessary to use pybind11, namely USING_pybind11.
  19. ``pybind11_LIBRARIES``
  20. Compile flags and python libraries (as needed) to link against.
  21. ``pybind11_LIBRARY``
  22. Empty.
  23. Available components: None
  24. Exported targets
  25. ================
  26. If pybind11 is found, this module defines the following ``IMPORTED``
  27. interface library targets:
  28. ``pybind11::module``
  29. for extension modules.
  30. ``pybind11::embed``
  31. for embedding the Python interpreter.
  32. Python headers, libraries (as needed by platform), and the C++ standard
  33. are attached to the target.
  34. Advanced targets are also supplied - these are primary for users building
  35. complex applications, and they are available in all modes:
  36. ``pybind11::headers``
  37. Just the pybind11 headers and minimum compile requirements.
  38. ``pybind11::pybind11``
  39. Python headers too.
  40. ``pybind11::python_link_helper``
  41. Just the "linking" part of ``pybind11:module``, for CMake < 3.15.
  42. ``pybind11::python2_no_register``
  43. Quiets the warning/error when mixing C++14+ and Python 2, also included in ``pybind11::module``.
  44. ``pybind11::thin_lto``
  45. An alternative to ``INTERPROCEDURAL_OPTIMIZATION``.
  46. ``pybind11::lto``
  47. An alternative to ``INTERPROCEDURAL_OPTIMIZATION`` (also avoids thin LTO on clang).
  48. ``pybind11::windows_extras``
  49. Adds bigobj and mp for MSVC.
  50. Modes
  51. =====
  52. There are two modes provided; classic, which is built on the old Python
  53. discovery packages in CMake, or the new FindPython mode, which uses FindPython
  54. from 3.12+ forward (3.15+ _highly_ recommended).
  55. New FindPython mode
  56. ^^^^^^^^^^^^^^^^^^^
  57. To activate this mode, either call ``find_package(Python COMPONENTS Interpreter Development)``
  58. before finding this package, or set the ``PYBIND11_FINDPYTHON`` variable to ON. In this mode,
  59. you can either use the basic targets, or use the FindPython tools:
  60. .. code-block:: cmake
  61. find_package(Python COMPONENTS Interpreter Development)
  62. find_package(pybind11 CONFIG)
  63. # pybind11 method:
  64. pybind11_add_module(MyModule1 src1.cpp)
  65. # Python method:
  66. Python_add_library(MyModule2 src2.cpp)
  67. target_link_libraries(MyModule2 pybind11::headers)
  68. set_target_properties(MyModule2 PROPERTIES
  69. INTERPROCEDURAL_OPTIMIZATION ON
  70. CXX_VISIBILITY_PRESET ON
  71. VISIBLITY_INLINES_HIDDEN ON)
  72. If you build targets yourself, you may be interested in stripping the output
  73. for reduced size; this is the one other feature that the helper function gives you.
  74. Classic mode
  75. ^^^^^^^^^^^^
  76. Set PythonLibsNew variables to influence python detection and
  77. CMAKE_CXX_STANDARD to influence standard setting.
  78. .. code-block:: cmake
  79. find_package(pybind11 CONFIG REQUIRED)
  80. # Create an extension module
  81. add_library(mylib MODULE main.cpp)
  82. target_link_libraries(mylib PUBLIC pybind11::module)
  83. # Or embed the Python interpreter into an executable
  84. add_executable(myexe main.cpp)
  85. target_link_libraries(myexe PUBLIC pybind11::embed)
  86. Hints
  87. =====
  88. The following variables can be set to guide the search for this package:
  89. ``pybind11_DIR``
  90. CMake variable, set to directory containing this Config file.
  91. ``CMAKE_PREFIX_PATH``
  92. CMake variable, set to root directory of this package.
  93. ``PATH``
  94. Environment variable, set to bin directory of this package.
  95. ``CMAKE_DISABLE_FIND_PACKAGE_pybind11``
  96. CMake variable, disables ``find_package(pybind11)`` when not ``REQUIRED``,
  97. perhaps to force internal build.
  98. Commands
  99. ========
  100. pybind11_add_module
  101. ^^^^^^^^^^^^^^^^^^^
  102. This module defines the following commands to assist with creating Python modules:
  103. .. code-block:: cmake
  104. pybind11_add_module(<target>
  105. [STATIC|SHARED|MODULE]
  106. [THIN_LTO] [OPT_SIZE] [NO_EXTRAS] [WITHOUT_SOABI]
  107. <files>...
  108. )
  109. Add a module and setup all helpers. You can select the type of the library; the
  110. default is ``MODULE``. There are several options:
  111. ``OPT_SIZE``
  112. Optimize for size, even if the ``CMAKE_BUILD_TYPE`` is not ``RelSize``.
  113. ``THIN_LTO``
  114. Use thin TLO instead of regular if there's a choice (pybind11's selection
  115. is disabled if ``CMAKE_INTERPROCEDURAL_OPTIMIZATIONS`` is set).
  116. ``WITHOUT_SOABI``
  117. Disable the SOABI component (``PYBIND11_NEWPYTHON`` mode only).
  118. ``NO_EXTRAS``
  119. Disable all extras, exit immediately after making the module.
  120. pybind11_strip
  121. ^^^^^^^^^^^^^^
  122. .. code-block:: cmake
  123. pybind11_strip(<target>)
  124. Strip a target after building it (linux/macOS), called by ``pybind11_add_module``.
  125. pybind11_extension
  126. ^^^^^^^^^^^^^^^^^^
  127. .. code-block:: cmake
  128. pybind11_extension(<target>)
  129. Sets the Python extension name correctly for Python on your platform, called by
  130. ``pybind11_add_module``.
  131. pybind11_find_import(module)
  132. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  133. .. code-block:: cmake
  134. pybind11_find_import(<module> [VERSION <number>] [REQUIRED] [QUIET])
  135. See if a module is installed. Use the registered name (the one on PyPI). You
  136. can specify a ``VERSION``, and you can specify ``REQUIRED`` or ``QUIET``. Only available if
  137. ``NOPYTHON`` mode is not active. Sets ``module_VERSION`` and ``module_FOUND``. Caches the
  138. result once a valid install is found.
  139. Suggested usage
  140. ===============
  141. Using ``find_package`` with version info is not recommended except for release versions.
  142. .. code-block:: cmake
  143. find_package(pybind11 CONFIG)
  144. find_package(pybind11 2.0 EXACT CONFIG REQUIRED)
  145. #]=============================================================================]
  146. @PACKAGE_INIT@
  147. # Location of pybind11/pybind11.h
  148. # This will be relative unless explicitly set as absolute
  149. set(pybind11_INCLUDE_DIR "@pybind11_INCLUDEDIR@")
  150. set(pybind11_LIBRARY "")
  151. set(pybind11_DEFINITIONS USING_pybind11)
  152. set(pybind11_VERSION_TYPE "@pybind11_VERSION_TYPE@")
  153. check_required_components(pybind11)
  154. if(TARGET pybind11::python_link_helper)
  155. # This has already been setup elsewhere, such as with a previous call or
  156. # add_subdirectory
  157. return()
  158. endif()
  159. include("${CMAKE_CURRENT_LIST_DIR}/pybind11Targets.cmake")
  160. # Easier to use / remember
  161. add_library(pybind11::headers IMPORTED INTERFACE)
  162. set_target_properties(pybind11::headers PROPERTIES INTERFACE_LINK_LIBRARIES
  163. pybind11::pybind11_headers)
  164. include("${CMAKE_CURRENT_LIST_DIR}/pybind11Common.cmake")
  165. if(NOT pybind11_FIND_QUIETLY)
  166. message(
  167. STATUS
  168. "Found pybind11: ${pybind11_INCLUDE_DIR} (found version \"${pybind11_VERSION}\" ${pybind11_VERSION_TYPE})"
  169. )
  170. endif()