@@ -10,26 +10,15 @@ if(NOT CMAKE_VERSION VERSION_LESS "3.27")
1010 cmake_policy (GET CMP0148 _pybind11_cmp0148)
1111endif ()
1212
13- cmake_minimum_required (VERSION 3.5)
14-
15- # The `cmake_minimum_required(VERSION 3.5...3.29)` syntax does not work with
16- # some versions of VS that have a patched CMake 3.11. This forces us to emulate
17- # the behavior using the following workaround:
18- if (${CMAKE_VERSION} VERSION_LESS 3.29)
19- cmake_policy (VERSION ${CMAKE_MAJOR_VERSION} .${CMAKE_MINOR_VERSION} )
20- else ()
21- cmake_policy (VERSION 3.29)
22- endif ()
13+ cmake_minimum_required (VERSION 3.15...4.0)
2314
2415if (_pybind11_cmp0148)
2516 cmake_policy (SET CMP0148 ${_pybind11_cmp0148} )
2617 unset (_pybind11_cmp0148)
2718endif ()
2819
2920# Avoid infinite recursion if tests include this as a subdirectory
30- if (DEFINED PYBIND11_MASTER_PROJECT)
31- return ()
32- endif ()
21+ include_guard (GLOBAL )
3322
3423# Extract project version from source
3524file (STRINGS "${CMAKE_CURRENT_SOURCE_DIR} /include/pybind11/detail/common.h"
@@ -74,16 +63,15 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
7463
7564 set (PYBIND11_MASTER_PROJECT ON )
7665
77- if (OSX AND CMAKE_VERSION VERSION_LESS 3.7)
78- # Bug in macOS CMake < 3.7 is unable to download catch
79- message (WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended" )
80- elseif (WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8)
81- # Only tested with 3.8+ in CI.
82- message (WARNING "CMAKE 3.8+ tested on Windows, previous versions untested" )
83- endif ()
84-
8566 message (STATUS "CMake ${CMAKE_VERSION} " )
8667
68+ if (DEFINED SKBUILD AND DEFINED ENV{PYBIND11_GLOBAL_SDIST})
69+ message (
70+ FATAL_ERROR
71+ "PYBIND11_GLOBAL_SDIST is not supported, use nox -s build_global or a pybind11-global SDist instead."
72+ )
73+ endif ()
74+
8775 if (CMAKE_CXX_STANDARD)
8876 set (CMAKE_CXX_EXTENSIONS OFF )
8977 set (CMAKE_CXX_STANDARD_REQUIRED ON )
@@ -100,7 +88,7 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
10088else ()
10189 set (PYBIND11_MASTER_PROJECT OFF )
10290 set (pybind11_system SYSTEM )
103- set (_pybind11_findpython_default OFF )
91+ set (_pybind11_findpython_default COMPAT )
10492endif ()
10593
10694# Options
@@ -111,8 +99,6 @@ option(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION
11199 "To enforce that a handle_type_name<> specialization exists" OFF )
112100option (PYBIND11_SIMPLE_GIL_MANAGEMENT
113101 "Use simpler GIL management logic that does not support disassociation" OFF )
114- option (PYBIND11_NUMPY_1_ONLY
115- "Disable NumPy 2 support to avoid changes to previous pybind11 versions." OFF )
116102set (PYBIND11_INTERNALS_VERSION
117103 ""
118104 CACHE STRING "Override the ABI version, may be used to enable the unstable ABI." )
@@ -124,45 +110,102 @@ endif()
124110if (PYBIND11_SIMPLE_GIL_MANAGEMENT)
125111 add_compile_definitions (PYBIND11_SIMPLE_GIL_MANAGEMENT)
126112endif ()
127- if (PYBIND11_NUMPY_1_ONLY)
128- add_compile_definitions (PYBIND11_NUMPY_1_ONLY)
129- endif ()
130113
131114cmake_dependent_option(
132115 USE_PYTHON_INCLUDE_DIR
133116 "Install pybind11 headers in Python include directory instead of default installation prefix"
134117 OFF "PYBIND11_INSTALL" OFF )
135118
136- cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" ${_pybind11_findpython_default}
137- "NOT CMAKE_VERSION VERSION_LESS 3.12" OFF )
119+ set (PYBIND11_FINDPYTHON
120+ ${_pybind11_findpython_default}
121+ CACHE STRING "Force new FindPython - NEW, OLD, COMPAT" )
122+
123+ if (PYBIND11_MASTER_PROJECT)
124+
125+ # Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
126+ # (makes transition easier while we support both modes).
127+ if (PYBIND11_FINDPYTHON
128+ AND DEFINED PYTHON_EXECUTABLE
129+ AND NOT DEFINED Python_EXECUTABLE)
130+ set (Python_EXECUTABLE "${PYTHON_EXECUTABLE} " )
131+ endif ()
138132
139- # Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
140- # (makes transition easier while we support both modes).
141- if (PYBIND11_MASTER_PROJECT
142- AND PYBIND11_FINDPYTHON
143- AND DEFINED PYTHON_EXECUTABLE
144- AND NOT DEFINED Python_EXECUTABLE)
145- set (Python_EXECUTABLE "${PYTHON_EXECUTABLE} " )
133+ # This is a shortcut that is primarily for the venv cmake preset,
134+ # but can be used to quickly setup tests manually, too
135+ set (PYBIND11_CREATE_WITH_UV
136+ ""
137+ CACHE STRING "Create a virtualenv if it doesn't exist" )
138+
139+ if (NOT PYBIND11_CREATE_WITH_UV STREQUAL "" )
140+ set (Python_ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR} /.venv" )
141+ if (EXISTS "${Python_ROOT_DIR} " )
142+ if (EXISTS "${CMAKE_BINARY_DIR} /CMakeCache.txt" )
143+ message (STATUS "Using existing venv at ${Python_ROOT_DIR} , remove or --fresh to recreate" )
144+ else ()
145+ # --fresh used to remove the cache
146+ file (REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR} /.venv" )
147+ endif ()
148+ endif ()
149+ if (NOT EXISTS "${Python_ROOT_DIR} " )
150+ find_program (UV uv REQUIRED)
151+ # CMake 3.19+ would be able to use COMMAND_ERROR_IS_FATAL
152+ message (
153+ STATUS "Creating venv with ${UV} venv -p ${PYBIND11_CREATE_WITH_UV} '${Python_ROOT_DIR} '" )
154+ execute_process (COMMAND ${UV} venv -p ${PYBIND11_CREATE_WITH_UV} "${Python_ROOT_DIR} "
155+ RESULT_VARIABLE _venv_result)
156+ if (_venv_result AND NOT _venv_result EQUAL 0)
157+ message (FATAL_ERROR "uv venv failed with '${_venv_result} '" )
158+ endif ()
159+ message (
160+ STATUS
161+ "Installing deps with ${UV} pip install -p '${Python_ROOT_DIR} ' -r tests/requirements.txt"
162+ )
163+ execute_process (
164+ COMMAND ${UV} pip install -p "${Python_ROOT_DIR} " -r
165+ "${CMAKE_CURRENT_SOURCE_DIR} /tests/requirements.txt" RESULT_VARIABLE _pip_result)
166+ if (_pip_result AND NOT _pip_result EQUAL 0)
167+ message (FATAL_ERROR "uv pip install failed with '${_pip_result} '" )
168+ endif ()
169+ endif ()
170+ else ()
171+ if (NOT DEFINED Python3_EXECUTABLE
172+ AND NOT DEFINED Python_EXECUTABLE
173+ AND NOT DEFINED Python_ROOT_DIR
174+ AND NOT DEFINED ENV{VIRTUALENV}
175+ AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR} /.venv" )
176+ message (STATUS "Autodetecting Python in virtual environment" )
177+ set (Python_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR} /.venv" )
178+ endif ()
179+ endif ()
146180endif ()
147181
148- # NB: when adding a header don't forget to also add it to setup.py
149182set (PYBIND11_HEADERS
150183 include /pybind11/detail/class.h
151184 include /pybind11/detail/common.h
152185 include /pybind11/detail/cpp_conduit.h
153186 include /pybind11/detail/descr.h
187+ include /pybind11/detail/dynamic_raw_ptr_cast_if_possible.h
188+ include /pybind11/detail/exception_translation.h
189+ include /pybind11/detail/function_record_pyobject.h
154190 include /pybind11/detail/init.h
155191 include /pybind11/detail/internals.h
192+ include /pybind11/detail/native_enum_data.h
193+ include /pybind11/detail/pybind11_namespace_macros.h
194+ include /pybind11/detail/struct_smart_holder.h
156195 include /pybind11/detail/type_caster_base.h
157196 include /pybind11/detail/typeid.h
197+ include /pybind11/detail/using_smart_holder.h
158198 include /pybind11/detail/value_and_holder.h
159- include /pybind11/detail/exception_translation.h
160199 include /pybind11/attr.h
161200 include /pybind11/buffer_info.h
162201 include /pybind11/cast.h
163202 include /pybind11/chrono.h
164203 include /pybind11/common.h
165204 include /pybind11/complex.h
205+ include /pybind11/conduit/pybind11_conduit_v1.h
206+ include /pybind11/conduit/pybind11_platform_abi_id.h
207+ include /pybind11/conduit/wrap_include_python_h.h
208+ include /pybind11/critical_section.h
166209 include /pybind11/options .h
167210 include /pybind11/eigen.h
168211 include /pybind11/eigen/common.h
@@ -172,20 +215,25 @@ set(PYBIND11_HEADERS
172215 include /pybind11/eval.h
173216 include /pybind11/gil.h
174217 include /pybind11/gil_safe_call_once.h
218+ include /pybind11/gil_simple.h
175219 include /pybind11/iostream.h
176220 include /pybind11/functional.h
221+ include /pybind11/native_enum.h
177222 include /pybind11/numpy.h
178223 include /pybind11/operators.h
179224 include /pybind11/pybind11.h
180225 include /pybind11/pytypes.h
226+ include /pybind11/subinterpreter.h
181227 include /pybind11/stl.h
182228 include /pybind11/stl_bind.h
183229 include /pybind11/stl/filesystem.h
230+ include /pybind11/trampoline_self_life_support.h
184231 include /pybind11/type_caster_pyobject_ptr.h
185- include /pybind11/typing.h)
232+ include /pybind11/typing.h
233+ include /pybind11/warnings.h)
186234
187235# Compare with grep and warn if mismatched
188- if (PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12 )
236+ if (PYBIND11_MASTER_PROJECT)
189237 file (
190238 GLOB_RECURSE _pybind11_header_check
191239 LIST_DIRECTORIES false
@@ -203,10 +251,7 @@ if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12)
203251 endif ()
204252endif ()
205253
206- # CMake 3.12 added list(TRANSFORM <list> PREPEND
207- # But we can't use it yet
208- string (REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR} /include/" PYBIND11_HEADERS
209- "${PYBIND11_HEADERS} " )
254+ list (TRANSFORM PYBIND11_HEADERS PREPEND "${CMAKE_CURRENT_SOURCE_DIR} /" )
210255
211256# Cache variable so this can be used in parent projects
212257set (pybind11_INCLUDE_DIR
@@ -261,6 +306,9 @@ elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)
261306endif ()
262307
263308if (PYBIND11_INSTALL)
309+ if (DEFINED SKBUILD_PROJECT_NAME AND SKBUILD_PROJECT_NAME STREQUAL "pybind11_global" )
310+ install (DIRECTORY ${pybind11_INCLUDE_DIR} /pybind11 DESTINATION "${SKBUILD_HEADERS_DIR} " )
311+ endif ()
264312 install (DIRECTORY ${pybind11_INCLUDE_DIR} /pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
265313 set (PYBIND11_CMAKECONFIG_INSTALL_DIR
266314 "${CMAKE_INSTALL_DATAROOTDIR} /cmake/${PROJECT_NAME} "
@@ -276,25 +324,11 @@ if(PYBIND11_INSTALL)
276324 tools/${PROJECT_NAME} Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake"
277325 INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
278326
279- if (CMAKE_VERSION VERSION_LESS 3.14)
280- # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does
281- # not depend on architecture specific settings or libraries.
282- set (_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P} )
283- unset (CMAKE_SIZEOF_VOID_P )
284-
285- write_basic_package_version_file(
286- ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} ConfigVersion.cmake
287- VERSION ${PROJECT_VERSION}
288- COMPATIBILITY AnyNewerVersion)
289-
290- set (CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P} )
291- else ()
292- # CMake 3.14+ natively supports header-only libraries
293- write_basic_package_version_file(
294- ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} ConfigVersion.cmake
295- VERSION ${PROJECT_VERSION}
296- COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT)
297- endif ()
327+ # CMake natively supports header-only libraries
328+ write_basic_package_version_file(
329+ ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} ConfigVersion.cmake
330+ VERSION ${PROJECT_VERSION}
331+ COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT)
298332
299333 install (
300334 FILES ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake
@@ -341,6 +375,17 @@ if(PYBIND11_INSTALL)
341375 install (FILES "${CMAKE_CURRENT_BINARY_DIR} /pybind11.pc"
342376 DESTINATION "${CMAKE_INSTALL_DATAROOTDIR} /pkgconfig/" )
343377
378+ # When building a wheel, include __init__.py's for modules
379+ # (see https://github.com/pybind/pybind11/pull/5552)
380+ if (DEFINED SKBUILD_PROJECT_NAME AND SKBUILD_PROJECT_NAME STREQUAL "pybind11" )
381+ file (MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR} /empty" )
382+ file (TOUCH "${CMAKE_CURRENT_BINARY_DIR} /empty/__init__.py" )
383+ install (FILES "${CMAKE_CURRENT_BINARY_DIR} /empty/__init__.py"
384+ DESTINATION "${CMAKE_INSTALL_DATAROOTDIR} /" )
385+ install (FILES "${CMAKE_CURRENT_BINARY_DIR} /empty/__init__.py"
386+ DESTINATION "${CMAKE_INSTALL_DATAROOTDIR} /pkgconfig/" )
387+ endif ()
388+
344389 # Uninstall target
345390 if (PYBIND11_MASTER_PROJECT)
346391 configure_file ("${CMAKE_CURRENT_SOURCE_DIR} /tools/cmake_uninstall.cmake.in"
0 commit comments