|
| 1 | +# Copyright (c) 2018 Open Source Foundries Limited. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +'''Common code used by commands which execute runners. |
| 6 | +''' |
| 7 | + |
| 8 | +import argparse |
| 9 | +from os import getcwd, chdir |
| 10 | +from subprocess import CalledProcessError |
| 11 | +from textwrap import dedent |
| 12 | + |
| 13 | +from .. import cmake |
| 14 | +from .. import log |
| 15 | +from ..runner import get_runner_cls |
| 16 | +from . import CommandContextError |
| 17 | + |
| 18 | + |
| 19 | +def add_parser_common(parser_adder, command): |
| 20 | + parser = parser_adder.add_parser( |
| 21 | + command.name, |
| 22 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 23 | + description=command.description) |
| 24 | + |
| 25 | + parser.add_argument('-d', '--build-dir', |
| 26 | + help='''Build directory to obtain runner information |
| 27 | + from; default is the current working directory.''') |
| 28 | + parser.add_argument('-c', '--cmake-cache', default=cmake.DEFAULT_CACHE, |
| 29 | + help='''Path to CMake cache file containing runner |
| 30 | + configuration (this is generated by the Zephyr |
| 31 | + build system when compiling binaries); |
| 32 | + default: {}. |
| 33 | +
|
| 34 | + If this is a relative path, it is assumed relative to |
| 35 | + the build directory. An absolute path can also be |
| 36 | + given instead.'''.format(cmake.DEFAULT_CACHE)) |
| 37 | + parser.add_argument('-r', '--runner', |
| 38 | + help='''If given, overrides any cached {} |
| 39 | + runner.'''.format(command.name)) |
| 40 | + parser.add_argument('--skip-rebuild', action='store_true', |
| 41 | + help='''If given, do not rebuild the application |
| 42 | + before running {} commands.'''.format(command.name)) |
| 43 | + |
| 44 | + return parser |
| 45 | + |
| 46 | + |
| 47 | +def desc_common(command_name): |
| 48 | + return dedent('''\ |
| 49 | + Any options not recognized by this command are passed to the |
| 50 | + back-end {command} runner. |
| 51 | +
|
| 52 | + If you need to pass an option to a runner which has the |
| 53 | + same name as one recognized by this command, you can |
| 54 | + end argument parsing with a '--', like so: |
| 55 | +
|
| 56 | + west {command} --{command}-arg=value -- --runner-arg=value2 |
| 57 | + '''.format(**{'command': command_name})) |
| 58 | + |
| 59 | + |
| 60 | +def do_run_common(command, args, runner_args, cached_runner_var): |
| 61 | + command_name = command.name |
| 62 | + build_dir = args.build_dir or getcwd() |
| 63 | + |
| 64 | + if not args.skip_rebuild: |
| 65 | + try: |
| 66 | + cmake.run_build(build_dir) |
| 67 | + except CalledProcessError: |
| 68 | + if args.build_dir: |
| 69 | + log.die('cannot run {}, build in {} failed'.format( |
| 70 | + command_name, args.build_dir)) |
| 71 | + else: |
| 72 | + log.die('cannot run {}; no --build-dir given and build in ' |
| 73 | + 'current directory {} failed'.format(command_name, |
| 74 | + build_dir)) |
| 75 | + |
| 76 | + # Temporary hack: we need to ensure we're running from the build |
| 77 | + # directory for now. Otherwise, the BuildConfiguration objects |
| 78 | + # that get created by the runners look for .config in the wrong |
| 79 | + # places. |
| 80 | + chdir(build_dir) |
| 81 | + |
| 82 | + # TODO: build this by joining with build_dir once the above chdir |
| 83 | + # goes away. |
| 84 | + cache_file = args.cmake_cache |
| 85 | + cache = cmake.CMakeCache(cache_file) |
| 86 | + board = cache['CACHED_BOARD'] |
| 87 | + available = cache.get_list('ZEPHYR_RUNNERS') |
| 88 | + if not available: |
| 89 | + log.wrn('No cached runners are available in', cache_file) |
| 90 | + runner = args.runner or cache.get(cached_runner_var) |
| 91 | + |
| 92 | + if runner is None: |
| 93 | + raise CommandContextError(dedent(""" |
| 94 | + No {} runner available for {}. Please either specify one |
| 95 | + manually, or check your board's documentation for |
| 96 | + alternative instructions.""".format(command_name, board))) |
| 97 | + |
| 98 | + log.inf('Using runner:', runner) |
| 99 | + if runner not in available: |
| 100 | + log.wrn('Runner {} is not configured for use with {}, ' |
| 101 | + 'this may not work'.format(runner, board)) |
| 102 | + runner_cls = get_runner_cls(runner) |
| 103 | + if command_name not in runner_cls.capabilities().commands: |
| 104 | + log.die('Runner {} does not support command {}'.format( |
| 105 | + runner, command_name)) |
| 106 | + |
| 107 | + cached_common_args = cache.get_list('ZEPHYR_RUNNER_ARGS_COMMON') |
| 108 | + cached_runner_args = cache.get_list( |
| 109 | + 'ZEPHYR_RUNNER_ARGS_{}'.format(cmake.make_c_identifier(runner))) |
| 110 | + # Construct the final command line arguments, create a |
| 111 | + # runner-specific parser to handle them, and run the command. |
| 112 | + assert isinstance(runner_args, list), runner_args |
| 113 | + final_runner_args = (cached_common_args + cached_runner_args + |
| 114 | + runner_args + [command_name]) |
| 115 | + |
| 116 | + # Having the runners themselves be the place where their argument |
| 117 | + # parsing is handled is hackish; it's an artifact of the time |
| 118 | + # before the runner package was part of west. |
| 119 | + # |
| 120 | + # TODO: refactor runner argument parsing higher up into west. |
| 121 | + parser = argparse.ArgumentParser(prog=runner) |
| 122 | + runner_cls.add_parser(parser) |
| 123 | + parsed_args = parser.parse_args(args=final_runner_args) |
| 124 | + parsed_args.verbose = args.verbose |
| 125 | + runner = runner_cls.create_from_args(parsed_args) |
| 126 | + runner.run(command_name) |
0 commit comments