@@ -348,7 +348,8 @@ function with the following signature:
348348.. code-block :: cmake
349349
350350 pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
351- [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] source1 [source2 ...])
351+ [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] [PRECOMPILE | NO_PRECOMPILE]
352+ source1 [source2 ...])
352353
353354 This function behaves very much like CMake's builtin ``add_library `` (in fact,
354355it's a wrapper function around that command). It will add a library target
@@ -404,6 +405,64 @@ optimizations remain disabled.
404405
405406.. _ThinLTO : http://clang.llvm.org/docs/ThinLTO.html
406407
408+ .. _precompile-mode :
409+
410+ Pre-compiling part of pybind11
411+ ------------------------------
412+
413+ pybind11 is header-only by default: every translation unit compiles its own
414+ copy of the non-template implementation. The opt-in *precompiled * mode
415+ compiles that implementation once, into a static library that is built inside
416+ your own project with your own flags, which reduces build time (especially
417+ for projects with many translation units or many modules in one build).
418+
419+ .. code-block :: cmake
420+
421+ pybind11_add_module(example PRECOMPILE example.cpp)
422+
423+ The first ``PRECOMPILE `` target creates the library target
424+ ``pybind11::precompiled ``; further targets reuse it. Set the CMake variable
425+ ``PYBIND11_PRECOMPILE `` to make it the default for all
426+ ``pybind11_add_module `` calls; use ``NO_PRECOMPILE `` on a target to opt back
427+ out. For targets you create yourself, call the ``pybind11_precompile() ``
428+ function and link ``pybind11::precompiled `` PRIVATE; the target carries the
429+ required ``PYBIND11_PRECOMPILED `` compile definition PUBLIC, so your sources
430+ are compiled correctly automatically.
431+
432+ Requirements and caveats:
433+
434+ * The library and every module linking it must agree on the configuration
435+ macros ``PYBIND11_INTERNALS_VERSION ``, ``Py_GIL_DISABLED ``,
436+ ``PYBIND11_SIMPLE_GIL_MANAGEMENT ``, and
437+ ``PYBIND11_DETAILED_ERROR_MESSAGES `` (the last one defaults on in debug
438+ builds). A mismatch produces one readable undefined symbol at link time
439+ referencing ``pybind11_precompiled_config ``.
440+ * The library picks up your directory-level flags and C++ standard when it is
441+ first created, so set those before the first ``PRECOMPILE `` target.
442+ * The library is static and per-build-tree by design; it is never installed
443+ or shared between projects. Each extension module links its own copy,
444+ which preserves pybind11's per-module state exactly as in header-only
445+ mode.
446+ * Not available with ``PYBIND11_NOPYTHON `` (the library needs Python
447+ headers).
448+
449+ For build systems other than CMake, the same sources ship with the pybind11
450+ package: compile ``pybind11_combined.cpp `` from the directory reported by
451+ ``python -m pybind11 --srcdir `` (also available as
452+ ``pybind11.get_source_dir() `` and the ``srcdir `` pkg-config variable) into
453+ your extension and define ``PYBIND11_PRECOMPILED `` for every translation
454+ unit. With setuptools, ``Pybind11Extension(..., precompile=True) `` does this
455+ for you. With Meson:
456+
457+ .. code-block :: meson
458+
459+ pybind11_dep = dependency('pybind11')
460+ pybind11_src = pybind11_dep.get_variable('srcdir')
461+ py.extension_module('example',
462+ ['example.cpp', pybind11_src / 'pybind11_combined.cpp'],
463+ cpp_args : ['-DPYBIND11_PRECOMPILED'],
464+ dependencies : [pybind11_dep])
465+
407466 Configuration variables
408467-----------------------
409468
0 commit comments