idf.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. #!/usr/bin/env python
  2. #
  3. # 'idf.py' is a top-level config/build command line tool for ESP-IDF
  4. #
  5. # You don't have to use idf.py, you can use cmake directly
  6. # (or use cmake in an IDE)
  7. #
  8. #
  9. #
  10. # Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License");
  13. # you may not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS,
  20. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. #
  24. # WARNING: we don't check for Python build-time dependencies until
  25. # check_environment() function below. If possible, avoid importing
  26. # any external libraries here - put in external script, or import in
  27. # their specific function instead.
  28. import codecs
  29. import json
  30. import locale
  31. import os
  32. import os.path
  33. import subprocess
  34. import sys
  35. from collections import Counter, OrderedDict
  36. from importlib import import_module
  37. from pkgutil import iter_modules
  38. # pyc files remain in the filesystem when switching between branches which might raise errors for incompatible
  39. # idf.py extentions. Therefore, pyc file generation is turned off:
  40. sys.dont_write_bytecode = True
  41. from idf_py_actions.errors import FatalError # noqa: E402
  42. from idf_py_actions.tools import (executable_exists, idf_version, merge_action_lists, realpath) # noqa: E402
  43. # Use this Python interpreter for any subprocesses we launch
  44. PYTHON = sys.executable
  45. # note: os.environ changes don't automatically propagate to child processes,
  46. # you have to pass env=os.environ explicitly anywhere that we create a process
  47. os.environ["PYTHON"] = sys.executable
  48. # Name of the program, normally 'idf.py'.
  49. # Can be overridden from idf.bat using IDF_PY_PROGRAM_NAME
  50. PROG = os.getenv("IDF_PY_PROGRAM_NAME", sys.argv[0])
  51. def check_environment():
  52. """
  53. Verify the environment contains the top-level tools we need to operate
  54. (cmake will check a lot of other things)
  55. """
  56. checks_output = []
  57. if not executable_exists(["cmake", "--version"]):
  58. print_idf_version()
  59. raise FatalError("'cmake' must be available on the PATH to use %s" % PROG)
  60. # verify that IDF_PATH env variable is set
  61. # find the directory idf.py is in, then the parent directory of this, and assume this is IDF_PATH
  62. detected_idf_path = realpath(os.path.join(os.path.dirname(__file__), ".."))
  63. if "IDF_PATH" in os.environ:
  64. set_idf_path = realpath(os.environ["IDF_PATH"])
  65. if set_idf_path != detected_idf_path:
  66. print(
  67. "WARNING: IDF_PATH environment variable is set to %s but %s path indicates IDF directory %s. "
  68. "Using the environment variable directory, but results may be unexpected..." %
  69. (set_idf_path, PROG, detected_idf_path))
  70. else:
  71. print("Setting IDF_PATH environment variable: %s" % detected_idf_path)
  72. os.environ["IDF_PATH"] = detected_idf_path
  73. # check Python version
  74. if sys.version_info[0] < 3:
  75. print("WARNING: Support for Python 2 is deprecated and will be removed in future versions.")
  76. elif sys.version_info[0] == 3 and sys.version_info[1] < 6:
  77. print("WARNING: Python 3 versions older than 3.6 are not supported.")
  78. # check Python dependencies
  79. checks_output.append("Checking Python dependencies...")
  80. try:
  81. out = subprocess.check_output(
  82. [
  83. os.environ["PYTHON"],
  84. os.path.join(os.environ["IDF_PATH"], "tools", "check_python_dependencies.py"),
  85. ],
  86. env=os.environ,
  87. )
  88. checks_output.append(out.decode('utf-8', 'ignore').strip())
  89. except subprocess.CalledProcessError as e:
  90. print(e.output.decode('utf-8', 'ignore'))
  91. print_idf_version()
  92. raise SystemExit(1)
  93. return checks_output
  94. def _safe_relpath(path, start=None):
  95. """ Return a relative path, same as os.path.relpath, but only if this is possible.
  96. It is not possible on Windows, if the start directory and the path are on different drives.
  97. """
  98. try:
  99. return os.path.relpath(path, os.curdir if start is None else start)
  100. except ValueError:
  101. return os.path.abspath(path)
  102. def print_idf_version():
  103. version = idf_version()
  104. if version:
  105. print("ESP-IDF %s" % version)
  106. else:
  107. print("ESP-IDF version unknown")
  108. class PropertyDict(dict):
  109. def __getattr__(self, name):
  110. if name in self:
  111. return self[name]
  112. else:
  113. raise AttributeError("'PropertyDict' object has no attribute '%s'" % name)
  114. def __setattr__(self, name, value):
  115. self[name] = value
  116. def __delattr__(self, name):
  117. if name in self:
  118. del self[name]
  119. else:
  120. raise AttributeError("'PropertyDict' object has no attribute '%s'" % name)
  121. def init_cli(verbose_output=None):
  122. # Click is imported here to run it after check_environment()
  123. import click
  124. class Deprecation(object):
  125. """Construct deprecation notice for help messages"""
  126. def __init__(self, deprecated=False):
  127. self.deprecated = deprecated
  128. self.since = None
  129. self.removed = None
  130. self.exit_with_error = None
  131. self.custom_message = ""
  132. if isinstance(deprecated, dict):
  133. self.custom_message = deprecated.get("message", "")
  134. self.since = deprecated.get("since", None)
  135. self.removed = deprecated.get("removed", None)
  136. self.exit_with_error = deprecated.get("exit_with_error", None)
  137. elif isinstance(deprecated, str):
  138. self.custom_message = deprecated
  139. def full_message(self, type="Option"):
  140. if self.exit_with_error:
  141. return "%s is deprecated %sand was removed%s.%s" % (
  142. type,
  143. "since %s " % self.since if self.since else "",
  144. " in %s" % self.removed if self.removed else "",
  145. " %s" % self.custom_message if self.custom_message else "",
  146. )
  147. else:
  148. return "%s is deprecated %sand will be removed in%s.%s" % (
  149. type,
  150. "since %s " % self.since if self.since else "",
  151. " %s" % self.removed if self.removed else " future versions",
  152. " %s" % self.custom_message if self.custom_message else "",
  153. )
  154. def help(self, text, type="Option", separator=" "):
  155. text = text or ""
  156. return self.full_message(type) + separator + text if self.deprecated else text
  157. def short_help(self, text):
  158. text = text or ""
  159. return ("Deprecated! " + text) if self.deprecated else text
  160. def check_deprecation(ctx):
  161. """Prints deprectation warnings for arguments in given context"""
  162. for option in ctx.command.params:
  163. default = () if option.multiple else option.default
  164. if isinstance(option, Option) and option.deprecated and ctx.params[option.name] != default:
  165. deprecation = Deprecation(option.deprecated)
  166. if deprecation.exit_with_error:
  167. raise FatalError("Error: %s" % deprecation.full_message('Option "%s"' % option.name))
  168. else:
  169. print("Warning: %s" % deprecation.full_message('Option "%s"' % option.name))
  170. class Task(object):
  171. def __init__(self, callback, name, aliases, dependencies, order_dependencies, action_args):
  172. self.callback = callback
  173. self.name = name
  174. self.dependencies = dependencies
  175. self.order_dependencies = order_dependencies
  176. self.action_args = action_args
  177. self.aliases = aliases
  178. def __call__(self, context, global_args, action_args=None):
  179. if action_args is None:
  180. action_args = self.action_args
  181. self.callback(self.name, context, global_args, **action_args)
  182. class Action(click.Command):
  183. def __init__(
  184. self,
  185. name=None,
  186. aliases=None,
  187. deprecated=False,
  188. dependencies=None,
  189. order_dependencies=None,
  190. hidden=False,
  191. **kwargs):
  192. super(Action, self).__init__(name, **kwargs)
  193. self.name = self.name or self.callback.__name__
  194. self.deprecated = deprecated
  195. self.hidden = hidden
  196. if aliases is None:
  197. aliases = []
  198. self.aliases = aliases
  199. self.help = self.help or self.callback.__doc__
  200. if self.help is None:
  201. self.help = ""
  202. if dependencies is None:
  203. dependencies = []
  204. if order_dependencies is None:
  205. order_dependencies = []
  206. # Show first line of help if short help is missing
  207. self.short_help = self.short_help or self.help.split("\n")[0]
  208. if deprecated:
  209. deprecation = Deprecation(deprecated)
  210. self.short_help = deprecation.short_help(self.short_help)
  211. self.help = deprecation.help(self.help, type="Command", separator="\n")
  212. # Add aliases to help string
  213. if aliases:
  214. aliases_help = "Aliases: %s." % ", ".join(aliases)
  215. self.help = "\n".join([self.help, aliases_help])
  216. self.short_help = " ".join([aliases_help, self.short_help])
  217. self.unwrapped_callback = self.callback
  218. if self.callback is not None:
  219. def wrapped_callback(**action_args):
  220. return Task(
  221. callback=self.unwrapped_callback,
  222. name=self.name,
  223. dependencies=dependencies,
  224. order_dependencies=order_dependencies,
  225. action_args=action_args,
  226. aliases=self.aliases,
  227. )
  228. self.callback = wrapped_callback
  229. def invoke(self, ctx):
  230. if self.deprecated:
  231. deprecation = Deprecation(self.deprecated)
  232. message = deprecation.full_message('Command "%s"' % self.name)
  233. if deprecation.exit_with_error:
  234. raise FatalError("Error: %s" % message)
  235. else:
  236. print("Warning: %s" % message)
  237. self.deprecated = False # disable Click's built-in deprecation handling
  238. # Print warnings for options
  239. check_deprecation(ctx)
  240. return super(Action, self).invoke(ctx)
  241. class Argument(click.Argument):
  242. """
  243. Positional argument
  244. names - alias of 'param_decls'
  245. """
  246. def __init__(self, **kwargs):
  247. names = kwargs.pop("names")
  248. super(Argument, self).__init__(names, **kwargs)
  249. class Scope(object):
  250. """
  251. Scope for sub-command option.
  252. possible values:
  253. - default - only available on defined level (global/action)
  254. - global - When defined for action, also available as global
  255. - shared - Opposite to 'global': when defined in global scope, also available for all actions
  256. """
  257. SCOPES = ("default", "global", "shared")
  258. def __init__(self, scope=None):
  259. if scope is None:
  260. self._scope = "default"
  261. elif isinstance(scope, str) and scope in self.SCOPES:
  262. self._scope = scope
  263. elif isinstance(scope, Scope):
  264. self._scope = str(scope)
  265. else:
  266. raise FatalError("Unknown scope for option: %s" % scope)
  267. @property
  268. def is_global(self):
  269. return self._scope == "global"
  270. @property
  271. def is_shared(self):
  272. return self._scope == "shared"
  273. def __str__(self):
  274. return self._scope
  275. class Option(click.Option):
  276. """Option that knows whether it should be global"""
  277. def __init__(self, scope=None, deprecated=False, hidden=False, **kwargs):
  278. """
  279. Keyword arguments additional to Click's Option class:
  280. names - alias of 'param_decls'
  281. deprecated - marks option as deprecated. May be boolean, string (with custom deprecation message)
  282. or dict with optional keys:
  283. since: version of deprecation
  284. removed: version when option will be removed
  285. custom_message: Additional text to deprecation warning
  286. """
  287. kwargs["param_decls"] = kwargs.pop("names")
  288. super(Option, self).__init__(**kwargs)
  289. self.deprecated = deprecated
  290. self.scope = Scope(scope)
  291. self.hidden = hidden
  292. if deprecated:
  293. deprecation = Deprecation(deprecated)
  294. self.help = deprecation.help(self.help)
  295. if self.scope.is_global:
  296. self.help += " This option can be used at most once either globally, or for one subcommand."
  297. def get_help_record(self, ctx):
  298. # Backport "hidden" parameter to click 5.0
  299. if self.hidden:
  300. return
  301. return super(Option, self).get_help_record(ctx)
  302. class CLI(click.MultiCommand):
  303. """Action list contains all actions with options available for CLI"""
  304. def __init__(self, all_actions=None, verbose_output=None, help=None):
  305. super(CLI, self).__init__(
  306. chain=True,
  307. invoke_without_command=True,
  308. result_callback=self.execute_tasks,
  309. context_settings={"max_content_width": 140},
  310. help=help,
  311. )
  312. self._actions = {}
  313. self.global_action_callbacks = []
  314. self.commands_with_aliases = {}
  315. if verbose_output is None:
  316. verbose_output = []
  317. self.verbose_output = verbose_output
  318. if all_actions is None:
  319. all_actions = {}
  320. shared_options = []
  321. # Global options
  322. for option_args in all_actions.get("global_options", []):
  323. option = Option(**option_args)
  324. self.params.append(option)
  325. if option.scope.is_shared:
  326. shared_options.append(option)
  327. # Global options validators
  328. self.global_action_callbacks = all_actions.get("global_action_callbacks", [])
  329. # Actions
  330. for name, action in all_actions.get("actions", {}).items():
  331. arguments = action.pop("arguments", [])
  332. options = action.pop("options", [])
  333. if arguments is None:
  334. arguments = []
  335. if options is None:
  336. options = []
  337. self._actions[name] = Action(name=name, **action)
  338. for alias in [name] + action.get("aliases", []):
  339. self.commands_with_aliases[alias] = name
  340. for argument_args in arguments:
  341. self._actions[name].params.append(Argument(**argument_args))
  342. # Add all shared options
  343. for option in shared_options:
  344. self._actions[name].params.append(option)
  345. for option_args in options:
  346. option = Option(**option_args)
  347. if option.scope.is_shared:
  348. raise FatalError(
  349. '"%s" is defined for action "%s". '
  350. ' "shared" options can be declared only on global level' % (option.name, name))
  351. # Promote options to global if see for the first time
  352. if option.scope.is_global and option.name not in [o.name for o in self.params]:
  353. self.params.append(option)
  354. self._actions[name].params.append(option)
  355. def list_commands(self, ctx):
  356. return sorted(filter(lambda name: not self._actions[name].hidden, self._actions))
  357. def get_command(self, ctx, name):
  358. if name in self.commands_with_aliases:
  359. return self._actions.get(self.commands_with_aliases.get(name))
  360. # Trying fallback to build target (from "all" action) if command is not known
  361. else:
  362. return Action(name=name, callback=self._actions.get('fallback').unwrapped_callback)
  363. def _print_closing_message(self, args, actions):
  364. # print a closing message of some kind
  365. #
  366. if "flash" in str(actions) or "dfu" in str(actions):
  367. print("Done")
  368. return
  369. if not os.path.exists(os.path.join(args.build_dir, "flasher_args.json")):
  370. print("Done")
  371. return
  372. # Otherwise, if we built any binaries print a message about
  373. # how to flash them
  374. def print_flashing_message(title, key):
  375. with open(os.path.join(args.build_dir, "flasher_args.json")) as f:
  376. flasher_args = json.load(f)
  377. def flasher_path(f):
  378. return _safe_relpath(os.path.join(args.build_dir, f))
  379. if key != "project": # flashing a single item
  380. if key not in flasher_args:
  381. # This is the case for 'idf.py bootloader' if Secure Boot is on, need to follow manual flashing steps
  382. print("\n%s build complete." % title)
  383. return
  384. cmd = ""
  385. if (key == "bootloader"): # bootloader needs --flash-mode, etc to be passed in
  386. cmd = " ".join(flasher_args["write_flash_args"]) + " "
  387. cmd += flasher_args[key]["offset"] + " "
  388. cmd += flasher_path(flasher_args[key]["file"])
  389. else: # flashing the whole project
  390. cmd = " ".join(flasher_args["write_flash_args"]) + " "
  391. flash_items = sorted(
  392. ((o, f) for (o, f) in flasher_args["flash_files"].items() if len(o) > 0),
  393. key=lambda x: int(x[0], 0),
  394. )
  395. for o, f in flash_items:
  396. cmd += o + " " + flasher_path(f) + " "
  397. print("\n%s build complete. To flash, run this command:" % title)
  398. print(
  399. "%s %s -p %s -b %s --before %s --after %s --chip %s %s write_flash %s" % (
  400. PYTHON,
  401. _safe_relpath("%s/components/esptool_py/esptool/esptool.py" % os.environ["IDF_PATH"]),
  402. args.port or "(PORT)",
  403. args.baud,
  404. flasher_args["extra_esptool_args"]["before"],
  405. flasher_args["extra_esptool_args"]["after"],
  406. flasher_args["extra_esptool_args"]["chip"],
  407. "--no-stub" if not flasher_args["extra_esptool_args"]["stub"] else "",
  408. cmd.strip(),
  409. ))
  410. print(
  411. "or run 'idf.py -p %s %s'" % (
  412. args.port or "(PORT)",
  413. key + "-flash" if key != "project" else "flash",
  414. ))
  415. if "all" in actions or "build" in actions:
  416. print_flashing_message("Project", "project")
  417. else:
  418. if "app" in actions:
  419. print_flashing_message("App", "app")
  420. if "partition_table" in actions:
  421. print_flashing_message("Partition Table", "partition_table")
  422. if "bootloader" in actions:
  423. print_flashing_message("Bootloader", "bootloader")
  424. def execute_tasks(self, tasks, **kwargs):
  425. ctx = click.get_current_context()
  426. global_args = PropertyDict(kwargs)
  427. # Show warning if some tasks are present several times in the list
  428. dupplicated_tasks = sorted(
  429. [item for item, count in Counter(task.name for task in tasks).items() if count > 1])
  430. if dupplicated_tasks:
  431. dupes = ", ".join('"%s"' % t for t in dupplicated_tasks)
  432. print(
  433. "WARNING: Command%s found in the list of commands more than once. " %
  434. ("s %s are" % dupes if len(dupplicated_tasks) > 1 else " %s is" % dupes) +
  435. "Only first occurence will be executed.")
  436. # Set propagated global options.
  437. # These options may be set on one subcommand, but available in the list of global arguments
  438. for task in tasks:
  439. for key in list(task.action_args):
  440. option = next((o for o in ctx.command.params if o.name == key), None)
  441. if option and (option.scope.is_global or option.scope.is_shared):
  442. local_value = task.action_args.pop(key)
  443. global_value = global_args[key]
  444. default = () if option.multiple else option.default
  445. if global_value != default and local_value != default and global_value != local_value:
  446. raise FatalError(
  447. 'Option "%s" provided for "%s" is already defined to a different value. '
  448. "This option can appear at most once in the command line." % (key, task.name))
  449. if local_value != default:
  450. global_args[key] = local_value
  451. # Show warnings about global arguments
  452. check_deprecation(ctx)
  453. # Make sure that define_cache_entry is mutable list and can be modified in callbacks
  454. global_args.define_cache_entry = list(global_args.define_cache_entry)
  455. # Execute all global action callback - first from idf.py itself, then from extensions
  456. for action_callback in ctx.command.global_action_callbacks:
  457. action_callback(ctx, global_args, tasks)
  458. # Always show help when command is not provided
  459. if not tasks:
  460. print(ctx.get_help())
  461. ctx.exit()
  462. # Build full list of tasks to and deal with dependencies and order dependencies
  463. tasks_to_run = OrderedDict()
  464. while tasks:
  465. task = tasks[0]
  466. tasks_dict = dict([(t.name, t) for t in tasks])
  467. dependecies_processed = True
  468. # If task have some dependecies they have to be executed before the task.
  469. for dep in task.dependencies:
  470. if dep not in tasks_to_run.keys():
  471. # If dependent task is in the list of unprocessed tasks move to the front of the list
  472. if dep in tasks_dict.keys():
  473. dep_task = tasks.pop(tasks.index(tasks_dict[dep]))
  474. # Otherwise invoke it with default set of options
  475. # and put to the front of the list of unprocessed tasks
  476. else:
  477. print(
  478. 'Adding "%s"\'s dependency "%s" to list of commands with default set of options.' %
  479. (task.name, dep))
  480. dep_task = ctx.invoke(ctx.command.get_command(ctx, dep))
  481. # Remove options with global scope from invoke tasks because they are alread in global_args
  482. for key in list(dep_task.action_args):
  483. option = next((o for o in ctx.command.params if o.name == key), None)
  484. if option and (option.scope.is_global or option.scope.is_shared):
  485. dep_task.action_args.pop(key)
  486. tasks.insert(0, dep_task)
  487. dependecies_processed = False
  488. # Order only dependencies are moved to the front of the queue if they present in command list
  489. for dep in task.order_dependencies:
  490. if dep in tasks_dict.keys() and dep not in tasks_to_run.keys():
  491. tasks.insert(0, tasks.pop(tasks.index(tasks_dict[dep])))
  492. dependecies_processed = False
  493. if dependecies_processed:
  494. # Remove task from list of unprocessed tasks
  495. tasks.pop(0)
  496. # And add to the queue
  497. if task.name not in tasks_to_run.keys():
  498. tasks_to_run.update([(task.name, task)])
  499. # Run all tasks in the queue
  500. # when global_args.dry_run is true idf.py works in idle mode and skips actual task execution
  501. if not global_args.dry_run:
  502. for task in tasks_to_run.values():
  503. name_with_aliases = task.name
  504. if task.aliases:
  505. name_with_aliases += " (aliases: %s)" % ", ".join(task.aliases)
  506. print("Executing action: %s" % name_with_aliases)
  507. task(ctx, global_args, task.action_args)
  508. self._print_closing_message(global_args, tasks_to_run.keys())
  509. return tasks_to_run
  510. # That's a tiny parser that parse project-dir even before constructing
  511. # fully featured click parser to be sure that extensions are loaded from the right place
  512. @click.command(
  513. add_help_option=False,
  514. context_settings={
  515. "allow_extra_args": True,
  516. "ignore_unknown_options": True
  517. },
  518. )
  519. @click.option("-C", "--project-dir", default=os.getcwd())
  520. def parse_project_dir(project_dir):
  521. return realpath(project_dir)
  522. project_dir = parse_project_dir(standalone_mode=False)
  523. all_actions = {}
  524. # Load extensions from components dir
  525. idf_py_extensions_path = os.path.join(os.environ["IDF_PATH"], "tools", "idf_py_actions")
  526. extension_dirs = [realpath(idf_py_extensions_path)]
  527. extra_paths = os.environ.get("IDF_EXTRA_ACTIONS_PATH")
  528. if extra_paths is not None:
  529. for path in extra_paths.split(';'):
  530. path = realpath(path)
  531. if path not in extension_dirs:
  532. extension_dirs.append(path)
  533. extensions = []
  534. for directory in extension_dirs:
  535. if directory and not os.path.exists(directory):
  536. print('WARNING: Directroy with idf.py extensions doesn\'t exist:\n %s' % directory)
  537. continue
  538. sys.path.append(directory)
  539. for _finder, name, _ispkg in sorted(iter_modules([directory])):
  540. if name.endswith('_ext'):
  541. extensions.append((name, import_module(name)))
  542. # Load component manager if available and not explicitly disabled
  543. if os.getenv('IDF_COMPONENT_MANAGER', None) != '0':
  544. try:
  545. from idf_component_manager import idf_extensions
  546. extensions.append(('component_manager_ext', idf_extensions))
  547. os.environ['IDF_COMPONENT_MANAGER'] = '1'
  548. except ImportError:
  549. pass
  550. for name, extension in extensions:
  551. try:
  552. all_actions = merge_action_lists(all_actions, extension.action_extensions(all_actions, project_dir))
  553. except AttributeError:
  554. print('WARNING: Cannot load idf.py extension "%s"' % name)
  555. # Load extensions from project dir
  556. if os.path.exists(os.path.join(project_dir, "idf_ext.py")):
  557. sys.path.append(project_dir)
  558. try:
  559. from idf_ext import action_extensions
  560. except ImportError:
  561. print("Error importing extension file idf_ext.py. Skipping.")
  562. print("Please make sure that it contains implementation (even if it's empty) of add_action_extensions")
  563. try:
  564. all_actions = merge_action_lists(all_actions, action_extensions(all_actions, project_dir))
  565. except NameError:
  566. pass
  567. cli_help = (
  568. "ESP-IDF CLI build management tool. "
  569. "For commands that are not known to idf.py an attempt to execute it as a build system target will be made.")
  570. return CLI(help=cli_help, verbose_output=verbose_output, all_actions=all_actions)
  571. def main():
  572. checks_output = check_environment()
  573. cli = init_cli(verbose_output=checks_output)
  574. cli(sys.argv[1:], prog_name=PROG)
  575. def _valid_unicode_config():
  576. # Python 2 is always good
  577. if sys.version_info[0] == 2:
  578. return True
  579. # With python 3 unicode environment is required
  580. try:
  581. return codecs.lookup(locale.getpreferredencoding()).name != "ascii"
  582. except Exception:
  583. return False
  584. def _find_usable_locale():
  585. try:
  586. locales = subprocess.Popen(["locale", "-a"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
  587. except OSError:
  588. locales = ""
  589. if isinstance(locales, bytes):
  590. locales = locales.decode("ascii", "replace")
  591. usable_locales = []
  592. for line in locales.splitlines():
  593. locale = line.strip()
  594. locale_name = locale.lower().replace("-", "")
  595. # C.UTF-8 is the best option, if supported
  596. if locale_name == "c.utf8":
  597. return locale
  598. if locale_name.endswith(".utf8"):
  599. # Make a preference of english locales
  600. if locale.startswith("en_"):
  601. usable_locales.insert(0, locale)
  602. else:
  603. usable_locales.append(locale)
  604. if not usable_locales:
  605. raise FatalError(
  606. "Support for Unicode filenames is required, but no suitable UTF-8 locale was found on your system."
  607. " Please refer to the manual for your operating system for details on locale reconfiguration.")
  608. return usable_locales[0]
  609. if __name__ == "__main__":
  610. try:
  611. # On MSYS2 we need to run idf.py with "winpty" in order to be able to cancel the subprocesses properly on
  612. # keyboard interrupt (CTRL+C).
  613. # Using an own global variable for indicating that we are running with "winpty" seems to be the most suitable
  614. # option as os.environment['_'] contains "winpty" only when it is run manually from console.
  615. WINPTY_VAR = "WINPTY"
  616. WINPTY_EXE = "winpty"
  617. if ("MSYSTEM" in os.environ) and (not os.environ.get("_", "").endswith(WINPTY_EXE)
  618. and WINPTY_VAR not in os.environ):
  619. if 'menuconfig' in sys.argv:
  620. # don't use winpty for menuconfig because it will print weird characters
  621. main()
  622. else:
  623. os.environ[WINPTY_VAR] = "1" # the value is of no interest to us
  624. # idf.py calls itself with "winpty" and WINPTY global variable set
  625. ret = subprocess.call([WINPTY_EXE, sys.executable] + sys.argv, env=os.environ)
  626. if ret:
  627. raise SystemExit(ret)
  628. elif os.name == "posix" and not _valid_unicode_config():
  629. # Trying to find best utf-8 locale available on the system and restart python with it
  630. best_locale = _find_usable_locale()
  631. print(
  632. "Your environment is not configured to handle unicode filenames outside of ASCII range."
  633. " Environment variable LC_ALL is temporary set to %s for unicode support." % best_locale)
  634. os.environ["LC_ALL"] = best_locale
  635. ret = subprocess.call([sys.executable] + sys.argv, env=os.environ)
  636. if ret:
  637. raise SystemExit(ret)
  638. else:
  639. main()
  640. except FatalError as e:
  641. print(e)
  642. sys.exit(2)