Skip to content

RFC: Parse Kconfig to get driver API/feature info #50724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scripts/dts/gen_driver_kconfig_dts.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def compat2kconfig(compat):
printfile(f'')
printfile(f'config DT_HAS_{compat_ident}_ENABLED')
printfile(f'\tdef_bool $(dt_compat_enabled,$(DT_COMPAT_{compat_ident}))')
printfile(f'')
printfile(f'if DT_HAS_{compat_ident}_ENABLED')
printfile(f'config DT_{compat_ident}')
printfile(f'\tstring')
printfile(f'\tdefault "{compat}"')
printfile(f'endif')

def main():
global kconfig_file
Expand Down
75 changes: 74 additions & 1 deletion scripts/kconfig/kconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@
import os
import sys
import textwrap
import pickle
from pathlib import Path
ZEPHYR_BASE = str(Path(__file__).resolve().parents[2])
sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts", "dts",
"python-devicetree", "src"))

from devicetree import edtlib

# Zephyr doesn't use tristate symbols. They're supported here just to make the
# script a bit more generic.
from kconfiglib import Kconfig, split_expr, expr_value, expr_str, BOOL, \
TRISTATE, TRI_TO_STR, AND, OR
TRISTATE, TRI_TO_STR, AND, OR, STRING, TYPE_TO_STR, Symbol


def main():
Expand Down Expand Up @@ -96,6 +103,72 @@ def main():
# Write the list of parsed Kconfig files to a file
write_kconfig_filenames(kconf, args.kconfig_list_out)

EDT_PICKLE = os.environ.get("EDT_PICKLE")

# The "if" handles a missing dts.
if EDT_PICKLE is not None and os.path.isfile(EDT_PICKLE):
with open(EDT_PICKLE, 'rb') as f:
edt = pickle.load(f)
else:
edt = None

SYM_TO_TYPE = {
"FLASH_HAS_DRIVER_ENABLED": "api",
"SERIAL_HAS_DRIVER": "api",
"ENTROPY_HAS_DRIVER": "api",
"SERIAL_SUPPORT_INTERRUPT": "feature",
"SERIAL_SUPPORT_ASYNC": "feature",
}

if edt:
for compat in edt.compat2okay.keys():
device_sym = compat2symbol(kconf, compat)

if device_sym:
# print("C: %s" % compat)
# print("N: %s" % device_sym.name)
# print(device_sym)
# for r in device_sym.referenced:
# if isinstance(r, Symbol) and r != kconf.y:
# print("R: %s" % r.name)
for (sym, cond) in device_sym.selects:
# print("S: %s" % sym.name)
if sym.name in SYM_TO_TYPE:
print('dts compatible: "%s" Kconfig Symbol: %s' % (compat, device_sym.name))
print("%s - %s" % (SYM_TO_TYPE[sym.name], sym.name))
print("")

def compat2symbol(kconf, compat):
dt_sym = None
for sym in kconf.syms.values():
if sym.type == STRING and sym.str_value == compat:
dt_sym = sym

if dt_sym == None:
# print("error found no symbol matching %s" % compat)
return None

dt_has_sym = None
for r in dt_sym.referenced:
if isinstance(r, Symbol):
if "DT_HAS_" in r.name:
dt_has_sym = r

if dt_has_sym == None:
print("error found no symbol matching %s" % compat)

device_sym = None
for sym in kconf.syms.values():
if sym == dt_sym:
continue
for r in sym.referenced:
if isinstance(r, Symbol) and r == dt_has_sym:
if device_sym:
print("ERROR - only expect one device symbol to match")
device_sym = sym

return device_sym


def check_no_promptless_assign(kconf):
# Checks that no promptless symbols are assigned
Expand Down