dt-extract-compatibles 4.2 KB

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