tools: idf.py: enable CLICOLOR_FORCE for interactive builds

If stdout is a TTY (meaning that the output is not redirected), tell
the build tool (GNU Make or Ninja) to enable colorized output.

GNU Make and Ninja also check if their stdout is redirected and
strip color escape sequences in that case. CLICOLOR_FORCE environment
variable overrides this behavior.

With this change, if the compiler was launched with the
-fcolor-diagnostics flag and idf.py output is not redirected, the
final output in the terminal will be colorized.

(-fcolor-diagnostics is handled at CMake level by the previous commit)
This commit is contained in:
Ivan Grokhotkov 2022-08-01 20:24:02 +02:00
parent 41cffbb2f2
commit 12abe1f316
No known key found for this signature in database
GPG Key ID: 1E050E141B280628
2 changed files with 8 additions and 6 deletions

View File

@ -12,7 +12,6 @@ GENERATORS: Dict[str, Union[str, Dict, list]] = collections.OrderedDict([
# - dry_run: command to run in dry run mode
# - verbose_flag: verbose flag
# - force_progression: one liner status of the progress
# - envvar: environment variables
('Ninja', {
'command': ['ninja'],
'version': ['ninja', '--version'],
@ -20,7 +19,6 @@ GENERATORS: Dict[str, Union[str, Dict, list]] = collections.OrderedDict([
'verbose_flag': '-v',
# as opposed to printing the status updates each in a in new line
'force_progression': True,
'envvar': {}
}),
])
@ -30,9 +28,7 @@ if os.name != 'nt':
'version': [MAKE_CMD, '--version'],
'dry_run': [MAKE_CMD, '-n'],
'verbose_flag': 'VERBOSE=1',
'force_progression': False,
# CLICOLOR_FORCE if set forcing make to print ANSI escape sequence
'envvar': {'CLICOLOR_FORCE': '1'}}
'force_progression': False}
URL_TO_DOC = 'https://docs.espressif.com/projects/esp-idf'

View File

@ -300,11 +300,17 @@ def run_target(target_name: str, args: 'PropertyDict', env: Optional[Dict]=None,
env = {}
generator_cmd = GENERATORS[args.generator]['command']
env.update(GENERATORS[args.generator]['envvar'])
if args.verbose:
generator_cmd += [GENERATORS[args.generator]['verbose_flag']]
# By default, GNU Make and Ninja strip away color escape sequences when they see that their stdout is redirected.
# If idf.py's stdout is not redirected, the final output is a TTY, so we can tell Make/Ninja to disable stripping
# of color escape sequences. (Requires Ninja v1.9.0 or later.)
if sys.stdout.isatty():
if 'CLICOLOR_FORCE' not in env:
env['CLICOLOR_FORCE'] = '1'
RunTool(generator_cmd[0], generator_cmd + [target_name], args.build_dir, env, custom_error_handler, hints=not args.no_hints,
force_progression=force_progression, interactive=interactive)()