blob: 91c7bfe2bde043972f2d40da04603068e7117e96 [file] [log] [blame]
yang.lid6fe6242022-01-13 14:44:24 +08001# Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved.
2
3# SPDX-License-Identifier: MIT
4
bin.chen1a426d32021-10-13 10:52:36 +08005# SPDX-License-Identifier: Apache-2.0
6
7# On Windows, instruct Python to output UTF-8 even when not
8# interacting with a terminal. This is required since Python scripts
9# are invoked by CMake code and, on Windows, standard I/O encoding defaults
10# to the current code page if not connected to a terminal, which is often
11# not what we want.
xiaohu.huangd414ef02024-06-25 10:23:43 +080012if(WIN32)
bin.chen1a426d32021-10-13 10:52:36 +080013 set(ENV{PYTHONIOENCODING} "utf-8")
14endif()
15
16set(PYTHON_MINIMUM_REQUIRED 3.6)
17
18# We are using foreach here, instead of find_program(PYTHON_EXECUTABLE_SYSTEM_DEFAULT "python" "python3")
19# cause just using find_program directly could result in a python2.7 as python, and not finding a valid python3.
20foreach(PYTHON_PREFER ${PYTHON_PREFER} ${WEST_PYTHON} "python" "python3")
21 find_program(PYTHON_PREFER_EXECUTABLE ${PYTHON_PREFER})
22 if(PYTHON_PREFER_EXECUTABLE)
23 execute_process (COMMAND "${PYTHON_PREFER_EXECUTABLE}" -c
24 "import sys; sys.stdout.write('.'.join([str(x) for x in sys.version_info[:2]]))"
25 RESULT_VARIABLE result
26 OUTPUT_VARIABLE version
27 ERROR_QUIET
28 OUTPUT_STRIP_TRAILING_WHITESPACE)
29
30 if(version VERSION_LESS PYTHON_MINIMUM_REQUIRED)
31 set(PYTHON_PREFER_EXECUTABLE "PYTHON_PREFER_EXECUTABLE-NOTFOUND")
32 else()
33 set(PYTHON_MINIMUM_REQUIRED ${version})
34 set(PYTHON_EXACT EXACT)
35 # Python3_ROOT_DIR ensures that location will be preferred by FindPython3.
36 # On Linux, this has no impact as it will usually be /usr/bin
37 # but on Windows it solve issues when both 32 and 64 bit versions are
38 # installed, as version is not enough and FindPython3 might pick the
39 # version not on %PATH%. Setting Python3_ROOT_DIR ensures we are using
40 # the version we just tested.
41 get_filename_component(PYTHON_PATH ${PYTHON_PREFER_EXECUTABLE} DIRECTORY)
42 set(Python3_ROOT_DIR ${PYTHON_PATH})
43 break()
44 endif()
45 endif()
46endforeach()
47
48find_package(Python3 ${PYTHON_MINIMUM_REQUIRED} REQUIRED ${PYTHON_EXACT})
xiaohu.huangd414ef02024-06-25 10:23:43 +080049if(Python3_FOUND)
50 set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
51else()
52 message(FATAL_ERROR "Python not found. Please install Python.")
53endif()