Skip to content

Commit 28dbce4

Browse files
authored
feat: require CMake 3.15+ (pybind#5304)
* feat: require CMake 3.15+ Signed-off-by: Henry Schreiner <[email protected]> * Apply suggestions from code review * Update CMakeLists.txt * fix: adapt for CMake 3.30+ (using 3.18+) Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent d893f97 commit 28dbce4

File tree

20 files changed

+113
-291
lines changed

20 files changed

+113
-291
lines changed

.github/CONTRIBUTING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ nox -s build
8181
### Full setup
8282

8383
To setup an ideal development environment, run the following commands on a
84-
system with CMake 3.14+:
84+
system with CMake 3.15+:
8585

8686
```bash
8787
python3 -m venv venv
@@ -96,8 +96,8 @@ Tips:
9696
* You can use `virtualenv` (faster, from PyPI) instead of `venv`.
9797
* You can select any name for your environment folder; if it contains "env" it
9898
will be ignored by git.
99-
* If you don't have CMake 3.14+, just add "cmake" to the pip install command.
100-
* You can use `-DPYBIND11_FINDPYTHON=ON` to use FindPython on CMake 3.12+
99+
* If you don't have CMake 3.15+, just add "cmake" to the pip install command.
100+
* You can use `-DPYBIND11_FINDPYTHON=ON` to use FindPython.
101101
* In classic mode, you may need to set `-DPYTHON_EXECUTABLE=/path/to/python`.
102102
FindPython uses `-DPython_ROOT_DIR=/path/to` or
103103
`-DPython_EXECUTABLE=/path/to/python`.
@@ -149,8 +149,8 @@ To run the tests, you can "build" the check target:
149149
cmake --build build --target check
150150
```
151151

152-
`--target` can be spelled `-t` in CMake 3.15+. You can also run individual
153-
tests with these targets:
152+
`--target` can be spelled `-t`. You can also run individual tests with these
153+
targets:
154154

155155
* `pytest`: Python tests only, using the
156156
[pytest](https://docs.pytest.org/en/stable/) framework

.github/workflows/configure.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ jobs:
3131
include:
3232
- runs-on: ubuntu-20.04
3333
arch: x64
34-
cmake: "3.5"
34+
cmake: "3.15"
3535

3636
- runs-on: ubuntu-20.04
3737
arch: x64
3838
cmake: "3.29"
3939

4040
- runs-on: macos-13
4141
arch: x64
42-
cmake: "3.8"
42+
cmake: "3.15"
4343

4444
- runs-on: windows-2019
4545
arch: x64 # x86 compilers seem to be missing on 2019 image

CMakeLists.txt

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,15 @@ if(NOT CMAKE_VERSION VERSION_LESS "3.27")
1010
cmake_policy(GET CMP0148 _pybind11_cmp0148)
1111
endif()
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...3.30)
2314

2415
if(_pybind11_cmp0148)
2516
cmake_policy(SET CMP0148 ${_pybind11_cmp0148})
2617
unset(_pybind11_cmp0148)
2718
endif()
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
3524
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/pybind11/detail/common.h"
@@ -74,14 +63,6 @@ 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

8768
if(CMAKE_CXX_STANDARD)
@@ -133,8 +114,7 @@ cmake_dependent_option(
133114
"Install pybind11 headers in Python include directory instead of default installation prefix"
134115
OFF "PYBIND11_INSTALL" OFF)
135116

136-
cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" ${_pybind11_findpython_default}
137-
"NOT CMAKE_VERSION VERSION_LESS 3.12" OFF)
117+
option(PYBIND11_FINDPYTHON "Force new FindPython" ${_pybind11_findpython_default})
138118

139119
# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
140120
# (makes transition easier while we support both modes).
@@ -183,7 +163,7 @@ set(PYBIND11_HEADERS
183163
include/pybind11/typing.h)
184164

185165
# Compare with grep and warn if mismatched
186-
if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12)
166+
if(PYBIND11_MASTER_PROJECT)
187167
file(
188168
GLOB_RECURSE _pybind11_header_check
189169
LIST_DIRECTORIES false
@@ -201,10 +181,7 @@ if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12)
201181
endif()
202182
endif()
203183

