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