blob: 79c2bdf5975948157014dc20d9dadec62208401e [file] [log] [blame]
Channagoud Kadabi1a82c2b2016-07-01 12:28:20 -07001#! /usr/bin/env python
2
3# Copyright (c) 2015, The Linux Foundation. All rights reserved.
4#
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
15# permission.
16#
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.
28
29"""
30Android kernel configuration validator.
31
32The Android kernel reference trees contain some config stubs of
33configuration options that are required for Android to function
34correctly, and additional ones that are recommended.
35
36This script can help compare these base configs with the ".config"
37output of the compiler to determine if the proper configs are defined.
38"""
39
40from collections import namedtuple
41from optparse import OptionParser
42import re
43import sys
44
45version = "check-config.py, version 0.0.1"
46
47req_re = re.compile(r'''^CONFIG_(.*)=(.*)$''')
48forb_re = re.compile(r'''^# CONFIG_(.*) is not set$''')
49comment_re = re.compile(r'''^(#.*|)$''')
50
51Enabled = namedtuple('Enabled', ['name', 'value'])
52Disabled = namedtuple('Disabled', ['name'])
53
54def walk_config(name):
55 with open(name, 'r') as fd:
56 for line in fd:
57 line = line.rstrip()
58 m = req_re.match(line)
59 if m:
60 yield Enabled(m.group(1), m.group(2))
61 continue
62
63 m = forb_re.match(line)
64 if m:
65 yield Disabled(m.group(1))
66 continue
67
68 m = comment_re.match(line)
69 if m:
70 continue
71
72 print "WARNING: Unknown .config line: ", line
73
74class Checker():
75 def __init__(self):
76 self.required = {}
77 self.exempted = set()
78 self.forbidden = set()
79
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)
88
89 def add_exempted(self, fname):
90 with open(fname, 'r') as fd:
91 for line in fd:
92 line = line.rstrip()
93 self.exempted.add(line)
94
95 def check(self, path):
96 failure = False
97
98 # Don't run this for mdm targets
99 if re.search('mdm', path):
100 print "Not applicable to mdm targets... bypassing"
101 else:
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:
105 continue
106
107 if type(ent) is Enabled:
108 if ent.name in self.forbidden:
109 print "error: Config should not be present: %s" %ent.name
110 failure = True
111
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])
115 failure = True
116
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
120 failure = True
121
122 if failure:
123 sys.exit(1)
124
125def main():
126 usage = """%prog [options] path/to/.config"""
127 parser = OptionParser(usage=usage, version=version)
128 parser.add_option('-r', '--required', dest="required",
129 action="append")
130 parser.add_option('-e', '--exempted', dest="exempted",
131 action="append")
132 (options, args) = parser.parse_args()
133 if len(args) != 1:
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")
137
138 ch = Checker()
139 for r in options.required:
140 ch.add_required(r)
141 for e in options.exempted:
142 ch.add_exempted(e)
143
144 ch.check(args[0])
145
146if __name__ == '__main__':
147 main()