zephyr/scripts/support/zephyr_flash_debug.py
Sebastian Bøe 4971d2a084 cmake: Fix "make VERBOSE=1"
It is desired behaviour that when 'make VERBOSE=1' is invoked, the
underlying scripts will be invoked in 'verbose mode', e.g. they have
the flag --verbose passed to them.

This patch modifies all the underlying scripts in the build system to
inject --verbose into the command line when the environment variable
VERBOSE is set.

The environment variable VERBOSE is a CMake concept. CMake will
generate Makefile's that read this environment variable and try to
behave accordingly. Unfortunately, the generated ninja build systems
behave differently and will have to be invoked like this:

VERBOSE=1 ninja -v

This fixes #4851

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
2018-01-10 22:14:53 -05:00

110 lines
3.5 KiB
Python
Executable file

#! /usr/bin/env python3
# Copyright (c) 2017 Linaro Limited.
# Copyright (c) 2017 Open Source Foundries Limited.
#
# SPDX-License-Identifier: Apache-2.0
"""Zephyr flash/debug script
This helper script is the build system's entry point to Zephyr's
"runner" Python package. This package provides ZephyrBinaryRunner,
which is a standard interface for flashing and debugging boards
supported by Zephyr, as well as backend-specific scripts for tools
such as OpenOCD, pyOCD, etc.
"""
import argparse
import functools
import sys
import os
from runner.core import ZephyrBinaryRunner
def print_runners_handler(args):
for cls in ZephyrBinaryRunner.get_runners():
print(cls.name())
def runner_handler(cls, args):
runner = cls.create_from_args(args)
# This relies on ZephyrBinaryRunner.add_parser() having command as
# its single positional argument; see its docstring for details.
runner.run(args.command)
def main():
# Argument handling is split into a two-level structure, with
# common options to the script first, then a sub-command (i.e. a
# runner name), then options and arguments for that sub-command
# (like 'flash --some-option=value').
#
# For top-level help (including a list of runners), run:
#
# $ZEPHYR_BASE/.../SCRIPT.py -h
#
# For help on a particular RUNNER (like 'pyocd'), run:
#
# $ZEPHYR_BASE/.../SCRIPT.py RUNNER -h
#
# For verbose output, use:
#
# $ZEPHYR_BASE/.../SCRIPT.py [-v|--verbose] RUNNER [--runner-options]
#
# Note that --verbose comes *before* RUNNER, not after!
#
# Other commands (for now just the "runners" command, which prints
# the available runners) are handled the same way:
#
# $ZEPHYR_BASE/.../SCRIPT.py runners
top_parser = argparse.ArgumentParser()
top_parser.add_argument('-v', '--verbose',
default=False, action='store_true',
help='If set, enable verbose output.')
sub_parsers = top_parser.add_subparsers(dest='top_command')
# Handlers for each subcommand.
handlers = {}
# The 'runners' command just prints the runners. It takes no arguments.
sub_parsers.add_parser('runners')
handlers['runners'] = print_runners_handler
# Add a sub-command for each runner. (It's a bit hackish for runners
# to know about argparse, but it's good enough for now.)
for cls in ZephyrBinaryRunner.get_runners():
if cls.name() in handlers:
print('Runner {} name is already a top-level command'.format(
cls.name()),
file=sys.sterr)
sys.exit(1)
sub_parser = sub_parsers.add_parser(cls.name())
cls.add_parser(sub_parser)
handlers[cls.name()] = functools.partial(runner_handler, cls)
args = top_parser.parse_args()
if "VERBOSE" in os.environ:
args.verbose = 1
if args.top_command is None:
choices = ', '.join(handlers.keys())
print('Missing command or runner; choices: {}'.format(choices),
file=sys.stderr)
sys.exit(1)
try:
handlers[args.top_command](args)
except Exception as e:
if args.verbose:
raise
else:
print('Error: {}'.format(e), file=sys.stderr)
print(('(Re-run as "{} --verbose {} ..." '
'or set CMAKE_VERBOSE_MAKEFILE for a stack trace.)').format(
sys.argv[0], args.top_command),
file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()