zephyr/scripts/support/zephyr_flash_debug.py
Marti Bolivar e33ec242fd scripts: refactor flash/debug scripts to remove "shell"
The Python-based runners have replaced the old shell scripts. Refactor
the build system accordingly:

- FLASH_SCRIPT is now BOARD_FLASH_RUNNER
- DEBUG_SCRIPT is now BOARD_DEBUG_RUNNER

The values, rather than being the names of files, are now the names of
runners in scripts/support/runner. They are still short, descriptive
names like "openocd", "jlink", "em-starterkit", etc.

Adjust the zephyr_flash_debug.py call and runner internals
accordingly. Have each runner class report a name and the commands it
can handle. This lets us move some boilerplate from each do_run()
method into the common run() routine, and enables further improvements
in future patches.

The handles_command() method is temporary, and will be replaced by a
more general mechanism for describing runner capabilities in a
subsequent patch. The initial use case for extending this is to add
device tree awareness to the runners.

To try to avoid user confusion, abort the configuration if an
xxx_SCRIPT is defined.

Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2017-12-15 09:57:30 -05:00

58 lines
1.8 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 sys
from runner.core import ZephyrBinaryRunner, get_env_bool_or
# TODO: Stop using environment variables.
#
# Migrate the build system so we can use an argparse.ArgumentParser and
# per-flasher subparsers, so invoking the script becomes something like:
#
# python zephyr_flash_debug.py openocd --openocd-bin=/openocd/path ...
#
# For now, maintain compatibility.
def run(runner_name, command, debug):
try:
runner = ZephyrBinaryRunner.create_runner(runner_name, command, debug)
except ValueError:
print('runner {} is not available or does not support {}'.format(
runner_name, command),
file=sys.stderr)
raise
runner.run(command)
if __name__ == '__main__':
commands = {'flash', 'debug', 'debugserver'}
debug = True
try:
debug = get_env_bool_or('VERBOSE', False)
if len(sys.argv) != 3 or sys.argv[2] not in commands:
raise ValueError('usage: {} <runner-name> <{}>'.format(
sys.argv[0], '|'.join(commands)))
run(sys.argv[1], sys.argv[2], debug)
except Exception as e:
if debug:
raise
else:
print('Error: {}'.format(e), file=sys.stderr)
print('Re-run with VERBOSE=1 for a stack trace.',
file=sys.stderr)
sys.exit(1)