pybind11Config.cmake.in 6.88 KB
Newer Older
1
#[=============================================================================[.rst:
Henry Schreiner's avatar
Henry Schreiner committed
2
3

pybind11Config.cmake
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
####################

Exported variables
==================

This module sets the following variables in your project:

``pybind11_FOUND``
  true if pybind11 and all required components found on the system
``pybind11_VERSION``
  pybind11 version in format Major.Minor.Release
``pybind11_VERSION_TYPE``
  pybind11 version type (dev, release)
``pybind11_INCLUDE_DIRS``
  Directories where pybind11 and python headers are located.
``pybind11_INCLUDE_DIR``
  Directory where pybind11 headers are located.
``pybind11_DEFINITIONS``
  Definitions necessary to use pybind11, namely USING_pybind11.
``pybind11_LIBRARIES``
  Compile flags and python libraries (as needed) to link against.
``pybind11_LIBRARY``
  Empty.
Henry Schreiner's avatar
Henry Schreiner committed
27
28
29
30

Available components: None


31
32
Exported targets
================
Henry Schreiner's avatar
Henry Schreiner committed
33

34
35
If pybind11 is found, this module defines the following ``IMPORTED``
interface library targets:
Henry Schreiner's avatar
Henry Schreiner committed
36

37
38
39
40
``pybind11::module``
  for extension modules.
``pybind11::embed``
  for embedding the Python interpreter.
Henry Schreiner's avatar
Henry Schreiner committed
41
42
43

Python headers, libraries (as needed by platform), and the C++ standard
are attached to the target.
44
45

Advanced targets are also supplied - these are primary for users building
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
complex applications, and they are available in all modes:

``pybind11::headers``
  Just the pybind11 headers and minimum compile requirements.
``pybind11::pybind11``
  Python headers too.
``pybind11::python_link_helper``
  Just the "linking" part of ``pybind11:module``, for CMake < 3.15.
``pybind11::python2_no_register``
  Quiets the warning/error when mixing C++14+ and Python 2, also included in ``pybind11::module``.
``pybind11::thin_lto``
  An alternative to ``INTERPROCEDURAL_OPTIMIZATION``.
``pybind11::lto``
  An alternative to ``INTERPROCEDURAL_OPTIMIZATION`` (also avoids thin LTO on clang).
``pybind11::windows_extras``
  Adds bigobj and mp for MSVC.

Modes
=====
65
66
67
68
69

There are two modes provided; classic, which is built on the old Python
discovery packages in CMake, or the new FindPython mode, which uses FindPython
from 3.12+ forward (3.15+ _highly_ recommended).

70
71
New FindPython mode
^^^^^^^^^^^^^^^^^^^
72
73
74

To activate this mode, either call ``find_package(Python COMPONENTS Interpreter Development)``
before finding this package, or set the ``PYBIND11_FINDPYTHON`` variable to ON. In this mode,
75
76
77
you can either use the basic targets, or use the FindPython tools:

.. code-block:: cmake
78
79
80
81
82
83
84
85
86
87
88
89

  find_package(Python COMPONENTS Interpreter Development)
  find_package(pybind11 CONFIG)

  # pybind11 method:
  pybind11_add_module(MyModule1 src1.cpp)

  # Python method:
  Python_add_library(MyModule2 src2.cpp)
  target_link_libraries(MyModule2 pybind11::headers)
  set_target_properties(MyModule2 PROPERTIES
                                  INTERPROCEDURAL_OPTIMIZATION ON
90
                                  CXX_VISIBILITY_PRESET ON
91
92
93
94
95
                                  VISIBLITY_INLINES_HIDDEN ON)

If you build targets yourself, you may be interested in stripping the output
for reduced size; this is the one other feature that the helper function gives you.

96
97
Classic mode
^^^^^^^^^^^^
Henry Schreiner's avatar
Henry Schreiner committed
98
99

Set PythonLibsNew variables to influence python detection and
100
101
102
CMAKE_CXX_STANDARD to influence standard setting.

.. code-block:: cmake
Henry Schreiner's avatar
Henry Schreiner committed
103
104
105
106
107

  find_package(pybind11 CONFIG REQUIRED)

  # Create an extension module
  add_library(mylib MODULE main.cpp)
108
  target_link_libraries(mylib PUBLIC pybind11::module)
Henry Schreiner's avatar
Henry Schreiner committed
109
110
111

  # Or embed the Python interpreter into an executable
  add_executable(myexe main.cpp)
112
  target_link_libraries(myexe PUBLIC pybind11::embed)
Henry Schreiner's avatar
Henry Schreiner committed
113
114


