|
| 1 | +# Copyright (c) 2024 ARM Limited. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# CMake script to find the Python interpreter and either install or find |
| 5 | +# Mbed's dependencies. |
| 6 | + |
| 7 | +option(MBED_CREATE_PYTHON_VENV "If true, Mbed OS will create its own virtual environment (venv) and install its Python packages there. This removes the need to manually install Python packages." TRUE) |
| 8 | + |
| 9 | +if(MBED_CREATE_PYTHON_VENV) |
| 10 | + # Use the venv. |
| 11 | + |
| 12 | + # Note: venv is stored in the source directory as it can be shared between all the build directories |
| 13 | + # (not target specific) |
| 14 | + set(MBED_VENV_LOCATION ${CMAKE_SOURCE_DIR}/mbed-os/venv) |
| 15 | + set(VENV_STAMP_FILE ${MBED_VENV_LOCATION}/mbed-venv.stamp) |
| 16 | + set(MBED_REQUIREMENTS_TXT_LOCATION "${CMAKE_CURRENT_LIST_DIR}/../requirements.txt") |
| 17 | + |
| 18 | + # Find Python3, using the venv if it already exists |
| 19 | + set (ENV{VIRTUAL_ENV} ${MBED_VENV_LOCATION}) |
| 20 | + set (Python3_FIND_VIRTUALENV FIRST) |
| 21 | + find_package(Python3 REQUIRED COMPONENTS Interpreter) |
| 22 | + include(CheckPythonPackage) |
| 23 | + |
| 24 | + set(NEED_TO_CREATE_VENV FALSE) |
| 25 | + set(NEED_TO_INSTALL_PACKAGES FALSE) |
| 26 | + if(NOT EXISTS "${VENV_STAMP_FILE}") |
| 27 | + set(NEED_TO_CREATE_VENV TRUE) |
| 28 | + set(NEED_TO_INSTALL_PACKAGES TRUE) |
| 29 | + elseif("${MBED_REQUIREMENTS_TXT_LOCATION}" IS_NEWER_THAN "${VENV_STAMP_FILE}") |
| 30 | + set(NEED_TO_INSTALL_PACKAGES TRUE) |
| 31 | + endif() |
| 32 | + |
| 33 | + if(NEED_TO_CREATE_VENV) |
| 34 | + # Create venv. |
| 35 | + # Using approach from here: https://discourse.cmake.org/t/possible-to-create-a-python-virtual-env-from-cmake-and-then-find-it-with-findpython3/1132/2 |
| 36 | + message(STATUS "Mbed: Creating virtual environment with Python interpreter ${Python3_EXECUTABLE}") |
| 37 | + execute_process( |
| 38 | + COMMAND ${Python3_EXECUTABLE} -m venv ${MBED_VENV_LOCATION} |
| 39 | + COMMAND_ERROR_IS_FATAL ANY |
| 40 | + ) |
| 41 | + |
| 42 | + ## Reset FindPython3 cache variables so it will run again |
| 43 | + unset(Python3_EXECUTABLE) |
| 44 | + unset(_Python3_EXECUTABLE CACHE) |
| 45 | + unset(_Python3_INTERPRETER_PROPERTIES CACHE) |
| 46 | + unset(_Python3_INTERPRETER_SIGNATURE CACHE) |
| 47 | + ## Launch a new search for Python3 |
| 48 | + find_package (Python3 REQUIRED COMPONENTS Interpreter) |
| 49 | + endif() |
| 50 | + |
| 51 | + if(NEED_TO_INSTALL_PACKAGES) |
| 52 | + message(STATUS "Mbed: Installing Python requirements for Mbed into venv") |
| 53 | + # Upgrade pip first in case it needs an upgrade, to prevent a warning being printed |
| 54 | + execute_process( |
| 55 | + COMMAND ${Python3_EXECUTABLE} -m pip install --upgrade pip |
| 56 | + COMMAND_ERROR_IS_FATAL ANY |
| 57 | + ) |
| 58 | + execute_process( |
| 59 | + COMMAND ${Python3_EXECUTABLE} -m pip install -r ${MBED_REQUIREMENTS_TXT_LOCATION} |
| 60 | + COMMAND_ERROR_IS_FATAL ANY |
| 61 | + ) |
| 62 | + |
| 63 | + message(STATUS "Mbed: venv created successfully") |
| 64 | + file(TOUCH ${VENV_STAMP_FILE}) |
| 65 | + endif() |
| 66 | + |
| 67 | + # We always have the memap deps with the venv |
| 68 | + set(HAVE_MEMAP_DEPS TRUE) |
| 69 | + |
| 70 | +else() |
| 71 | + |
| 72 | + find_package(Python3 REQUIRED COMPONENTS Interpreter) |
| 73 | + include(CheckPythonPackage) |
| 74 | + |
| 75 | + |
| 76 | + # Check python packages |
| 77 | + set(PYTHON_PACKAGES_TO_CHECK intelhex prettytable future jinja2) |
| 78 | + foreach(PACKAGE_NAME ${PYTHON_PACKAGES_TO_CHECK}) |
| 79 | + string(TOUPPER ${PACKAGE_NAME} PACKAGE_NAME_UCASE) # Ucase name needed for CMake variable |
| 80 | + string(TOLOWER ${PACKAGE_NAME} PACKAGE_NAME_LCASE) # Lcase name needed for import statement |
| 81 | + |
| 82 | + check_python_package(${PACKAGE_NAME_LCASE} HAVE_PYTHON_${PACKAGE_NAME_UCASE}) |
| 83 | + if(NOT HAVE_PYTHON_${PACKAGE_NAME_UCASE}) |
| 84 | + message(WARNING "Missing Python dependency ${PACKAGE_NAME}") |
| 85 | + endif() |
| 86 | + endforeach() |
| 87 | + |
| 88 | + # Check deps for memap |
| 89 | + if(Python3_FOUND AND HAVE_PYTHON_INTELHEX AND HAVE_PYTHON_PRETTYTABLE) |
| 90 | + set(HAVE_MEMAP_DEPS TRUE) |
| 91 | + else() |
| 92 | + set(HAVE_MEMAP_DEPS FALSE) |
| 93 | + message(STATUS "Missing Python dependencies (at least one of: python3, intelhex, prettytable) so the memory map cannot be printed") |
| 94 | + endif() |
| 95 | + |
| 96 | +endif() |
0 commit comments