Ivan Smirnov | 5cbfda5 | 2017-08-30 20:58:43 +0100 | [diff] [blame] | 1 | .. _compiling: |
| 2 | |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 3 | Build systems |
| 4 | ############# |
| 5 | |
Henry Schreiner | fd61f50 | 2020-09-16 17:13:41 -0400 | [diff] [blame] | 6 | .. _build-setuptools: |
| 7 | |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 8 | Building with setuptools |
| 9 | ======================== |
| 10 | |
| 11 | For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay |
| 12 | has kindly provided an example project which shows how to set up everything, |
| 13 | including automatic generation of documentation using Sphinx. Please refer to |
Wenzel Jakob | ca8dc08 | 2016-06-03 14:24:17 +0200 | [diff] [blame] | 14 | the [python_example]_ repository. |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 15 | |
Wenzel Jakob | ca8dc08 | 2016-06-03 14:24:17 +0200 | [diff] [blame] | 16 | .. [python_example] https://github.com/pybind/python_example |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 17 | |
Henry Schreiner | fd61f50 | 2020-09-16 17:13:41 -0400 | [diff] [blame] | 18 | A helper file is provided with pybind11 that can simplify usage with setuptools. |
| 19 | |
| 20 | To use pybind11 inside your ``setup.py``, you have to have some system to |
| 21 | ensure that ``pybind11`` is installed when you build your package. There are |
| 22 | four possible ways to do this, and pybind11 supports all four: You can ask all |
| 23 | users to install pybind11 beforehand (bad), you can use |
| 24 | :ref:`setup_helpers-pep518` (good, but very new and requires Pip 10), |
| 25 | :ref:`setup_helpers-setup_requires` (discouraged by Python packagers now that |
| 26 | PEP 518 is available, but it still works everywhere), or you can |
| 27 | :ref:`setup_helpers-copy-manually` (always works but you have to manually sync |
| 28 | your copy to get updates). |
| 29 | |
| 30 | An example of a ``setup.py`` using pybind11's helpers: |
| 31 | |
| 32 | .. code-block:: python |
| 33 | |
| 34 | from setuptools import setup |
| 35 | from pybind11.setup_helpers import Pybind11Extension |
| 36 | |
| 37 | ext_modules = [ |
| 38 | Pybind11Extension( |
| 39 | "python_example", |
| 40 | ["src/main.cpp"], |
| 41 | ), |
| 42 | ] |
| 43 | |
| 44 | setup( |
| 45 | ..., |
| 46 | ext_modules=ext_modules |
| 47 | ) |
| 48 | |
| 49 | If you want to do an automatic search for the highest supported C++ standard, |
| 50 | that is supported via a ``build_ext`` command override; it will only affect |
| 51 | ``Pybind11Extensions``: |
| 52 | |
| 53 | .. code-block:: python |
| 54 | |
| 55 | from setuptools import setup |
| 56 | from pybind11.setup_helpers import Pybind11Extension, build_ext |
| 57 | |
| 58 | ext_modules = [ |
| 59 | Pybind11Extension( |
| 60 | "python_example", |
| 61 | ["src/main.cpp"], |
| 62 | ), |
| 63 | ] |
| 64 | |
| 65 | setup( |
| 66 | ..., |
| 67 | cmdclass={"build_ext": build_ext}, |
| 68 | ext_modules=ext_modules |
| 69 | ) |
| 70 | |
| 71 | .. _setup_helpers-pep518: |
| 72 | |
| 73 | PEP 518 requirements (Pip 10+ required) |
| 74 | --------------------------------------- |
| 75 | |
| 76 | If you use `PEP 518's <https://www.python.org/dev/peps/pep-0518/>`_ |
| 77 | ``pyproject.toml`` file, you can ensure that ``pybind11`` is available during |
| 78 | the compilation of your project. When this file exists, Pip will make a new |
| 79 | virtual environment, download just the packages listed here in ``requires=``, |
| 80 | and build a wheel (binary Python package). It will then throw away the |
| 81 | environment, and install your wheel. |
| 82 | |
| 83 | Your ``pyproject.toml`` file will likely look something like this: |
| 84 | |
| 85 | .. code-block:: toml |
| 86 | |
| 87 | [build-system] |
| 88 | requires = ["setuptools", "wheel", "pybind11==2.6.0"] |
| 89 | build-backend = "setuptools.build_meta" |
| 90 | |
| 91 | .. note:: |
| 92 | |
| 93 | The main drawback to this method is that a `PEP 517`_ compliant build tool, |
| 94 | such as Pip 10+, is required for this approach to work; older versions of |
| 95 | Pip completely ignore this file. If you distribute binaries (called wheels |
| 96 | in Python) using something like `cibuildwheel`_, remember that ``setup.py`` |
| 97 | and ``pyproject.toml`` are not even contained in the wheel, so this high |
| 98 | Pip requirement is only for source builds, and will not affect users of |
| 99 | your binary wheels. |
| 100 | |
| 101 | .. _PEP 517: https://www.python.org/dev/peps/pep-0517/ |
| 102 | .. _cibuildwheel: https://cibuildwheel.readthedocs.io |
| 103 | |
| 104 | .. _setup_helpers-setup_requires: |
| 105 | |
| 106 | Classic ``setup_requires`` |
| 107 | -------------------------- |
| 108 | |
| 109 | If you want to support old versions of Pip with the classic |
| 110 | ``setup_requires=["pybind11"]`` keyword argument to setup, which triggers a |
| 111 | two-phase ``setup.py`` run, then you will need to use something like this to |
| 112 | ensure the first pass works (which has not yet installed the ``setup_requires`` |
| 113 | packages, since it can't install something it does not know about): |
| 114 | |
| 115 | .. code-block:: python |
| 116 | |
| 117 | try: |
| 118 | from pybind11.setup_helpers import Pybind11Extension |
| 119 | except ImportError: |
| 120 | from setuptools import Extension as Pybind11Extension |
| 121 | |
| 122 | |
| 123 | It doesn't matter that the Extension class is not the enhanced subclass for the |
| 124 | first pass run; and the second pass will have the ``setup_requires`` |
| 125 | requirements. |
| 126 | |
| 127 | This is obviously more of a hack than the PEP 518 method, but it supports |
| 128 | ancient versions of Pip. |
| 129 | |
| 130 | .. _setup_helpers-copy-manually: |
| 131 | |
| 132 | Copy manually |
| 133 | ------------- |
| 134 | |
| 135 | You can also copy ``setup_helpers.py`` directly to your project; it was |
| 136 | designed to be usable standalone, like the old example ``setup.py``. You can |
| 137 | set ``include_pybind11=False`` to skip including the pybind11 package headers, |
| 138 | so you can use it with git submodules and a specific git version. If you use |
| 139 | this, you will need to import from a local file in ``setup.py`` and ensure the |
| 140 | helper file is part of your MANIFEST. |
| 141 | |
| 142 | |
| 143 | .. versionchanged:: 2.6 |
| 144 | |
| 145 | Added ``setup_helpers`` file. |
| 146 | |
Wenzel Jakob | a439cca | 2016-05-17 10:47:52 +0200 | [diff] [blame] | 147 | Building with cppimport |
| 148 | ======================== |
| 149 | |
Dean Moldovan | 8665ee8 | 2017-08-17 15:01:43 +0200 | [diff] [blame] | 150 | [cppimport]_ is a small Python import hook that determines whether there is a C++ |
| 151 | source file whose name matches the requested module. If there is, the file is |
| 152 | compiled as a Python extension using pybind11 and placed in the same folder as |
| 153 | the C++ source file. Python is then able to find the module and load it. |
Wenzel Jakob | a439cca | 2016-05-17 10:47:52 +0200 | [diff] [blame] | 154 | |
| 155 | .. [cppimport] https://github.com/tbenthompson/cppimport |
| 156 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 157 | .. _cmake: |
| 158 | |
| 159 | Building with CMake |
| 160 | =================== |
| 161 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 162 | For C++ codebases that have an existing CMake-based build system, a Python |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 163 | extension module can be created with just a few lines of code: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 164 | |
| 165 | .. code-block:: cmake |
| 166 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 167 | cmake_minimum_required(VERSION 3.4...3.18) |
| 168 | project(example LANGUAGES CXX) |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 169 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 170 | add_subdirectory(pybind11) |
| 171 | pybind11_add_module(example example.cpp) |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 172 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 173 | This assumes that the pybind11 repository is located in a subdirectory named |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 174 | :file:`pybind11` and that the code is located in a file named :file:`example.cpp`. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 175 | The CMake command ``add_subdirectory`` will import the pybind11 project which |
| 176 | provides the ``pybind11_add_module`` function. It will take care of all the |
| 177 | details needed to build a Python extension module on any platform. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 178 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 179 | A working sample project, including a way to invoke CMake from :file:`setup.py` for |
| 180 | PyPI integration, can be found in the [cmake_example]_ repository. |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 181 | |
Wenzel Jakob | aa79af0 | 2016-06-03 12:23:24 +0200 | [diff] [blame] | 182 | .. [cmake_example] https://github.com/pybind/cmake_example |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 183 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 184 | .. versionchanged:: 2.6 |
| 185 | CMake 3.4+ is required. |
| 186 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 187 | pybind11_add_module |
| 188 | ------------------- |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 189 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 190 | To ease the creation of Python extension modules, pybind11 provides a CMake |
| 191 | function with the following signature: |
| 192 | |
| 193 | .. code-block:: cmake |
| 194 | |
| 195 | pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL] |
Wenzel Jakob | 36c666f | 2020-09-04 23:31:05 +0200 | [diff] [blame] | 196 | [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] source1 [source2 ...]) |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 197 | |
| 198 | This function behaves very much like CMake's builtin ``add_library`` (in fact, |
| 199 | it's a wrapper function around that command). It will add a library target |
| 200 | called ``<name>`` to be built from the listed source files. In addition, it |
| 201 | will take care of all the Python-specific compiler and linker flags as well |
| 202 | as the OS- and Python-version-specific file extension. The produced target |
| 203 | ``<name>`` can be further manipulated with regular CMake commands. |
| 204 | |
| 205 | ``MODULE`` or ``SHARED`` may be given to specify the type of library. If no |
| 206 | type is given, ``MODULE`` is used by default which ensures the creation of a |
| 207 | Python-exclusive module. Specifying ``SHARED`` will create a more traditional |
| 208 | dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL`` |
| 209 | removes this target from the default build (see CMake docs for details). |
| 210 | |
| 211 | Since pybind11 is a template library, ``pybind11_add_module`` adds compiler |
| 212 | flags to ensure high quality code generation without bloat arising from long |
Jason Rhinelander | 97aa54f | 2017-08-10 12:08:42 -0400 | [diff] [blame] | 213 | symbol names and duplication of code in different translation units. It |
| 214 | sets default visibility to *hidden*, which is required for some pybind11 |
| 215 | features and functionality when attempting to load multiple pybind11 modules |
| 216 | compiled under different pybind11 versions. It also adds additional flags |
| 217 | enabling LTO (Link Time Optimization) and strip unneeded symbols. See the |
| 218 | :ref:`FAQ entry <faq:symhidden>` for a more detailed explanation. These |
| 219 | latter optimizations are never applied in ``Debug`` mode. If ``NO_EXTRAS`` is |
| 220 | given, they will always be disabled, even in ``Release`` mode. However, this |
| 221 | will result in code bloat and is generally not recommended. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 222 | |
| 223 | As stated above, LTO is enabled by default. Some newer compilers also support |
| 224 | different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause |
| 225 | the function to prefer this flavor if available. The function falls back to |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 226 | regular LTO if ``-flto=thin`` is not available. If |
| 227 | ``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` is set (either ON or OFF), then that |
| 228 | will be respected instead of the built-in flag search. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 229 | |
Wenzel Jakob | 36c666f | 2020-09-04 23:31:05 +0200 | [diff] [blame] | 230 | The ``OPT_SIZE`` flag enables size-based optimization equivalent to the |
| 231 | standard ``/Os`` or ``-Os`` compiler flags and the ``MinSizeRel`` build type, |
| 232 | which avoid optimizations that that can substantially increase the size of the |
| 233 | resulting binary. This flag is particularly useful in projects that are split |
| 234 | into performance-critical parts and associated bindings. In this case, we can |
| 235 | compile the project in release mode (and hence, optimize performance globally), |
| 236 | and specify ``OPT_SIZE`` for the binding target, where size might be the main |
| 237 | concern as performance is often less critical here. A ~25% size reduction has |
| 238 | been observed in practice. This flag only changes the optimization behavior at |
| 239 | a per-target level and takes precedence over the global CMake build type |
| 240 | (``Release``, ``RelWithDebInfo``) except for ``Debug`` builds, where |
| 241 | optimizations remain disabled. |
| 242 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 243 | .. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html |
| 244 | |
| 245 | Configuration variables |
| 246 | ----------------------- |
| 247 | |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 248 | By default, pybind11 will compile modules with the compiler default or the |
Henry Schreiner | 1651c32 | 2020-07-30 16:04:26 -0400 | [diff] [blame] | 249 | minimum standard required by pybind11, whichever is higher. You can set the |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 250 | standard explicitly with |
| 251 | `CMAKE_CXX_STANDARD <https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html>`_: |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 252 | |
| 253 | .. code-block:: cmake |
| 254 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 255 | set(CMAKE_CXX_STANDARD 14) # or 11, 14, 17, 20 |
| 256 | set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported |
| 257 | set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensionsn off |
Jason Rhinelander | 77710ff | 2017-05-09 14:37:48 -0400 | [diff] [blame] | 258 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 259 | |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 260 | The variables can also be set when calling CMake from the command line using |
| 261 | the ``-D<variable>=<value>`` flag. You can also manually set ``CXX_STANDARD`` |
| 262 | on a target or use ``target_compile_features`` on your targets - anything that |
| 263 | CMake supports. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 264 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 265 | Classic Python support: The target Python version can be selected by setting |
| 266 | ``PYBIND11_PYTHON_VERSION`` or an exact Python installation can be specified |
| 267 | with ``PYTHON_EXECUTABLE``. For example: |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 268 | |
| 269 | .. code-block:: bash |
| 270 | |
| 271 | cmake -DPYBIND11_PYTHON_VERSION=3.6 .. |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 272 | |
| 273 | # Another method: |
| 274 | cmake -DPYTHON_EXECUTABLE=/path/to/python .. |
| 275 | |
Henry Schreiner | 1651c32 | 2020-07-30 16:04:26 -0400 | [diff] [blame] | 276 | # This often is a good way to get the current Python, works in environments: |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 277 | cmake -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") .. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 278 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 279 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 280 | find_package vs. add_subdirectory |
| 281 | --------------------------------- |
| 282 | |
| 283 | For CMake-based projects that don't include the pybind11 repository internally, |
| 284 | an external installation can be detected through ``find_package(pybind11)``. |
| 285 | See the `Config file`_ docstring for details of relevant CMake variables. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 286 | |
| 287 | .. code-block:: cmake |
| 288 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 289 | cmake_minimum_required(VERSION 3.4...3.18) |
| 290 | project(example LANGUAGES CXX) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 291 | |
| 292 | find_package(pybind11 REQUIRED) |
| 293 | pybind11_add_module(example example.cpp) |
| 294 | |
nstelzen | c251434 | 2019-06-10 16:35:36 +0200 | [diff] [blame] | 295 | Note that ``find_package(pybind11)`` will only work correctly if pybind11 |
| 296 | has been correctly installed on the system, e. g. after downloading or cloning |
| 297 | the pybind11 repository : |
| 298 | |
| 299 | .. code-block:: bash |
| 300 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 301 | # Classic CMake |
nstelzen | c251434 | 2019-06-10 16:35:36 +0200 | [diff] [blame] | 302 | cd pybind11 |
| 303 | mkdir build |
| 304 | cd build |
| 305 | cmake .. |
| 306 | make install |
| 307 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 308 | # CMake 3.15+ |
| 309 | cd pybind11 |
| 310 | cmake -S . -B build |
| 311 | cmake --build build -j 2 # Build on 2 cores |
| 312 | cmake --install build |
| 313 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 314 | Once detected, the aforementioned ``pybind11_add_module`` can be employed as |
| 315 | before. The function usage and configuration variables are identical no matter |
| 316 | if pybind11 is added as a subdirectory or found as an installed package. You |
| 317 | can refer to the same [cmake_example]_ repository for a full sample project |
| 318 | -- just swap out ``add_subdirectory`` for ``find_package``. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 319 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 320 | .. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in |
| 321 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 322 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 323 | .. _find-python-mode: |
| 324 | |
| 325 | FindPython mode |
| 326 | --------------- |
| 327 | |
| 328 | CMake 3.12+ (3.15+ recommended) added a new module called FindPython that had a |
| 329 | highly improved search algorithm and modern targets and tools. If you use |
| 330 | FindPython, pybind11 will detect this and use the existing targets instead: |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 331 | |
| 332 | .. code-block:: cmake |
| 333 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 334 | cmake_minumum_required(VERSION 3.15...3.18) |
| 335 | project(example LANGUAGES CXX) |
| 336 | |
| 337 | find_package(Python COMPONENTS Interpreter Development REQUIRED) |
| 338 | find_package(pybind11 CONFIG REQUIRED) |
| 339 | # or add_subdirectory(pybind11) |
| 340 | |
| 341 | pybind11_add_module(example example.cpp) |
| 342 | |
| 343 | You can also use the targets (as listed below) with FindPython. If you define |
| 344 | ``PYBIND11_FINDPYTHON``, pybind11 will perform the FindPython step for you |
| 345 | (mostly useful when building pybind11's own tests, or as a way to change search |
| 346 | algorithms from the CMake invocation, with ``-DPYBIND11_FINDPYTHON=ON``. |
| 347 | |
| 348 | .. warning:: |
| 349 | |
| 350 | If you use FindPython2 and FindPython3 to dual-target Python, use the |
| 351 | individual targets listed below, and avoid targets that directly include |
| 352 | Python parts. |
| 353 | |
| 354 | There are `many ways to hint or force a discovery of a specific Python |
| 355 | installation <https://cmake.org/cmake/help/latest/module/FindPython.html>`_), |
| 356 | setting ``Python_ROOT_DIR`` may be the most common one (though with |
| 357 | virtualenv/venv support, and Conda support, this tends to find the correct |
| 358 | Python version more often than the old system did). |
| 359 | |
| 360 | .. versionadded:: 2.6 |
| 361 | |
| 362 | Advanced: interface library targets |
| 363 | ----------------------------------- |
| 364 | |
| 365 | Pybind11 supports modern CMake usage patterns with a set of interface targets, |
| 366 | available in all modes. The targets provided are: |
| 367 | |
| 368 | ``pybind11::headers`` |
| 369 | Just the pybind11 headers and minimum compile requirements |
| 370 | |
| 371 | ``pybind11::python2_no_register`` |
| 372 | Quiets the warning/error when mixing C++14 or higher and Python 2 |
| 373 | |
| 374 | ``pybind11::pybind11`` |
| 375 | Python headers + ``pybind11::headers`` + ``pybind11::python2_no_register`` (Python 2 only) |
| 376 | |
| 377 | ``pybind11::python_link_helper`` |
| 378 | Just the "linking" part of pybind11:module |
| 379 | |
| 380 | ``pybind11::module`` |
| 381 | Everything for extension modules - ``pybind11::pybind11`` + ``Python::Module`` (FindPython CMake 3.15+) or ``pybind11::python_link_helper`` |
| 382 | |
| 383 | ``pybind11::embed`` |
| 384 | Everything for embedding the Python interpreter - ``pybind11::pybind11`` + ``Python::Embed`` (FindPython) or Python libs |
| 385 | |
| 386 | ``pybind11::lto`` / ``pybind11::thin_lto`` |
| 387 | An alternative to `INTERPROCEDURAL_OPTIMIZATION` for adding link-time optimization. |
| 388 | |
| 389 | ``pybind11::windows_extras`` |
| 390 | ``/bigobj`` and ``/mp`` for MSVC. |
| 391 | |
Wenzel Jakob | 36c666f | 2020-09-04 23:31:05 +0200 | [diff] [blame] | 392 | ``pybind11::opt_size`` |
| 393 | ``/Os`` for MSVC, ``-Os`` for other compilers. Does nothing for debug builds. |
| 394 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 395 | Two helper functions are also provided: |
| 396 | |
| 397 | ``pybind11_strip(target)`` |
| 398 | Strips a target (uses ``CMAKE_STRIP`` after the target is built) |
| 399 | |
| 400 | ``pybind11_extension(target)`` |
| 401 | Sets the correct extension (with SOABI) for a target. |
| 402 | |
| 403 | You can use these targets to build complex applications. For example, the |
| 404 | ``add_python_module`` function is identical to: |
| 405 | |
| 406 | .. code-block:: cmake |
| 407 | |
| 408 | cmake_minimum_required(VERSION 3.4) |
| 409 | project(example LANGUAGES CXX) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 410 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 411 | find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 412 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 413 | add_library(example MODULE main.cpp) |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 414 | |
| 415 | target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras) |
| 416 | |
| 417 | pybind11_extension(example) |
| 418 | pybind11_strip(example) |
| 419 | |
| 420 | set_target_properties(example PROPERTIES CXX_VISIBILITY_PRESET "hidden" |
| 421 | CUDA_VISIBILITY_PRESET "hidden") |
| 422 | |
| 423 | Instead of setting properties, you can set ``CMAKE_*`` variables to initialize these correctly. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 424 | |
| 425 | .. warning:: |
| 426 | |
| 427 | Since pybind11 is a metatemplate library, it is crucial that certain |
| 428 | compiler flags are provided to ensure high quality code generation. In |
| 429 | contrast to the ``pybind11_add_module()`` command, the CMake interface |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 430 | provides a *composable* set of targets to ensure that you retain flexibility. |
| 431 | It can be expecially important to provide or set these properties; the |
| 432 | :ref:`FAQ <faq:symhidden>` contains an explanation on why these are needed. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 433 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 434 | .. versionadded:: 2.6 |
Wenzel Jakob | f3de2d5 | 2016-12-26 13:34:28 +0100 | [diff] [blame] | 435 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 436 | .. _nopython-mode: |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 437 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 438 | Advanced: NOPYTHON mode |
| 439 | ----------------------- |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 440 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 441 | If you want complete control, you can set ``PYBIND11_NOPYTHON`` to completely |
| 442 | disable Python integration (this also happens if you run ``FindPython2`` and |
| 443 | ``FindPython3`` without running ``FindPython``). This gives you complete |
| 444 | freedom to integrate into an existing system (like `Scikit-Build's |
| 445 | <https://scikit-build.readthedocs.io>`_ ``PythonExtensions``). |
| 446 | ``pybind11_add_module`` and ``pybind11_extension`` will be unavailable, and the |
| 447 | targets will be missing any Python specific behavior. |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 448 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 449 | .. versionadded:: 2.6 |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 450 | |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 451 | Embedding the Python interpreter |
| 452 | -------------------------------- |
| 453 | |
| 454 | In addition to extension modules, pybind11 also supports embedding Python into |
| 455 | a C++ executable or library. In CMake, simply link with the ``pybind11::embed`` |
| 456 | target. It provides everything needed to get the interpreter running. The Python |
| 457 | headers and libraries are attached to the target. Unlike ``pybind11::module``, |
| 458 | there is no need to manually set any additional properties here. For more |
| 459 | information about usage in C++, see :doc:`/advanced/embedding`. |
| 460 | |
| 461 | .. code-block:: cmake |
| 462 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 463 | cmake_minimum_required(VERSION 3.4...3.18) |
| 464 | project(example LANGUAGES CXX) |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 465 | |
| 466 | find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) |
| 467 | |
Dean Moldovan | 8f6c129 | 2017-05-31 13:48:39 +0200 | [diff] [blame] | 468 | add_executable(example main.cpp) |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 469 | target_link_libraries(example PRIVATE pybind11::embed) |
| 470 | |
Ivan Smirnov | 5cbfda5 | 2017-08-30 20:58:43 +0100 | [diff] [blame] | 471 | .. _building_manually: |
| 472 | |
| 473 | Building manually |
| 474 | ================= |
| 475 | |
| 476 | pybind11 is a header-only library, hence it is not necessary to link against |
| 477 | any special libraries and there are no intermediate (magic) translation steps. |
| 478 | |
| 479 | On Linux, you can compile an example such as the one given in |
| 480 | :ref:`simple_example` using the following command: |
| 481 | |
| 482 | .. code-block:: bash |
| 483 | |
| 484 | $ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` |
| 485 | |
| 486 | The flags given here assume that you're using Python 3. For Python 2, just |
| 487 | change the executable appropriately (to ``python`` or ``python2``). |
| 488 | |
| 489 | The ``python3 -m pybind11 --includes`` command fetches the include paths for |
| 490 | both pybind11 and Python headers. This assumes that pybind11 has been installed |
| 491 | using ``pip`` or ``conda``. If it hasn't, you can also manually specify |
| 492 | ``-I <path-to-pybind11>/include`` together with the Python includes path |
| 493 | ``python3-config --includes``. |
| 494 | |
| 495 | Note that Python 2.7 modules don't use a special suffix, so you should simply |
| 496 | use ``example.so`` instead of ``example`python3-config --extension-suffix```. |
| 497 | Besides, the ``--extension-suffix`` option may or may not be available, depending |
| 498 | on the distribution; in the latter case, the module extension can be manually |
| 499 | set to ``.so``. |
| 500 | |
Henry Schreiner | fd61f50 | 2020-09-16 17:13:41 -0400 | [diff] [blame] | 501 | On macOS: the build command is almost the same but it also requires passing |
Ivan Smirnov | 5cbfda5 | 2017-08-30 20:58:43 +0100 | [diff] [blame] | 502 | the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when |
| 503 | building the module: |
| 504 | |
| 505 | .. code-block:: bash |
| 506 | |
| 507 | $ c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` |
| 508 | |
| 509 | In general, it is advisable to include several additional build parameters |
| 510 | that can considerably reduce the size of the created binary. Refer to section |
| 511 | :ref:`cmake` for a detailed example of a suitable cross-platform CMake-based |
| 512 | build system that works on all platforms including Windows. |
| 513 | |
| 514 | .. note:: |
| 515 | |
| 516 | On Linux and macOS, it's better to (intentionally) not link against |
| 517 | ``libpython``. The symbols will be resolved when the extension library |
| 518 | is loaded into a Python binary. This is preferable because you might |
| 519 | have several different installations of a given Python version (e.g. the |
| 520 | system-provided Python, and one that ships with a piece of commercial |
| 521 | software). In this way, the plugin will work with both versions, instead |
| 522 | of possibly importing a second Python library into a process that already |
| 523 | contains one (which will lead to a segfault). |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 524 | |
Griffin Downs | a4cee36 | 2020-09-16 05:07:06 -0700 | [diff] [blame] | 525 | |
Henry Schreiner | dec33c2 | 2020-09-16 20:00:19 -0400 | [diff] [blame] | 526 | Building with Bazel |
Griffin Downs | a4cee36 | 2020-09-16 05:07:06 -0700 | [diff] [blame] | 527 | =================== |
Griffin Downs | a4cee36 | 2020-09-16 05:07:06 -0700 | [diff] [blame] | 528 | |
Henry Schreiner | dec33c2 | 2020-09-16 20:00:19 -0400 | [diff] [blame] | 529 | You can build with the Bazel build system using the `pybind11_bazel |
| 530 | <https://github.com/pybind/pybind11_bazel>`_ repository. |
Griffin Downs | a4cee36 | 2020-09-16 05:07:06 -0700 | [diff] [blame] | 531 | |
Wenzel Jakob | f3de2d5 | 2016-12-26 13:34:28 +0100 | [diff] [blame] | 532 | Generating binding code automatically |
| 533 | ===================================== |
| 534 | |
| 535 | The ``Binder`` project is a tool for automatic generation of pybind11 binding |
| 536 | code by introspecting existing C++ codebases using LLVM/Clang. See the |
| 537 | [binder]_ documentation for details. |
| 538 | |
| 539 | .. [binder] http://cppbinder.readthedocs.io/en/latest/about.html |
Dustin Spicuzza | 2c4cd84 | 2019-11-24 02:36:48 -0500 | [diff] [blame] | 540 | |
| 541 | [AutoWIG]_ is a Python library that wraps automatically compiled libraries into |
| 542 | high-level languages. It parses C++ code using LLVM/Clang technologies and |
| 543 | generates the wrappers using the Mako templating engine. The approach is automatic, |
| 544 | extensible, and applies to very complex C++ libraries, composed of thousands of |
| 545 | classes or incorporating modern meta-programming constructs. |
| 546 | |
| 547 | .. [AutoWIG] https://github.com/StatisKit/AutoWIG |
Dustin Spicuzza | 6f3470f | 2020-08-10 16:10:45 -0400 | [diff] [blame] | 548 | |
| 549 | [robotpy-build]_ is a is a pure python, cross platform build tool that aims to |
| 550 | simplify creation of python wheels for pybind11 projects, and provide |
| 551 | cross-project dependency management. Additionally, it is able to autogenerate |
| 552 | customizable pybind11-based wrappers by parsing C++ header files. |
| 553 | |
| 554 | .. [robotpy-build] https://robotpy-build.readthedocs.io |