Skip to content

[BUG]: Warning when using CMake 3.27, CMP0148 policy is ignored #4785

Open
@polmes

Description

@polmes

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

v2.11.1

Problem description

The latest version of pybind11 (v2.11.1) results in a warning when configuring a project (that includes pybind11) with CMake 3.27 that reads:

[cmake] CMake Warning (dev) at extern/pybind11/tools/FindPythonLibsNew.cmake:98 (find_package):
[cmake]   Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
[cmake]   are removed.  Run "cmake --help-policy CMP0148" for policy details.  Use
[cmake]   the cmake_policy command to set the policy and suppress this warning.
[cmake] 
[cmake] Call Stack (most recent call first):
[cmake]   extern/pybind11/tools/pybind11Tools.cmake:50 (find_package)
[cmake]   extern/pybind11/tools/pybind11Common.cmake:192 (include)
[cmake]   extern/pybind11/CMakeLists.txt:210 (include)
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.

This issue occurs in pybind11Tools.cmake, which is called from pybind11Common.cmake when it determines that it should be using the "classic" CMake mode that relies on the deprecated FindPythonInterp and FindPythonLibs modules as detailed here.

With the changes from #4719, CMake 3.27 should by default switch to the "new" pybind11ToolsNew.cmake, which relies on the more modern FindPython CMake modules.

However, this is not happening automatically. Furthermore, if one sets either of the following policies on the root project:

cmake_policy(VERSION 3.26)

or

cmake_policy(SET CMP0148 OLD)

the behavior is exactly the same, defaulting to the old pybind11Tools.cmake and printing the same warning about the CMP0148 policy.

Of course, looking at the logic in the code, one can easily figure out that setting

set(PYBIND11_FINDPYTHON ON)

on the root project will get around this issue, and it indeed does, so this is not a critical issue as there is a fairly simple workaround.

Having said that, pybind11 should be able to inherit the CMP0148 policy from the root project and, if one is not provided and the current CMake version is 3.27, it should automatically switch to the pybind11ToolsNew.cmake logic.

As far as I can tell, the reason why this is not happening automatically is that in the pybind11 root CMakeLists.txt, the CMake policy is being set as:

cmake_policy(VERSION 3.26)

for CMake versions >= 3.26.

Thus, when CMake tries to get the current policy value in pybind11Common.cmake with:

cmake_policy(GET CMP0148 _pybind11_missing_old_python)

the check always returns an empty string as policy CMP0148 is not defined if the CMake policy version is 3.26 (was added in 3.27).

This can easily be resolved by updating the root CMakeLists.txt (and possibly other CMakeLists.txt files in the repo) to:

cmake_policy(VERSION 3.27)

for CMake versions >= 3.27.

I can confirm that making the above change forces CMake 3.27 in my project to use the new logic from pybind11ToolsNew.cmake and the warning goes away, exactly the same way as if the PYBIND11_FINDPYTHON flag had been enabled.

I can make a PR with the changes, but I wanted to hear from @henryiii or @rwgk (from the original #4719 PR) if there was any reason why this change was not implemented at that time.

Thanks!

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions