Skip to content

Commit 6db773c

Browse files
Install pyocd and cysecuretools lazily
1 parent 92b9a76 commit 6db773c

File tree

5 files changed

+48
-14
lines changed

5 files changed

+48
-14
lines changed

targets/TARGET_Cypress/scripts/mbed_set_post_build_cypress.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
include(mbed_target_functions)
55

6+
# Make sure we have the python packages we need
7+
mbed_check_or_install_python_package(HAVE_PYTHON_CYSECURETOOLS cysecuretools "cysecuretools~=6.0")
8+
if(NOT HAVE_PYTHON_CYSECURETOOLS)
9+
message(FATAL_ERROR "Python package required for signing not found.")
10+
endif()
11+
612
#
713
# Merge Cortex-M4 HEX and a Cortex-M0 HEX.
814
#
@@ -63,8 +69,6 @@ macro(mbed_post_build_psoc6_sign_image
6369
)
6470
if("${cypress_psoc6_target}" STREQUAL "${MBED_TARGET}")
6571
function(mbed_post_build_function target)
66-
find_package(Python3)
67-
6872
set(post_build_command
6973
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PSOC6.py
7074
sign

targets/upload_method_cfg/SFE_ARTEMIS.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# 1. This board does not have an onboard debugger. You must use an external debugger, e.g. a PicoProbe
77
# or J-Link, if you wish to debug code.
88
# 2. Support for this device exists in PyOCD main branch but has not been released yet (as of Jun 2025).
9-
# To use PyOCD, you need to manually install it from the git repository by running (inside the Mbed OS venv):
9+
# This version will be used automatically by Mbed if the python venv is enabled. If not, you need to install it via:
1010
# pip install git+https://github.com/pyocd/pyOCD.git
1111

1212
set(UPLOAD_METHOD_DEFAULT NONE)

tools/cmake/mbed_python_interpreter.cmake

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# CMake script to find the Python interpreter and either install or find
55
# Mbed's dependencies.
66

7+
include(CheckPythonPackage)
8+
79
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)
810

911
get_filename_component(MBED_CE_TOOLS_BASE_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
@@ -105,7 +107,6 @@ if(MBED_CREATE_PYTHON_VENV)
105107
else()
106108

107109
find_package(Python3 REQUIRED COMPONENTS Interpreter)
108-
include(CheckPythonPackage)
109110

110111
# The cmsis_mcu_descr module was written from scratch by Mbed CE.
111112
# So, this check will ensure that the user has installed the Mbed CE version of mbed_tools
@@ -134,7 +135,34 @@ find_program(mbedhtrun
134135
REQUIRED)
135136

136137
find_program(memap
137-
NAMES memap
138-
HINTS ${PYTHON_SCRIPT_LOC_HINTS}
139-
DOC "Path to memap Python script."
140-
REQUIRED)
138+
NAMES memap
139+
HINTS ${PYTHON_SCRIPT_LOC_HINTS}
140+
DOC "Path to memap Python script."
141+
REQUIRED)
142+
143+
#
144+
# Utility function to check for a Python package with the given import name.
145+
# If the package is not found and the Mbed venv is in use,
146+
# then the package will be installed by passing PACKAGE_INSTALL_CONSTRAINT to Pip.
147+
# If the install fails or the venv is not being used, FOUND_VAR will be set to false.
148+
#
149+
function(mbed_check_or_install_python_package FOUND_VAR PACKAGE_IMPORT_NAME PACKAGE_INSTALL_CONSTRAINT)
150+
check_python_package(${PACKAGE_IMPORT_NAME} ${FOUND_VAR})
151+
152+
if(NOT ${FOUND_VAR})
153+
# If we are using the Mbed venv, we can install the package automatically.
154+
if(MBED_CREATE_PYTHON_VENV)
155+
message(STATUS "Mbed: Installing ${PACKAGE_INSTALL_CONSTRAINT} into Mbed's Python virtualenv")
156+
execute_process(
157+
COMMAND ${Python3_EXECUTABLE} -m pip install ${PACKAGE_INSTALL_CONSTRAINT}
158+
RESULT_VARIABLE PIP_INSTALL_RESULT
159+
)
160+
if(NOT PIP_INSTALL_RESULT EQUAL 0)
161+
message(WARNING "Installation of ${PACKAGE_INSTALL_CONSTRAINT} via pip failed.")
162+
else()
163+
# Redo the check to confirm it's installed
164+
check_python_package(${PACKAGE_IMPORT_NAME} ${FOUND_VAR})
165+
endif()
166+
endif()
167+
endif()
168+
endfunction(mbed_check_or_install_python_package)

tools/cmake/upload_methods/UploadMethodPYOCD.cmake

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99

1010
set(UPLOAD_SUPPORTS_DEBUG TRUE)
1111

12-
### Check if upload method can be enabled on this machine
13-
include(CheckPythonPackage)
14-
check_python_package(pyocd HAVE_PYOCD)
15-
set(UPLOAD_PYOCD_FOUND ${HAVE_PYOCD})
12+
### Find PyOCD package
13+
14+
# Use the Git version so that we get Ambiq Apollo3 support (as that was not included in the latest release
15+
# before PyOCD development stopped, as of Jun 2025)
16+
mbed_check_or_install_python_package(HAVE_PYTHON_PYOCD pyocd git+https://github.com/pyocd/pyOCD.git)
17+
18+
set(UPLOAD_PYOCD_FOUND ${HAVE_PYTHON_PYOCD})
1619

1720
### Function to generate upload target
1821
set(PYOCD_PROBE_ARGS "" CACHE INTERNAL "" FORCE)

tools/pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ dependencies = [
5454

5555
# needed for signing secure images --------------------------------------------------------
5656
"cryptography",
57-
"cbor",
58-
"cysecuretools~=6.0",
57+
"cbor"
5958
]
6059

6160
[project.optional-dependencies]

0 commit comments

Comments
 (0)