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