yang.li | d6fe624 | 2022-01-13 14:44:24 +0800 | [diff] [blame] | 1 | # Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 2 | |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
bin.chen | 1a426d3 | 2021-10-13 10:52:36 +0800 | [diff] [blame] | 5 | # 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.huang | d414ef0 | 2024-06-25 10:23:43 +0800 | [diff] [blame] | 12 | if(WIN32) |
bin.chen | 1a426d3 | 2021-10-13 10:52:36 +0800 | [diff] [blame] | 13 | set(ENV{PYTHONIOENCODING} "utf-8") |
| 14 | endif() |
| 15 | |
| 16 | set(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. |
| 20 | foreach(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() |
| 46 | endforeach() |
| 47 | |
| 48 | find_package(Python3 ${PYTHON_MINIMUM_REQUIRED} REQUIRED ${PYTHON_EXACT}) |
xiaohu.huang | d414ef0 | 2024-06-25 10:23:43 +0800 | [diff] [blame] | 49 | if(Python3_FOUND) |
| 50 | set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE}) |
| 51 | else() |
| 52 | message(FATAL_ERROR "Python not found. Please install Python.") |
| 53 | endif() |