From 68e5933e971f9b4fe5af1539aa94e3c8aae3b910 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Thu, 10 May 2018 15:28:17 -0400 Subject: [PATCH] scripts: west: add build directory to runner configuration Add the build directory to the central runner configuration. This is commonly useful information. The first place we can use it is to eliminate guessing the current working directory when building objects to parse .config. It's not necessary to modify the build system for this, so leave the relevant command line flag among the general options. Signed-off-by: Marti Bolivar --- scripts/meta/west/cmd/run_common.py | 22 ++++++++-------------- scripts/meta/west/runner/core.py | 10 +++++++--- scripts/meta/west/runner/dfu.py | 2 +- scripts/meta/west/runner/jlink.py | 2 +- scripts/meta/west/runner/pyocd.py | 2 +- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/scripts/meta/west/cmd/run_common.py b/scripts/meta/west/cmd/run_common.py index d40278a4cb8..597351f3822 100644 --- a/scripts/meta/west/cmd/run_common.py +++ b/scripts/meta/west/cmd/run_common.py @@ -6,7 +6,7 @@ ''' import argparse -from os import getcwd, chdir +from os import getcwd, path from subprocess import CalledProcessError from textwrap import dedent @@ -24,6 +24,7 @@ def add_parser_common(parser_adder, command): description=command.description) group = parser.add_argument_group(title='General Options') + group.add_argument('-d', '--build-dir', help='''Build directory to obtain runner information from; default is the current working directory.''') @@ -90,8 +91,8 @@ def desc_common(command_name): '''.format(**{'command': command_name})) -def cached_runner_config(cache): - '''Parse the RunnerConfig from a CMake Cache.''' +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'] kernel_elf = cache['ZEPHYR_RUNNER_CONFIG_KERNEL_ELF'] kernel_hex = cache['ZEPHYR_RUNNER_CONFIG_KERNEL_HEX'] @@ -100,7 +101,8 @@ def cached_runner_config(cache): openocd = cache.get('ZEPHYR_RUNNER_CONFIG_OPENOCD') openocd_search = cache.get('ZEPHYR_RUNNER_CONFIG_OPENOCD_SEARCH') - return RunnerConfig(board_dir, kernel_elf, kernel_hex, kernel_bin, + return RunnerConfig(build_dir, board_dir, + kernel_elf, kernel_hex, kernel_bin, gdb=gdb, openocd=openocd, openocd_search=openocd_search) @@ -130,21 +132,13 @@ def do_run_common(command, args, runner_args, cached_runner_var): 'current directory {} failed'.format(command_name, build_dir)) - # Temporary hack: we need to ensure we're running from the build - # directory for now. Otherwise, the BuildConfiguration objects - # that get created by the runners look for .config in the wrong - # places. - chdir(build_dir) - # Runner creation, phase 1. # # Get the default runner name from the cache, allowing a command # line override. Get the ZephyrBinaryRunner class by name, and # make sure it supports the command. - # TODO: build this by joining with build_dir once the above chdir - # goes away. - cache_file = args.cmake_cache + cache_file = path.join(build_dir, args.cmake_cache) cache = cmake.CMakeCache(cache_file) board = cache['CACHED_BOARD'] available = cache.get_list('ZEPHYR_RUNNERS') @@ -175,7 +169,7 @@ def do_run_common(command, args, runner_args, cached_runner_var): # - Pull the RunnerConfig out of the cache # - Override cached values with applicable command-line options - cfg = cached_runner_config(cache) + cfg = cached_runner_config(build_dir, cache) _override_config_from_namespace(cfg, args) # Runner creation, phase 3. diff --git a/scripts/meta/west/runner/core.py b/scripts/meta/west/runner/core.py index 8cdff1f5f55..5d5b1ad0a3d 100644 --- a/scripts/meta/west/runner/core.py +++ b/scripts/meta/west/runner/core.py @@ -191,16 +191,20 @@ class RunnerConfig: This class's __slots__ contains exactly the configuration variables. ''' - __slots__ = ['board_dir', 'kernel_elf', 'kernel_hex', 'kernel_bin', - 'gdb', 'openocd', 'openocd_search'] + __slots__ = ['build_dir', 'board_dir', 'kernel_elf', 'kernel_hex', + 'kernel_bin', 'gdb', 'openocd', 'openocd_search'] # TODO: revisit whether we can get rid of some of these. Having # tool-specific configuration options here is a layering # violation, but it's very convenient to have a single place to # store the locations of tools (like gdb and openocd) that are # needed by multiple ZephyrBinaryRunner subclasses. - def __init__(self, board_dir, kernel_elf, kernel_hex, kernel_bin, + def __init__(self, build_dir, board_dir, + kernel_elf, kernel_hex, kernel_bin, gdb=None, openocd=None, openocd_search=None): + self.build_dir = build_dir + '''Zephyr application build directory''' + self.board_dir = board_dir '''Zephyr board directory''' diff --git a/scripts/meta/west/runner/dfu.py b/scripts/meta/west/runner/dfu.py index 78f2b970b31..6063ad55622 100644 --- a/scripts/meta/west/runner/dfu.py +++ b/scripts/meta/west/runner/dfu.py @@ -73,7 +73,7 @@ class DfuUtilBinaryRunner(ZephyrBinaryRunner): if args.dfuse: args.dt_flash = True # --dfuse implies --dt-flash. - build_conf = BuildConfiguration(os.getcwd()) + build_conf = BuildConfiguration(cfg.build_dir) dcfg = DfuSeConfig(address=cls.get_flash_address(args, build_conf), options=args.dfuse_modifiers) else: diff --git a/scripts/meta/west/runner/jlink.py b/scripts/meta/west/runner/jlink.py index 4729278488a..7aadf62a6e9 100644 --- a/scripts/meta/west/runner/jlink.py +++ b/scripts/meta/west/runner/jlink.py @@ -68,7 +68,7 @@ class JLinkBinaryRunner(ZephyrBinaryRunner): @classmethod def create(cls, cfg, args): - build_conf = BuildConfiguration(os.getcwd()) + build_conf = BuildConfiguration(cfg.build_dir) flash_addr = cls.get_flash_address(args, build_conf) return JLinkBinaryRunner(cfg, args.device, commander=args.commander, diff --git a/scripts/meta/west/runner/pyocd.py b/scripts/meta/west/runner/pyocd.py index c86c1d18cbd..b8a6ee4ed4a 100644 --- a/scripts/meta/west/runner/pyocd.py +++ b/scripts/meta/west/runner/pyocd.py @@ -86,7 +86,7 @@ class PyOcdBinaryRunner(ZephyrBinaryRunner): daparg), level=log.VERBOSE_VERY) args.daparg = daparg - build_conf = BuildConfiguration(os.getcwd()) + build_conf = BuildConfiguration(cfg.build_dir) flash_addr = cls.get_flash_address(args, build_conf) return PyOcdBinaryRunner(