tools.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import asyncio
  4. import json
  5. import os
  6. import re
  7. import subprocess
  8. import sys
  9. from asyncio.subprocess import Process
  10. from io import open
  11. from types import FunctionType
  12. from typing import Any, Dict, Generator, List, Match, Optional, TextIO, Tuple, Union
  13. import click
  14. import yaml
  15. from idf_py_actions.errors import NoSerialPortFoundError
  16. from .constants import GENERATORS
  17. from .errors import FatalError
  18. # Name of the program, normally 'idf.py'.
  19. # Can be overridden from idf.bat using IDF_PY_PROGRAM_NAME
  20. PROG = os.getenv('IDF_PY_PROGRAM_NAME', 'idf.py')
  21. # environment variable used during click shell completion run
  22. SHELL_COMPLETE_VAR = '_IDF.PY_COMPLETE'
  23. # was shell completion invoked?
  24. SHELL_COMPLETE_RUN = SHELL_COMPLETE_VAR in os.environ
  25. def executable_exists(args: List) -> bool:
  26. try:
  27. subprocess.check_output(args)
  28. return True
  29. except Exception:
  30. return False
  31. def _idf_version_from_cmake() -> Optional[str]:
  32. version_path = os.path.join(os.environ['IDF_PATH'], 'tools/cmake/version.cmake')
  33. regex = re.compile(r'^\s*set\s*\(\s*IDF_VERSION_([A-Z]{5})\s+(\d+)')
  34. ver = {}
  35. try:
  36. with open(version_path) as f:
  37. for line in f:
  38. m = regex.match(line)
  39. if m:
  40. ver[m.group(1)] = m.group(2)
  41. return 'v%s.%s.%s' % (ver['MAJOR'], ver['MINOR'], ver['PATCH'])
  42. except (KeyError, OSError):
  43. sys.stderr.write('WARNING: Cannot find ESP-IDF version in version.cmake\n')
  44. return None
  45. def get_target(path: str, sdkconfig_filename: str='sdkconfig') -> Optional[str]:
  46. path = os.path.join(path, sdkconfig_filename)
  47. return get_sdkconfig_value(path, 'CONFIG_IDF_TARGET')
  48. def idf_version() -> Optional[str]:
  49. """Print version of ESP-IDF"""
  50. # Try to get version from git:
  51. try:
  52. version: Optional[str] = subprocess.check_output([
  53. 'git',
  54. '--git-dir=%s' % os.path.join(os.environ['IDF_PATH'], '.git'),
  55. '--work-tree=%s' % os.environ['IDF_PATH'],
  56. 'describe', '--tags', '--dirty', '--match', 'v*.*',
  57. ]).decode('utf-8', 'ignore').strip()
  58. except (subprocess.CalledProcessError, UnicodeError):
  59. # if failed, then try to parse cmake.version file
  60. sys.stderr.write('WARNING: Git version unavailable, reading from source\n')
  61. version = _idf_version_from_cmake()
  62. return version
  63. def get_default_serial_port() -> Any:
  64. # Import is done here in order to move it after the check_environment()
  65. # ensured that pyserial has been installed
  66. try:
  67. import esptool
  68. import serial.tools.list_ports
  69. ports = list(sorted(p.device for p in serial.tools.list_ports.comports()))
  70. # high baud rate could cause the failure of creation of the connection
  71. esp = esptool.get_default_connected_device(serial_list=ports, port=None, connect_attempts=4,
  72. initial_baud=115200)
  73. if esp is None:
  74. raise NoSerialPortFoundError(
  75. "No serial ports found. Connect a device, or use '-p PORT' option to set a specific port.")
  76. serial_port = esp.serial_port
  77. esp._port.close()
  78. return serial_port
  79. except NoSerialPortFoundError:
  80. raise
  81. except Exception as e:
  82. raise FatalError('An exception occurred during detection of the serial port: {}'.format(e))
  83. # function prints warning when autocompletion is not being performed
  84. # set argument stream to sys.stderr for errors and exceptions
  85. def print_warning(message: str, stream: TextIO=None) -> None:
  86. if not SHELL_COMPLETE_RUN:
  87. print(message, file=stream or sys.stderr)
  88. def color_print(message: str, color: str, newline: Optional[str]='\n') -> None:
  89. """ Print a message to stderr with colored highlighting """
  90. ansi_normal = '\033[0m'
  91. sys.stderr.write('%s%s%s%s' % (color, message, ansi_normal, newline))
  92. sys.stderr.flush()
  93. def yellow_print(message: str, newline: Optional[str]='\n') -> None:
  94. ansi_yellow = '\033[0;33m'
  95. color_print(message, ansi_yellow, newline)
  96. def red_print(message: str, newline: Optional[str]='\n') -> None:
  97. ansi_red = '\033[1;31m'
  98. color_print(message, ansi_red, newline)
  99. def debug_print_idf_version() -> None:
  100. print_warning(f'ESP-IDF {idf_version() or "version unknown"}')
  101. def generate_hints(*filenames: str) -> Generator:
  102. """Getting output files and printing hints on how to resolve errors based on the output."""
  103. with open(os.path.join(os.path.dirname(__file__), 'hints.yml'), 'r') as file:
  104. hints = yaml.safe_load(file)
  105. for file_name in filenames:
  106. with open(file_name, 'r') as file:
  107. output = ' '.join(line.strip() for line in file if line.strip())
  108. for hint in hints:
  109. variables_list = hint.get('variables')
  110. hint_list, hint_vars, re_vars = [], [], []
  111. match: Optional[Match[str]] = None
  112. try:
  113. if variables_list:
  114. for variables in variables_list:
  115. hint_vars = variables['hint_variables']
  116. re_vars = variables['re_variables']
  117. regex = hint['re'].format(*re_vars)
  118. if re.compile(regex).search(output):
  119. try:
  120. hint_list.append(hint['hint'].format(*hint_vars))
  121. except KeyError as e:
  122. red_print('Argument {} missing in {}. Check hints.yml file.'.format(e, hint))
  123. sys.exit(1)
  124. else:
  125. match = re.compile(hint['re']).search(output)
  126. except KeyError as e:
  127. red_print('Argument {} missing in {}. Check hints.yml file.'.format(e, hint))
  128. sys.exit(1)
  129. except re.error as e:
  130. red_print('{} from hints.yml have {} problem. Check hints.yml file.'.format(hint['re'], e))
  131. sys.exit(1)
  132. if hint_list:
  133. for message in hint_list:
  134. yield ' '.join(['HINT:', message])
  135. elif match:
  136. extra_info = ', '.join(match.groups()) if hint.get('match_to_output', '') else ''
  137. try:
  138. yield ' '.join(['HINT:', hint['hint'].format(extra_info)])
  139. except KeyError:
  140. raise KeyError("Argument 'hint' missing in {}. Check hints.yml file.".format(hint))
  141. def fit_text_in_terminal(out: str) -> str:
  142. """Fit text in terminal, if the string is not fit replace center with `...`"""
  143. space_for_dots = 3 # Space for "..."
  144. terminal_width, _ = os.get_terminal_size()
  145. if not terminal_width:
  146. return out
  147. if terminal_width <= space_for_dots:
  148. # if the wide of the terminal is too small just print dots
  149. return '.' * terminal_width
  150. if len(out) >= terminal_width:
  151. elide_size = (terminal_width - space_for_dots) // 2
  152. # cut out the middle part of the output if it does not fit in the terminal
  153. return '...'.join([out[:elide_size], out[len(out) - elide_size:]])
  154. return out
  155. class RunTool:
  156. def __init__(self, tool_name: str, args: List, cwd: str, env: Dict=None, custom_error_handler: FunctionType=None, build_dir: str=None,
  157. hints: bool=True, force_progression: bool=False, interactive: bool=False) -> None:
  158. self.tool_name = tool_name
  159. self.args = args
  160. self.cwd = cwd
  161. self.env = env
  162. self.custom_error_handler = custom_error_handler
  163. # build_dir sets by tools that do not use build directory as cwd
  164. self.build_dir = build_dir or cwd
  165. self.hints = hints
  166. self.force_progression = force_progression
  167. self.interactive = interactive
  168. def __call__(self) -> None:
  169. def quote_arg(arg: str) -> str:
  170. """ Quote the `arg` with whitespace in them because it can cause problems when we call it from a subprocess."""
  171. if re.match(r"^(?![\'\"]).*\s.*", arg):
  172. return ''.join(["'", arg, "'"])
  173. return arg
  174. self.args = [str(arg) for arg in self.args]
  175. display_args = ' '.join(quote_arg(arg) for arg in self.args)
  176. print('Running %s in directory %s' % (self.tool_name, quote_arg(self.cwd)))
  177. print('Executing "%s"...' % str(display_args))
  178. env_copy = dict(os.environ)
  179. env_copy.update(self.env or {})
  180. process: Union[Process, subprocess.CompletedProcess[bytes]]
  181. if self.hints:
  182. process, stderr_output_file, stdout_output_file = asyncio.run(self.run_command(self.args, env_copy))
  183. else:
  184. process = subprocess.run(self.args, env=env_copy, cwd=self.cwd)
  185. stderr_output_file, stdout_output_file = None, None
  186. if process.returncode == 0:
  187. return
  188. if self.custom_error_handler:
  189. self.custom_error_handler(process.returncode, stderr_output_file, stdout_output_file)
  190. return
  191. if stderr_output_file and stdout_output_file:
  192. for hint in generate_hints(stderr_output_file, stdout_output_file):
  193. yellow_print(hint)
  194. raise FatalError('{} failed with exit code {}, output of the command is in the {} and {}'.format(self.tool_name, process.returncode,
  195. stderr_output_file, stdout_output_file))
  196. raise FatalError('{} failed with exit code {}'.format(self.tool_name, process.returncode))
  197. async def run_command(self, cmd: List, env_copy: Dict) -> Tuple[Process, Optional[str], Optional[str]]:
  198. """ Run the `cmd` command with capturing stderr and stdout from that function and return returncode
  199. and of the command, the id of the process, paths to captured output """
  200. log_dir_name = 'log'
  201. try:
  202. os.mkdir(os.path.join(self.build_dir, log_dir_name))
  203. except FileExistsError:
  204. pass
  205. # Note: we explicitly pass in os.environ here, as we may have set IDF_PATH there during startup
  206. # limit was added for avoiding error in idf.py confserver
  207. try:
  208. p = await asyncio.create_subprocess_exec(*cmd, env=env_copy, limit=1024 * 256, cwd=self.cwd, stdout=asyncio.subprocess.PIPE,
  209. stderr=asyncio.subprocess.PIPE)
  210. except NotImplementedError:
  211. sys.exit(f'ERROR: {sys.executable} doesn\'t support asyncio. The issue can be worked around by re-running idf.py with the "--no-hints" argument.')
  212. stderr_output_file = os.path.join(self.build_dir, log_dir_name, f'idf_py_stderr_output_{p.pid}')
  213. stdout_output_file = os.path.join(self.build_dir, log_dir_name, f'idf_py_stdout_output_{p.pid}')
  214. if p.stderr and p.stdout: # it only to avoid None type in p.std
  215. await asyncio.gather(
  216. self.read_and_write_stream(p.stderr, stderr_output_file, sys.stderr),
  217. self.read_and_write_stream(p.stdout, stdout_output_file, sys.stdout))
  218. await p.wait() # added for avoiding None returncode
  219. return p, stderr_output_file, stdout_output_file
  220. async def read_and_write_stream(self, input_stream: asyncio.StreamReader, output_filename: str,
  221. output_stream: TextIO) -> None:
  222. """read the output of the `input_stream` and then write it into `output_filename` and `output_stream`"""
  223. def delete_ansi_escape(text: str) -> str:
  224. ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
  225. return ansi_escape.sub('', text)
  226. def print_progression(output: str) -> None:
  227. # Print a new line on top of the previous line
  228. print('\r' + fit_text_in_terminal(output.strip('\n\r')) + '\x1b[K', end='', file=output_stream)
  229. output_stream.flush()
  230. async def read_stream() -> Optional[str]:
  231. try:
  232. output_b = await input_stream.readline()
  233. return output_b.decode(errors='ignore')
  234. except (asyncio.LimitOverrunError, asyncio.IncompleteReadError) as e:
  235. print(e, file=sys.stderr)
  236. return None
  237. except AttributeError:
  238. return None
  239. async def read_interactive_stream() -> Optional[str]:
  240. buffer = b''
  241. while True:
  242. output_b = await input_stream.read(1)
  243. if not output_b:
  244. return None
  245. try:
  246. return (buffer + output_b).decode()
  247. except UnicodeDecodeError:
  248. buffer += output_b
  249. if len(buffer) > 4:
  250. # Multi-byte character contain up to 4 bytes and if buffer have more then 4 bytes
  251. # and still can not decode it we can just ignore some bytes
  252. return buffer.decode(errors='ignore')
  253. try:
  254. with open(output_filename, 'w', encoding='utf8') as output_file:
  255. while True:
  256. if self.interactive:
  257. output = await read_interactive_stream()
  258. else:
  259. output = await read_stream()
  260. if not output:
  261. break
  262. output_noescape = delete_ansi_escape(output)
  263. # Always remove escape sequences when writing the build log.
  264. output_file.write(output_noescape)
  265. # If idf.py output is redirected and the output stream is not a TTY,
  266. # strip the escape sequences as well.
  267. # (There shouldn't be any, but just in case.)
  268. if not output_stream.isatty():
  269. output = output_noescape
  270. if self.force_progression and output[0] == '[' and '-v' not in self.args and output_stream.isatty():
  271. # print output in progression way but only the progression related (that started with '[') and if verbose flag is not set
  272. print_progression(output)
  273. else:
  274. output_stream.write(output)
  275. output_stream.flush()
  276. except (RuntimeError, EnvironmentError) as e:
  277. yellow_print('WARNING: The exception {} was raised and we can\'t capture all your {} and '
  278. 'hints on how to resolve errors can be not accurate.'.format(e, output_stream.name.strip('<>')))
  279. def run_tool(*args: Any, **kwargs: Any) -> None:
  280. # Added in case someone uses run_tool externally in idf.py extensions
  281. return RunTool(*args, **kwargs)()
  282. def run_target(target_name: str, args: 'PropertyDict', env: Optional[Dict]=None,
  283. custom_error_handler: FunctionType=None, force_progression: bool=False, interactive: bool=False) -> None:
  284. """Run target in build directory."""
  285. if env is None:
  286. env = {}
  287. generator_cmd = GENERATORS[args.generator]['command']
  288. if args.verbose:
  289. generator_cmd += [GENERATORS[args.generator]['verbose_flag']]
  290. # By default, GNU Make and Ninja strip away color escape sequences when they see that their stdout is redirected.
  291. # If idf.py's stdout is not redirected, the final output is a TTY, so we can tell Make/Ninja to disable stripping
  292. # of color escape sequences. (Requires Ninja v1.9.0 or later.)
  293. if sys.stdout.isatty():
  294. if 'CLICOLOR_FORCE' not in env:
  295. env['CLICOLOR_FORCE'] = '1'
  296. RunTool(generator_cmd[0], generator_cmd + [target_name], args.build_dir, env, custom_error_handler, hints=not args.no_hints,
  297. force_progression=force_progression, interactive=interactive)()
  298. def _strip_quotes(value: str, regexp: re.Pattern=re.compile(r"^\"(.*)\"$|^'(.*)'$|^(.*)$")) -> Optional[str]:
  299. """
  300. Strip quotes like CMake does during parsing cache entries
  301. """
  302. matching_values = regexp.match(value)
  303. return [x for x in matching_values.groups() if x is not None][0].rstrip() if matching_values is not None else None
  304. def _parse_cmakecache(path: str) -> Dict:
  305. """
  306. Parse the CMakeCache file at 'path'.
  307. Returns a dict of name:value.
  308. CMakeCache entries also each have a "type", but this is currently ignored.
  309. """
  310. result = {}
  311. with open(path, encoding='utf-8') as f:
  312. for line in f:
  313. # cmake cache lines look like: CMAKE_CXX_FLAGS_DEBUG:STRING=-g
  314. # groups are name, type, value
  315. m = re.match(r'^([^#/:=]+):([^:=]+)=(.*)\n$', line)
  316. if m:
  317. result[m.group(1)] = m.group(3)
  318. return result
  319. def _parse_cmdl_cmakecache(entries: List) -> Dict[str, str]:
  320. """
  321. Parse list of CMake cache entries passed in via the -D option.
  322. Returns a dict of name:value.
  323. """
  324. result: Dict = {}
  325. for entry in entries:
  326. key, value = entry.split('=', 1)
  327. value = _strip_quotes(value)
  328. result[key] = value
  329. return result
  330. def _new_cmakecache_entries(cache: Dict, cache_cmdl: Dict) -> bool:
  331. for entry in cache_cmdl:
  332. if entry not in cache:
  333. return True
  334. if cache_cmdl[entry] != cache[entry]:
  335. return True
  336. return False
  337. def _detect_cmake_generator(prog_name: str) -> Any:
  338. """
  339. Find the default cmake generator, if none was specified. Raises an exception if no valid generator is found.
  340. """
  341. for (generator_name, generator) in GENERATORS.items():
  342. if executable_exists(generator['version']):
  343. return generator_name
  344. raise FatalError("To use %s, either the 'ninja' or 'GNU make' build tool must be available in the PATH" % prog_name)
  345. def ensure_build_directory(args: 'PropertyDict', prog_name: str, always_run_cmake: bool=False,
  346. env: Dict=None) -> None:
  347. """Check the build directory exists and that cmake has been run there.
  348. If this isn't the case, create the build directory (if necessary) and
  349. do an initial cmake run to configure it.
  350. This function will also check args.generator parameter. If the parameter is incompatible with
  351. the build directory, an error is raised. If the parameter is None, this function will set it to
  352. an auto-detected default generator or to the value already configured in the build directory.
  353. """
  354. if not executable_exists(['cmake', '--version']):
  355. debug_print_idf_version()
  356. raise FatalError(f'"cmake" must be available on the PATH to use {PROG}')
  357. project_dir = args.project_dir
  358. # Verify the project directory
  359. if not os.path.isdir(project_dir):
  360. if not os.path.exists(project_dir):
  361. raise FatalError('Project directory %s does not exist' % project_dir)
  362. else:
  363. raise FatalError('%s must be a project directory' % project_dir)
  364. if not os.path.exists(os.path.join(project_dir, 'CMakeLists.txt')):
  365. raise FatalError('CMakeLists.txt not found in project directory %s' % project_dir)
  366. # Verify/create the build directory
  367. build_dir = args.build_dir
  368. if not os.path.isdir(build_dir):
  369. os.makedirs(build_dir)
  370. # Parse CMakeCache, if it exists
  371. cache_path = os.path.join(build_dir, 'CMakeCache.txt')
  372. cache = _parse_cmakecache(cache_path) if os.path.exists(cache_path) else {}
  373. args.define_cache_entry.append('CCACHE_ENABLE=%d' % args.ccache)
  374. cache_cmdl = _parse_cmdl_cmakecache(args.define_cache_entry)
  375. # Validate IDF_TARGET
  376. _check_idf_target(args, prog_name, cache, cache_cmdl, env)
  377. if always_run_cmake or _new_cmakecache_entries(cache, cache_cmdl):
  378. if args.generator is None:
  379. args.generator = _detect_cmake_generator(prog_name)
  380. try:
  381. cmake_args = [
  382. 'cmake',
  383. '-G',
  384. args.generator,
  385. '-DPYTHON_DEPS_CHECKED=1',
  386. '-DPYTHON={}'.format(sys.executable),
  387. '-DESP_PLATFORM=1',
  388. ]
  389. if args.cmake_warn_uninitialized:
  390. cmake_args += ['--warn-uninitialized']
  391. if args.define_cache_entry:
  392. cmake_args += ['-D' + d for d in args.define_cache_entry]
  393. cmake_args += [project_dir]
  394. hints = not args.no_hints
  395. RunTool('cmake', cmake_args, cwd=args.build_dir, env=env, hints=hints)()
  396. except Exception:
  397. # don't allow partially valid CMakeCache.txt files,
  398. # to keep the "should I run cmake?" logic simple
  399. if os.path.exists(cache_path):
  400. os.remove(cache_path)
  401. raise
  402. # need to update cache so subsequent access in this method would reflect the result of the previous cmake run
  403. cache = _parse_cmakecache(cache_path) if os.path.exists(cache_path) else {}
  404. try:
  405. generator = cache['CMAKE_GENERATOR']
  406. except KeyError:
  407. generator = _detect_cmake_generator(prog_name)
  408. if args.generator is None:
  409. args.generator = (generator) # reuse the previously configured generator, if none was given
  410. if generator != args.generator:
  411. raise FatalError("Build is configured for generator '%s' not '%s'. Run '%s fullclean' to start again." %
  412. (generator, args.generator, prog_name))
  413. try:
  414. home_dir = cache['CMAKE_HOME_DIRECTORY']
  415. if os.path.realpath(home_dir) != os.path.realpath(project_dir):
  416. raise FatalError(
  417. "Build directory '%s' configured for project '%s' not '%s'. Run '%s fullclean' to start again." %
  418. (build_dir, os.path.realpath(home_dir), os.path.realpath(project_dir), prog_name))
  419. except KeyError:
  420. pass # if cmake failed part way, CMAKE_HOME_DIRECTORY may not be set yet
  421. try:
  422. python = cache['PYTHON']
  423. if python != sys.executable:
  424. raise FatalError(
  425. "'{}' is currently active in the environment while the project was configured with '{}'. "
  426. "Run '{} fullclean' to start again.".format(sys.executable, python, prog_name))
  427. except KeyError:
  428. pass
  429. def merge_action_lists(*action_lists: Dict) -> Dict:
  430. merged_actions: Dict = {
  431. 'global_options': [],
  432. 'actions': {},
  433. 'global_action_callbacks': [],
  434. }
  435. for action_list in action_lists:
  436. merged_actions['global_options'].extend(action_list.get('global_options', []))
  437. merged_actions['actions'].update(action_list.get('actions', {}))
  438. merged_actions['global_action_callbacks'].extend(action_list.get('global_action_callbacks', []))
  439. return merged_actions
  440. def get_sdkconfig_filename(args: 'PropertyDict', cache_cmdl: Dict=None) -> str:
  441. """
  442. Get project's sdkconfig file name.
  443. """
  444. if not cache_cmdl:
  445. cache_cmdl = _parse_cmdl_cmakecache(args.define_cache_entry)
  446. config = cache_cmdl.get('SDKCONFIG')
  447. if config:
  448. return os.path.abspath(config)
  449. proj_desc_path = os.path.join(args.build_dir, 'project_description.json')
  450. try:
  451. with open(proj_desc_path, 'r') as f:
  452. proj_desc = json.load(f)
  453. return str(proj_desc['config_file'])
  454. except (OSError, KeyError):
  455. pass
  456. return os.path.join(args.project_dir, 'sdkconfig')
  457. def get_sdkconfig_value(sdkconfig_file: str, key: str) -> Optional[str]:
  458. """
  459. Return the value of given key from sdkconfig_file.
  460. If sdkconfig_file does not exist or the option is not present, returns None.
  461. """
  462. assert key.startswith('CONFIG_')
  463. if not os.path.exists(sdkconfig_file):
  464. return None
  465. # keep track of the last seen value for the given key
  466. value = None
  467. # if the value is quoted, this excludes the quotes from the value
  468. pattern = re.compile(r"^{}=\"?([^\"]*)\"?$".format(key))
  469. with open(sdkconfig_file, 'r') as f:
  470. for line in f:
  471. match = re.match(pattern, line)
  472. if match:
  473. value = match.group(1)
  474. return value
  475. def is_target_supported(project_path: str, supported_targets: List) -> bool:
  476. """
  477. Returns True if the active target is supported, or False otherwise.
  478. """
  479. return get_target(project_path) in supported_targets
  480. def _check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict,
  481. cache_cmdl: Dict, env: Dict=None) -> None:
  482. """
  483. Cross-check the three settings (sdkconfig, CMakeCache, environment) and if there is
  484. mismatch, fail with instructions on how to fix this.
  485. """
  486. sdkconfig = get_sdkconfig_filename(args, cache_cmdl)
  487. idf_target_from_sdkconfig = get_sdkconfig_value(sdkconfig, 'CONFIG_IDF_TARGET')
  488. idf_target_from_env = os.environ.get('IDF_TARGET')
  489. idf_target_from_cache = cache.get('IDF_TARGET')
  490. idf_target_from_cache_cmdl = cache_cmdl.get('IDF_TARGET')
  491. # Called from set-target action. The original sdkconfig will be renamed
  492. # in cmake, so ignore any CONFIG_IDF_TARGET which may be defined in
  493. # stale sdkconfig.
  494. if env and env.get('_IDF_PY_SET_TARGET_ACTION') == '1':
  495. idf_target_from_sdkconfig = None
  496. if idf_target_from_env:
  497. # Let's check that IDF_TARGET values are consistent
  498. if idf_target_from_sdkconfig and idf_target_from_sdkconfig != idf_target_from_env:
  499. raise FatalError("Project sdkconfig '{cfg}' was generated for target '{t_conf}', but environment variable IDF_TARGET "
  500. "is set to '{t_env}'. Run '{prog} set-target {t_env}' to generate new sdkconfig file for target {t_env}."
  501. .format(cfg=sdkconfig, t_conf=idf_target_from_sdkconfig, t_env=idf_target_from_env, prog=prog_name))
  502. if idf_target_from_cache and idf_target_from_cache != idf_target_from_env:
  503. raise FatalError("Target settings are not consistent: '{t_env}' in the environment, '{t_cache}' in CMakeCache.txt. "
  504. "Run '{prog} fullclean' to start again."
  505. .format(t_env=idf_target_from_env, t_cache=idf_target_from_cache, prog=prog_name))
  506. if idf_target_from_cache_cmdl and idf_target_from_cache_cmdl != idf_target_from_env:
  507. raise FatalError("Target '{t_cmdl}' specified on command line is not consistent with "
  508. "target '{t_env}' in the environment."
  509. .format(t_cmdl=idf_target_from_cache_cmdl, t_env=idf_target_from_env))
  510. elif idf_target_from_cache_cmdl:
  511. # Check if -DIDF_TARGET is consistent with target in CMakeCache.txt
  512. if idf_target_from_cache and idf_target_from_cache != idf_target_from_cache_cmdl:
  513. raise FatalError("Target '{t_cmdl}' specified on command line is not consistent with "
  514. "target '{t_cache}' in CMakeCache.txt. Run '{prog} set-target {t_cmdl}' to re-generate "
  515. 'CMakeCache.txt.'
  516. .format(t_cache=idf_target_from_cache, t_cmdl=idf_target_from_cache_cmdl, prog=prog_name))
  517. elif idf_target_from_cache:
  518. # This shouldn't happen, unless the user manually edits CMakeCache.txt or sdkconfig, but let's check anyway.
  519. if idf_target_from_sdkconfig and idf_target_from_cache != idf_target_from_sdkconfig:
  520. raise FatalError("Project sdkconfig '{cfg}' was generated for target '{t_conf}', but CMakeCache.txt contains '{t_cache}'. "
  521. "To keep the setting in sdkconfig ({t_conf}) and re-generate CMakeCache.txt, run '{prog} fullclean'. "
  522. "To re-generate sdkconfig for '{t_cache}' target, run '{prog} set-target {t_cache}'."
  523. .format(cfg=sdkconfig, t_conf=idf_target_from_sdkconfig, t_cache=idf_target_from_cache, prog=prog_name))
  524. class TargetChoice(click.Choice):
  525. """
  526. A version of click.Choice with two special features:
  527. - ignores hyphens
  528. - not case sensitive
  529. """
  530. def __init__(self, choices: List) -> None:
  531. super(TargetChoice, self).__init__(choices, case_sensitive=False)
  532. def convert(self, value: Any, param: click.Parameter, ctx: click.Context) -> Any:
  533. def normalize(string: str) -> str:
  534. return string.lower().replace('-', '')
  535. saved_token_normalize_func = ctx.token_normalize_func
  536. ctx.token_normalize_func = normalize
  537. try:
  538. return super(TargetChoice, self).convert(value, param, ctx)
  539. finally:
  540. ctx.token_normalize_func = saved_token_normalize_func
  541. class PropertyDict(dict):
  542. def __getattr__(self, name: str) -> Any:
  543. if name in self:
  544. return self[name]
  545. else:
  546. raise AttributeError("'PropertyDict' object has no attribute '%s'" % name)
  547. def __setattr__(self, name: str, value: Any) -> None:
  548. self[name] = value
  549. def __delattr__(self, name: str) -> None:
  550. if name in self:
  551. del self[name]
  552. else:
  553. raise AttributeError("'PropertyDict' object has no attribute '%s'" % name)