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