CMake find_package for Python3 fails upon finding Python 2

Viewed 599

While moving to CMake 3.17.3 (from 3.13.3) we have stumbled on a problem with Python. In the root CMakeLists.txt we now have:

find_package(Python3 REQUIRED COMPONENTS Interpreter)

However, this fails with the following:

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)

while the Python found (/build/ltesdkroot/Platforms/LINUX/MB_PS_LFS_REL_2020_07_0068/sdk/bld-tools/x86_64-pc-linux-gnu/bin/python) reports version Python 2.7.15+.

Searching and experimenting I found out that adding:

set(Python3_FIND_STRATEGY VERSION)

before the find_package helps. With this added Python is found:

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

Now, the problem is that FindPython3 documentation states the following:

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.

So, the LOCATION should work OK based on my understanding of the description. It should ignore the /build/ltesdkroot/Platforms/LINUX/MB_PS_LFS_REL_2020_07_0068/sdk/bld-tools/x86_64-pc-linux-gnu/bin/python since it doesn't satisfy version constraints (it is Python 2 rather than Python 3) and should continue to find the /opt/python/x86_64/3.6.0/bin-wrapped/python3.6 which does satisfy the constraints. (For completeness, I tried adding explicit 3.6 version in find_package and it didn't change anything.)

Another issue is whether I should set Python3_FIND_STRATEGY or use CMP0094 directly. The CMP0094 documentation has a somewhat scary warning:

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

So, I'm not sure which approach (setting Python3_FIND_STRATEGY or activating OLD for CMP0094) is the desired (stable) approach.

0 Answers
Related