204-
# CMake 3.12 added list(TRANSFORM <list> PREPEND
205-
# But we can't use it yet
206-
string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS
207-
"${PYBIND11_HEADERS}")
184+
list(TRANSFORM PYBIND11_HEADERS PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
208185

209186
# Cache variable so this can be used in parent projects
210187
set(pybind11_INCLUDE_DIR
@@ -274,25 +251,11 @@ if(PYBIND11_INSTALL)
274251
tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
275252
INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
276253

277-
if(CMAKE_VERSION VERSION_LESS 3.14)
278-
# Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does
279-
# not depend on architecture specific settings or libraries.
280-
set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
281-
unset(CMAKE_SIZEOF_VOID_P)
282-
283-
write_basic_package_version_file(
284-
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
285-
VERSION ${PROJECT_VERSION}
286-
COMPATIBILITY AnyNewerVersion)
287-
288-
set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P})
289-
else()
290-
# CMake 3.14+ natively supports header-only libraries
291-
write_basic_package_version_file(
292-
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
293-
VERSION ${PROJECT_VERSION}
294-
COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT)
295-
endif()
254+
# CMake natively supports header-only libraries
255+
write_basic_package_version_file(
256+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
257+
VERSION ${PROJECT_VERSION}
258+
COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT)
296259

297260
install(
298261
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake

docs/advanced/embedding.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information, see :doc:`/compiling`.
1818

1919
.. code-block:: cmake
2020
21-
cmake_minimum_required(VERSION 3.5...3.29)
21+
cmake_minimum_required(VERSION 3.15...3.30)
2222
project(example)
2323
2424
find_package(pybind11 REQUIRED) # or `add_subdirectory(pybind11)`

docs/compiling.rst

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ A Python extension module can be created with just a few lines of code:
1818

1919
.. code-block:: cmake
2020
21-
cmake_minimum_required(VERSION 3.15...3.29)
21+
cmake_minimum_required(VERSION 3.15...3.30)
2222
project(example LANGUAGES CXX)
2323
2424
set(PYBIND11_FINDPYTHON ON)
@@ -319,11 +319,11 @@ Building with CMake
319319

320320
For C++ codebases that have an existing CMake-based build system, a Python
321321
extension module can be created with just a few lines of code, as seen above in
322-
the module section. Pybind11 currently supports a lower minimum if you don't
323-
use the modern FindPython, though be aware that CMake 3.27 removed the old
324-
mechanism, so pybind11 will automatically switch if the old mechanism is not
325-
available. Please opt into the new mechanism if at all possible. Our default
326-
may change in future versions. This is the minimum required:
322+
the module section. Pybind11 currently defaults to the old mechanism, though be
323+
aware that CMake 3.27 removed the old mechanism, so pybind11 will automatically
324+
switch if the old mechanism is not available. Please opt into the new mechanism
325+
if at all possible. Our default may change in future versions. This is the
326+
minimum required:
327327

328328

329329

@@ -333,6 +333,9 @@ may change in future versions. This is the minimum required:
333333
.. versionchanged:: 2.11
334334
CMake 3.5+ is required.
335335

336+
.. versionchanged:: 2.14
337+
CMake 3.15+ is required.
338+
336339

337340
Further information can be found at :doc:`cmake/index`.
338341

@@ -444,7 +447,7 @@ See the `Config file`_ docstring for details of relevant CMake variables.
444447

445448
.. code-block:: cmake
446449
447-
cmake_minimum_required(VERSION 3.4...3.18)
450+
cmake_minimum_required(VERSION 3.15...3.30)
448451
project(example LANGUAGES CXX)
449452
450453
find_package(pybind11 REQUIRED)
@@ -483,14 +486,13 @@ can refer to the same [cmake_example]_ repository for a full sample project
483486
FindPython mode
484487
---------------
485488

486-
CMake 3.12+ (3.15+ recommended, 3.18.2+ ideal) added a new module called
487-
FindPython that had a highly improved search algorithm and modern targets
488-
and tools. If you use FindPython, pybind11 will detect this and use the
489-
existing targets instead:
489+
Modern CMake (3.18.2+ ideal) added a new module called FindPython that had a
490+
highly improved search algorithm and modern targets and tools. If you use
491+
FindPython, pybind11 will detect this and use the existing targets instead:
490492

491493
.. code-block:: cmake
492494
493-
cmake_minimum_required(VERSION 3.15...3.22)
495+
cmake_minimum_required(VERSION 3.15...3.30)
494496
project(example LANGUAGES CXX)
495497
496498
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
@@ -541,7 +543,7 @@ available in all modes. The targets provided are:
541543
Just the "linking" part of pybind11:module
542544

543545
``pybind11::module``
544-
Everything for extension modules - ``pybind11::pybind11`` + ``Python::Module`` (FindPython CMake 3.15+) or ``pybind11::python_link_helper``
546+
Everything for extension modules - ``pybind11::pybind11`` + ``Python::Module`` (FindPython) or ``pybind11::python_link_helper``
545547

546548
``pybind11::embed``
547549
Everything for embedding the Python interpreter - ``pybind11::pybind11`` + ``Python::Python`` (FindPython) or Python libs
@@ -568,7 +570,7 @@ You can use these targets to build complex applications. For example, the
568570

569571
.. code-block:: cmake
570572
571-
cmake_minimum_required(VERSION 3.5...3.29)
573+
cmake_minimum_required(VERSION 3.15...3.30)
572574
project(example LANGUAGES CXX)
573575
574576
find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
@@ -626,7 +628,7 @@ information about usage in C++, see :doc:`/advanced/embedding`.
626628

627629
.. code-block:: cmake
628630
629-
cmake_minimum_required(VERSION 3.5...3.29)
631+
cmake_minimum_required(VERSION 3.15...3.30)
630632
project(example LANGUAGES CXX)
631633
632634
find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)

