Torsten Rasmussen | e85cfe4 | 2020-03-05 13:42:42 +0100 | [diff] [blame] | 1 | # Copyright (c) 2020 Nordic Semiconductor ASA |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | import argparse |
Martí Bolívar | d6f5f51 | 2020-03-31 17:40:50 -0700 | [diff] [blame] | 6 | from pathlib import Path |
| 7 | from shutil import rmtree |
Torsten Rasmussen | e85cfe4 | 2020-03-05 13:42:42 +0100 | [diff] [blame] | 8 | |
| 9 | from west.commands import WestCommand |
Martí Bolívar | d6f5f51 | 2020-03-31 17:40:50 -0700 | [diff] [blame] | 10 | from west import log |
Torsten Rasmussen | e85cfe4 | 2020-03-05 13:42:42 +0100 | [diff] [blame] | 11 | |
Torsten Rasmussen | e85cfe4 | 2020-03-05 13:42:42 +0100 | [diff] [blame] | 12 | from zcmake import run_cmake |
| 13 | |
| 14 | EXPORT_DESCRIPTION = '''\ |
| 15 | This command registers the current Zephyr installation as a CMake |
| 16 | config package in the CMake user package registry. |
| 17 | |
| 18 | In Windows, the CMake user package registry is found in: |
| 19 | HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ |
| 20 | |
| 21 | In Linux and MacOS, the CMake user package registry is found in: |
| 22 | ~/.cmake/packages/''' |
| 23 | |
| 24 | |
| 25 | class ZephyrExport(WestCommand): |
| 26 | |
| 27 | def __init__(self): |
| 28 | super().__init__( |
| 29 | 'zephyr-export', |
| 30 | # Keep this in sync with the string in west-commands.yml. |
| 31 | 'export Zephyr installation as a CMake config package', |
| 32 | EXPORT_DESCRIPTION, |
| 33 | accepts_unknown_args=False) |
| 34 | |
| 35 | def do_add_parser(self, parser_adder): |
| 36 | parser = parser_adder.add_parser( |
| 37 | self.name, |
| 38 | help=self.help, |
| 39 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 40 | description=self.description) |
| 41 | return parser |
| 42 | |
| 43 | def do_run(self, args, unknown_args): |
Martí Bolívar | d6f5f51 | 2020-03-31 17:40:50 -0700 | [diff] [blame] | 44 | # The 'share' subdirectory of the top level zephyr repository. |
| 45 | share = Path(__file__).parents[2] / 'share' |
Torsten Rasmussen | e85cfe4 | 2020-03-05 13:42:42 +0100 | [diff] [blame] | 46 | |
Torsten Rasmussen | edde894 | 2020-08-06 10:41:39 +0200 | [diff] [blame] | 47 | run_cmake_export(share / 'zephyr-package' / 'cmake') |
| 48 | run_cmake_export(share / 'zephyrunittest-package' / 'cmake') |
Torsten Rasmussen | e85cfe4 | 2020-03-05 13:42:42 +0100 | [diff] [blame] | 49 | |
Torsten Rasmussen | edde894 | 2020-08-06 10:41:39 +0200 | [diff] [blame] | 50 | def run_cmake_export(path): |
| 51 | # Run a package installation script. |
Martí Bolívar | d6f5f51 | 2020-03-31 17:40:50 -0700 | [diff] [blame] | 52 | # |
| 53 | # Filtering out lines that start with -- ignores the normal |
| 54 | # CMake status messages and instead only prints the important |
| 55 | # information. |
Torsten Rasmussen | e85cfe4 | 2020-03-05 13:42:42 +0100 | [diff] [blame] | 56 | |
Torsten Rasmussen | edde894 | 2020-08-06 10:41:39 +0200 | [diff] [blame] | 57 | lines = run_cmake(['-P', str(path / 'zephyr_export.cmake')], |
| 58 | capture_output=True) |
| 59 | msg = [line for line in lines if not line.startswith('-- ')] |
| 60 | log.inf('\n'.join(msg)) |
Torsten Rasmussen | 3074a7a | 2020-03-27 21:31:36 +0100 | [diff] [blame] | 61 | |
Martí Bolívar | d6f5f51 | 2020-03-31 17:40:50 -0700 | [diff] [blame] | 62 | def remove_if_exists(pathobj): |
| 63 | if pathobj.is_file(): |
| 64 | log.inf(f'- removing: {pathobj}') |
| 65 | pathobj.unlink() |
| 66 | elif pathobj.is_dir(): |
| 67 | log.inf(f'- removing: {pathobj}') |
| 68 | rmtree(pathobj) |