@@ -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,81 @@ 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 ``,
437+ ``PYBIND11_DETAILED_ERROR_MESSAGES `` (defaults on in debug builds),
438+ ``PYBIND11_HAS_SUBINTERPRETER_SUPPORT ``, and
439+ ``PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET ``. A mismatch produces one
440+ readable undefined symbol at link time referencing
441+ ``pybind11_precompiled_config ``.
442+ * Configuration macros that only change code inside the library (for example
443+ ``PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING ``) must be defined when the
444+ library is compiled; a definition only on your module has no effect.
445+ * The library picks up your directory-level flags and C++ standard when it is
446+ first created, so set those before the first ``PRECOMPILE `` target. A
447+ status message reports the directory that created the library.
448+ * The library is not compiled with link-time optimization. To enable LTO
449+ across the library boundary, set ``CMAKE_INTERPROCEDURAL_OPTIMIZATION ``
450+ before the first ``PRECOMPILE `` target. The per-target ``OPT_SIZE `` option
451+ also does not apply to the precompiled library; use a global setting (for
452+ example ``CMAKE_BUILD_TYPE=MinSizeRel ``) to also optimize the library for
453+ size.
454+ * The library is static and per-build-tree by design; it is never installed
455+ or shared between projects. Each extension module links its own copy,
456+ which preserves pybind11's per-module state exactly as in header-only
457+ mode.
458+ * Not available with ``PYBIND11_NOPYTHON `` (the library needs Python
459+ headers).
460+
461+ For build systems other than CMake, the same sources ship with the pybind11
462+ package: compile ``pybind11_combined.cpp `` from the directory reported by
463+ ``python -m pybind11 --srcdir `` (also available as
464+ ``pybind11.get_source_dir() `` and the ``srcdir `` pkg-config variable) into
465+ your extension and define ``PYBIND11_PRECOMPILED `` for every translation
466+ unit. With setuptools, ``Pybind11Extension(..., precompile=True) `` does this
467+ for you; also use the ``build_ext `` from ``pybind11.setup_helpers ``, which
468+ gives each extension its own copy of ``pybind11_combined.cpp `` and thus its
469+ own object file. Each extension compiles its own copy, so with many
470+ extensions in one ``setup.py `` the CMake path (which builds the library once
471+ per build tree) saves more time.
472+ With Meson:
473+
474+ .. code-block :: meson
475+
476+ pybind11_dep = dependency('pybind11')
477+ pybind11_src = pybind11_dep.get_variable('srcdir')
478+ py.extension_module('example',
479+ ['example.cpp', pybind11_src / 'pybind11_combined.cpp'],
480+ cpp_args : ['-DPYBIND11_PRECOMPILED'],
481+ dependencies : [pybind11_dep])
482+
407483 Configuration variables
408484-----------------------
409485
0 commit comments