forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_driver_kconfig_dts.py
executable file
·93 lines (68 loc) · 2.7 KB
/
gen_driver_kconfig_dts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
#
# Copyright (c) 2022 Kumar Gala <galak@kernel.org>
# Copyright (c) 2025 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
import argparse
import os
import yaml
try:
# Use the C LibYAML parser if available, rather than the Python parser.
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader # type: ignore
HEADER = """\
# Generated devicetree Kconfig
#
# SPDX-License-Identifier: Apache-2.0"""
KCONFIG_TEMPLATE = """
DT_COMPAT_{COMPAT} := {compat}
config DT_HAS_{COMPAT}_ENABLED
\tdef_bool $(dt_compat_enabled,$(DT_COMPAT_{COMPAT}))"""
# Character translation table used to derive Kconfig symbol names
TO_UNDERSCORES = str.maketrans("-,.@/+", "______")
def binding_paths(bindings_dirs):
# Yields paths to all bindings (.yaml files) in 'bindings_dirs'
for bindings_dir in bindings_dirs:
for root, _, filenames in os.walk(bindings_dir):
for filename in filenames:
if filename.endswith((".yaml", ".yml")):
yield os.path.join(root, filename)
def parse_args():
# Returns parsed command-line arguments
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument("--kconfig-out", required=True,
help="path to write the Kconfig file")
parser.add_argument("--bindings-dirs", nargs='+', required=True,
help="directory with bindings in YAML format, "
"we allow multiple")
return parser.parse_args()
def main():
args = parse_args()
compats = set()
for binding_path in binding_paths(args.bindings_dirs):
with open(binding_path, encoding="utf-8") as f:
try:
# Parsed PyYAML representation graph. For our purpose,
# we don't need the whole file converted into a dict.
root = yaml.compose(f, Loader=SafeLoader)
except yaml.YAMLError as e:
print(f"WARNING: '{binding_path}' appears in binding "
f"directories but isn't valid YAML: {e}")
continue
if not isinstance(root, yaml.MappingNode):
continue
for key, node in root.value:
if key.value == "compatible" and isinstance(node, yaml.ScalarNode):
compats.add(node.value)
break
with open(args.kconfig_out, "w", encoding="utf-8") as kconfig_file:
print(HEADER, file=kconfig_file)
for c in sorted(compats):
out = KCONFIG_TEMPLATE.format(
compat=c, COMPAT=c.upper().translate(TO_UNDERSCORES)
)
print(out, file=kconfig_file)
if __name__ == "__main__":
main()