Skip to content
Browse files

scripts: add --dry-run flags to west build

Analogously to the Make options with the same names, these print the
commands which would have run without running them.

Signed-off-by: Marti Bolivar <>
  • Loading branch information...
mbolivar authored and nashif committed May 1, 2019
1 parent a1ef696 commit 8465cf25c8e2b4cd3c2f840844652eb6b66bdbb0
Showing with 40 additions and 11 deletions.
  1. +9 −4 scripts/west_commands/
  2. +31 −7 scripts/west_commands/
@@ -18,7 +18,7 @@
west build [-h] [-b BOARD] [-d BUILD_DIR]
[-t TARGET] [-p {auto, always, never}] [-c] [--cmake-only]
[-f] [source_dir] -- [cmake_opt [cmake_opt ...]]
[-n] [-f] [source_dir] -- [cmake_opt [cmake_opt ...]]

@@ -139,6 +139,10 @@ def do_add_parser(self, parser_adder):
help='Force CMake to run')
parser.add_argument('--cmake-only', action='store_true',
help="Just run CMake; don't build. Implies -c.")
parser.add_argument('-n', '--just-print', '--dry-run', '--recon',
dest='dry_run', action='store_true',
help='''Just print the build commands which would
have run, without actually running them.''')
return parser

@@ -409,7 +413,7 @@ def _run_cmake(self, board, origin, cmake_opts):
if cmake_opts:
run_cmake(final_cmake_args, dry_run=self.args.dry_run)

def _run_pristine(self):
log.inf('Making build dir {} pristine'.format(self.build_dir))
@@ -424,8 +428,9 @@ def _run_pristine(self):
'Zephyr build system')

cmake_args = ['-P', '{}/cmake/pristine.cmake'.format(zb)]
run_cmake(cmake_args, cwd=self.build_dir)
run_cmake(cmake_args, cwd=self.build_dir, dry_run=self.args.dry_run)

def _run_build(self, target):
extra_args = ['--target', target] if target else []
run_build(self.build_dir, extra_args=extra_args)
run_build(self.build_dir, extra_args=extra_args,
@@ -24,12 +24,22 @@
'''Name of the default CMake generator.'''

def run_cmake(args, cwd=None, capture_output=False):
'''Run cmake to (re)generate a build system.

def run_cmake(args, cwd=None, capture_output=False, dry_run=False):
'''Run cmake to (re)generate a build system, a script, etc.
:param args: arguments to pass to CMake
:param cwd: directory to run CMake in, cwd is default
:param capture_output: if True, the output is returned instead of being
displayed (None is returned by default, or if
dry_run is also True)
:param dry_run: don't actually execute the command, just print what
would have been run
If capture_output is set to True, returns the output of the command instead
of displaying it on stdout/stderr..'''
cmake = shutil.which('cmake')
if cmake is None:
if cmake is None and not dry_run:
log.die('CMake is not installed or cannot be found; cannot build.')
cmd = [cmake] + args
kwargs = dict()
@@ -39,6 +49,12 @@ def run_cmake(args, cwd=None, capture_output=False):
kwargs['stderr'] = subprocess.STDOUT
if cwd:
kwargs['cwd'] = cwd

if dry_run:
in_cwd = ' (in {})'.format(cwd) if cwd else ''
log.inf('Dry run{}:'.format(in_cwd), quote_sh_list(cmd))
return None

log.dbg('Running CMake:', quote_sh_list(cmd), level=log.VERBOSE_NORMAL)
p = subprocess.Popen(cmd, **kwargs)
out, err = p.communicate()
@@ -52,10 +68,18 @@ def run_cmake(args, cwd=None, capture_output=False):
raise subprocess.CalledProcessError(p.returncode, p.args)

def run_build(build_directory, extra_args=(), cwd=None, capture_output=False):
'''Run cmake in build tool mode in `build_directory`'''
run_cmake(['--build', build_directory] + list(extra_args),
def run_build(build_directory, **kwargs):
'''Run cmake in build tool mode.
:param build_directory: runs "cmake --build build_directory"
:param extra_args: optional kwarg. List of additional CMake arguments;
these come after "--build <build_directory>"
on the command line.
Any additional keyword arguments are passed as-is to run_cmake().
extra_args = kwargs.pop('extra_args', [])
return run_cmake(['--build', build_directory] + extra_args, **kwargs)

def make_c_identifier(string):

0 comments on commit 8465cf2

Please sign in to comment.
You can’t perform that action at this time.