blob: 233044cc0468592b478f70e465279b229496450c [file] [log] [blame]
Marti Bolivar8c447992019-01-30 13:53:40 -07001# Copyright (c) 2018 Foundries.io
2#
3# SPDX-License-Identifier: Apache-2.0
4
5'''Helpers shared by multiple west extension command modules.
6
7Note that common helpers used by the flash and debug extension
8commands are in run_common -- that's for common code used by
9commands which specifically execute runners.'''
10
Martí Bolívar6dab1632020-02-10 16:53:06 -080011import os
12from pathlib import Path
13
Marti Bolivar8c447992019-01-30 13:53:40 -070014from west import log
Marti Bolivar8c447992019-01-30 13:53:40 -070015from west.commands import WestCommand
16
Marti Bolivar8c095282019-01-30 20:30:12 -070017from runners.core import RunnerConfig
18
Torsten Rasmussene819fa42020-03-10 14:52:35 +010019# This relies on this file being zephyr/scripts/foo/bar.py.
20# If you move this file, you'll break it, so be careful.
21THIS_ZEPHYR = Path(__file__).parent.parent.parent
22ZEPHYR_BASE = Path(os.environ.get('ZEPHYR_BASE', THIS_ZEPHYR))
23
24# FIXME we need a nicer way to handle imports from scripts and cmake than this.
25ZEPHYR_SCRIPTS = ZEPHYR_BASE / 'scripts'
26ZEPHYR_CMAKE = ZEPHYR_BASE / 'cmake'
Marti Bolivar8c447992019-01-30 13:53:40 -070027
28class Forceable(WestCommand):
29 '''WestCommand subclass for commands with a --force option.'''
30
Ulf Magnussonbb634162019-09-04 16:28:50 +020031 @staticmethod
32 def add_force_arg(parser):
Marti Bolivar8c447992019-01-30 13:53:40 -070033 '''Add a -f / --force option to the parser.'''
34 parser.add_argument('-f', '--force', action='store_true',
35 help='Ignore any errors and try to proceed')
36
37 def check_force(self, cond, msg):
38 '''Abort if the command needs to be forced and hasn't been.
39
40 The "forced" predicate must be in self.args.forced.
41
42 If cond and self.args.force are both False, scream and die
43 with message msg. Otherwise, return. That is, "cond" is a
44 condition which means everything is OK; if it's False, only
45 self.args.force being True can allow execution to proceed.
46 '''
47 if not (cond or self.args.force):
48 log.err(msg)
49 log.die('refusing to proceed without --force due to above error')
Marti Bolivar8c095282019-01-30 20:30:12 -070050
51
52def cached_runner_config(build_dir, cache):
53 '''Parse the RunnerConfig from a build directory and CMake Cache.'''
54 board_dir = cache['ZEPHYR_RUNNER_CONFIG_BOARD_DIR']
Marti Bolivar814bc7b2019-06-03 04:00:22 -060055 elf_file = cache.get('ZEPHYR_RUNNER_CONFIG_KERNEL_ELF')
56 hex_file = cache.get('ZEPHYR_RUNNER_CONFIG_KERNEL_HEX')
57 bin_file = cache.get('ZEPHYR_RUNNER_CONFIG_KERNEL_BIN')
Marti Bolivar8c095282019-01-30 20:30:12 -070058 gdb = cache.get('ZEPHYR_RUNNER_CONFIG_GDB')
59 openocd = cache.get('ZEPHYR_RUNNER_CONFIG_OPENOCD')
60 openocd_search = cache.get('ZEPHYR_RUNNER_CONFIG_OPENOCD_SEARCH')
61
62 return RunnerConfig(build_dir, board_dir,
63 elf_file, hex_file, bin_file,
64 gdb=gdb, openocd=openocd,
65 openocd_search=openocd_search)