1# Copyright (c) 2019 Nordic Semiconductor ASA
2#
3# SPDX-License-Identifier: Apache-2.0
4
5import argparse
6import os
7import re
8import sys
9import textwrap
10from pathlib import Path
11
12from west.commands import WestCommand
13from zephyr_ext_common import ZEPHYR_BASE
14
15sys.path.append(os.fspath(Path(__file__).parent.parent))
16import list_boards
17import zephyr_module
18
19
20class Boards(WestCommand):
21
22    def __init__(self):
23        super().__init__(
24            'boards',
25            # Keep this in sync with the string in west-commands.yml.
26            'display information about supported boards',
27            'Display information about boards',
28            accepts_unknown_args=False)
29
30    def do_add_parser(self, parser_adder):
31        default_fmt = '{name}'
32        parser = parser_adder.add_parser(
33            self.name,
34            help=self.help,
35            formatter_class=argparse.RawDescriptionHelpFormatter,
36            description=self.description,
37            epilog=textwrap.dedent(f'''\
38            FORMAT STRINGS
39            --------------
40
41            Boards are listed using a Python 3 format string. Arguments
42            to the format string are accessed by name.
43
44            The default format string is:
45
46            "{default_fmt}"
47
48            The following arguments are available:
49
50            - name: board name
51            - full_name: board full name (typically, its commercial name)
52            - qualifiers: board qualifiers (will be empty for legacy boards)
53            - arch: board architecture (deprecated)
54                    (arch is ambiguous for boards described in new hw model)
55            - dir: directory that contains the board definition
56            - vendor: board vendor
57            '''))
58
59        # Remember to update west-completion.bash if you add or remove
60        # flags
61        parser.add_argument('-f', '--format', default=default_fmt,
62                            help='''Format string to use to list each board;
63                                    see FORMAT STRINGS below.''')
64        parser.add_argument('-n', '--name', dest='name_re',
65                            help='''a regular expression; only boards whose
66                            names match NAME_RE will be listed''')
67        list_boards.add_args(parser)
68
69        return parser
70
71    def do_run(self, args, _):
72        if args.name_re is not None:
73            name_re = re.compile(args.name_re)
74        else:
75            name_re = None
76
77        module_settings = {
78            'arch_root': [ZEPHYR_BASE],
79            'board_root': [ZEPHYR_BASE],
80            'soc_root': [ZEPHYR_BASE],
81        }
82
83        for module in zephyr_module.parse_modules(ZEPHYR_BASE, self.manifest):
84            for key in module_settings:
85                root = module.meta.get('build', {}).get('settings', {}).get(key)
86                if root is not None:
87                    module_settings[key].append(Path(module.project) / root)
88
89        args.arch_roots += module_settings['arch_root']
90        args.board_roots += module_settings['board_root']
91        args.soc_roots += module_settings['soc_root']
92
93        for board in list_boards.find_boards(args):
94            if name_re is not None and not name_re.search(board.name):
95                continue
96            self.inf(args.format.format(name=board.name, arch=board.arch,
97                                        dir=board.dir, hwm=board.hwm, qualifiers=''))
98
99        for board in list_boards.find_v2_boards(args).values():
100            if name_re is not None and not name_re.search(board.name):
101                continue
102            self.inf(
103                args.format.format(
104                    name=board.name,
105                    full_name=board.full_name,
106                    dir=board.dir,
107                    hwm=board.hwm,
108                    vendor=board.vendor,
109                    qualifiers=list_boards.board_v2_qualifiers_csv(board),
110                )
111            )
112