blob: dfbf2580eb40cc9b53b9aaf47d8ac1fcb3d9c97a [file] [log] [blame]
Henrik Brix Andersena6a38912024-05-14 10:28:38 +02001#!/usr/bin/env python3
2
3# Copyright (c) 2024 Vestas Wind Systems A/S
4# Copyright (c) 2020 Nordic Semiconductor ASA
5# SPDX-License-Identifier: Apache-2.0
6
7import argparse
8from dataclasses import dataclass
9from pathlib import Path
10
11#
12# This is shared code between the build system's 'shields' target
13# and the 'west shields' extension command. If you change it, make
14# sure to test both ways it can be used.
15#
16# (It's done this way to keep west optional, making it possible to run
17# 'ninja shields' in a build directory without west installed.)
18#
19
20@dataclass(frozen=True)
21class Shield:
22 name: str
23 dir: Path
24
25def shield_key(shield):
26 return shield.name
27
28def find_shields(args):
29 ret = []
30
31 for root in args.board_roots:
32 for shields in find_shields_in(root):
33 ret.append(shields)
34
35 return sorted(ret, key=shield_key)
36
37def find_shields_in(root):
38 shields = root / 'boards' / 'shields'
39 ret = []
40
41 for maybe_shield in (shields).iterdir():
42 if not maybe_shield.is_dir():
43 continue
44 for maybe_kconfig in maybe_shield.iterdir():
45 if maybe_kconfig.name == 'Kconfig.shield':
46 for maybe_overlay in maybe_shield.iterdir():
47 file_name = maybe_overlay.name
48 if file_name.endswith('.overlay'):
49 shield_name = file_name[:-len('.overlay')]
50 ret.append(Shield(shield_name, maybe_shield))
51
52 return sorted(ret, key=shield_key)
53
54def parse_args():
55 parser = argparse.ArgumentParser(allow_abbrev=False)
56 add_args(parser)
57 return parser.parse_args()
58
59def add_args(parser):
60 # Remember to update west-completion.bash if you add or remove
61 # flags
62 parser.add_argument("--board-root", dest='board_roots', default=[],
63 type=Path, action='append',
64 help='add a board root, may be given more than once')
65
66def dump_shields(shields):
67 for shield in shields:
68 print(f' {shield.name}')
69
70if __name__ == '__main__':
71 dump_shields(find_shields(parse_args()))