115
116
Hints
=====
Henry Schreiner's avatar
Henry Schreiner committed
117

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
The following variables can be set to guide the search for this package:

``pybind11_DIR``
  CMake variable, set to directory containing this Config file.
``CMAKE_PREFIX_PATH``
  CMake variable, set to root directory of this package.
``PATH``
  Environment variable, set to bin directory of this package.
``CMAKE_DISABLE_FIND_PACKAGE_pybind11``
  CMake variable, disables ``find_package(pybind11)`` when not ``REQUIRED``,
  perhaps to force internal build.

Commands
========

pybind11_add_module
^^^^^^^^^^^^^^^^^^^

This module defines the following commands to assist with creating Python modules:

.. code-block:: cmake

  pybind11_add_module(<target>
    [STATIC|SHARED|MODULE]
142
    [THIN_LTO] [OPT_SIZE] [NO_EXTRAS] [WITHOUT_SOABI]
143
144
    <files>...
    )
Henry Schreiner's avatar
Henry Schreiner committed
145

146
147
Add a module and setup all helpers. You can select the type of the library; the
default is ``MODULE``. There are several options:
Henry Schreiner's avatar
Henry Schreiner committed
148

149
150
151
152
153
154
155
156
157
``OPT_SIZE``
  Optimize for size, even if the ``CMAKE_BUILD_TYPE`` is not ``RelSize``.
``THIN_LTO``
  Use thin TLO instead of regular if there's a choice (pybind11's selection
  is disabled if ``CMAKE_INTERPROCEDURAL_OPTIMIZATIONS`` is set).
``WITHOUT_SOABI``
  Disable the SOABI component (``PYBIND11_NEWPYTHON`` mode only).
``NO_EXTRAS``
  Disable all extras, exit immediately after making the module.
Henry Schreiner's avatar
Henry Schreiner committed
158

159
160
pybind11_strip
^^^^^^^^^^^^^^
161

162
.. code-block:: cmake
163

164
  pybind11_strip(<target>)
165

166
167
168
169
170
171
172
173
174
175
Strip a target after building it (linux/macOS), called by ``pybind11_add_module``.

pybind11_extension
^^^^^^^^^^^^^^^^^^

.. code-block:: cmake

    pybind11_extension(<target>)

Sets the Python extension name correctly for Python on your platform, called by
176
177
``pybind11_add_module``.

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
pybind11_find_import(module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: cmake

    pybind11_find_import(<module> [VERSION <number>] [REQUIRED] [QUIET])

See if a module is installed. Use the registered name (the one on PyPI). You
can specify a ``VERSION``, and you can specify ``REQUIRED`` or ``QUIET``. Only available if
``NOPYTHON`` mode is not active.  Sets ``module_VERSION`` and ``module_FOUND``. Caches the
result once a valid install is found.

Suggested usage
===============

Using ``find_package`` with version info is not recommended except for release versions.

.. code-block:: cmake

  find_package(pybind11 CONFIG)
  find_package(pybind11 2.0 EXACT CONFIG REQUIRED)

200
#]=============================================================================]
201
202
@PACKAGE_INIT@

203
# Location of pybind11/pybind11.h
204
205
# This will be relative unless explicitly set as absolute
set(pybind11_INCLUDE_DIR "@pybind11_INCLUDEDIR@")
206

207
208
set(pybind11_LIBRARY "")
set(pybind11_DEFINITIONS USING_pybind11)
Henry Schreiner's avatar
Henry Schreiner committed
209
set(pybind11_VERSION_TYPE "@pybind11_VERSION_TYPE@")
210

211
check_required_components(pybind11)
212

213
214
215
216
217
218
219
if(TARGET pybind11::python_link_helper)
  # This has already been setup elsewhere, such as with a previous call or
  # add_subdirectory
  return()
endif()

include("${CMAKE_CURRENT_LIST_DIR}/pybind11Targets.cmake")
Henry Schreiner's avatar
Henry Schreiner committed
220

221
222
223
224
225
# Easier to use / remember
add_library(pybind11::headers IMPORTED INTERFACE)
set_target_properties(pybind11::headers PROPERTIES INTERFACE_LINK_LIBRARIES
                                                   pybind11::pybind11_headers)

226
227
228
229
230
231
232
include("${CMAKE_CURRENT_LIST_DIR}/pybind11Common.cmake")

if(NOT pybind11_FIND_QUIETLY)
  message(
    STATUS
      "Found pybind11: ${pybind11_INCLUDE_DIR} (found version \"${pybind11_VERSION}\" ${pybind11_VERSION_TYPE})"
  )
233
endif()