3 # Copyright (c) 2015, The Linux Foundation. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of The Linux Foundation nor
13 # the names of its contributors may be used to endorse or promote
14 # products derived from this software without specific prior written
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 Android kernel configuration validator.
32 The Android kernel reference trees contain some config stubs of
33 configuration options that are required for Android to function
34 correctly, and additional ones that are recommended.
36 This script can help compare these base configs with the ".config"
37 output of the compiler to determine if the proper configs are defined.
40 from collections import namedtuple
41 from optparse import OptionParser
45 version = "check-config.py, version 0.0.1"
47 req_re = re.compile(r'''^CONFIG_(.*)=(.*)$''')
48 forb_re = re.compile(r'''^# CONFIG_(.*) is not set$''')
49 comment_re = re.compile(r'''^(#.*|)$''')
51 Enabled = namedtuple('Enabled', ['name', 'value'])
52 Disabled = namedtuple('Disabled', ['name'])
54 def walk_config(name):
55 with open(name, 'r') as fd:
58 m = req_re.match(line)
60 yield Enabled(m.group(1), m.group(2))
63 m = forb_re.match(line)
65 yield Disabled(m.group(1))
68 m = comment_re.match(line)
72 print "WARNING: Unknown .config line: ", line
78 self.forbidden = set()
80 def add_required(self, fname):
81 for ent in walk_config(fname):
82 if type(ent) is Enabled:
83 self.required[ent.name] = ent.value
84 elif type(ent) is Disabled:
85 if ent.name in self.required:
86 del self.required[ent.name]
87 self.forbidden.add(ent.name)
89 def add_exempted(self, fname):
90 with open(fname, 'r') as fd:
93 self.exempted.add(line)
95 def check(self, path):
98 # Don't run this for mdm targets
99 if re.search('mdm', path):
100 print "Not applicable to mdm targets... bypassing"
102 for ent in walk_config(path):
103 # Go to the next iteration if this config is exempt
104 if ent.name in self.exempted:
107 if type(ent) is Enabled:
108 if ent.name in self.forbidden:
109 print "error: Config should not be present: %s" %ent.name
112 if ent.name in self.required and ent.value != self.required[ent.name]:
113 print "error: Config has wrong value: %s %s expecting: %s" \
114 %(ent.name, ent.value, self.required[ent.name])
117 elif type(ent) is Disabled:
118 if ent.name in self.required:
119 print "error: Config should be present, but is disabled: %s" %ent.name
126 usage = """%prog [options] path/to/.config"""
127 parser = OptionParser(usage=usage, version=version)
128 parser.add_option('-r', '--required', dest="required",
130 parser.add_option('-e', '--exempted', dest="exempted",
132 (options, args) = parser.parse_args()
134 parser.error("Expecting a single path argument to .config")
135 elif options.required is None or options.exempted is None:
136 parser.error("Expecting a file containing required configurations")
139 for r in options.required:
141 for e in options.exempted:
146 if __name__ == '__main__':