3 # Generate kernel .config file based on special kernel.conf rules file.
11 if argc < 4 or argc > 5:
12 print "Usage: %s target_arch kernel.conf input-config [output-config]" % sys.argv[0]
16 kernelconfig = sys.argv[2]
17 inconfig = sys.argv[3]
19 outconfig = sys.argv[4]
23 from UserDict import UserDict
25 # odict from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
26 class odict(UserDict):
27 def __init__(self, dict = None):
29 UserDict.__init__(self, dict)
31 def __delitem__(self, key):
32 UserDict.__delitem__(self, key)
33 self._keys.remove(key)
35 def __setitem__(self, key, item):
36 UserDict.__setitem__(self, key, item)
37 if key not in self._keys: self._keys.append(key)
44 dict = UserDict.copy(self)
45 dict._keys = self._keys[:]
49 return zip(self._keys, self.values())
58 raise KeyError('dictionary is empty')
65 def setdefault(self, key, failobj = None):
66 UserDict.setdefault(self, key, failobj)
67 if key not in self._keys: self._keys.append(key)
69 def update(self, dict):
70 UserDict.update(self, dict)
71 for key in dict.keys():
72 if key not in self._keys: self._keys.append(key)
75 return map(self.get, self._keys)
80 f = open(kernelconfig, 'r')
82 if l[:6] == 'CONFIG_':
83 print "Omit CONFIG_ when specifing symbol name: %s" % l
87 if re.match('^#', l) or re.match('^\s*$', l):
90 if not re.match('^[0-9A-Z]+', l):
91 print "Unknown line: %s" % l
97 if dict.has_key(symbol):
98 print "Duplicate symbol: %s" % symbol
102 # inline symbols: for current arch, duplicates allowed
103 if symbol.find('=') > 0:
104 (symbol, value) = symbol.split('=')
105 # empty value means delete the symbol
107 if dict.has_key(symbol):
117 (key, value) = item.split('=')
120 print "Invalid line: %s" % l.strip()
124 print "Invalid line: %s" % l.strip()
129 print "Bad line: %s" % l
132 # take arch line, otherwise fallback to 'all'
133 if hash.has_key(arch):
134 # allow empty value to skip symbol on this arch
135 if not hash[arch] == "":
136 dict[symbol] = hash[arch]
138 if hash.has_key('all'):
139 dict[symbol] = hash['all']
146 f = open(inconfig, 'r')
151 cfg += '\n# PLD configs\n'
152 for symbol in dict.items():
156 rp = re.compile("^CONFIG_%s=.*$" % key, re.MULTILINE)
157 cfg = rp.sub("", cfg)
158 rp = re.compile("^# CONFIG_%s is not set$" % key, re.MULTILINE)
159 cfg = rp.sub("", cfg)
162 cfg += "CONFIG_%s=y\n" % key
164 cfg += "CONFIG_%s=m\n" % key
166 cfg += "# CONFIG_%s is not set\n" % key
167 elif re.compile('^"[^"]*"$').match(val):
168 cfg += "CONFIG_%s=%s\n" % (key, val)
169 elif re.compile('^-?[0-9]+$').match(val):
170 cfg += "CONFIG_%s=%s\n" % (key, val)
171 elif re.compile('^0x[0-9A-Fa-f]+$').match(val):
172 cfg += "CONFIG_%s=%s\n" % (key, val)
174 print "Unknown value [%s] for key: %s" % (val, key)
180 f = open(outconfig, 'w')