docs/faq.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ CMake configure line. (Replace ``$(which python)`` with a path to python if
258258
your prefer.)
259259

260260
You can alternatively try ``-DPYBIND11_FINDPYTHON=ON``, which will activate the
261-
new CMake FindPython support instead of pybind11's custom search. Requires
262-
CMake 3.12+, and 3.15+ or 3.18.2+ are even better. You can set this in your
263-
``CMakeLists.txt`` before adding or finding pybind11, as well.
261+
new CMake FindPython support instead of pybind11's custom search. Newer CMake,
262+
like, 3.18.2+, is recommended. You can set this in your ``CMakeLists.txt``
263+
before adding or finding pybind11, as well.
264264

265265
Inconsistent detection of Python version in CMake and pybind11
266266
==============================================================
@@ -281,11 +281,11 @@ There are three possible solutions:
281281
from CMake and rely on pybind11 in detecting Python version. If this is not
282282
possible, the CMake machinery should be called *before* including pybind11.
283283
2. Set ``PYBIND11_FINDPYTHON`` to ``True`` or use ``find_package(Python
284-
COMPONENTS Interpreter Development)`` on modern CMake (3.12+, 3.15+ better,
285-
3.18.2+ best). Pybind11 in these cases uses the new CMake FindPython instead
286-
of the old, deprecated search tools, and these modules are much better at
287-
finding the correct Python. If FindPythonLibs/Interp are not available
288-
(CMake 3.27+), then this will be ignored and FindPython will be used.
284+
COMPONENTS Interpreter Development)`` on modern CMake ( 3.18.2+ best).
285+
Pybind11 in these cases uses the new CMake FindPython instead of the old,
286+
deprecated search tools, and these modules are much better at finding the
287+
correct Python. If FindPythonLibs/Interp are not available (CMake 3.27+),
288+
then this will be ignored and FindPython will be used.
289289
3. Set ``PYBIND11_NOPYTHON`` to ``TRUE``. Pybind11 will not search for Python.
290290
However, you will have to use the target-based system, and do more setup
291291
yourself, because it does not know about or include things that depend on

tests/CMakeLists.txt

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@
55
# All rights reserved. Use of this source code is governed by a
66
# BSD-style license that can be found in the LICENSE file.
77

8-
cmake_minimum_required(VERSION 3.5)
9-
10-
# The `cmake_minimum_required(VERSION 3.5...3.29)` syntax does not work with
11-
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
12-
# the behavior using the following workaround:
13-
if(${CMAKE_VERSION} VERSION_LESS 3.29)
14-
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
15-
else()
16-
cmake_policy(VERSION 3.29)
17-
endif()
8+
cmake_minimum_required(VERSION 3.15...3.30)
189

1910
# Filter out items; print an optional message if any items filtered. This ignores extensions.
2011
#
@@ -76,8 +67,8 @@ project(pybind11_tests CXX)
7667
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../tools")
7768

