Python3的CMake find_包在查找Python2时失败

2024-10-02 14:27:56 发布

您现在位置:Python中文网/ 问答频道 /正文

在迁移到CMake 3.17.3(从3.13.3)时,我们偶然发现了Python的一个问题。在根目录CMakeLists.txt中,我们现在有:

find_package(Python3 REQUIRED COMPONENTS Interpreter)

但是,这在以下情况下失败:

CMake Error at /home/abadura/cmake/cmake-3.17.3-Linux-x86_64/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:164 (message):
  Could NOT find Python3 (missing: Python3_EXECUTABLE Interpreter)

      Reason given by package: 
          Interpreter: Wrong major version for the interpreter "/build/ltesdkroot/Platforms/LINUX/MB_PS_LFS_REL_2020_07_0068/sdk/bld-tools/x86_64-pc-linux-gnu/bin/python"

Call Stack (most recent call first):
  /home/abadura/cmake/cmake-3.17.3-Linux-x86_64/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:445 (_FPHSA_FAILURE_MESSAGE)
  /home/abadura/cmake/cmake-3.17.3-Linux-x86_64/share/cmake-3.17/Modules/FindPython/Support.cmake:2437 (find_package_handle_standard_args)
  /home/abadura/cmake/cmake-3.17.3-Linux-x86_64/share/cmake-3.17/Modules/FindPython3.cmake:309 (include)
  CMakeLists.txt:20 (find_package)

而Python发现(/build/ltesdkroot/Platforms/LINUX/MB_PS_LFS_REL_2020_07_0068/sdk/bld-tools/x86_64-pc-linux-gnu/bin/python)报告版本Python 2.7.15+

通过搜索和实验,我发现添加:

set(Python3_FIND_STRATEGY VERSION)

find_package帮助之前。通过此添加的Python,可以找到:

-- Found Python3: /opt/python/x86_64/3.6.0/bin-wrapped/python3.6 (found version "3.6.0") found components: Interpreter 

现在,问题是FindPython3 documentation陈述了以下内容:

Python3_FIND_STRATEGY

This variable defines how lookup will be done. The Python3_FIND_STRATEGY variable can be set to one of the following:

  • VERSION: Try to find the most recent version in all specified locations. This is the default if policy CMP0094 is undefined or set to OLD.
  • LOCATION: Stops lookup as soon as a version satisfying version constraints is founded. This is the default if policy CMP0094 is set to NEW.

因此,基于我对描述的理解,LOCATION应该可以正常工作。它应该忽略/build/ltesdkroot/Platforms/LINUX/MB_PS_LFS_REL_2020_07_0068/sdk/bld-tools/x86_64-pc-linux-gnu/bin/python,因为它不满足版本约束(它是python2而不是pytho3),并且应该继续查找满足约束的/opt/python/x86_64/3.6.0/bin-wrapped/python3.6。(为了完整性,我尝试在find_package中添加显式的3.6版本,但它没有改变任何东西。)

另一个问题是我应该设置Python3_FIND_STRATEGY还是直接使用CMP0094。{a2}文档有一个有点可怕的警告:

Note The OLD behavior of a policy is deprecated by definition and may be removed in a future version of CMake.

因此,我不确定哪种方法(为CMP0094设置Python3_FIND_STRATEGY或激活OLD)是理想的(稳定的)方法


Tags: thecmakepackagehomebinisversionlinux