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