66
77option (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 )
88
9+ get_filename_component (MBED_CE_TOOLS_BASE_DIR "${CMAKE_CURRENT_LIST_DIR} /.."  ABSOLUTE )
10+ 
911if (MBED_CREATE_PYTHON_VENV)
1012    # Use the venv. 
1113
1214    # Note: venv is stored in the source directory as it can be shared between all the build directories 
1315    # (not target specific) 
1416    set (MBED_VENV_LOCATION ${MBED_SOURCE_DIR} /venv)
1517    set (VENV_STAMP_FILE ${MBED_VENV_LOCATION} /mbed-venv.stamp)
16-     set (MBED_REQUIREMENTS_TXT_LOCATION  "${CMAKE_CURRENT_LIST_DIR}  /../requirements.txt " )
18+     set (MBED_PYPROJECT_TOML_LOCATION  "${MBED_CE_TOOLS_BASE_DIR}  /pyproject.toml " )
1719
18-     # Make it so modifying requirements.txt  will trigger a reconfigure 
19-     set_property (DIRECTORY  APPEND  PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBED_REQUIREMENTS_TXT_LOCATION } )
20+     # Make it so modifying pyproject.toml  will trigger a reconfigure 
21+     set_property (DIRECTORY  APPEND  PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBED_PYPROJECT_TOML_LOCATION } )
2022
21-     # Find Python3, using  the venv if it  already exists  
22-     set   (ENV{VIRTUAL_ENV} ${MBED_VENV_LOCATION} )
23-     set   (Python3_FIND_VIRTUALENV FIRST)
24-     find_package (Python3 REQUIRED  COMPONENTS Interpreter)
23+     # Find Python3 (this will get  the one in the  venv if we  already found it)  
24+     set (ENV{VIRTUAL_ENV} ${MBED_VENV_LOCATION} )
25+     set (Python3_FIND_VIRTUALENV FIRST)
26+     find_package (Python3 COMPONENTS Interpreter)
2527    include (CheckPythonPackage)
2628
2729    set (NEED_TO_CREATE_VENV FALSE )
2830    set (NEED_TO_INSTALL_PACKAGES FALSE )
29-     if (NOT  EXISTS  "${VENV_STAMP_FILE} " )
31+ 
32+     # Special situation: if we have a cached interpreter location in the venv dir, but Python could be found, 
33+     # this means that the venv was deleted or symlinks to a missing python install location. 
34+     # So, use the system python and recreate it. 
35+     if ("${Python3_EXECUTABLE} "  MATCHES  "${MBED_VENV_LOCATION} "  AND  NOT  Python3_FOUND)
36+         message (STATUS  "Python venv deleted or unusable. Recreating using system Python..." )
37+ 
38+         # Launch a new search for Python3 
39+         unset (Python3_EXECUTABLE)
40+         unset (_Python3_EXECUTABLE CACHE )
41+         unset (_Python3_INTERPRETER_PROPERTIES CACHE )
42+         unset (_Python3_INTERPRETER_SIGNATURE CACHE )
43+         set  (Python3_FIND_VIRTUALENV STANDARD)
44+         find_package (Python3 REQUIRED COMPONENTS Interpreter)
45+ 
46+         set (NEED_TO_CREATE_VENV TRUE )
47+         set (NEED_TO_INSTALL_PACKAGES TRUE )
48+     elseif (NOT  EXISTS  "${VENV_STAMP_FILE} " )
3049        set (NEED_TO_CREATE_VENV TRUE )
3150        set (NEED_TO_INSTALL_PACKAGES TRUE )
32-     elseif ("${MBED_REQUIREMENTS_TXT_LOCATION} "  IS_NEWER_THAN  "${VENV_STAMP_FILE} " )
51+     elseif (NOT  ("${Python3_EXECUTABLE} "  MATCHES  "${MBED_VENV_LOCATION} " ))
52+         # Alternately if we think we have the venv but FindPython didn't use it, that likely means it's 
53+         # missing or corrupted and we need to recreate it 
54+         set (NEED_TO_CREATE_VENV TRUE )
55+         set (NEED_TO_INSTALL_PACKAGES TRUE )
56+     elseif ("${MBED_PYPROJECT_TOML_LOCATION} "  IS_NEWER_THAN  "${VENV_STAMP_FILE} " )
3357        set (NEED_TO_INSTALL_PACKAGES TRUE )
3458    endif ()
3559
@@ -47,7 +71,8 @@ if(MBED_CREATE_PYTHON_VENV)
4771        unset (_Python3_EXECUTABLE CACHE )
4872        unset (_Python3_INTERPRETER_PROPERTIES CACHE )
4973        unset (_Python3_INTERPRETER_SIGNATURE CACHE )
50-         ## Launch a new search for Python3 
74+ 
75+         ## Launch a new search for Python3 in the venv 
5176        find_package  (Python3 REQUIRED COMPONENTS Interpreter)
5277    endif ()
5378
@@ -59,7 +84,7 @@ if(MBED_CREATE_PYTHON_VENV)
5984            COMMAND_ERROR_IS_FATAL ANY
6085        )
6186        execute_process (
62-             COMMAND  ${Python3_EXECUTABLE}  -m pip install  -r  ${MBED_REQUIREMENTS_TXT_LOCATION }
87+             COMMAND  ${Python3_EXECUTABLE}  -m pip install  ${MBED_CE_TOOLS_BASE_DIR }
6388            COMMAND_ERROR_IS_FATAL ANY
6489        )
6590
@@ -75,25 +100,12 @@ else()
75100    find_package (Python3 REQUIRED COMPONENTS Interpreter)
76101    include (CheckPythonPackage)
77102
103+     # The cmsis_mcu_descr module was written from scratch by Mbed CE. 
104+     # So, this check will ensure that the user has installed the Mbed CE version of mbed_tools 
105+     # and not the PyPI version (which we cannot do anything with because it's owned by ARM) 
106+     check_python_package(mbed_tools.cli.cmsis_mcu_descr HAVE_MBED_CE_TOOLS)
78107
79-     # Check python packages 
80-     set (PYTHON_PACKAGES_TO_CHECK intelhex prettytable future jinja2)
81-     foreach (PACKAGE_NAME ${PYTHON_PACKAGES_TO_CHECK} )
82-         string (TOUPPER ${PACKAGE_NAME}  PACKAGE_NAME_UCASE) # Ucase name needed for CMake variable 
83-         string (TOLOWER ${PACKAGE_NAME}  PACKAGE_NAME_LCASE) # Lcase name needed for import statement 
84- 
85-         check_python_package(${PACKAGE_NAME_LCASE}  HAVE_PYTHON_${PACKAGE_NAME_UCASE} )
86-         if (NOT  HAVE_PYTHON_${PACKAGE_NAME_UCASE} )
87-             message (WARNING "Missing Python dependency ${PACKAGE_NAME} " )
88-         endif ()
89-     endforeach ()
90- 
91-     # Check deps for memap 
92-     if (Python3_FOUND AND  HAVE_PYTHON_INTELHEX AND  HAVE_PYTHON_PRETTYTABLE)
93-         set (HAVE_MEMAP_DEPS TRUE )
94-     else ()
95-         set (HAVE_MEMAP_DEPS FALSE )
96-         message (STATUS  "Missing Python dependencies (at least one of: python3, intelhex, prettytable) so the memory map cannot be printed" )
108+     if (NOT  HAVE_MBED_CE_TOOLS)
109+         message (FATAL_ERROR "Did not detect the Mbed CE Python tools installed into the python interpreter ${Python3_EXECUTABLE} . Install them with a command like: ${Python3_EXECUTABLE}  -m pip install ${MBED_CE_TOOLS_BASE_DIR} " )
97110    endif ()
98- 
99111endif ()
0 commit comments