7869
option(PYBIND11_WERROR "Report all warnings as errors" OFF)
79-
option(DOWNLOAD_EIGEN "Download EIGEN (requires CMake 3.11+)" OFF)
80-
option(PYBIND11_CUDA_TESTS "Enable building CUDA tests (requires CMake 3.12+)" OFF)
70+
option(DOWNLOAD_EIGEN "Download EIGEN" OFF)
71+
option(PYBIND11_CUDA_TESTS "Enable building CUDA tests" OFF)
8172
set(PYBIND11_TEST_OVERRIDE
8273
""
8374
CACHE STRING "Tests from ;-separated list of *.cpp files will be built instead of all tests")
@@ -249,25 +240,21 @@ endif()
249240
if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
250241
# Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
251242
# Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
252-
# produces a fatal error if loaded from a pre-3.0 cmake.
253243
if(DOWNLOAD_EIGEN)
254-
if(CMAKE_VERSION VERSION_LESS 3.11)
255-
message(FATAL_ERROR "CMake 3.11+ required when using DOWNLOAD_EIGEN")
244+
if(CMAKE_VERSION VERSION_LESS 3.18)
245+
set(_opts)
246+
else()
247+
set(_opts SOURCE_SUBDIR no-cmake-build)
256248
endif()
257-
258249
include(FetchContent)
259250
FetchContent_Declare(
260251
eigen
261252
GIT_REPOSITORY "${PYBIND11_EIGEN_REPO}"
262-
GIT_TAG "${PYBIND11_EIGEN_VERSION_HASH}")
263-
264-
FetchContent_GetProperties(eigen)
265-
if(NOT eigen_POPULATED)
266-
message(
267-
STATUS
268-
"Downloading Eigen ${PYBIND11_EIGEN_VERSION_STRING} (${PYBIND11_EIGEN_VERSION_HASH}) from ${PYBIND11_EIGEN_REPO}"
269-
)
270-
FetchContent_Populate(eigen)
253+
GIT_TAG "${PYBIND11_EIGEN_VERSION_HASH}"
254+
${_opts})
255+
FetchContent_MakeAvailable(eigen)
256+
if(NOT CMAKE_VERSION VERSION_LESS 3.18)
257+
set(EIGEN3_INCLUDE_DIR "${eigen_SOURCE_DIR}")
271258
endif()
272259

273260
set(EIGEN3_INCLUDE_DIR ${eigen_SOURCE_DIR})
@@ -315,8 +302,7 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
315302
if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
316303
list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
317304
endif()
318-
message(
319-
STATUS "Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON on CMake 3.11+ to download")
305+
message(STATUS "Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON to download")
320306
endif()
321307
endif()
322308

@@ -501,12 +487,10 @@ foreach(target ${test_targets})
501487
endforeach()
502488

503489
# Provide nice organisation in IDEs
504-
if(NOT CMAKE_VERSION VERSION_LESS 3.8)
505-
source_group(
506-
TREE "${CMAKE_CURRENT_SOURCE_DIR}/../include"
507-
PREFIX "Header Files"
508-
FILES ${PYBIND11_HEADERS})
509-
endif()
490+
source_group(
491+
TREE "${CMAKE_CURRENT_SOURCE_DIR}/../include"
492+
PREFIX "Header Files"
493+
FILES ${PYBIND11_HEADERS})
510494

511495
# Make sure pytest is found or produce a warning
512496
pybind11_find_import(pytest VERSION 3.1)

tests/extra_python_package/test_files.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import zipfile
1010

1111
# These tests must be run explicitly
12-
# They require CMake 3.15+ (--install)
1312

1413
DIR = os.path.abspath(os.path.dirname(__file__))
1514
MAIN_DIR = os.path.dirname(os.path.dirname(DIR))

tests/pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ name = "pybind11_tests"
1010
version = "0.0.1"
1111
dependencies = ["pytest", "pytest-timeout", "numpy", "scipy"]
1212

13-
[tool.scikit-build]
14-
# Hide a warning while we also support CMake < 3.15
15-
cmake.version = ">=3.15"
16-
1713
[tool.scikit-build.cmake.define]
1814
PYBIND11_FINDPYTHON = true
1915

0 commit comments

Comments
 (0)