Sistema:
Mac OSX 15.0
apple clang 16.0.0
cmake 3.30.1
Nas versões recentes do CMake, os procedimentos para encontrar o Python foram alterados.
Suponha que eu tenha a seguinte configuração de diretório
extern/vcpkg/ # cloned from github and bootstrapped
extern/pybind11/ # cloned from github
CMakeLists.txt
main.cpp
Com o seguinte conteúdo:
# CMakeLists.txt
# cmake_minimum_required(VERSION 3.26.0 FATAL_ERROR)
cmake_minimum_required(VERSION 3.27.0 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
# set (CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/extern/vcpkg/scripts/buildsystems/vcpkg.cmake)
set(PROJECT mytest)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
project(${PROJECT})
add_subdirectory(extern/pybind11)
pybind11_add_module(${PROJECT} main.cpp)
// main.cpp
#include <pybind11/pybind11.h>
#include <iostream>
void say_hello(){ std::cout << "Hello world!" << std::endl; }
PYBIND11_MODULE(mytest, m) { m.def("say_hello", &say_hello); }
Então eu construo com:
rm -rf build || true && cmake -H. -Bbuild && cmake --build build --target all
Com esta configuração, o módulo é construído e executado perfeitamente
Se eu descomentar a CAMKE_TOOLCHAIN_FILE
linha, a compilação falhará com a seguinte mensagem:
CMake Error at extern/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package):
By not providing "FindPythonInterp.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"PythonInterp", but CMake did not find one.
Could not find a package configuration file provided by "PythonInterp"
(requested version 3.8) with any of the following names:
PythonInterpConfig.cmake
pythoninterp-config.cmake
Add the installation prefix of "PythonInterp" to CMAKE_PREFIX_PATH or set
"PythonInterp_DIR" to a directory containing one of the above files. If
"PythonInterp" provides a separate development package or SDK, be sure it
has been installed.
Call Stack (most recent call first):
extern/pybind11/tools/FindPythonLibsNew.cmake:114 (find_package)
extern/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package)
extern/pybind11/tools/pybind11Tools.cmake:44 (find_package)
extern/pybind11/tools/pybind11Common.cmake:200 (include)
extern/pybind11/CMakeLists.txt:229 (include)
Se eu reverter a versão mínima necessária do cmake, as configurações de política serão alteradas e a compilação será bem-sucedida novamente, mas com o seguinte aviso do pybind:
CMake Warning (dev) at extern/pybind11/tools/FindPythonLibsNew.cmake:101 (message):
Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
are removed. Run "cmake --help-policy CMP0148" for policy details. Use
the cmake_policy command to set the policy and suppress this warning, or
preferably upgrade to using FindPython, either by calling it explicitly
before pybind11, or by setting PYBIND11_FINDPYTHON ON before pybind11.
Call Stack (most recent call first):
extern/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package)
extern/pybind11/tools/pybind11Tools.cmake:44 (find_package)
extern/pybind11/tools/pybind11Common.cmake:200 (include)
extern/pybind11/CMakeLists.txt:229 (include)
Eu realmente não sei o que vcpkg vai afetar as configurações aqui, então se alguém souber, seria útil entender. Além disso, estou tentando entender se há algo que eu possa fazer para corrigir esse problema para que eu possa usar as configurações de política atualizadas
Pybind11 faz um jogo sujo com a política CMP0148, veja por exemplo o código em CMakeLists.txt:15 . Provavelmente, vcpkg modifica essa política por seus próprios motivos, e essa modificação confunde Pybind11 (de acordo com seu log de erros, Pybind11 tenta usar
FindPythonInterp.cmake
um script que está marcado como removido no CMake 3.27).Como seu requisito mínimo para a versão do CMake é acima de 3.12, onde
FindPython.cmake
o script foi adicionado, você pode dizer com segurança ao Pybind11 para usar incondicionalmente esse script em vez dos obsoletos. Isso pode ser alcançado definindoantes de descer para o código Pybind11. (É isso que a última mensagem de aviso sugere.)