|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | import argparse |
| 6 | +from pathlib import Path |
| 7 | +from shutil import rmtree |
| 8 | +from subprocess import CalledProcessError |
6 | 9 |
|
7 | 10 | from west.commands import WestCommand |
| 11 | +from west import log |
8 | 12 |
|
9 | | -from pathlib import PurePath |
10 | 13 | from zcmake import run_cmake |
11 | 14 |
|
12 | 15 | EXPORT_DESCRIPTION = '''\ |
@@ -39,38 +42,43 @@ def do_add_parser(self, parser_adder): |
39 | 42 | return parser |
40 | 43 |
|
41 | 44 | 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: |
75 | 62 | 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