]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - scripts/checkkconfigsymbols.py
HID: add Benjamin Tissoires as designated reviewer / co-maintainer
[mirror_ubuntu-artful-kernel.git] / scripts / checkkconfigsymbols.py
CommitLineData
4b6fda0b 1#!/usr/bin/env python2
24fe1f03 2
b1a3f243 3"""Find Kconfig symbols that are referenced but not defined."""
24fe1f03 4
c7455663 5# (c) 2014-2015 Valentin Rothberg <valentinrothberg@gmail.com>
cc641d55 6# (c) 2014 Stefan Hengelein <stefan.hengelein@fau.de>
24fe1f03 7#
cc641d55 8# Licensed under the terms of the GNU GPL License version 2
24fe1f03
VR
9
10
11import os
12import re
b1a3f243 13import sys
24fe1f03 14from subprocess import Popen, PIPE, STDOUT
b1a3f243 15from optparse import OptionParser
24fe1f03 16
cc641d55
VR
17
18# regex expressions
24fe1f03 19OPERATORS = r"&|\(|\)|\||\!"
cc641d55
VR
20FEATURE = r"(?:\w*[A-Z0-9]\w*){2,}"
21DEF = r"^\s*(?:menu){,1}config\s+(" + FEATURE + r")\s*"
24fe1f03 22EXPR = r"(?:" + OPERATORS + r"|\s|" + FEATURE + r")+"
0bd38ae3
VR
23DEFAULT = r"default\s+.*?(?:if\s.+){,1}"
24STMT = r"^\s*(?:if|select|depends\s+on|(?:" + DEFAULT + r"))\s+" + EXPR
cc641d55 25SOURCE_FEATURE = r"(?:\W|\b)+[D]{,1}CONFIG_(" + FEATURE + r")"
24fe1f03 26
cc641d55 27# regex objects
24fe1f03 28REGEX_FILE_KCONFIG = re.compile(r".*Kconfig[\.\w+\-]*$")
0bd38ae3 29REGEX_FEATURE = re.compile(r'(?!\B"[^"]*)' + FEATURE + r'(?![^"]*"\B)')
cc641d55
VR
30REGEX_SOURCE_FEATURE = re.compile(SOURCE_FEATURE)
31REGEX_KCONFIG_DEF = re.compile(DEF)
24fe1f03
VR
32REGEX_KCONFIG_EXPR = re.compile(EXPR)
33REGEX_KCONFIG_STMT = re.compile(STMT)
34REGEX_KCONFIG_HELP = re.compile(r"^\s+(help|---help---)\s*$")
35REGEX_FILTER_FEATURES = re.compile(r"[A-Za-z0-9]$")
0bd38ae3 36REGEX_NUMERIC = re.compile(r"0[xX][0-9a-fA-F]+|[0-9]+")
24fe1f03
VR
37
38
b1a3f243
VR
39def parse_options():
40 """The user interface of this module."""
41 usage = "%prog [options]\n\n" \
42 "Run this tool to detect Kconfig symbols that are referenced but " \
43 "not defined in\nKconfig. The output of this tool has the " \
44 "format \'Undefined symbol\\tFile list\'\n\n" \
45 "If no option is specified, %prog will default to check your\n" \
46 "current tree. Please note that specifying commits will " \
47 "\'git reset --hard\'\nyour current tree! You may save " \
48 "uncommitted changes to avoid losing data."
49
50 parser = OptionParser(usage=usage)
51
52 parser.add_option('-c', '--commit', dest='commit', action='store',
53 default="",
54 help="Check if the specified commit (hash) introduces "
55 "undefined Kconfig symbols.")
56
57 parser.add_option('-d', '--diff', dest='diff', action='store',
58 default="",
59 help="Diff undefined symbols between two commits. The "
60 "input format bases on Git log's "
61 "\'commmit1..commit2\'.")
62
a42fa92c
VR
63 parser.add_option('-f', '--find', dest='find', action='store_true',
64 default=False,
65 help="Find and show commits that may cause symbols to be "
66 "missing. Required to run with --diff.")
67
cf132e4a
VR
68 parser.add_option('-i', '--ignore', dest='ignore', action='store',
69 default="",
70 help="Ignore files matching this pattern. Note that "
71 "the pattern needs to be a Python regex. To "
72 "ignore defconfigs, specify -i '.*defconfig'.")
73
b1a3f243
VR
74 parser.add_option('', '--force', dest='force', action='store_true',
75 default=False,
76 help="Reset current Git tree even when it's dirty.")
77
78 (opts, _) = parser.parse_args()
79
80 if opts.commit and opts.diff:
81 sys.exit("Please specify only one option at once.")
82
83 if opts.diff and not re.match(r"^[\w\-\.]+\.\.[\w\-\.]+$", opts.diff):
84 sys.exit("Please specify valid input in the following format: "
85 "\'commmit1..commit2\'")
86
87 if opts.commit or opts.diff:
88 if not opts.force and tree_is_dirty():
89 sys.exit("The current Git tree is dirty (see 'git status'). "
90 "Running this script may\ndelete important data since it "
91 "calls 'git reset --hard' for some performance\nreasons. "
92 " Please run this script in a clean Git tree or pass "
93 "'--force' if you\nwant to ignore this warning and "
94 "continue.")
95
a42fa92c
VR
96 if opts.commit:
97 opts.find = False
98
cf132e4a
VR
99 if opts.ignore:
100 try:
101 re.match(opts.ignore, "this/is/just/a/test.c")
102 except:
103 sys.exit("Please specify a valid Python regex.")
104
b1a3f243
VR
105 return opts
106
107
24fe1f03
VR
108def main():
109 """Main function of this module."""
b1a3f243
VR
110 opts = parse_options()
111
112 if opts.commit or opts.diff:
113 head = get_head()
114
115 # get commit range
116 commit_a = None
117 commit_b = None
118 if opts.commit:
119 commit_a = opts.commit + "~"
120 commit_b = opts.commit
121 elif opts.diff:
122 split = opts.diff.split("..")
123 commit_a = split[0]
124 commit_b = split[1]
125 undefined_a = {}
126 undefined_b = {}
127
128 # get undefined items before the commit
129 execute("git reset --hard %s" % commit_a)
cf132e4a 130 undefined_a = check_symbols(opts.ignore)
b1a3f243
VR
131
132 # get undefined items for the commit
133 execute("git reset --hard %s" % commit_b)
cf132e4a 134 undefined_b = check_symbols(opts.ignore)
b1a3f243
VR
135
136 # report cases that are present for the commit but not before
e9533ae5 137 for feature in sorted(undefined_b):
b1a3f243
VR
138 # feature has not been undefined before
139 if not feature in undefined_a:
e9533ae5 140 files = sorted(undefined_b.get(feature))
c7455663 141 print "%s\t%s" % (yel(feature), ", ".join(files))
a42fa92c
VR
142 if opts.find:
143 commits = find_commits(feature, opts.diff)
c7455663 144 print red(commits)
b1a3f243
VR
145 # check if there are new files that reference the undefined feature
146 else:
e9533ae5
VR
147 files = sorted(undefined_b.get(feature) -
148 undefined_a.get(feature))
b1a3f243 149 if files:
c7455663 150 print "%s\t%s" % (yel(feature), ", ".join(files))
a42fa92c
VR
151 if opts.find:
152 commits = find_commits(feature, opts.diff)
c7455663 153 print red(commits)
b1a3f243
VR
154
155 # reset to head
156 execute("git reset --hard %s" % head)
157
158 # default to check the entire tree
159 else:
cf132e4a 160 undefined = check_symbols(opts.ignore)
e9533ae5
VR
161 for feature in sorted(undefined):
162 files = sorted(undefined.get(feature))
c7455663
VR
163 print "%s\t%s" % (yel(feature), ", ".join(files))
164
165
166def yel(string):
167 """
168 Color %string yellow.
169 """
170 return "\033[33m%s\033[0m" % string
171
172
173def red(string):
174 """
175 Color %string red.
176 """
177 return "\033[31m%s\033[0m" % string
b1a3f243
VR
178
179
180def execute(cmd):
181 """Execute %cmd and return stdout. Exit in case of error."""
182 pop = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True)
183 (stdout, _) = pop.communicate() # wait until finished
184 if pop.returncode != 0:
185 sys.exit(stdout)
186 return stdout
187
188
a42fa92c
VR
189def find_commits(symbol, diff):
190 """Find commits changing %symbol in the given range of %diff."""
191 commits = execute("git log --pretty=oneline --abbrev-commit -G %s %s"
192 % (symbol, diff))
193 return commits
194
195
b1a3f243
VR
196def tree_is_dirty():
197 """Return true if the current working tree is dirty (i.e., if any file has
198 been added, deleted, modified, renamed or copied but not committed)."""
199 stdout = execute("git status --porcelain")
200 for line in stdout:
201 if re.findall(r"[URMADC]{1}", line[:2]):
202 return True
203 return False
204
205
206def get_head():
207 """Return commit hash of current HEAD."""
208 stdout = execute("git rev-parse HEAD")
209 return stdout.strip('\n')
210
211
cf132e4a 212def check_symbols(ignore):
b1a3f243 213 """Find undefined Kconfig symbols and return a dict with the symbol as key
cf132e4a
VR
214 and a list of referencing files as value. Files matching %ignore are not
215 checked for undefined symbols."""
24fe1f03
VR
216 source_files = []
217 kconfig_files = []
218 defined_features = set()
cc641d55 219 referenced_features = dict() # {feature: [files]}
24fe1f03
VR
220
221 # use 'git ls-files' to get the worklist
b1a3f243 222 stdout = execute("git ls-files")
24fe1f03
VR
223 if len(stdout) > 0 and stdout[-1] == "\n":
224 stdout = stdout[:-1]
225
226 for gitfile in stdout.rsplit("\n"):
208d5115
VR
227 if ".git" in gitfile or "ChangeLog" in gitfile or \
228 ".log" in gitfile or os.path.isdir(gitfile) or \
229 gitfile.startswith("tools/"):
24fe1f03
VR
230 continue
231 if REGEX_FILE_KCONFIG.match(gitfile):
232 kconfig_files.append(gitfile)
233 else:
cc641d55 234 # all non-Kconfig files are checked for consistency
24fe1f03
VR
235 source_files.append(gitfile)
236
237 for sfile in source_files:
cf132e4a
VR
238 if ignore and re.match(ignore, sfile):
239 # do not check files matching %ignore
240 continue
24fe1f03
VR
241 parse_source_file(sfile, referenced_features)
242
243 for kfile in kconfig_files:
cf132e4a
VR
244 if ignore and re.match(ignore, kfile):
245 # do not collect references for files matching %ignore
246 parse_kconfig_file(kfile, defined_features, dict())
247 else:
248 parse_kconfig_file(kfile, defined_features, referenced_features)
24fe1f03 249
b1a3f243 250 undefined = {} # {feature: [files]}
24fe1f03 251 for feature in sorted(referenced_features):
cc641d55
VR
252 # filter some false positives
253 if feature == "FOO" or feature == "BAR" or \
254 feature == "FOO_BAR" or feature == "XXX":
255 continue
24fe1f03
VR
256 if feature not in defined_features:
257 if feature.endswith("_MODULE"):
cc641d55 258 # avoid false positives for kernel modules
24fe1f03
VR
259 if feature[:-len("_MODULE")] in defined_features:
260 continue
b1a3f243
VR
261 undefined[feature] = referenced_features.get(feature)
262 return undefined
24fe1f03
VR
263
264
265def parse_source_file(sfile, referenced_features):
266 """Parse @sfile for referenced Kconfig features."""
267 lines = []
268 with open(sfile, "r") as stream:
269 lines = stream.readlines()
270
271 for line in lines:
272 if not "CONFIG_" in line:
273 continue
274 features = REGEX_SOURCE_FEATURE.findall(line)
275 for feature in features:
276 if not REGEX_FILTER_FEATURES.search(feature):
277 continue
cc641d55
VR
278 sfiles = referenced_features.get(feature, set())
279 sfiles.add(sfile)
280 referenced_features[feature] = sfiles
24fe1f03
VR
281
282
283def get_features_in_line(line):
284 """Return mentioned Kconfig features in @line."""
285 return REGEX_FEATURE.findall(line)
286
287
288def parse_kconfig_file(kfile, defined_features, referenced_features):
289 """Parse @kfile and update feature definitions and references."""
290 lines = []
291 skip = False
292
293 with open(kfile, "r") as stream:
294 lines = stream.readlines()
295
296 for i in range(len(lines)):
297 line = lines[i]
298 line = line.strip('\n')
cc641d55 299 line = line.split("#")[0] # ignore comments
24fe1f03
VR
300
301 if REGEX_KCONFIG_DEF.match(line):
302 feature_def = REGEX_KCONFIG_DEF.findall(line)
303 defined_features.add(feature_def[0])
304 skip = False
305 elif REGEX_KCONFIG_HELP.match(line):
306 skip = True
307 elif skip:
cc641d55 308 # ignore content of help messages
24fe1f03
VR
309 pass
310 elif REGEX_KCONFIG_STMT.match(line):
311 features = get_features_in_line(line)
cc641d55 312 # multi-line statements
24fe1f03
VR
313 while line.endswith("\\"):
314 i += 1
315 line = lines[i]
316 line = line.strip('\n')
317 features.extend(get_features_in_line(line))
318 for feature in set(features):
0bd38ae3
VR
319 if REGEX_NUMERIC.match(feature):
320 # ignore numeric values
321 continue
24fe1f03
VR
322 paths = referenced_features.get(feature, set())
323 paths.add(kfile)
324 referenced_features[feature] = paths
325
326
327if __name__ == "__main__":
328 main()