1 #!/usr/local/bin/python
3 # This script analyzes sys/conf/files*, sys/conf/options*,
4 # sys/conf/NOTES, and sys/*/conf/NOTES and checks for inconsistencies
5 # such as options or devices that are not specified in any NOTES files
6 # or MI devices specified in MD NOTES files.
15 print >>sys.stderr, "notescheck <path>"
17 print >>sys.stderr, "Where 'path' is a path to a kernel source tree."
19 # These files are used to determine if a path is a valid kernel source tree.
20 requiredfiles = ['conf/files', 'conf/options', 'conf/NOTES']
22 # This special platform string is used for managing MI options.
23 global_platform = 'global'
25 # This is a global string that represents the current file and line
29 # Format the contents of a set into a sorted, comma-separated string
38 return "%s and %s" % (l[0], l[1])
43 s = "%s, %s" % (s, item)
44 s = "%s, and %s" % (s, l[-1])
47 # This class actually covers both options and devices. For each named
48 # option we maintain two different lists. One is the list of
49 # platforms that the option was defined in via an options or files
50 # file. The other is the list of platforms that the option was tested
51 # in via a NOTES file. All options are stored as lowercase since
52 # config(8) treats the names as case-insensitive.
54 def __init__(self, name):
60 def set_type(self, type):
63 self.type_location = location
64 elif self.type != type:
65 print "WARN: Attempt to change type of %s from %s to %s%s" % \
66 (self.name, self.type, type, location)
67 print " Previous type set%s" % (self.type_location)
69 def add_define(self, platform):
70 self.defines.add(platform)
72 def add_test(self, platform):
73 self.tests.add(platform)
76 if self.type == 'option':
77 return 'option %s' % (self.name.upper())
80 return '%s %s' % (self.type, self.name)
83 # If the defined and tested sets are equal, then this option
85 if self.defines == self.tests:
88 # If the tested set contains the global platform, then this
90 if global_platform in self.tests:
93 if global_platform in self.defines:
94 # If the device is defined globally ans is never tested, whine.
95 if len(self.tests) == 0:
96 print 'WARN: %s is defined globally but never tested' % \
100 # If the device is defined globally and is tested on
101 # multiple MD platforms, then it is ok. This often occurs
102 # for drivers that are shared across multiple, but not
103 # all, platforms (e.g. acpi, agp).
104 if len(self.tests) > 1:
107 # If a device is defined globally but is only tested on a
108 # single MD platform, then whine about this.
109 print 'WARN: %s is defined globally but only tested in %s NOTES' % \
110 (self.title(), format_set(self.tests))
113 # If an option or device is never tested, whine.
114 if len(self.tests) == 0:
115 print 'WARN: %s is defined in %s but never tested' % \
116 (self.title(), format_set(self.defines))
119 # The set of MD platforms where this option is defined, but not tested.
120 notest = self.defines - self.tests
122 print 'WARN: %s is not tested in %s NOTES' % \
123 (self.title(), format_set(notest))
126 print 'ERROR: bad state for %s: defined in %s, tested in %s' % \
127 (self.title(), format_set(self.defines), format_set(self.tests))
129 # This class maintains a dictionary of options keyed by name.
134 # Look up the object for a given option by name. If the option
135 # doesn't already exist, then add a new option.
136 def find(self, name):
138 if name in self.options:
139 return self.options[name]
140 option = Option(name)
141 self.options[name] = option
144 # Warn about inconsistencies
146 keys = self.options.keys()
149 option = self.options[key]
152 # Global map of options
155 # Look for MD NOTES files to build our list of platforms. We ignore
156 # platforms that do not have a NOTES file.
157 def find_platforms(tree):
159 for file in glob.glob(tree + '*/conf/NOTES'):
160 if not file.startswith(tree):
161 print >>sys.stderr, "Bad MD NOTES file %s" %(file)
163 platforms.append(file[len(tree):].split('/')[0])
164 if global_platform in platforms:
165 print >>sys.stderr, "Found MD NOTES file for global platform"
169 # Parse a file that has escaped newlines. Any escaped newlines are
170 # coalesced and each logical line is passed to the callback function.
171 # This also skips blank lines and comments.
172 def parse_file(file, callback, *args):
179 # Update parsing location
181 location = ' at %s:%d' % (file, i)
186 # If the previous line had an escaped newline, append this
188 if current is not None:
189 line = current + line
192 # If the line ends in a '\', set current to the line (minus
193 # the escape) and continue.
194 if len(line) > 0 and line[-1] == '\\':
198 # Skip blank lines or lines with only whitespace
199 if len(line) == 0 or len(line.split()) == 0:
202 # Skip comment lines. Any line whose first non-space
203 # character is a '#' is considered a comment.
204 if line.split()[0][0] == '#':
207 # Invoke the callback on this line
208 callback(line, *args)
209 if current is not None:
210 callback(current, *args)
214 # Split a line into words on whitespace with the exception that quoted
215 # strings are always treated as a single word.
220 # First, split the line on quote characters.
221 groups = line.split('"')
223 # Ensure we have an even number of quotes. The 'groups' array
224 # will contain 'number of quotes' + 1 entries, so it should have
225 # an odd number of entries.
226 if len(groups) % 2 == 0:
227 print >>sys.stderr, "Failed to tokenize: %s%s" (line, location)
230 # String split all the "odd" groups since they are not quoted strings.
238 for word in group.split():
243 # Parse a sys/conf/files* file adding defines for any options
244 # encountered. Note files does not differentiate between options and
246 def parse_files_line(line, platform):
247 words = tokenize(line)
249 # Skip include lines.
250 if words[0] == 'include':
253 # Skip standard lines as they have no devices or options.
254 if words[1] == 'standard':
257 # Remaining lines better be optional or mandatory lines.
258 if words[1] != 'optional' and words[1] != 'mandatory':
259 print >>sys.stderr, "Invalid files line: %s%s" % (line, location)
261 # Drop the first two words and begin parsing keywords and devices.
263 for word in words[2:]:
269 if word == 'no-obj' or word == 'no-implicit-rule' or \
270 word == 'before-depend' or word == 'local' or \
271 word == 'no-depend' or word == 'profiling-routine' or \
275 # Skip keywords and their following argument
276 if word == 'dependency' or word == 'clean' or \
277 word == 'compile-with' or word == 'warning':
285 option = options.find(word)
286 option.add_define(platform)
288 # Parse a sys/conf/options* file adding defines for any options
289 # encountered. Unlike a files file, options files only add options.
290 def parse_options_line(line, platform):
291 # The first word is the option name.
292 name = line.split()[0]
294 # Ignore DEV_xxx options. These are magic options that are
295 # aliases for 'device xxx'.
296 if name.startswith('DEV_'):
299 option = options.find(name)
300 option.add_define(platform)
301 option.set_type('option')
303 # Parse a sys/conf/NOTES file adding tests for any options or devices
305 def parse_notes_line(line, platform):
308 # Skip lines with just whitespace
312 if words[0] == 'device' or words[0] == 'devices':
313 option = options.find(words[1])
314 option.add_test(platform)
315 option.set_type('device')
318 if words[0] == 'option' or words[0] == 'options':
319 option = options.find(words[1].split('=')[0])
320 option.add_test(platform)
321 option.set_type('option')
327 if len(sys.argv) != 2:
331 # Ensure the path has a trailing '/'.
335 for file in requiredfiles:
336 if not os.path.exists(tree + file):
337 print>> sys.stderr, "Kernel source tree missing %s" % (file)
340 platforms = find_platforms(tree)
342 # First, parse global files.
343 parse_file(tree + 'conf/files', parse_files_line, global_platform)
344 parse_file(tree + 'conf/options', parse_options_line, global_platform)
345 parse_file(tree + 'conf/NOTES', parse_notes_line, global_platform)
347 # Next, parse MD files.
348 for platform in platforms:
349 files_file = tree + 'conf/files.' + platform
350 if os.path.exists(files_file):
351 parse_file(files_file, parse_files_line, platform)
352 options_file = tree + 'conf/options.' + platform
353 if os.path.exists(options_file):
354 parse_file(options_file, parse_options_line, platform)
355 parse_file(tree + platform + '/conf/NOTES', parse_notes_line, platform)
360 if __name__ == "__main__":