tools.py 33 KB

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