zephyr/scripts/west_commands/zephyr_ext_common.py
Torsten Rasmussen e819fa46cd scripts: west-commands: Updates needed when removing ZEPHYR_BASE env
This commit includes the following fixes in order to remove environment
setting of ZEPHYR_BASE is west extension commands.

- Build command
  west build --pristine will now use the ZEPHYR_BASE variable found
  in CMakeCache.txt in the build folder.

  This ensures that the pristine command is executed from the same
  Zephyr that was used for compilation.

- Board command
  The west boards command no longer sets Zephyr base before invoking
  cmake -P cmake/boards.cmake

  Instead boards.cmake uses find_package(Zephyr) to ensure consistent
  behavior with Zephyr samples, so that the detection of Zephyr base is
  uniform across CMake commands.

  It also changes BOARD_ROOT_SPACE_SEPARATED to BOARD_ROOT in order to
  be consistent with existing user documentation.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-03-27 16:23:46 +01:00

66 lines
2.4 KiB
Python

# Copyright (c) 2018 Foundries.io
#
# SPDX-License-Identifier: Apache-2.0
'''Helpers shared by multiple west extension command modules.
Note that common helpers used by the flash and debug extension
commands are in run_common -- that's for common code used by
commands which specifically execute runners.'''
import os
from pathlib import Path
from west import log
from west.commands import WestCommand
from runners.core import RunnerConfig
# This relies on this file being zephyr/scripts/foo/bar.py.
# If you move this file, you'll break it, so be careful.
THIS_ZEPHYR = Path(__file__).parent.parent.parent
ZEPHYR_BASE = Path(os.environ.get('ZEPHYR_BASE', THIS_ZEPHYR))
# FIXME we need a nicer way to handle imports from scripts and cmake than this.
ZEPHYR_SCRIPTS = ZEPHYR_BASE / 'scripts'
ZEPHYR_CMAKE = ZEPHYR_BASE / 'cmake'
class Forceable(WestCommand):
'''WestCommand subclass for commands with a --force option.'''
@staticmethod
def add_force_arg(parser):
'''Add a -f / --force option to the parser.'''
parser.add_argument('-f', '--force', action='store_true',
help='Ignore any errors and try to proceed')
def check_force(self, cond, msg):
'''Abort if the command needs to be forced and hasn't been.
The "forced" predicate must be in self.args.forced.
If cond and self.args.force are both False, scream and die
with message msg. Otherwise, return. That is, "cond" is a
condition which means everything is OK; if it's False, only
self.args.force being True can allow execution to proceed.
'''
if not (cond or self.args.force):
log.err(msg)
log.die('refusing to proceed without --force due to above error')
def cached_runner_config(build_dir, cache):
'''Parse the RunnerConfig from a build directory and CMake Cache.'''
board_dir = cache['ZEPHYR_RUNNER_CONFIG_BOARD_DIR']
elf_file = cache.get('ZEPHYR_RUNNER_CONFIG_KERNEL_ELF')
hex_file = cache.get('ZEPHYR_RUNNER_CONFIG_KERNEL_HEX')
bin_file = cache.get('ZEPHYR_RUNNER_CONFIG_KERNEL_BIN')
gdb = cache.get('ZEPHYR_RUNNER_CONFIG_GDB')
openocd = cache.get('ZEPHYR_RUNNER_CONFIG_OPENOCD')
openocd_search = cache.get('ZEPHYR_RUNNER_CONFIG_OPENOCD_SEARCH')
return RunnerConfig(build_dir, board_dir,
elf_file, hex_file, bin_file,
gdb=gdb, openocd=openocd,
openocd_search=openocd_search)