Re: diet-kconfig: a script to trim unneeded kconfigs

From: Chris Li
Date: Fri Sep 19 2008 - 19:57:41 EST


On Fri, Sep 19, 2008 at 4:55 PM, Chris Li <lkml@xxxxxxxxxxx> wrote:
> Incidentally, I wrote such a script as well. In 64 lines of python code.
> I attach the script in question in the mail. I use a approach very
> similar to yours. But I work around some of the nasty part.

Oops, I forget to attach the script. Here it is.

Chris
#!/usr/bin/python
import re
import os

class Kbuild:
def __init__(self, root):
self.root = root
self.confs = {} # dict of lists
self.modules = {}

self.read_kbuild()

def all_kbuild(self):
cmd = 'find . '\
' -regex "%s/[a-z].*/\\(Makefile\\|Kbuild\\)"' \
' -not -regex ".*/\(' \
'arch\|Documents\|firmware\|scripts\|raid6test\)/.*"' \
% self.root
return os.popen(cmd).read().split()

def read_kbuild(self):
allfile = [open(f).read() for f in self.all_kbuild()]
data = "\n".join(allfile)
data = re.sub(r"(?m)^ifeq\s+\(\$\((CONFIG_\S+)\s*\),\s*m\).*",
r"obj-$(\1) \\", data)
data = re.sub(r"(?m)\\\n\s*|obj-m ", '', data)
all = re.findall( r"(?m)obj-\$\((CONFIG_\S+)\)\s*[:+]=(.*)", data)
self.data = data
for (conf, modules) in all:
mods = re.findall(r"(\S+)\.o", modules)
for m in mods:
m = re.sub("-","_",m)
#print conf, "->", m
self.confs.setdefault(conf, []).append(m)
self.modules.setdefault(m, []).append(conf)
def read_modules(self):
data = open("/proc/modules").read()
return re.findall(r"(?m)^\S+", data)

def test_module(self, m):
all, conf = m.group(0,1)
if conf in self.confs:
return "# %s is not set"%conf
return all

def filter(self, config):
#print self.data
for m in self.read_modules():
if not self.modules.has_key(m):
print m,": Uknown module, may be firemware?"
continue
for c in self.modules[m]:
if c in self.confs:
del self.confs[c]
data = open(config).read()
data = re.sub(r"(?m)^(CONFIG_\S+)=m", self.test_module, data)
out = config + ".min"
print "Writing new config to:", out
open(out, "w").write(data)

def main():
kbuild = Kbuild(".")
kbuild.filter(".config")
main()