Skip to content

Commit

Permalink
fix(cmake): upgrade maximum supported CMake version to 3.27 (#4786)
Browse files Browse the repository at this point in the history
* Upgrade maximum supported CMake version to 3.27 to fix warning with CMP0148 policy (#4785)

* Update `macos_brew_install_llvm` pipeline to use expected Python installation

* Fix `Python_EXECUTABLE` Cmake variable typo

* Apply suggestions from code review

* fix: use FindPython for CMake 3.18+ by default for pybind11's tests

Signed-off-by: Henry Schreiner <[email protected]>

* tests: fix issues with finding Python

Signed-off-by: Henry Schreiner <[email protected]>

* tests: also set executable on subdir tests

Signed-off-by: Henry Schreiner <[email protected]>

* fix(cmake): correct logic for FindPython

Signed-off-by: Henry Schreiner <[email protected]>

* Update ci.yml

* Revert "Update ci.yml"

This reverts commit 33798ad.

---------

Signed-off-by: Henry Schreiner <[email protected]>
Co-authored-by: Henry Schreiner <[email protected]>
  • Loading branch information
polmes and henryiii authored Dec 14, 2023
1 parent 7d538a4 commit daea113
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 35 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,10 @@ jobs:
- name: Configure C++11
# LTO leads to many undefined reference like
# `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&)
run: cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=11 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -S . -B build
run: >-
cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=11 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
-S . -B build
- name: Build C++11
run: cmake --build build -j 2
Expand All @@ -991,7 +994,10 @@ jobs:
run: git clean -fdx

- name: Configure C++14
run: cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=14 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -S . -B build2
run: >-
cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=14 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
-S . -B build2
- name: Build C++14
run: cmake --build build2 -j 2
Expand All @@ -1009,7 +1015,10 @@ jobs:
run: git clean -fdx

- name: Configure C++17
run: cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=17 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -S . -B build3
run: >-
cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=17 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
-S . -B build3
- name: Build C++17
run: cmake --build build3 -j 2
Expand Down
23 changes: 19 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ endif()

cmake_minimum_required(VERSION 3.5)

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.26)
cmake_policy(VERSION 3.27)
endif()

if(_pybind11_cmp0148)
Expand Down Expand Up @@ -92,9 +92,15 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(pybind11_system "")

set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if(CMAKE_VERSION VERSION_LESS "3.18")
set(_pybind11_findpython_default OFF)
else()
set(_pybind11_findpython_default ON)
endif()
else()
set(PYBIND11_MASTER_PROJECT OFF)
set(pybind11_system SYSTEM)
set(_pybind11_findpython_default OFF)
endif()

# Options
Expand All @@ -116,9 +122,18 @@ cmake_dependent_option(
"Install pybind11 headers in Python include directory instead of default installation prefix"
OFF "PYBIND11_INSTALL" OFF)

cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF
cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" ${_pybind11_findpython_default}
"NOT CMAKE_VERSION VERSION_LESS 3.12" OFF)

# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
# (makes transition easier while we support both modes).
if(PYBIND11_MASTER_PROJECT
AND PYBIND11_FINDPYTHON
AND DEFINED PYTHON_EXECUTABLE
AND NOT DEFINED Python_EXECUTABLE)
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
endif()

# NB: when adding a header don't forget to also add it to setup.py
set(PYBIND11_HEADERS
include/pybind11/detail/class.h
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced/embedding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ information, see :doc:`/compiling`.

.. code-block:: cmake
cmake_minimum_required(VERSION 3.5...3.26)
cmake_minimum_required(VERSION 3.5...3.27)
project(example)
find_package(pybind11 REQUIRED) # or `add_subdirectory(pybind11)`
Expand Down
6 changes: 3 additions & 3 deletions docs/compiling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ extension module can be created with just a few lines of code:

.. code-block:: cmake
cmake_minimum_required(VERSION 3.5...3.26)
cmake_minimum_required(VERSION 3.5...3.27)
project(example LANGUAGES CXX)
add_subdirectory(pybind11)
Expand Down Expand Up @@ -498,7 +498,7 @@ You can use these targets to build complex applications. For example, the

.. code-block:: cmake
cmake_minimum_required(VERSION 3.5...3.26)
cmake_minimum_required(VERSION 3.5...3.27)
project(example LANGUAGES CXX)
find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
Expand Down Expand Up @@ -556,7 +556,7 @@ information about usage in C++, see :doc:`/advanced/embedding`.

.. code-block:: cmake
cmake_minimum_required(VERSION 3.5...3.26)
cmake_minimum_required(VERSION 3.5...3.27)
project(example LANGUAGES CXX)
find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
Expand Down
6 changes: 3 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

cmake_minimum_required(VERSION 3.5)

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.26)
cmake_policy(VERSION 3.27)
endif()

