scripts: west: Run pristine.cmake directly instead of the target

When making a build folder pristine until now we were running the
'pristine' build target. The issue with that is that ninja/make or
whatever build tool is being used might decide to re-run CMake itself if
some of the dependencies have changes. This might trigger an error that
is unfriendly and unnecessary, since the user is explicitly asking for
the build folder to be wiped before starting a fresh build.
To avoid this issue restor to running directly the CMake script that the
'pristine' build target itself uses, so as to make sure that the build
folder is wiped unconditionally regardless of changes made to the tree.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
This commit is contained in:
Carles Cufi 2019-04-18 16:46:12 +02:00 committed by Carles Cufí
parent 01a2bebc83
commit 3a88dce9c5
2 changed files with 38 additions and 17 deletions

View file

@ -165,14 +165,15 @@ extension commands (found in :file:`scripts/west_commands`).
* - Option
- Description
* - ``build.pristine``
- String. Controls the way in which ``west build`` may run the ``pristine``
target before building. Can take the following values:
- String. Controls the way in which ``west build`` may clean the build
folder before building. Can take the following values:
- ``never`` (default): Never automatically run the ``pristine`` target.
- ``auto``: ``west build`` will automatically run the ``pristine``
target before building, if a build system is present and the build
will fail otherwise (e.g. the user has specified a different board or
application from the one previously used to make the build
- ``never`` (default): Never automatically make the build folder
pristine.
- ``auto``: ``west build`` will automatically make the build folder
pristine before building, if a build system is present and the build
would fail otherwise (e.g. the user has specified a different board
or application from the one previously used to make the build
directory).
- ``always``: Always run the ``pristine`` target before building, if
- ``always``: Always make the build folder pristine before building, if
a build system is present.

View file

@ -4,6 +4,8 @@
import argparse
import os
import shutil
import subprocess
from west import log
from west import cmake
@ -113,11 +115,11 @@ class Build(Forceable):
'clean', 'pristine', etc.)''')
parser.add_argument('-p', '--pristine', choices=['auto', 'always',
'never'], action=AlwaysIfMissing, nargs='?',
help='''Control whether the pristine target is run
before building if a build system is present in the
build dir. --pristine is the same as
--pristine=always. If set to auto, the pristine
target will be run only if required based on the
help='''Control whether the build folder is made
pristine before building if a build system is
present in the build dir. --pristine is the same as
--pristine=always. If set to auto, the build folder
will be made pristine only if required based on the
existing build system and the options provided.
This allows for reusing a build folder even if it
contains build files for a different board or
@ -159,8 +161,7 @@ class Build(Forceable):
self.auto_pristine))
if is_zephyr_build(self.build_dir):
if pristine == 'always':
log.inf('Making build dir {} pristine'.format(self.build_dir))
self._run_build('pristine')
self._run_pristine()
self.run_cmake = True
else:
self._update_cache()
@ -338,8 +339,7 @@ class Build(Forceable):
format(self.build_dir, cached_board, self.args.board))
if self.auto_pristine and (apps_mismatched or boards_mismatched):
log.inf('Making build dir {} pristine'.format(self.build_dir))
self._run_build('pristine')
self._run_pristine()
self.cmake_cache = None
log.dbg('run_cmake:', True, level=log.VERBOSE_EXTREME)
self.run_cmake = True
@ -373,6 +373,26 @@ class Build(Forceable):
final_cmake_args.extend(cmake_opts)
cmake.run_cmake(final_cmake_args)
def _run_pristine(self):
log.inf('Making build dir {} pristine'.format(self.build_dir))
zb = os.environ.get('ZEPHYR_BASE')
if not zb:
log.die('Internal error: ZEPHYR_BASE not set in the environment, '
'and should have been by the main script')
if not is_zephyr_build(self.build_dir):
log.die('Refusing to run pristine on a folder that is not a Zephyr '
'build system')
cmake_args = ['-P', '{}/cmake/pristine.cmake'.format(zb)]
cmake = shutil.which('cmake')
if cmake is None:
log.die('CMake is not installed or cannot be found; cannot make '
'the build folder pristine')
cmd = [cmake] + cmake_args
subprocess.check_call(cmd, cwd=self.build_dir)
def _run_build(self, target):
extra_args = ['--target', target] if target else []
cmake.run_build(self.build_dir, extra_args=extra_args)