localmodconfig: Check if configs are already set for selects
[linux-block.git] / scripts / kconfig / streamline_config.pl
CommitLineData
dcc60243
SR
1#!/usr/bin/perl -w
2#
cce1dac8 3# Copyright 2005-2009 - Steven Rostedt
dcc60243
SR
4# Licensed under the terms of the GNU GPL License version 2
5#
6# It's simple enough to figure out how this works.
7# If not, then you can ask me at stripconfig@goodmis.org
8#
9# What it does?
10#
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.
15#
16# It gives you the ability to turn off all the modules that are
17# not loaded on your system.
18#
19# Howto:
20#
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"
32#
33# Now your kernel is ready to be built with only the modules that
34# are loaded.
35#
36# Here's what I did with my Debian distribution.
37#
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
44#
cf5a189d 45use strict;
22d550ae 46use Getopt::Long;
cf5a189d 47
dcc60243 48my $config = ".config";
dcc60243 49
cdfc4795
SR
50my $uname = `uname -r`;
51chomp $uname;
52
53my @searchconfigs = (
a9024838
SR
54 {
55 "file" => ".config",
56 "exec" => "cat",
57 },
cdfc4795
SR
58 {
59 "file" => "/proc/config.gz",
60 "exec" => "zcat",
61 },
810b2be6
SR
62 {
63 "file" => "/boot/config-$uname",
64 "exec" => "cat",
65 },
cdfc4795
SR
66 {
67 "file" => "/boot/vmlinuz-$uname",
68 "exec" => "scripts/extract-ikconfig",
69 "test" => "scripts/extract-ikconfig",
70 },
71 {
72 "file" => "vmlinux",
73 "exec" => "scripts/extract-ikconfig",
74 "test" => "scripts/extract-ikconfig",
75 },
76 {
77 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
78 "exec" => "scripts/extract-ikconfig",
79 "test" => "scripts/extract-ikconfig",
80 },
81 {
82 "file" => "kernel/configs.ko",
83 "exec" => "scripts/extract-ikconfig",
84 "test" => "scripts/extract-ikconfig",
85 },
86 {
87 "file" => "kernel/configs.o",
88 "exec" => "scripts/extract-ikconfig",
89 "test" => "scripts/extract-ikconfig",
90 },
cdfc4795
SR
91);
92
93sub find_config {
94 foreach my $conf (@searchconfigs) {
95 my $file = $conf->{"file"};
96
97 next if ( ! -f "$file");
98
99 if (defined($conf->{"test"})) {
100 `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
101 next if ($?);
102 }
103
104 my $exec = $conf->{"exec"};
105
106 print STDERR "using config: '$file'\n";
107
108 open(CIN, "$exec $file |") || die "Failed to run $exec $file";
109 return;
110 }
111 die "No config file found";
112}
113
114find_config;
115
4f4c51c9
SR
116# Read in the entire config file into config_file
117my @config_file = <CIN>;
118close CIN;
119
22d550ae
AL
120# Parse options
121my $localmodconfig = 0;
122my $localyesconfig = 0;
123
124GetOptions("localmodconfig" => \$localmodconfig,
125 "localyesconfig" => \$localyesconfig);
126
463bf900
SR
127# Get the build source and top level Kconfig file (passed in)
128my $ksource = $ARGV[0];
129my $kconfig = $ARGV[1];
f597a718 130my $lsmod_file = $ENV{'LSMOD'};
463bf900 131
17431928
TF
132my @makefiles = `find $ksource -name Makefile 2>/dev/null`;
133chomp @makefiles;
134
dcc60243
SR
135my %depends;
136my %selects;
137my %prompts;
138my %objects;
139my $var;
13d7e938
SR
140my $iflevel = 0;
141my @ifdeps;
dcc60243 142
dcc60243
SR
143# prevent recursion
144my %read_kconfigs;
145
146sub read_kconfig {
147 my ($kconfig) = @_;
148
149 my $state = "NONE";
150 my $config;
151 my @kconfigs;
152
20d19047
SR
153 my $cont = 0;
154 my $line;
155
4908980b
SR
156 my $source = "$ksource/$kconfig";
157 my $last_source = "";
158
159 # Check for any environment variables used
160 while ($source =~ /\$(\w+)/ && $last_source ne $source) {
161 my $env = $1;
162 $last_source = $source;
163 $source =~ s/\$$env/$ENV{$env}/;
164 }
165
166 open(KIN, "$source") || die "Can't open $kconfig";
dcc60243
SR
167 while (<KIN>) {
168 chomp;
169
20d19047
SR
170 # Make sure that lines ending with \ continue
171 if ($cont) {
172 $_ = $line . " " . $_;
173 }
174
175 if (s/\\$//) {
176 $cont = 1;
177 $line = $_;
178 next;
179 }
180
181 $cont = 0;
182
dcc60243
SR
183 # collect any Kconfig sources
184 if (/^source\s*"(.*)"/) {
185 $kconfigs[$#kconfigs+1] = $1;
186 }
187
188 # configs found
8ef17fa2 189 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
dcc60243 190 $state = "NEW";
8ef17fa2 191 $config = $2;
dcc60243 192
0b58a99e 193 # Add depends for 'if' nesting
13d7e938
SR
194 for (my $i = 0; $i < $iflevel; $i++) {
195 if ($i) {
196 $depends{$config} .= " " . $ifdeps[$i];
197 } else {
198 $depends{$config} = $ifdeps[$i];
199 }
200 $state = "DEP";
201 }
202
dcc60243
SR
203 # collect the depends for the config
204 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
205 $state = "DEP";
206 $depends{$config} = $1;
207 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
208 $depends{$config} .= " " . $1;
209
210 # Get the configs that select this config
211 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
0b58a99e
SR
212 my $conf = $1;
213 if (defined($selects{$conf})) {
214 $selects{$conf} .= " " . $config;
dcc60243 215 } else {
0b58a99e 216 $selects{$conf} = $config;
dcc60243
SR
217 }
218
219 # configs without prompts must be selected
220 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
221 # note if the config has a prompt
ccece60a 222 $prompts{$config} = 1;
dcc60243 223
13d7e938
SR
224 # Check for if statements
225 } elsif (/^if\s+(.*\S)\s*$/) {
226 my $deps = $1;
227 # remove beginning and ending non text
228 $deps =~ s/^[^a-zA-Z0-9_]*//;
229 $deps =~ s/[^a-zA-Z0-9_]*$//;
230
231 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
232
233 $ifdeps[$iflevel++] = join ':', @deps;
234
235 } elsif (/^endif/) {
236
237 $iflevel-- if ($iflevel);
238
dcc60243
SR
239 # stop on "help"
240 } elsif (/^\s*help\s*$/) {
241 $state = "NONE";
242 }
243 }
244 close(KIN);
245
246 # read in any configs that were found.
247 foreach $kconfig (@kconfigs) {
248 if (!defined($read_kconfigs{$kconfig})) {
249 $read_kconfigs{$kconfig} = 1;
250 read_kconfig($kconfig);
251 }
252 }
253}
254
255if ($kconfig) {
256 read_kconfig($kconfig);
257}
258
0b58a99e 259# Makefiles can use variables to define their dependencies
364212fd
SR
260sub convert_vars {
261 my ($line, %vars) = @_;
262
263 my $process = "";
264
265 while ($line =~ s/^(.*?)(\$\((.*?)\))//) {
266 my $start = $1;
267 my $variable = $2;
268 my $var = $3;
269
270 if (defined($vars{$var})) {
271 $process .= $start . $vars{$var};
272 } else {
273 $process .= $start . $variable;
274 }
275 }
276
277 $process .= $line;
278
279 return $process;
280}
281
dcc60243
SR
282# Read all Makefiles to map the configs to the objects
283foreach my $makefile (@makefiles) {
dcc60243 284
d060d963 285 my $line = "";
364212fd 286 my %make_vars;
20d19047 287
dcc60243
SR
288 open(MIN,$makefile) || die "Can't open $makefile";
289 while (<MIN>) {
d060d963
SR
290 # if this line ends with a backslash, continue
291 chomp;
292 if (/^(.*)\\$/) {
293 $line .= $1;
294 next;
dcc60243 295 }
d060d963
SR
296
297 $line .= $_;
298 $_ = $line;
299 $line = "";
300
301 my $objs;
dcc60243 302
0b58a99e 303 # Convert variables in a line (could define configs)
364212fd
SR
304 $_ = convert_vars($_, %make_vars);
305
dcc60243
SR
306 # collect objects after obj-$(CONFIG_FOO_BAR)
307 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
308 $var = $1;
309 $objs = $2;
364212fd
SR
310
311 # check if variables are set
312 } elsif (/^\s*(\S+)\s*[:]?=\s*(.*\S)/) {
313 $make_vars{$1} = $2;
dcc60243
SR
314 }
315 if (defined($objs)) {
dcc60243
SR
316 foreach my $obj (split /\s+/,$objs) {
317 $obj =~ s/-/_/g;
318 if ($obj =~ /(.*)\.o$/) {
1d1d1fea 319 # Objects may be enabled by more than one config.
dcc60243
SR
320 # Store configs in an array.
321 my @arr;
322
323 if (defined($objects{$1})) {
324 @arr = @{$objects{$1}};
325 }
326
327 $arr[$#arr+1] = $var;
328
329 # The objects have a hash mapping to a reference
330 # of an array of configs.
331 $objects{$1} = \@arr;
332 }
333 }
334 }
335 }
336 close(MIN);
337}
338
339my %modules;
340
615f0833
SR
341if (defined($lsmod_file)) {
342 if ( ! -f $lsmod_file) {
f597a718
AL
343 if ( -f $ENV{'objtree'}."/".$lsmod_file) {
344 $lsmod_file = $ENV{'objtree'}."/".$lsmod_file;
345 } else {
346 die "$lsmod_file not found";
347 }
615f0833
SR
348 }
349 if ( -x $lsmod_file) {
350 # the file is executable, run it
351 open(LIN, "$lsmod_file|");
352 } else {
353 # Just read the contents
354 open(LIN, "$lsmod_file");
88f66ea9 355 }
615f0833
SR
356} else {
357
358 # see what modules are loaded on this system
359 my $lsmod;
360
cf5a189d 361 foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
615f0833
SR
362 if ( -x "$dir/lsmod" ) {
363 $lsmod = "$dir/lsmod";
364 last;
365 }
88f66ea9 366}
615f0833
SR
367 if (!defined($lsmod)) {
368 # try just the path
369 $lsmod = "lsmod";
370 }
371
372 open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod";
88f66ea9
SR
373}
374
dcc60243
SR
375while (<LIN>) {
376 next if (/^Module/); # Skip the first line.
377 if (/^(\S+)/) {
378 $modules{$1} = 1;
379 }
380}
381close (LIN);
382
383# add to the configs hash all configs that are needed to enable
0b58a99e
SR
384# a loaded module. This is a direct obj-${CONFIG_FOO} += bar.o
385# where we know we need bar.o so we add FOO to the list.
dcc60243
SR
386my %configs;
387foreach my $module (keys(%modules)) {
388 if (defined($objects{$module})) {
e5199edb 389 my @arr = @{$objects{$module}};
dcc60243
SR
390 foreach my $conf (@arr) {
391 $configs{$conf} = $module;
392 }
393 } else {
394 # Most likely, someone has a custom (binary?) module loaded.
395 print STDERR "$module config not found!!\n";
396 }
397}
398
4f4c51c9
SR
399# Read the current config, and see what is enabled. We want to
400# ignore configs that we would not enable anyway.
401
402my %orig_configs;
dcc60243 403my $valid = "A-Za-z_0-9";
4f4c51c9
SR
404
405foreach my $line (@config_file) {
406 $_ = $line;
407
408 if (/(CONFIG_[$valid]*)=(m|y)/) {
409 $orig_configs{$1} = $2;
410 }
411}
412
dcc60243
SR
413my $repeat = 1;
414
415#
416# Note, we do not care about operands (like: &&, ||, !) we want to add any
417# config that is in the depend list of another config. This script does
418# not enable configs that are not already enabled. If we come across a
419# config A that depends on !B, we can still add B to the list of depends
420# to keep on. If A was on in the original config, B would not have been
421# and B would not be turned on by this script.
422#
d4bb58b5 423sub parse_config_depends
dcc60243
SR
424{
425 my ($p) = @_;
426
427 while ($p =~ /[$valid]/) {
428
429 if ($p =~ /^[^$valid]*([$valid]+)/) {
430 my $conf = "CONFIG_" . $1;
431
432 $p =~ s/^[^$valid]*[$valid]+//;
433
4f4c51c9
SR
434 # We only need to process if the depend config is a module
435 if (!defined($orig_configs{$conf}) || !$orig_configs{conf} eq "m") {
436 next;
437 }
438
dcc60243
SR
439 if (!defined($configs{$conf})) {
440 # We must make sure that this config has its
441 # dependencies met.
442 $repeat = 1; # do again
443 $configs{$conf} = 1;
444 }
445 } else {
446 die "this should never happen";
447 }
448 }
449}
450
d4bb58b5
SR
451# Select is treated a bit differently than depends. We call this
452# when a config has no prompt and requires another config to be
453# selected. We use to just select all configs that selected this
454# config, but found that that can balloon into enabling hundreds
455# of configs that we do not care about.
456#
457# The idea is we look at all the configs that select it. If one
458# is already in our list of configs to enable, then there's nothing
459# else to do. If there isn't, we pick the first config that was
460# enabled in the orignal config and use that.
461sub parse_config_selects
462{
463 my ($config, $p) = @_;
dcc60243 464
d4bb58b5
SR
465 my $next_config;
466
467 while ($p =~ /[$valid]/) {
468
469 if ($p =~ /^[^$valid]*([$valid]+)/) {
470 my $conf = "CONFIG_" . $1;
471
472 $p =~ s/^[^$valid]*[$valid]+//;
dcc60243 473
d4bb58b5
SR
474 # Make sure that this config exists in the current .config file
475 if (!defined($orig_configs{$conf})) {
476 next;
477 }
478
479 # Check if something other than a module selects this config
480 if (defined($orig_configs{$conf}) && $orig_configs{$conf} ne "m") {
481 # we are good with this
482 return;
483 }
484 if (defined($configs{$conf})) {
485 # A set config selects this config, we are good
486 return;
487 }
488 # Set this config to be selected
489 if (!defined($next_config)) {
490 $next_config = $conf;
491 }
492 } else {
493 die "this should never happen";
dcc60243 494 }
d4bb58b5 495 }
dcc60243 496
d4bb58b5
SR
497 # If no possible config selected this, then something happened.
498 if (!defined($next_config)) {
499 print STDERR "WARNING: $config is required, but nothing in the\n";
500 print STDERR " current config selects it.\n";
501 return;
502 }
503
504 # If we are here, then we found no config that is set and
505 # selects this config. Repeat.
506 $repeat = 1;
507 # Make this config need to be selected
508 $configs{$next_config} = 1;
509}
510
511my %process_selects;
512
513# loop through all configs, select their dependencies.
514sub loop_depend {
515 $repeat = 1;
516
517 while ($repeat) {
518 $repeat = 0;
519
520 forloop:
521 foreach my $config (keys %configs) {
522
523 # If this config is not a module, we do not need to process it
524 if (defined($orig_configs{$config}) && $orig_configs{$config} ne "m") {
525 next forloop;
526 }
527
528 $config =~ s/^CONFIG_//;
529
530 if (defined($depends{$config})) {
531 # This config has dependencies. Make sure they are also included
532 parse_config_depends $depends{$config};
533 }
534
535 # If the config has no prompt, then we need to check if a config
536 # that is enabled selected it. Or if we need to enable one.
537 if (!defined($prompts{$config}) && defined($selects{$config})) {
538 $process_selects{$config} = 1;
539 }
dcc60243 540 }
d4bb58b5
SR
541 }
542}
543
544sub loop_select {
545
546 foreach my $config (keys %process_selects) {
547 $config =~ s/^CONFIG_//;
dcc60243
SR
548
549 # config has no prompt and must be selected.
d4bb58b5 550 parse_config_selects $config, $selects{$config};
dcc60243
SR
551 }
552}
553
d4bb58b5
SR
554while ($repeat) {
555 # Get the first set of configs and their dependencies.
556 loop_depend;
557
558 $repeat = 0;
559
560 # Now we need to see if we have to check selects;
561 loop_select;
562}
563
dcc60243
SR
564my %setconfigs;
565
566# Finally, read the .config file and turn off any module enabled that
567# we could not find a reason to keep enabled.
4f4c51c9
SR
568foreach my $line (@config_file) {
569 $_ = $line;
744ffcbe
SR
570
571 if (/CONFIG_IKCONFIG/) {
572 if (/# CONFIG_IKCONFIG is not set/) {
573 # enable IKCONFIG at least as a module
574 print "CONFIG_IKCONFIG=m\n";
575 # don't ask about PROC
d08ca277 576 print "# CONFIG_IKCONFIG_PROC is not set\n";
744ffcbe
SR
577 } else {
578 print;
579 }
580 next;
581 }
582
583 if (/^(CONFIG.*)=(m|y)/) {
584 if (defined($configs{$1})) {
22d550ae
AL
585 if ($localyesconfig) {
586 $setconfigs{$1} = 'y';
587 } else {
588 $setconfigs{$1} = $2;
589 }
744ffcbe
SR
590 } elsif ($2 eq "m") {
591 print "# $1 is not set\n";
d08ca277 592 next;
dcc60243 593 }
744ffcbe 594 }
d08ca277 595 print;
dcc60243 596}
dcc60243
SR
597
598# Integrity check, make sure all modules that we want enabled do
599# indeed have their configs set.
600loop:
601foreach my $module (keys(%modules)) {
602 if (defined($objects{$module})) {
603 my @arr = @{$objects{$module}};
604 foreach my $conf (@arr) {
605 if (defined($setconfigs{$conf})) {
606 next loop;
607 }
608 }
609 print STDERR "module $module did not have configs";
610 foreach my $conf (@arr) {
611 print STDERR " " , $conf;
612 }
613 print STDERR "\n";
614 }
615}