| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: GPL-2.0-only |
| 3 |
|
| 4 |
import fnmatch |
| 5 |
import os |
| 6 |
import re |
| 7 |
import argparse |
| 8 |
|
| 9 |
|
| 10 |
def parse_of_declare_macros(data, include_driver_macros=True): |
| 11 |
""" Find all compatible strings in OF_DECLARE() style macros """ |
| 12 |
compat_list = [] |
| 13 |
# CPU_METHOD_OF_DECLARE does not have a compatible string |
| 14 |
if include_driver_macros: |
| 15 |
re_macros = r'(?<!CPU_METHOD_)(IRQCHIP|OF)_(DECLARE|MATCH)(_DRIVER)?\(.*?\)' |
| 16 |
else: |
| 17 |
re_macros = r'(?<!CPU_METHOD_)(IRQCHIP|OF)_(DECLARE|MATCH)\(.*?\)' |
| 18 |
for m in re.finditer(re_macros, data): |
| 19 |
try: |
| 20 |
compat = re.search(r'"(.*?)"', m[0])[1] |
| 21 |
except: |
| 22 |
# Fails on compatible strings in #define, so just skip |
| 23 |
continue |
| 24 |
compat_list += [compat] |
| 25 |
|
| 26 |
return compat_list |
| 27 |
|
| 28 |
|
| 29 |
def parse_of_device_id(data, match_table_list=None): |
| 30 |
""" Find all compatible strings in of_device_id structs """ |
| 31 |
compat_list = [] |
| 32 |
for m in re.finditer(r'of_device_id(\s+\S+)?\s+(\S+)\[\](\s+\S+)?\s*=\s*({.*?);', data): |
| 33 |
if match_table_list is not None and m[2] not in match_table_list: |
| 34 |
continue |
| 35 |
compat_list += re.findall(r'\.compatible\s+=\s+"(\S+)"', m[4]) |
| 36 |
|
| 37 |
return compat_list |
| 38 |
|
| 39 |
|
| 40 |
def parse_of_match_table(data): |
| 41 |
""" Find all driver's of_match_table """ |
| 42 |
match_table_list = [] |
| 43 |
for m in re.finditer(r'\.of_match_table\s+=\s+(of_match_ptr\()?([a-zA-Z0-9_-]+)', data): |
| 44 |
match_table_list.append(m[2]) |
| 45 |
|
| 46 |
return match_table_list |
| 47 |
|
| 48 |
|
| 49 |
def parse_of_functions(data, func_name): |
| 50 |
""" Find all compatibles in the last argument of a given function """ |
| 51 |
compat_list = [] |
| 52 |
for m in re.finditer(rf'{func_name}\(([a-zA-Z0-9_>\(\)"\-]+,\s)*"([a-zA-Z0-9_,-]+)"\)', data): |
| 53 |
compat_list.append(m[2]) |
| 54 |
|
| 55 |
return compat_list |
| 56 |
|
| 57 |
|
| 58 |
def parse_compatibles(file, compat_ignore_list): |
| 59 |
with open(file, 'r', encoding='utf-8') as f: |
| 60 |
data = f.read().replace('\n', '') |
| 61 |
|
| 62 |
if compat_ignore_list is not None: |
| 63 |
# For a compatible in the DT to be matched to a driver it needs to show |
| 64 |
# up in a driver's of_match_table |
| 65 |
match_table_list = parse_of_match_table(data) |
| 66 |
compat_list = parse_of_device_id(data, match_table_list) |
| 67 |
|
| 68 |
compat_list = [compat for compat in compat_list if compat not in compat_ignore_list] |
| 69 |
else: |
| 70 |
compat_list = parse_of_declare_macros(data) |
| 71 |
compat_list += parse_of_device_id(data) |
| 72 |
compat_list += parse_of_functions(data, "_is_compatible") |
| 73 |
compat_list += parse_of_functions(data, "of_find_compatible_node") |
| 74 |
compat_list += parse_of_functions(data, "for_each_compatible_node") |
| 75 |
compat_list += parse_of_functions(data, "for_each_compatible_node_scoped") |
| 76 |
compat_list += parse_of_functions(data, "of_get_compatible_child") |
| 77 |
|
| 78 |
return compat_list |
| 79 |
|
| 80 |
def parse_compatibles_to_ignore(file): |
| 81 |
with open(file, 'r', encoding='utf-8') as f: |
| 82 |
data = f.read().replace('\n', '') |
| 83 |
|
| 84 |
# Compatibles that show up in OF_DECLARE macros can't be expected to |
| 85 |
# match a driver, except for the _DRIVER ones. |
| 86 |
return parse_of_declare_macros(data, include_driver_macros=False) |
| 87 |
|
| 88 |
|
| 89 |
def print_compat(filename, compatibles): |
| 90 |
if not compatibles: |
| 91 |
return |
| 92 |
if show_filename: |
| 93 |
compat_str = ' '.join(compatibles) |
| 94 |
print(filename + ": compatible(s): " + compat_str) |
| 95 |
else: |
| 96 |
print(*compatibles, sep='\n') |
| 97 |
|
| 98 |
def glob_without_symlinks(root, glob): |
| 99 |
for path, dirs, files in os.walk(root): |
| 100 |
# Ignore hidden directories |
| 101 |
for d in dirs: |
| 102 |
if fnmatch.fnmatch(d, ".*"): |
| 103 |
dirs.remove(d) |
| 104 |
for f in files: |
| 105 |
if fnmatch.fnmatch(f, glob): |
| 106 |
yield os.path.join(path, f) |
| 107 |
|
| 108 |
def files_to_parse(path_args): |
| 109 |
for f in path_args: |
| 110 |
if os.path.isdir(f): |
| 111 |
for filename in glob_without_symlinks(f, "*.c"): |
| 112 |
yield filename |
| 113 |
else: |
| 114 |
yield f |
| 115 |
|
| 116 |
show_filename = False |
| 117 |
|
| 118 |
if __name__ == "__main__": |
| 119 |
ap = argparse.ArgumentParser() |
| 120 |
ap.add_argument("cfile", type=str, nargs='*', help="C source files or directories to parse") |
| 121 |
ap.add_argument('-H', '--with-filename', help="Print filename with compatibles", action="store_true") |
| 122 |
ap.add_argument('-d', '--driver-match', help="Only print compatibles that should match to a driver", action="store_true") |
| 123 |
args = ap.parse_args() |
| 124 |
|
| 125 |
show_filename = args.with_filename |
| 126 |
compat_ignore_list = None |
| 127 |
|
| 128 |
if args.driver_match: |
| 129 |
compat_ignore_list = [] |
| 130 |
for f in files_to_parse(args.cfile): |
| 131 |
compat_ignore_list.extend(parse_compatibles_to_ignore(f)) |
| 132 |
|
| 133 |
for f in files_to_parse(args.cfile): |
| 134 |
compat_list = parse_compatibles(f, compat_ignore_list) |
| 135 |
print_compat(f, compat_list) |