# Filter out items; print an optional message if any items filtered. This ignores extensions.
Expand Down
3 changes: 1 addition & 2 deletions tests/test_cmake_build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ function(pybind11_add_build_test name)

set(build_options "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")

list(APPEND build_options "-DPYBIND11_FINDPYTHON=${PYBIND11_FINDPYTHON}")
if(PYBIND11_FINDPYTHON)
list(APPEND build_options "-DPYBIND11_FINDPYTHON=${PYBIND11_FINDPYTHON}")

if(DEFINED Python_ROOT_DIR)
list(APPEND build_options "-DPython_ROOT_DIR=${Python_ROOT_DIR}")
endif()
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cmake_build/installed_embed/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
cmake_minimum_required(VERSION 3.5)

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.26)
cmake_policy(VERSION 3.27)
endif()

project(test_installed_embed CXX)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cmake_build/installed_function/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
cmake_minimum_required(VERSION 3.5)
project(test_installed_module CXX)

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.26)
cmake_policy(VERSION 3.27)
endif()

project(test_installed_function CXX)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cmake_build/installed_target/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
cmake_minimum_required(VERSION 3.5)

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.26)
cmake_policy(VERSION 3.27)
endif()

project(test_installed_target CXX)
Expand Down
12 changes: 9 additions & 3 deletions tests/test_cmake_build/subdirectory_embed/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
cmake_minimum_required(VERSION 3.5)

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.26)
cmake_policy(VERSION 3.27)
endif()

project(test_subdirectory_embed CXX)
Expand All @@ -16,6 +16,12 @@ set(PYBIND11_INSTALL
CACHE BOOL "")
set(PYBIND11_EXPORT_NAME test_export)

# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
# (makes transition easier while we support both modes).
if(DEFINED PYTHON_EXECUTABLE AND NOT DEFINED Python_EXECUTABLE)
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
endif()

add_subdirectory("${pybind11_SOURCE_DIR}" pybind11)

# Test basic target functionality
Expand Down
12 changes: 9 additions & 3 deletions tests/test_cmake_build/subdirectory_function/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
cmake_minimum_required(VERSION 3.5)

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.26)
cmake_policy(VERSION 3.27)
endif()

project(test_subdirectory_function CXX)

# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
# (makes transition easier while we support both modes).
if(DEFINED PYTHON_EXECUTABLE AND NOT DEFINED Python_EXECUTABLE)
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
endif()

add_subdirectory("${pybind11_SOURCE_DIR}" pybind11)
pybind11_add_module(test_subdirectory_function ../main.cpp)
set_target_properties(test_subdirectory_function PROPERTIES OUTPUT_NAME test_cmake_build)
Expand Down
12 changes: 9 additions & 3 deletions tests/test_cmake_build/subdirectory_target/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
cmake_minimum_required(VERSION 3.5)

# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.26)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.26)
cmake_policy(VERSION 3.27)
endif()

project(test_subdirectory_target CXX)

# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
# (makes transition easier while we support both modes).
if(DEFINED PYTHON_EXECUTABLE AND NOT DEFINED Python_EXECUTABLE)
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
endif()

add_subdirectory("${pybind11_SOURCE_DIR}" pybind11)

add_library(test_subdirectory_target MODULE ../main.cpp)
Expand Down
2 changes: 1 addition & 1 deletion tools/pybind11NewTools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if(NOT Python_FOUND AND NOT Python3_FOUND)
# If we are in submodule mode, export the Python targets to global targets.
# If this behavior is not desired, FindPython _before_ pybind11.
if(NOT is_config
AND NOT Python_ARTIFACTS_INTERACTIVE
AND Python_ARTIFACTS_INTERACTIVE
AND _pybind11_global_keyword STREQUAL "")
if(TARGET Python::Python)
set_property(TARGET Python::Python PROPERTY IMPORTED_GLOBAL TRUE)
Expand Down

0 comments on commit daea113

Please sign in to comment.