Skip to content

Commit 1d3436a

Browse files
committed
feat: expose the precompiled-mode sources to non-CMake builds
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
1 parent b8d7622 commit 1d3436a

6 files changed

Lines changed: 36 additions & 5 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ if(PYBIND11_INSTALL)
343343
install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION "${SKBUILD_HEADERS_DIR}")
344344
endif()
345345
install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
346-
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/
347-
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pybind11/src")
346+
set(pybind11_install_srcdir "${CMAKE_INSTALL_DATAROOTDIR}/pybind11/src")
347+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/ DESTINATION "${pybind11_install_srcdir}")
348348
set(PYBIND11_CMAKECONFIG_INSTALL_DIR
349349
"${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}"
350350
CACHE STRING "install path for pybind11Config.cmake")
@@ -355,9 +355,9 @@ if(PYBIND11_INSTALL)
355355
set(pybind11_INCLUDEDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_INCLUDEDIR}")
356356
endif()
357357
if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}")
358-
set(pybind11_SRCDIR "${CMAKE_INSTALL_DATAROOTDIR}/pybind11/src")
358+
set(pybind11_SRCDIR "${pybind11_install_srcdir}")
359359
else()
360-
set(pybind11_SRCDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_DATAROOTDIR}/pybind11/src")
360+
set(pybind11_SRCDIR "\$\{PACKAGE_PREFIX_DIR\}/${pybind11_install_srcdir}")
361361
endif()
362362

363363
configure_package_config_file(
@@ -410,6 +410,7 @@ if(PYBIND11_INSTALL)
410410
endif()
411411
endif()
412412
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
413+
join_paths(srcdir_for_pc_file "\${prefix}" "${pybind11_install_srcdir}")
413414
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in"
414415
"${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc" @ONLY)
415416
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc"

pybind11/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99

1010
from ._version import __version__, version_info
11-
from .commands import get_cmake_dir, get_include, get_pkgconfig_dir
11+
from .commands import get_cmake_dir, get_include, get_pkgconfig_dir, get_source_dir
1212

1313
__all__ = (
1414
"__version__",
1515
"get_cmake_dir",
1616
"get_include",
1717
"get_pkgconfig_dir",
18+
"get_source_dir",
1819
"version_info",
1920
)

pybind11/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
get_include_dirs,
1818
get_ldflags,
1919
get_pkgconfig_dir,
20+
get_source_dir,
2021
)
2122

2223

@@ -50,6 +51,12 @@ def main() -> None:
5051
action="store_true",
5152
help="Print the pkgconfig directory, ideal for setting $PKG_CONFIG_PATH.",
5253
)
54+
parser.add_argument(
55+
"--srcdir",
56+
action="store_true",
57+
help="Print the directory containing the library sources for the optional"
58+
" precompiled mode.",
59+
)
5360
parser.add_argument(
5461
"--extension-suffix",
5562
action="store_true",
@@ -101,6 +108,8 @@ def main() -> None:
101108
print(quote(get_cmake_dir()))
102109
if args.pkgconfigdir:
103110
print(quote(get_pkgconfig_dir()))
111+
if args.srcdir:
112+
print(quote(get_source_dir()))
104113
if args.extension_suffix:
105114
print(ext_suffix)
106115

pybind11/commands.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ def get_include(user: bool = False) -> str: # noqa: ARG001
5252
return installed_path if os.path.exists(installed_path) else source_path
5353

5454

55+
def get_source_dir() -> str:
56+
"""
57+
Return the path to the pybind11 library sources, for the optional
58+
precompiled mode. Compile ``pybind11_combined.cpp`` (or the individual
59+
``.cpp`` files) with ``PYBIND11_PRECOMPILED`` defined, and define that
60+
macro for every translation unit that includes pybind11.
61+
"""
62+
installed_path = os.path.join(DIR, "share", "pybind11", "src")
63+
source_path = os.path.join(os.path.dirname(DIR), "src")
64+
if os.path.exists(installed_path):
65+
return installed_path
66+
if os.path.exists(source_path):
67+
return source_path
68+
69+
msg = "pybind11 library sources not found (pybind11 not installed?)"
70+
raise ImportError(msg)
71+
72+
5573
def get_cmake_dir() -> str:
5674
"""
5775
Return the path to the pybind11 CMake module directory.

tests/extra_python_package/test_files.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
PKGCONFIG = """\
3434
prefix=${{pcfiledir}}/../../
3535
includedir=${{prefix}}/include
36+
srcdir=${{prefix}}/share/pybind11/src
3637
3738
Name: pybind11
3839
Description: Seamless operability between C++11 and Python

tools/pybind11.pc.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
prefix=@prefix_for_pc_file@
22
includedir=@includedir_for_pc_file@
3+
srcdir=@srcdir_for_pc_file@
34

45
Name: @PROJECT_NAME@
56
Description: Seamless operability between C++11 and Python

0 commit comments

Comments
 (0)