Skip to content

Commit 3a88dce

Browse files
committed
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 <[email protected]>
1 parent 01a2beb commit 3a88dce

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

doc/guides/west/config.rst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,15 @@ extension commands (found in :file:`scripts/west_commands`).
165165
* - Option
166166
- Description
167167
* - ``build.pristine``
168-
- String. Controls the way in which ``west build`` may run the ``pristine``
169-
target before building. Can take the following values:
170-
171-
- ``never`` (default): Never automatically run the ``pristine`` target.
172-
- ``auto``: ``west build`` will automatically run the ``pristine``
173-
target before building, if a build system is present and the build
174-
will fail otherwise (e.g. the user has specified a different board or
175-
application from the one previously used to make the build
168+
- String. Controls the way in which ``west build`` may clean the build
169+
folder before building. Can take the following values:
170+
171+
- ``never`` (default): Never automatically make the build folder
172+
pristine.
173+
- ``auto``: ``west build`` will automatically make the build folder
174+
pristine before building, if a build system is present and the build
175+
would fail otherwise (e.g. the user has specified a different board
176+
or application from the one previously used to make the build
176177
directory).
177-
- ``always``: Always run the ``pristine`` target before building, if
178+
- ``always``: Always make the build folder pristine before building, if
178179
a build system is present.

scripts/west_commands/build.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import argparse
66
import os
7+
import shutil
8+
import subprocess
79

810
from west import log
911
from west import cmake
@@ -113,11 +115,11 @@ def do_add_parser(self, parser_adder):
113115
'clean', 'pristine', etc.)''')
114116
parser.add_argument('-p', '--pristine', choices=['auto', 'always',
115117
'never'], action=AlwaysIfMissing, nargs='?',
116-
help='''Control whether the pristine target is run
117-
before building if a build system is present in the
118-
build dir. --pristine is the same as
119-
--pristine=always. If set to auto, the pristine
120-
target will be run only if required based on the
118+
help='''Control whether the build folder is made
119+
pristine before building if a build system is
120+
present in the build dir. --pristine is the same as
121+
--pristine=always. If set to auto, the build folder
122+
will be made pristine only if required based on the
121123
existing build system and the options provided.
122124
This allows for reusing a build folder even if it
123125
contains build files for a different board or
@@ -159,8 +161,7 @@ def do_run(self, args, remainder):
159161
self.auto_pristine))
160162
if is_zephyr_build(self.build_dir):
161163
if pristine == 'always':
162-
log.inf('Making build dir {} pristine'.format(self.build_dir))
163-
self._run_build('pristine')
164+
self._run_pristine()
164165
self.run_cmake = True
165166
else:
166167
self._update_cache()
@@ -338,8 +339,7 @@ def _sanity_check(self):
338339
format(self.build_dir, cached_board, self.args.board))
339340

340341
if self.auto_pristine and (apps_mismatched or boards_mismatched):
341-
log.inf('Making build dir {} pristine'.format(self.build_dir))
342-
self._run_build('pristine')
342+
self._run_pristine()
343343
self.cmake_cache = None
344344
log.dbg('run_cmake:', True, level=log.VERBOSE_EXTREME)
345345
self.run_cmake = True
@@ -373,6 +373,26 @@ def _run_cmake(self, cmake_opts):
373373
final_cmake_args.extend(cmake_opts)
374374
cmake.run_cmake(final_cmake_args)
375375

376+
def _run_pristine(self):
377+
log.inf('Making build dir {} pristine'.format(self.build_dir))
378+
379+
zb = os.environ.get('ZEPHYR_BASE')
380+
if not zb:
381+
log.die('Internal error: ZEPHYR_BASE not set in the environment, '
382+
'and should have been by the main script')
383+
384+
if not is_zephyr_build(self.build_dir):
385+
log.die('Refusing to run pristine on a folder that is not a Zephyr '
386+
'build system')
387+
388+
cmake_args = ['-P', '{}/cmake/pristine.cmake'.format(zb)]
389+
cmake = shutil.which('cmake')
390+
if cmake is None:
391+
log.die('CMake is not installed or cannot be found; cannot make '
392+
'the build folder pristine')
393+
cmd = [cmake] + cmake_args
394+
subprocess.check_call(cmd, cwd=self.build_dir)
395+
376396
def _run_build(self, target):
377397
extra_args = ['--target', target] if target else []
378398
cmake.run_build(self.build_dir, extra_args=extra_args)

0 commit comments

Comments
 (0)