Henry Schreiner | df11597 | 2020-07-31 22:45:19 -0400 | [diff] [blame] | 1 | #[=============================================================================[.rst |
| 2 | |
| 3 | pybind11Config.cmake |
| 4 | -------------------- |
| 5 | |
| 6 | PYBIND11 cmake module. |
| 7 | This module sets the following variables in your project:: |
| 8 | |
| 9 | pybind11_FOUND - true if pybind11 and all required components found on the system |
| 10 | pybind11_VERSION - pybind11 version in format Major.Minor.Release |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 11 | pybind11_VERSION_TYPE - pybind11 version type (dev, release) |
Henry Schreiner | df11597 | 2020-07-31 22:45:19 -0400 | [diff] [blame] | 12 | pybind11_INCLUDE_DIRS - Directories where pybind11 and python headers are located. |
| 13 | pybind11_INCLUDE_DIR - Directory where pybind11 headers are located. |
| 14 | pybind11_DEFINITIONS - Definitions necessary to use pybind11, namely USING_pybind11. |
| 15 | pybind11_LIBRARIES - compile flags and python libraries (as needed) to link against. |
| 16 | pybind11_LIBRARY - empty. |
| 17 | |
| 18 | |
| 19 | Available components: None |
| 20 | |
| 21 | |
| 22 | Exported targets:: |
| 23 | |
| 24 | If pybind11 is found, this module defines the following :prop_tgt:`IMPORTED` |
| 25 | interface library targets:: |
| 26 | |
| 27 | pybind11::module - for extension modules |
| 28 | pybind11::embed - for embedding the Python interpreter |
| 29 | |
| 30 | Python headers, libraries (as needed by platform), and the C++ standard |
| 31 | are attached to the target. |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 32 | |
| 33 | Advanced targets are also supplied - these are primary for users building |
| 34 | complex applications, and they are available in all modes:: |
| 35 | |
| 36 | pybind11::headers - Just the pybind11 headers and minimum compile requirements |
| 37 | pybind11::pybind11 - Python headers too |
| 38 | pybind11::python_link_helper - Just the "linking" part of pybind11:module, for CMake < 3.15 |
| 39 | pybind11::python2_no_register - Quiets the warning/error when mixing C++14+ and Python 2, also included in pybind11::module |
| 40 | pybind11::thin_lto - An alternative to INTERPROCEDURAL_OPTIMIZATION |
| 41 | pybind11::lto - An alternative to INTERPROCEDURAL_OPTIMIZATION (also avoids thin LTO on clang) |
| 42 | pybind11::windows_extras - Adds bigobj and mp for MSVC |
| 43 | |
| 44 | Modes:: |
| 45 | |
| 46 | There are two modes provided; classic, which is built on the old Python |
| 47 | discovery packages in CMake, or the new FindPython mode, which uses FindPython |
| 48 | from 3.12+ forward (3.15+ _highly_ recommended). |
| 49 | |
| 50 | New FindPython mode:: |
| 51 | |
| 52 | To activate this mode, either call ``find_package(Python COMPONENTS Interpreter Development)`` |
| 53 | before finding this package, or set the ``PYBIND11_FINDPYTHON`` variable to ON. In this mode, |
| 54 | you can either use the basic targets, or use the FindPython tools:: |
| 55 | |
| 56 | find_package(Python COMPONENTS Interpreter Development) |
| 57 | find_package(pybind11 CONFIG) |
| 58 | |
| 59 | # pybind11 method: |
| 60 | pybind11_add_module(MyModule1 src1.cpp) |
| 61 | |
| 62 | # Python method: |
| 63 | Python_add_library(MyModule2 src2.cpp) |
| 64 | target_link_libraries(MyModule2 pybind11::headers) |
| 65 | set_target_properties(MyModule2 PROPERTIES |
| 66 | INTERPROCEDURAL_OPTIMIZATION ON |
| 67 | CXX__VISIBILITY_PRESET ON |
| 68 | VISIBLITY_INLINES_HIDDEN ON) |
| 69 | |
| 70 | If you build targets yourself, you may be interested in stripping the output |
| 71 | for reduced size; this is the one other feature that the helper function gives you. |
| 72 | |
Henry Schreiner | df11597 | 2020-07-31 22:45:19 -0400 | [diff] [blame] | 73 | Classic mode:: |
| 74 | |
| 75 | Set PythonLibsNew variables to influence python detection and |
| 76 | CMAKE_CXX_STANDARD to influence standard setting. :: |
| 77 | |
| 78 | find_package(pybind11 CONFIG REQUIRED) |
Henry Schreiner | df11597 | 2020-07-31 22:45:19 -0400 | [diff] [blame] | 79 | |
| 80 | # Create an extension module |
| 81 | add_library(mylib MODULE main.cpp) |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 82 | target_link_libraries(mylib PUBLIC pybind11::module) |
Henry Schreiner | df11597 | 2020-07-31 22:45:19 -0400 | [diff] [blame] | 83 | |
| 84 | # Or embed the Python interpreter into an executable |
| 85 | add_executable(myexe main.cpp) |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 86 | target_link_libraries(myexe PUBLIC pybind11::embed) |
Henry Schreiner | df11597 | 2020-07-31 22:45:19 -0400 | [diff] [blame] | 87 | |
| 88 | Suggested usage:: |
| 89 | |
| 90 | find_package with version info is not recommended except for release versions. :: |
| 91 | |
| 92 | find_package(pybind11 CONFIG) |
| 93 | find_package(pybind11 2.0 EXACT CONFIG REQUIRED) |
| 94 | |
| 95 | |
| 96 | The following variables can be set to guide the search for this package:: |
| 97 | |
| 98 | pybind11_DIR - CMake variable, set to directory containing this Config file |
| 99 | CMAKE_PREFIX_PATH - CMake variable, set to root directory of this package |
| 100 | PATH - environment variable, set to bin directory of this package |
| 101 | CMAKE_DISABLE_FIND_PACKAGE_pybind11 - CMake variable, disables |
| 102 | find_package(pybind11) when not REQUIRED, perhaps to force internal build |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 103 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 104 | Helper functions:: |
| 105 | |
| 106 | pybind11_add_module(...) - Add a library and setup all helpers |
| 107 | pybind11_strip(target) - Strip a target after building it (linux/macOS) |
| 108 | pybind11_extension(target) - Injects the Python extension name |
| 109 | |
| 110 | See ``pybind11Tools.cmake`` or ``pybind11NewTools.cmake`` for details on |
| 111 | ``pybind11_add_module``. |
| 112 | |
| 113 | #]=============================================================================] |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 114 | @PACKAGE_INIT@ |
| 115 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 116 | # Location of pybind11/pybind11.h |
| 117 | set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@") |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 118 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 119 | set(pybind11_LIBRARY "") |
| 120 | set(pybind11_DEFINITIONS USING_pybind11) |
Henry Schreiner | df11597 | 2020-07-31 22:45:19 -0400 | [diff] [blame] | 121 | set(pybind11_VERSION_TYPE "@pybind11_VERSION_TYPE@") |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 122 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 123 | check_required_components(pybind11) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 124 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 125 | if(TARGET pybind11::python_link_helper) |
| 126 | # This has already been setup elsewhere, such as with a previous call or |
| 127 | # add_subdirectory |
| 128 | return() |
| 129 | endif() |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 130 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 131 | include("${CMAKE_CURRENT_LIST_DIR}/pybind11Targets.cmake") |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 132 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 133 | include("${CMAKE_CURRENT_LIST_DIR}/pybind11Common.cmake") |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 134 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame^] | 135 | if(NOT pybind11_FIND_QUIETLY) |
| 136 | message( |
| 137 | STATUS |
| 138 | "Found pybind11: ${pybind11_INCLUDE_DIR} (found version \"${pybind11_VERSION}\" ${pybind11_VERSION_TYPE})" |
Henry Schreiner | ed6de12 | 2020-08-01 23:47:47 -0400 | [diff] [blame] | 139 | ) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 140 | endif() |