Skip to content

Commit d6f5f51

Browse files
mbolivar-nordicnashif
authored andcommitted
scripts: fix west zephyr-export error handling
Fix two issues: 1. The script assumes the default CMake generator build tool platform is installed. On Linux at least, that's Make instead of Ninja, but Make might not be installed since Zephyr recommends Ninja. On Windows, that might be VS Code or nmake. Calling `cmake -P pristine` instead of `cmake --build <path> --target pristine` has the benefit of removing the dependency on a build command, and hence the default generator is not relevant. 2. It also assumes run_cmake() returns control, and therefore pristine can be run. However, if the cmake command fails hard (say, due to issue #1 before this patch), run_cmake() throws an exception instead. Fix that by trying to run the pristine target in a finally block instead, and adding some manual cleanup steps in case the build system is in a bad state and pristine fails too. Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no> Signed-off-by: Torsten Rasmussen <torsten.rasmussen@nordicsemi.no>
1 parent 94de3e9 commit d6f5f51

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

scripts/west_commands/export.py

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import argparse
6+
from pathlib import Path
7+
from shutil import rmtree
8+
from subprocess import CalledProcessError
69

710
from west.commands import WestCommand
11+
from west import log
812

9-
from pathlib import PurePath
1013
from zcmake import run_cmake
1114

1215
EXPORT_DESCRIPTION = '''\
@@ -39,38 +42,43 @@ def do_add_parser(self, parser_adder):
3942
return parser
4043

4144
def do_run(self, args, unknown_args):
42-
zephyr_config_package_path = PurePath(__file__).parents[2] \
43-
/ 'share' / 'zephyr-package' / 'cmake'
44-
45-
cmake_args = ['-S', f'{zephyr_config_package_path}',
46-
'-B', f'{zephyr_config_package_path}']
47-
lines = run_cmake(cmake_args, capture_output=True)
48-
49-
# Let's clean up, as Zephyr has now been exported, and we no longer
50-
# need the generated files.
51-
cmake_args = ['--build', f'{zephyr_config_package_path}',
52-
'--target', 'pristine']
53-
run_cmake(cmake_args, capture_output=True)
54-
55-
# Let's ignore the normal CMake printing and instead only print
56-
# the important information.
57-
msg = [line for line in lines if not line.startswith('-- ')]
58-
print('\n'.join(msg))
59-
60-
zephyr_unittest_config_package_path = PurePath(__file__).parents[2] \
61-
/ 'share' / 'zephyrunittest-package' / 'cmake'
62-
63-
cmake_args = ['-S', f'{zephyr_unittest_config_package_path}',
64-
'-B', f'{zephyr_unittest_config_package_path}']
65-
lines = run_cmake(cmake_args, capture_output=True)
66-
67-
# Let's clean up, as Zephyr has now been exported, and we no longer
68-
# need the generated files.
69-
cmake_args = ['--build', f'{zephyr_unittest_config_package_path}',
70-
'--target', 'pristine']
71-
run_cmake(cmake_args, capture_output=True)
72-
73-
# Let's ignore the normal CMake printing and instead only print
74-
# the important information.
45+
# The 'share' subdirectory of the top level zephyr repository.
46+
share = Path(__file__).parents[2] / 'share'
47+
48+
run_cmake_and_clean_up(share / 'zephyr-package' / 'cmake')
49+
run_cmake_and_clean_up(share / 'zephyrunittest-package' / 'cmake')
50+
51+
def run_cmake_and_clean_up(path):
52+
# Run a package installation script, cleaning up afterwards.
53+
#
54+
# Filtering out lines that start with -- ignores the normal
55+
# CMake status messages and instead only prints the important
56+
# information.
57+
58+
try:
59+
lines = run_cmake(['-S', str(path), '-B', str(path)],
60+
capture_output=True)
61+
finally:
7562
msg = [line for line in lines if not line.startswith('-- ')]
76-
print('\n'.join(msg))
63+
log.inf('\n'.join(msg))
64+
clean_up(path)
65+
66+
def clean_up(path):
67+
try:
68+
run_cmake(['-P', str(path / 'pristine.cmake')],
69+
capture_output=True)
70+
except CalledProcessError:
71+
# Do our best to clean up even though CMake failed.
72+
log.wrn(f'Failed to make {path} pristine; '
73+
'removing known generated files...')
74+
for subpath in ['CMakeCache.txt', 'CMakeFiles', 'build.ninja',
75+
'cmake_install.cmake', 'rules.ninja']:
76+
remove_if_exists(Path(path) / subpath)
77+
78+
def remove_if_exists(pathobj):
79+
if pathobj.is_file():
80+
log.inf(f'- removing: {pathobj}')
81+
pathobj.unlink()
82+
elif pathobj.is_dir():
83+
log.inf(f'- removing: {pathobj}')
84+
rmtree(pathobj)

0 commit comments

Comments
 (0)