1313import json
1414import pathlib
1515import sys
16+ import typing
1617from pathlib import Path
1718from typing import TYPE_CHECKING , Any
1819
19- import click
20+ from click . parser import split_arg_string
2021from platformio .proc import exec_command
2122from SCons .Script import ARGUMENTS , DefaultEnvironment
2223
2930
3031# Directories
3132FRAMEWORK_DIR = Path (platform .get_package_dir ("framework-mbed-ce" ))
32- BUILD_DIR = Path (env .subst ("$BUILD_DIR" ))
33- PROJECT_DIR = Path (env .subst ("$PROJECT_DIR" ))
34- PROJECT_SRC_DIR = Path (env .subst ("$PROJECT_SRC_DIR" ))
33+ BUILD_DIR = Path (typing . cast ( str , env .subst ("$BUILD_DIR" ) ))
34+ PROJECT_DIR = Path (typing . cast ( str , env .subst ("$PROJECT_DIR" ) ))
35+ PROJECT_SRC_DIR = Path (typing . cast ( str , env .subst ("$PROJECT_SRC_DIR" ) ))
3536CMAKE_API_DIR = BUILD_DIR / ".cmake" / "api" / "v1"
3637CMAKE_API_QUERY_DIR = CMAKE_API_DIR / "query"
3738CMAKE_API_REPLY_DIR = CMAKE_API_DIR / "reply"
6162
6263
6364def get_mbed_target () -> str :
64- board_type = env .subst ("$BOARD" )
65+ board_type = typing . cast ( str , env .subst ("$BOARD" ) )
6566 variant = PIO_VARIANT_TO_MBED_TARGET [board_type ] if board_type in PIO_VARIANT_TO_MBED_TARGET else board_type .upper ()
6667 return board .get ("build.mbed_variant" , variant )
6768
@@ -72,7 +73,7 @@ def is_proper_mbed_ce_project() -> bool:
7273
7374def create_default_project_files () -> None :
7475 if not PROJECT_MBED_APP_JSON5 .exists ():
75- PROJECT_MBED_APP_JSON5 .write_text (
76+ _ = PROJECT_MBED_APP_JSON5 .write_text (
7677 """
7778{
7879 "target_overrides": {
@@ -111,9 +112,12 @@ def is_cmake_reconfigure_required() -> bool:
111112
112113def run_tool (command_and_args : list [str ] | None = None ) -> None :
113114 result = exec_command (command_and_args )
114- if result ["returncode" ] != 0 :
115- sys .stderr .write (result ["out" ] + "\n " )
116- sys .stderr .write (result ["err" ] + "\n " )
115+
116+ # Note: Pyright seems to think that this will always fail due to missing type annotations
117+ # for exec_command(), but I believe the actual code is fine.
118+ if result ["returncode" ] != 0 : # pyright: ignore[reportUnnecessaryComparison]
119+ print (result ["out" ], file = sys .stderr )
120+ print (result ["err" ], file = sys .stderr )
117121 env .Exit (1 )
118122
119123 if int (ARGUMENTS .get ("PIOVERBOSE" , 0 )):
@@ -139,7 +143,7 @@ def get_cmake_code_model(cmake_args: list) -> dict:
139143 (BUILD_DIR / "CMakeCache.txt" ).touch ()
140144
141145 if not CMAKE_API_REPLY_DIR .is_dir () or not any (CMAKE_API_REPLY_DIR .iterdir ()):
142- sys . stderr . write ("Error: Couldn't find CMake API response file\n " )
146+ print ("Error: Couldn't find CMake API response file" , file = sys . stderr )
143147 env .Exit (1 )
144148
145149 codemodel = {}
@@ -153,10 +157,10 @@ def get_cmake_code_model(cmake_args: list) -> dict:
153157
154158
155159def get_target_config (project_configs : dict , target_index : int ) -> dict [str , Any ]:
156- target_json = project_configs . get ( "targets" ) [target_index ].get ("jsonFile" , "" )
160+ target_json = project_configs [ "targets" ] [target_index ].get ("jsonFile" , "" )
157161 target_config_file = CMAKE_API_REPLY_DIR / target_json
158162 if not target_config_file .is_file ():
159- sys . stderr . write (f"Error: Couldn't find target config { target_json } \n " )
163+ print (f"Error: Couldn't find target config { target_json } " , file = sys . stderr )
160164 env .Exit (1 )
161165
162166 with target_config_file .open (encoding = "utf-8" ) as fp :
@@ -165,7 +169,7 @@ def get_target_config(project_configs: dict, target_index: int) -> dict[str, Any
165169
166170def load_target_configurations (cmake_codemodel : dict ) -> dict :
167171 configs = {}
168- project_configs = cmake_codemodel . get ( "configurations" ) [0 ]
172+ project_configs = cmake_codemodel [ "configurations" ] [0 ]
169173 for config in project_configs .get ("projects" , []):
170174 for target_index in config .get ("targetIndexes" , []):
171175 target_config = get_target_config (project_configs , target_index )
@@ -242,12 +246,12 @@ def get_app_defines(app_config: dict) -> list[tuple[str, str]]:
242246 "-DMBED_TARGET=" + get_mbed_target (),
243247 "-DUPLOAD_METHOD=NONE" , # Disable Mbed CE upload method system as PlatformIO has its own
244248 # Add in any extra options from higher layers
245- * click . parser . split_arg_string (board .get ("build.cmake_extra_args" , "" )),
249+ * split_arg_string (board .get ("build.cmake_extra_args" , "" )),
246250 ]
247251)
248252
249253if not project_codemodel :
250- sys . stderr . write ("Error: Couldn't find code model generated by CMake\n " )
254+ print ("Error: Couldn't find code model generated by CMake" , file = sys . stderr )
251255 env .Exit (1 )
252256
253257target_configs = load_target_configurations (project_codemodel )
@@ -270,7 +274,7 @@ def get_app_defines(app_config: dict) -> list[tuple[str, str]]:
270274# within this archive.
271275mbed_ce_lib_path = pathlib .Path ("$BUILD_DIR" ) / "mbed-os" / "libmbed-os.a"
272276link_args = ["-Wl,--whole-archive" , '"' + str (mbed_ce_lib_path ) + '"' , "-Wl,--no-whole-archive" ]
273- env .Depends ("$BUILD_DIR/$PROGNAME$PROGSUFFIX" , str (mbed_ce_lib_path ))
277+ _ = env .Depends ("$BUILD_DIR/$PROGNAME$PROGSUFFIX" , str (mbed_ce_lib_path ))
274278
275279# Get other linker flags from Mbed. We want these to appear after the application objects and Mbed libraries
276280# because they contain the C/C++ library link flags.
@@ -297,5 +301,5 @@ def get_app_defines(app_config: dict) -> list[tuple[str, str]]:
297301# Note that this seems to execute CMake, causing the code model query to be re-done.
298302# So, we have to do this after we are done using the results of said query.
299303project_ld_script = generate_project_ld_script ()
300- env .Depends ("$BUILD_DIR/$PROGNAME$PROGSUFFIX" , str (project_ld_script ))
304+ _ = env .Depends ("$BUILD_DIR/$PROGNAME$PROGSUFFIX" , str (project_ld_script ))
301305env .Append (LDSCRIPT_PATH = str (project_ld_script ))
0 commit comments