Skip to content

Commit 3a62b17

Browse files
committed
cmake: shields: introduce shield.yml
While legacy shields are still supported, this introduces a shield.yml file similar to board.yml that allows to more explicitly declare a shield and to set some useful metadata such as vendor and full name. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 66fda82 commit 3a62b17

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

doc/hardware/porting/shields.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,29 @@ under :zephyr_file:`boards/shields`:
1717
.. code-block:: none
1818
1919
boards/shields/<shield>
20+
├── shield.yml
2021
├── <shield>.overlay
2122
├── Kconfig.shield
2223
├── Kconfig.defconfig
2324
└── pre_dt_shield.cmake
2425
2526
These files provides shield configuration as follows:
2627

28+
* **shield.yml**: This file provides metadata about the shield in YAML format.
29+
It must contain the following fields:
30+
31+
* ``name``: Name of the shield used in Kconfig and build system (required)
32+
* ``full_name``: Full commercial name of the shield (required)
33+
* ``vendor``: Manufacturer/vendor of the shield (required)
34+
35+
Example:
36+
37+
.. code-block:: yaml
38+
39+
name: foo_shield
40+
full_name: Foo Shield for Arduino
41+
vendor: acme
42+
2743
* **<shield>.overlay**: This file provides a shield description in devicetree
2844
format that is merged with the board's :ref:`devicetree <dt-guide>`
2945
before compilation.

scripts/list_shields.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@
66

77
import argparse
88
import json
9+
import sys
910
from dataclasses import dataclass
1011
from pathlib import Path
1112

13+
import pykwalify.core
14+
import yaml
15+
16+
try:
17+
from yaml import CSafeLoader as SafeLoader
18+
except ImportError:
19+
from yaml import SafeLoader
20+
21+
SHIELD_SCHEMA_PATH = str(Path(__file__).parent / 'schemas' / 'shield-schema.yml')
22+
with open(SHIELD_SCHEMA_PATH) as f:
23+
shield_schema = yaml.load(f.read(), Loader=SafeLoader)
24+
25+
SHIELD_YML = 'shield.yml'
26+
1227
#
1328
# This is shared code between the build system's 'shields' target
1429
# and the 'west shields' extension command. If you change it, make
@@ -22,10 +37,21 @@
2237
class Shield:
2338
name: str
2439
dir: Path
40+
full_name: str | None = None
41+
vendor: str | None = None
2542

2643
def shield_key(shield):
2744
return shield.name
2845

46+
def process_shield_data(shield_data, shield_dir):
47+
# Create shield from yaml data
48+
return Shield(
49+
name=shield_data['name'],
50+
dir=shield_dir,
51+
full_name=shield_data.get('full_name'),
52+
vendor=shield_data.get('vendor')
53+
)
54+
2955
def find_shields(args):
3056
ret = []
3157

@@ -45,6 +71,28 @@ def find_shields_in(root):
4571
for maybe_shield in (shields).iterdir():
4672
if not maybe_shield.is_dir():
4773
continue
74+
75+
# Check for shield.yml first
76+
shield_yml = maybe_shield / SHIELD_YML
77+
if shield_yml.is_file():
78+
with shield_yml.open('r', encoding='utf-8') as f:
79+
shield_data = yaml.load(f.read(), Loader=SafeLoader)
80+
81+
try:
82+
pykwalify.core.Core(source_data=shield_data, schema_data=shield_schema).validate()
83+
except pykwalify.errors.SchemaError as e:
84+
sys.exit(f'ERROR: Malformed shield.yml in file: {shield_yml.as_posix()}\n{e}')
85+
86+
if 'shields' in shield_data:
87+
# Multiple shields format
88+
for shield_info in shield_data['shields']:
89+
ret.append(process_shield_data(shield_info, maybe_shield))
90+
elif 'shield' in shield_data:
91+
# Single shield format
92+
ret.append(process_shield_data(shield_data['shield'], maybe_shield))
93+
continue
94+
95+
# Fallback to legacy method if no shield.yml
4896
for maybe_kconfig in maybe_shield.iterdir():
4997
if maybe_kconfig.name == 'Kconfig.shield':
5098
for maybe_overlay in maybe_shield.iterdir():

scripts/schemas/shield-schema.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# Copyright The Zephyr Project Contributors
4+
5+
# A pykwalify schema for basic validation of the structure of a shield metadata YAML file.
6+
#
7+
# The shield.yml file can contain either a single shield definition or a list of shields.
8+
9+
schema;shield-schema:
10+
type: map
11+
mapping:
12+
name:
13+
required: true
14+
type: str
15+
desc: Name of the shield (used in Kconfig and build system)
16+
full_name:
17+
required: true
18+
type: str
19+
desc: Full name of the shield (typically the commercial name)
20+
vendor:
21+
required: true
22+
type: str
23+
desc: Manufacturer/vendor of the shield
24+
25+
type: map
26+
range:
27+
min: 1
28+
max: 1
29+
mapping:
30+
shield:
31+
include: shield-schema
32+
shields:
33+
type: seq
34+
sequence:
35+
- include: shield-schema

0 commit comments

Comments
 (0)