|
| 1 | +# Copyright (c) 2024 Nordic Semiconductor ASA |
| 2 | +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import re |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from west.commands import WestCommand |
| 10 | + |
| 11 | + |
| 12 | +def soc_series_detect(board_dir): |
| 13 | + found_defconfig = None |
| 14 | + for defconfig in board_dir.glob("Kconfig.*"): |
| 15 | + if defconfig.name == "Kconfig.defconfig": |
| 16 | + continue |
| 17 | + |
| 18 | + found_defconfig = defconfig |
| 19 | + break |
| 20 | + |
| 21 | + if not found_defconfig: |
| 22 | + raise FileNotFoundError("Board defconfig file not found") |
| 23 | + |
| 24 | + with open(found_defconfig, "r") as f: |
| 25 | + for line in f.readlines(): |
| 26 | + m = re.match(".*select SOC_(NRF[0-9]{2}[A-Z]{,1}).*", line) |
| 27 | + if not m: |
| 28 | + continue |
| 29 | + |
| 30 | + return m.group(1).lower() |
| 31 | + |
| 32 | + raise ValueError("Unsupported board") |
| 33 | + |
| 34 | + |
| 35 | +class NcsBoardActions(WestCommand): |
| 36 | + def __init__(self): |
| 37 | + super().__init__( |
| 38 | + "ncs-board-actions", "Obtain available actions for any board", "" |
| 39 | + ) |
| 40 | + |
| 41 | + def do_add_parser(self, parser_adder): |
| 42 | + parser = parser_adder.add_parser( |
| 43 | + self.name, help=self.help, description=self.description |
| 44 | + ) |
| 45 | + |
| 46 | + parser.add_argument( |
| 47 | + "-d", "--board-dir", type=Path, help="Target board directory" |
| 48 | + ) |
| 49 | + |
| 50 | + return parser |
| 51 | + |
| 52 | + def do_run(self, args, unknown_args): |
| 53 | + board_dir = args.board_dir |
| 54 | + if not board_dir: |
| 55 | + board_dir = Path(os.getcwd()) |
| 56 | + |
| 57 | + board_spec = board_dir / "board.yml" |
| 58 | + if not board_spec.exists(): |
| 59 | + raise FileNotFoundError("Invalid board directory") |
| 60 | + |
| 61 | + series = soc_series_detect(board_dir) |
| 62 | + commands = [] |
| 63 | + |
| 64 | + if series == "nrf54h": |
| 65 | + commands.extend( |
| 66 | + [ |
| 67 | + { |
| 68 | + "name": "Create/Edit BICR", |
| 69 | + "command": "west", |
| 70 | + "args": ["ncs-bicr", "--board-dir", str(board_dir.resolve())], |
| 71 | + "properties": { |
| 72 | + "providesJsonSchema": True, |
| 73 | + } |
| 74 | + } |
| 75 | + ] |
| 76 | + ) |
| 77 | + |
| 78 | + print(json.dumps({"commands": commands})) |
0 commit comments