feat: expose the precompiled-mode sources to non-CMake builds (#6137)
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
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ea1b3a1..f53a646 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -343,8 +343,8 @@
install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION "${SKBUILD_HEADERS_DIR}")
endif()
install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
- install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/
- DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pybind11/src")
+ set(pybind11_install_srcdir "${CMAKE_INSTALL_DATAROOTDIR}/pybind11/src")
+ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/ DESTINATION "${pybind11_install_srcdir}")
set(PYBIND11_CMAKECONFIG_INSTALL_DIR
"${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}"
CACHE STRING "install path for pybind11Config.cmake")
@@ -355,9 +355,9 @@
set(pybind11_INCLUDEDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_INCLUDEDIR}")
endif()
if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}")
- set(pybind11_SRCDIR "${CMAKE_INSTALL_DATAROOTDIR}/pybind11/src")
+ set(pybind11_SRCDIR "${pybind11_install_srcdir}")
else()
- set(pybind11_SRCDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_DATAROOTDIR}/pybind11/src")
+ set(pybind11_SRCDIR "\$\{PACKAGE_PREFIX_DIR\}/${pybind11_install_srcdir}")
endif()
configure_package_config_file(
@@ -410,6 +410,7 @@
endif()
endif()
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
+ join_paths(srcdir_for_pc_file "\${prefix}" "${pybind11_install_srcdir}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc"
diff --git a/pybind11/__init__.py b/pybind11/__init__.py
index 3882b2b..1d66e11 100644
--- a/pybind11/__init__.py
+++ b/pybind11/__init__.py
@@ -8,12 +8,13 @@
from ._version import __version__, version_info
-from .commands import get_cmake_dir, get_include, get_pkgconfig_dir
+from .commands import get_cmake_dir, get_include, get_pkgconfig_dir, get_source_dir
__all__ = (
"__version__",
"get_cmake_dir",
"get_include",
"get_pkgconfig_dir",
+ "get_source_dir",
"version_info",
)
diff --git a/pybind11/__main__.py b/pybind11/__main__.py
index ce597c7..98e9184 100644
--- a/pybind11/__main__.py
+++ b/pybind11/__main__.py
@@ -17,6 +17,7 @@
get_include_dirs,
get_ldflags,
get_pkgconfig_dir,
+ get_source_dir,
)
@@ -51,6 +52,12 @@
help="Print the pkgconfig directory, ideal for setting $PKG_CONFIG_PATH.",
)
parser.add_argument(
+ "--srcdir",
+ action="store_true",
+ help="Print the directory containing the library sources for the optional"
+ " precompiled mode.",
+ )
+ parser.add_argument(
"--extension-suffix",
action="store_true",
help="Print the extension for a Python module",
@@ -101,6 +108,8 @@
print(quote(get_cmake_dir()))
if args.pkgconfigdir:
print(quote(get_pkgconfig_dir()))
+ if args.srcdir:
+ print(quote(get_source_dir()))
if args.extension_suffix:
print(ext_suffix)
diff --git a/pybind11/commands.py b/pybind11/commands.py
index 8bd0a9b..573b7cf 100644
--- a/pybind11/commands.py
+++ b/pybind11/commands.py
@@ -52,6 +52,24 @@
return installed_path if os.path.exists(installed_path) else source_path
+def get_source_dir() -> str:
+ """
+ Return the path to the pybind11 library sources, for the optional
+ precompiled mode. Compile ``pybind11_combined.cpp`` (or the individual
+ ``.cpp`` files) with ``PYBIND11_PRECOMPILED`` defined, and define that
+ macro for every translation unit that includes pybind11.
+ """
+ installed_path = os.path.join(DIR, "share", "pybind11", "src")
+ source_path = os.path.join(os.path.dirname(DIR), "src")
+ if os.path.exists(installed_path):
+ return installed_path
+ if os.path.exists(source_path):
+ return source_path
+
+ msg = "pybind11 library sources not found (pybind11 not installed?)"
+ raise ImportError(msg)
+
+
def get_cmake_dir() -> str:
"""
Return the path to the pybind11 CMake module directory.
diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py
index 58dbca4..a14f057 100644
--- a/tests/extra_python_package/test_files.py
+++ b/tests/extra_python_package/test_files.py
@@ -33,6 +33,7 @@
PKGCONFIG = """\
prefix=${{pcfiledir}}/../../
includedir=${{prefix}}/include
+srcdir=${{prefix}}/share/pybind11/src
Name: pybind11
Description: Seamless operability between C++11 and Python
diff --git a/tools/pybind11.pc.in b/tools/pybind11.pc.in
index 402f0b3..8b5af0a 100644
--- a/tools/pybind11.pc.in
+++ b/tools/pybind11.pc.in
@@ -1,5 +1,6 @@
prefix=@prefix_for_pc_file@
includedir=@includedir_for_pc_file@
+srcdir=@srcdir_for_pc_file@
Name: @PROJECT_NAME@
Description: Seamless operability between C++11 and Python