]>
git.ipfire.org Git - thirdparty/kernel/stable.git/blob - scripts/dtc/dt-extract-compatibles
2 # SPDX-License-Identifier: GPL-2.0-only
10 def parse_of_declare_macros(data
, include_driver_macros
=True):
11 """ Find all compatible strings in OF_DECLARE() style macros """
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)?\(.*?\)'
17 re_macros
= r
'(?<!CPU_METHOD_)(IRQCHIP|OF)_(DECLARE|MATCH)\(.*?\)'
18 for m
in re
.finditer(re_macros
, data
):
20 compat
= re
.search(r
'"(.*?)"', m
[0])[1]
22 # Fails on compatible strings in #define, so just skip
24 compat_list
+= [compat
]
29 def parse_of_device_id(data
, match_table_list
=None):
30 """ Find all compatible strings in of_device_id structs """
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
:
35 compat_list
+= re
.findall(r
'\.compatible\s+=\s+"(\S+)"', m
[4])
40 def parse_of_match_table(data
):
41 """ Find all driver's of_match_table """
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])
46 return match_table_list
49 def parse_compatibles(file, compat_ignore_list
):
50 with
open(file, 'r', encoding
='utf-8') as f
:
51 data
= f
.read().replace('\n', '')
53 if compat_ignore_list
is not None:
54 # For a compatible in the DT to be matched to a driver it needs to show
55 # up in a driver's of_match_table
56 match_table_list
= parse_of_match_table(data
)
57 compat_list
= parse_of_device_id(data
, match_table_list
)
59 compat_list
= [compat
for compat
in compat_list
if compat
not in compat_ignore_list
]
61 compat_list
= parse_of_declare_macros(data
)
62 compat_list
+= parse_of_device_id(data
)
66 def parse_compatibles_to_ignore(file):
67 with
open(file, 'r', encoding
='utf-8') as f
:
68 data
= f
.read().replace('\n', '')
70 # Compatibles that show up in OF_DECLARE macros can't be expected to
71 # match a driver, except for the _DRIVER ones.
72 return parse_of_declare_macros(data
, include_driver_macros
=False)
75 def print_compat(filename
, compatibles
):
79 compat_str
= ' '.join(compatibles
)
80 print(filename
+ ": compatible(s): " + compat_str
)
82 print(*compatibles
, sep
='\n')
84 def files_to_parse(path_args
):
87 for filename
in glob
.iglob(f
+ "/**/*.c", recursive
=True):
94 if __name__
== "__main__":
95 ap
= argparse
.ArgumentParser()
96 ap
.add_argument("cfile", type=str, nargs
='*', help="C source files or directories to parse")
97 ap
.add_argument('-H', '--with-filename', help="Print filename with compatibles", action
="store_true")
98 ap
.add_argument('-d', '--driver-match', help="Only print compatibles that should match to a driver", action
="store_true")
99 args
= ap
.parse_args()
101 show_filename
= args
.with_filename
102 compat_ignore_list
= None
104 if args
.driver_match
:
105 compat_ignore_list
= []
106 for f
in files_to_parse(args
.cfile
):
107 compat_ignore_list
.extend(parse_compatibles_to_ignore(f
))
109 for f
in files_to_parse(args
.cfile
):
110 compat_list
= parse_compatibles(f
, compat_ignore_list
)
111 print_compat(f
, compat_list
)