Skip to content

Commit 4fb111b

Browse files
authored
fix(cmake): correctly detect FindPython policy and better warning (#4806)
1 parent d06d536 commit 4fb111b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
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+
# Propagate this policy (FindPythonInterp removal) so it can be detected later
9+
if(NOT CMAKE_VERSION VERSION_LESS "3.27")
10+
cmake_policy(GET CMP0148 _pybind11_cmp0148)
11+
endif()
12+
813
cmake_minimum_required(VERSION 3.5)
914

1015
# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with
@@ -16,6 +21,11 @@ else()
1621
cmake_policy(VERSION 3.26)
1722
endif()
1823

24+
if(_pybind11_cmp0148)
25+
cmake_policy(SET CMP0148 ${_pybind11_cmp0148})
26+
unset(_pybind11_cmp0148)
27+
endif()
28+
1929
# Avoid infinite recursion if tests include this as a subdirectory
2030
if(DEFINED PYBIND11_MASTER_PROJECT)
2131
return()

tools/FindPythonLibsNew.cmake

+16
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ if(NOT PythonLibsNew_FIND_VERSION)
9595
set(PythonLibsNew_FIND_VERSION "3.6")
9696
endif()
9797

98+
if(NOT CMAKE_VERSION VERSION_LESS "3.27")
99+
cmake_policy(GET CMP0148 _pybind11_cmp0148)
100+
if(NOT _pybind11_cmp0148)
101+
message(
102+
AUTHOR_WARNING
103+
"Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs "
104+
"modules are removed. Run \"cmake --help-policy CMP0148\" for policy "
105+
"details. Use the cmake_policy command to set the policy and suppress "
106+
"this warning, or preferably upgrade to using FindPython, either by "
107+
"calling it explicitly before pybind11, or by setting "
108+
"PYBIND11_FINDPYTHON ON before pybind11.")
109+
endif()
110+
cmake_policy(SET CMP0148 OLD)
111+
unset(_pybind11_cmp0148)
112+
endif()
113+
98114
find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} ${_pythonlibs_required}
99115
${_pythonlibs_quiet})
100116

0 commit comments

Comments
 (0)