diff --git a/scripts/west_commands/build_helpers.py b/scripts/west_commands/build_helpers.py index 7f3b7021de5..3303bc67cbb 100644 --- a/scripts/west_commands/build_helpers.py +++ b/scripts/west_commands/build_helpers.py @@ -21,22 +21,25 @@ DEFAULT_CMAKE_GENERATOR = 'Ninja' '''Name of the default CMake generator.''' BUILD_DIR_DESCRIPTION = '''\ -Build directory. If missing and run in a Zephyr build directory, it is -used; otherwise, it's "{}".'''.format( - DEFAULT_BUILD_DIR) +Build directory. If not given, {}/ is used; otherwise if the current directory +is a Zephyr build directory, it is used.'''.format(DEFAULT_BUILD_DIR) def find_build_dir(dir): '''Heuristic for finding a build directory. - If the given argument is truthy, it is returned. Otherwise, if + If the given argument is truthy, it is returned. Otherwise if the + DEFAULT_BUILD_DIR is a build directory, it is returned. Next, if the current working directory is a build directory, it is - returned. Otherwise, DEFAULT_BUILD_DIR is returned.''' + returned. Finally, DEFAULT_BUILD_DIR is returned.''' if dir: build_dir = dir else: cwd = os.getcwd() - if is_zephyr_build(cwd): + default = os.path.join(cwd, DEFAULT_BUILD_DIR) + if is_zephyr_build(default): + build_dir = default + elif is_zephyr_build(cwd): build_dir = cwd else: build_dir = DEFAULT_BUILD_DIR diff --git a/scripts/west_commands/run_common.py b/scripts/west_commands/run_common.py index 5c4f8579f13..1b36b66cb29 100644 --- a/scripts/west_commands/run_common.py +++ b/scripts/west_commands/run_common.py @@ -13,7 +13,7 @@ import textwrap from west import cmake from west import log from west import util -from west.build import DEFAULT_BUILD_DIR, is_zephyr_build +from build_helpers import find_build_dir, is_zephyr_build from west.commands import CommandContextError from runners import get_runner_cls, ZephyrBinaryRunner @@ -128,16 +128,14 @@ def _build_dir(args, die_if_none=True): if args.build_dir: return args.build_dir - cwd = getcwd() - default = path.join(cwd, DEFAULT_BUILD_DIR) - if is_zephyr_build(default): - return default - elif is_zephyr_build(cwd): - return cwd + dir = find_build_dir(None) + + if is_zephyr_build(dir): + return dir elif die_if_none: log.die('--build-dir was not given, and neither {} ' 'nor {} are zephyr build directories.'. - format(default, cwd)) + format(getcwd(), dir)) else: return None