Merge branch 'akpm'
[linux-2.6/next.git] / scripts / kconfig / streamline_config.pl
blobec7afce4c88d3155d95c454915b5efb249568548
1 #!/usr/bin/perl -w
3 # Copyright 2005-2009 - Steven Rostedt
4 # Licensed under the terms of the GNU GPL License version 2
6 # It's simple enough to figure out how this works.
7 # If not, then you can ask me at stripconfig@goodmis.org
9 # What it does?
11 # If you have installed a Linux kernel from a distribution
12 # that turns on way too many modules than you need, and
13 # you only want the modules you use, then this program
14 # is perfect for you.
16 # It gives you the ability to turn off all the modules that are
17 # not loaded on your system.
19 # Howto:
21 # 1. Boot up the kernel that you want to stream line the config on.
22 # 2. Change directory to the directory holding the source of the
23 # kernel that you just booted.
24 # 3. Copy the configuraton file to this directory as .config
25 # 4. Have all your devices that you need modules for connected and
26 # operational (make sure that their corresponding modules are loaded)
27 # 5. Run this script redirecting the output to some other file
28 # like config_strip.
29 # 6. Back up your old config (if you want too).
30 # 7. copy the config_strip file to .config
31 # 8. Run "make oldconfig"
33 # Now your kernel is ready to be built with only the modules that
34 # are loaded.
36 # Here's what I did with my Debian distribution.
38 # cd /usr/src/linux-2.6.10
39 # cp /boot/config-2.6.10-1-686-smp .config
40 # ~/bin/streamline_config > config_strip
41 # mv .config config_sav
42 # mv config_strip .config
43 # make oldconfig
45 use strict;
46 use Getopt::Long;
48 my $config = ".config";
50 my $uname = `uname -r`;
51 chomp $uname;
53 my @searchconfigs = (
55 "file" => ".config",
56 "exec" => "cat",
59 "file" => "/proc/config.gz",
60 "exec" => "zcat",
63 "file" => "/boot/config-$uname",
64 "exec" => "cat",
67 "file" => "/boot/vmlinuz-$uname",
68 "exec" => "scripts/extract-ikconfig",
69 "test" => "scripts/extract-ikconfig",
72 "file" => "vmlinux",
73 "exec" => "scripts/extract-ikconfig",
74 "test" => "scripts/extract-ikconfig",
77 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
78 "exec" => "scripts/extract-ikconfig",
79 "test" => "scripts/extract-ikconfig",
82 "file" => "kernel/configs.ko",
83 "exec" => "scripts/extract-ikconfig",
84 "test" => "scripts/extract-ikconfig",
87 "file" => "kernel/configs.o",
88 "exec" => "scripts/extract-ikconfig",
89 "test" => "scripts/extract-ikconfig",
93 sub find_config {
94 foreach my $conf (@searchconfigs) {
95 my $file = $conf->{"file"};
97 next if ( ! -f "$file");
99 if (defined($conf->{"test"})) {
100 `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
101 next if ($?);
104 my $exec = $conf->{"exec"};
106 print STDERR "using config: '$file'\n";
108 open(CIN, "$exec $file |") || die "Failed to run $exec $file";
109 return;
111 die "No config file found";
114 find_config;
116 # Parse options
117 my $localmodconfig = 0;
118 my $localyesconfig = 0;
120 GetOptions("localmodconfig" => \$localmodconfig,
121 "localyesconfig" => \$localyesconfig);
123 # Get the build source and top level Kconfig file (passed in)
124 my $ksource = $ARGV[0];
125 my $kconfig = $ARGV[1];
126 my $lsmod_file = $ENV{'LSMOD'};
128 my @makefiles = `find $ksource -name Makefile 2>/dev/null`;
129 chomp @makefiles;
131 my %depends;
132 my %selects;
133 my %prompts;
134 my %objects;
135 my $var;
136 my $iflevel = 0;
137 my @ifdeps;
139 # prevent recursion
140 my %read_kconfigs;
142 sub read_kconfig {
143 my ($kconfig) = @_;
145 my $state = "NONE";
146 my $config;
147 my @kconfigs;
149 my $cont = 0;
150 my $line;
152 my $source = "$ksource/$kconfig";
153 my $last_source = "";
155 # Check for any environment variables used
156 while ($source =~ /\$(\w+)/ && $last_source ne $source) {
157 my $env = $1;
158 $last_source = $source;
159 $source =~ s/\$$env/$ENV{$env}/;
162 open(KIN, "$source") || die "Can't open $kconfig";
163 while (<KIN>) {
164 chomp;
166 # Make sure that lines ending with \ continue
167 if ($cont) {
168 $_ = $line . " " . $_;
171 if (s/\\$//) {
172 $cont = 1;
173 $line = $_;
174 next;
177 $cont = 0;
179 # collect any Kconfig sources
180 if (/^source\s*"(.*)"/) {
181 $kconfigs[$#kconfigs+1] = $1;
184 # configs found
185 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
186 $state = "NEW";
187 $config = $2;
189 for (my $i = 0; $i < $iflevel; $i++) {
190 if ($i) {
191 $depends{$config} .= " " . $ifdeps[$i];
192 } else {
193 $depends{$config} = $ifdeps[$i];
195 $state = "DEP";
198 # collect the depends for the config
199 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
200 $state = "DEP";
201 $depends{$config} = $1;
202 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
203 $depends{$config} .= " " . $1;
205 # Get the configs that select this config
206 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
207 if (defined($selects{$1})) {
208 $selects{$1} .= " " . $config;
209 } else {
210 $selects{$1} = $config;
213 # configs without prompts must be selected
214 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
215 # note if the config has a prompt
216 $prompts{$config} = 1;
218 # Check for if statements
219 } elsif (/^if\s+(.*\S)\s*$/) {
220 my $deps = $1;
221 # remove beginning and ending non text
222 $deps =~ s/^[^a-zA-Z0-9_]*//;
223 $deps =~ s/[^a-zA-Z0-9_]*$//;
225 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
227 $ifdeps[$iflevel++] = join ':', @deps;
229 } elsif (/^endif/) {
231 $iflevel-- if ($iflevel);
233 # stop on "help"
234 } elsif (/^\s*help\s*$/) {
235 $state = "NONE";
238 close(KIN);
240 # read in any configs that were found.
241 foreach $kconfig (@kconfigs) {
242 if (!defined($read_kconfigs{$kconfig})) {
243 $read_kconfigs{$kconfig} = 1;
244 read_kconfig($kconfig);
249 if ($kconfig) {
250 read_kconfig($kconfig);
253 # Read all Makefiles to map the configs to the objects
254 foreach my $makefile (@makefiles) {
256 my $cont = 0;
258 open(MIN,$makefile) || die "Can't open $makefile";
259 while (<MIN>) {
260 my $objs;
262 # is this a line after a line with a backslash?
263 if ($cont && /(\S.*)$/) {
264 $objs = $1;
266 $cont = 0;
268 # collect objects after obj-$(CONFIG_FOO_BAR)
269 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
270 $var = $1;
271 $objs = $2;
273 if (defined($objs)) {
274 # test if the line ends with a backslash
275 if ($objs =~ m,(.*)\\$,) {
276 $objs = $1;
277 $cont = 1;
280 foreach my $obj (split /\s+/,$objs) {
281 $obj =~ s/-/_/g;
282 if ($obj =~ /(.*)\.o$/) {
283 # Objects may be enabled by more than one config.
284 # Store configs in an array.
285 my @arr;
287 if (defined($objects{$1})) {
288 @arr = @{$objects{$1}};
291 $arr[$#arr+1] = $var;
293 # The objects have a hash mapping to a reference
294 # of an array of configs.
295 $objects{$1} = \@arr;
300 close(MIN);
303 my %modules;
305 if (defined($lsmod_file)) {
306 if ( ! -f $lsmod_file) {
307 if ( -f $ENV{'objtree'}."/".$lsmod_file) {
308 $lsmod_file = $ENV{'objtree'}."/".$lsmod_file;
309 } else {
310 die "$lsmod_file not found";
313 if ( -x $lsmod_file) {
314 # the file is executable, run it
315 open(LIN, "$lsmod_file|");
316 } else {
317 # Just read the contents
318 open(LIN, "$lsmod_file");
320 } else {
322 # see what modules are loaded on this system
323 my $lsmod;
325 foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
326 if ( -x "$dir/lsmod" ) {
327 $lsmod = "$dir/lsmod";
328 last;
331 if (!defined($lsmod)) {
332 # try just the path
333 $lsmod = "lsmod";
336 open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod";
339 while (<LIN>) {
340 next if (/^Module/); # Skip the first line.
341 if (/^(\S+)/) {
342 $modules{$1} = 1;
345 close (LIN);
347 # add to the configs hash all configs that are needed to enable
348 # a loaded module.
349 my %configs;
350 foreach my $module (keys(%modules)) {
351 if (defined($objects{$module})) {
352 my @arr = @{$objects{$module}};
353 foreach my $conf (@arr) {
354 $configs{$conf} = $module;
356 } else {
357 # Most likely, someone has a custom (binary?) module loaded.
358 print STDERR "$module config not found!!\n";
362 my $valid = "A-Za-z_0-9";
363 my $repeat = 1;
366 # Note, we do not care about operands (like: &&, ||, !) we want to add any
367 # config that is in the depend list of another config. This script does
368 # not enable configs that are not already enabled. If we come across a
369 # config A that depends on !B, we can still add B to the list of depends
370 # to keep on. If A was on in the original config, B would not have been
371 # and B would not be turned on by this script.
373 sub parse_config_dep_select
375 my ($p) = @_;
377 while ($p =~ /[$valid]/) {
379 if ($p =~ /^[^$valid]*([$valid]+)/) {
380 my $conf = "CONFIG_" . $1;
382 $p =~ s/^[^$valid]*[$valid]+//;
384 if (!defined($configs{$conf})) {
385 # We must make sure that this config has its
386 # dependencies met.
387 $repeat = 1; # do again
388 $configs{$conf} = 1;
390 } else {
391 die "this should never happen";
396 while ($repeat) {
397 $repeat = 0;
399 foreach my $config (keys %configs) {
400 $config =~ s/^CONFIG_//;
402 if (defined($depends{$config})) {
403 # This config has dependencies. Make sure they are also included
404 parse_config_dep_select $depends{$config};
407 if (defined($prompts{$config}) || !defined($selects{$config})) {
408 next;
411 # config has no prompt and must be selected.
412 parse_config_dep_select $selects{$config};
416 my %setconfigs;
418 # Finally, read the .config file and turn off any module enabled that
419 # we could not find a reason to keep enabled.
420 while(<CIN>) {
422 if (/CONFIG_IKCONFIG/) {
423 if (/# CONFIG_IKCONFIG is not set/) {
424 # enable IKCONFIG at least as a module
425 print "CONFIG_IKCONFIG=m\n";
426 # don't ask about PROC
427 print "# CONFIG_IKCONFIG_PROC is not set\n";
428 } else {
429 print;
431 next;
434 if (/^(CONFIG.*)=(m|y)/) {
435 if (defined($configs{$1})) {
436 if ($localyesconfig) {
437 $setconfigs{$1} = 'y';
438 } else {
439 $setconfigs{$1} = $2;
441 } elsif ($2 eq "m") {
442 print "# $1 is not set\n";
443 next;
446 print;
448 close(CIN);
450 # Integrity check, make sure all modules that we want enabled do
451 # indeed have their configs set.
452 loop:
453 foreach my $module (keys(%modules)) {
454 if (defined($objects{$module})) {
455 my @arr = @{$objects{$module}};
456 foreach my $conf (@arr) {
457 if (defined($setconfigs{$conf})) {
458 next loop;
461 print STDERR "module $module did not have configs";
462 foreach my $conf (@arr) {
463 print STDERR " " , $conf;
465 print STDERR "\n";