Skip to content

stack: complete pre-compile stack - #6146

Draft
henryiii wants to merge 14 commits into
masterfrom
feat/precompile-docs
Draft

stack: complete pre-compile stack#6146
henryiii wants to merge 14 commits into
masterfrom
feat/precompile-docs

Conversation

@henryiii

@henryiii henryiii commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

This is the full stack, to show all changes at once. GH stacked PRs don't seem to have that option.

We'll actually merge #6138 as a stack (#6139 is the stack, but doesn't link).

  • chore(cmake): carry option-driven defines on pybind11_headers INTERFACE
  • feat: add PYBIND11_NOINLINE_ATTR and PYBIND11_INLINE macros
  • feat(cmake): opt-in precompiled pybind11 static library, starting with pytypes.h
  • fix(tidy): keep the tidy preset header-only
  • fix(cmake): compile the precompiled library with the interpreter ABI macros
  • feat: move detail/class.h definitions to class-inl.h
  • fix: suppress -Wredundant-decls for class.h declarations
  • feat: move type_caster_base.h non-template definitions to type_caster_base-inl.h
  • fix: make gil.h self-contained for PyPy/GraalPy and silence -Wredundant-decls
  • feat: move internals.h, exception_translation.h, and pybind11_fail out of line
  • fix: drop duplicate translate_exception declaration
  • feat: move pybind11.h non-template definitions to pybind11-inl.h
  • fix: drop redundant redeclarations in pybind11.h
  • feat: expose the precompiled-mode sources to non-CMake builds
  • docs: document the opt-in precompiled mode

📚 Documentation preview 📚: https://pybind11--6146.org.readthedocs.build/

Decompose PYBIND11_NOINLINE into the attribute part plus inline, and
add PYBIND11_INLINE, which becomes empty under PYBIND11_PRECOMPILED.
Groundwork for optional pre-compilation; all current expansions are
unchanged and PYBIND11_INLINE is not used yet.

Assisted-by: ClaudeCode:claude-fable-5
…h pytypes.h

Split the out-of-line pytypes.h definitions into pytypes-inl.h (fmtlib/
CLI11 style): inline by default, compiled once into a per-project static
library when PYBIND11_PRECOMPILED is defined. Infrastructure:

- pybind11_precompile() creates the lazy pybind11::precompiled STATIC
  library from the installed or in-tree src/ sources; PRECOMPILE /
  NO_PRECOMPILE keywords on pybind11_add_module and a global
  PYBIND11_PRECOMPILE switch select it per target.
- A link-time guard symbol encodes PYBIND11_INTERNALS_VERSION,
  Py_GIL_DISABLED, PYBIND11_SIMPLE_GIL_MANAGEMENT, and
  PYBIND11_DETAILED_ERROR_MESSAGES, so a configuration mismatch is one
  readable undefined symbol.
- src/ is installed to share/pybind11/src (wheel stays pure);
  src/pybind11_combined.cpp is a single-TU build for non-CMake use.
- Tests: PYBIND11_TEST_PRECOMPILE builds the whole suite against the
  library, two new test_cmake_build cases, packaging file lists, tidy
  preset, and a 3-platform CI job.

Assisted-by: ClaudeCode:claude-fable-5
In precompiled mode the -inl.h definitions are intentionally
non-inline, so misc-definitions-in-headers fires on every one. The
header-only tidy build already analyzes all -inl.h bodies via the
bottom-of-header includes.

Assisted-by: ClaudeCode:claude-fable-5
…macros

pybind11::pybind11 only carries headers; Py_GIL_DISABLED lives on
Python::Module via pybind11::module. Without it the library is
ABI-mismatched on free-threaded builds, and on Windows the pyconfig.h
autolink pragma in its objects requests pythonXY.lib instead of
pythonXYt.lib.

Assisted-by: ClaudeCode:claude-fable-5
All 32 functions in detail/class.h are non-template plumbing (type/slot
machinery); move them out of line for the precompiled mode. Forward
declarations of these functions in other headers lose their inline
keyword to stay ODR-consistent in both modes.

Assisted-by: ClaudeCode:claude-fable-5
Several functions are forward-declared in headers that cannot include
class.h; GCC's -Wredundant-decls (used in CI cxx_flags) flags the
second declaration.

Assisted-by: ClaudeCode:claude-fable-5
…_base-inl.h

Moves the free functions (type-info lookup and registration, instance
layout, isinstance_generic, cpp_conduit_method, type_info_description),
the loader_life_support members (the function-local thread_local stack
stays per-module in both modes), and the two heavy type_caster_generic
members (the type_info constructor and the main cast overload).
Templates, including load_impl<>, stay in the header.

Assisted-by: ClaudeCode:claude-fable-5
…nt-decls

gil.h evaluated PYBIND11_SIMPLE_GIL_MANAGEMENT before including common.h,
which defines it on PyPy/GraalPy. Every existing TU included common.h
first through pybind11.h, so this only surfaced when src/type_caster_base.cpp
reached gil.h directly. Also suppress GCC -Wredundant-decls for the
isinstance_generic declaration duplicated in pytypes.h.

Assisted-by: ClaudeCode:claude-fable-5
…t of line

The internals accessor family (get_internals, ensure_internals, the
local-internals key and capsules, exception translators) moves into
internals-inl.h; per-module identity is unchanged because the
function-local statics move with their functions into whatever binary
each module links. Also adds common-inl.h (pybind11_fail) and
exception_translation-inl.h. Tiny hot accessors and all templates stay
in the headers.

Assisted-by: ClaudeCode:claude-fable-5
Assisted-by: ClaudeCode:claude-fable-5
The headline of the split: cpp_function's make_function_record,
initialize_generic, destruct, and the 440-line dispatcher move out of
line, along with generic_type::initialize, enum_base, the function
signature generators, module cache helpers, keep_alive_impl,
get_type_override, error_already_set::what, and detail::print. The
templated initialize(), descr.h machinery, and the module/class_ API
stay in the header.

Assisted-by: ClaudeCode:claude-fable-5
These functions are already declared in function_record_pyobject.h,
attr.h, and type_caster_base.h; GCC 13 -Wredundant-decls rejects the
repeats.

Assisted-by: ClaudeCode:claude-fable-5
Adds pybind11.get_source_dir() / python -m pybind11 --srcdir and a
srcdir variable in pybind11.pc, so build systems such as Meson can
compile src/pybind11_combined.cpp with PYBIND11_PRECOMPILED defined.

Assisted-by: ClaudeCode:claude-fable-5
Assisted-by: ClaudeCode:claude-fable-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant