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 <marti.bolivar@nordicsemi.no>
This commit is contained in:
Marti Bolivar 2019-05-01 17:24:23 -06:00 committed by Anas Nashif
parent a1ef6962ae
commit 8465cf25c8
2 changed files with 40 additions and 11 deletions

View file

@ -18,7 +18,7 @@ _ARG_SEPARATOR = '--'
BUILD_USAGE = '''\
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 ...]]
'''
BUILD_DESCRIPTION = '''\
@ -139,6 +139,10 @@ class Build(Forceable):
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.''')
self.add_force_arg(parser)
return parser
@ -409,7 +413,7 @@ class Build(Forceable):
DEFAULT_CMAKE_GENERATOR))]
if cmake_opts:
final_cmake_args.extend(cmake_opts)
run_cmake(final_cmake_args)
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 @@ class Build(Forceable):
'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,
dry_run=self.args.dry_run)

View file

@ -24,12 +24,22 @@ DEFAULT_CACHE = 'CMakeCache.txt'
DEFAULT_CMAKE_GENERATOR = 'Ninja'
'''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),
capture_output=capture_output)
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):