Commit | Line | Data |
---|---|---|
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 | 45 | use strict; |
22d550ae | 46 | use Getopt::Long; |
cf5a189d | 47 | |
dcc60243 | 48 | my $config = ".config"; |
dcc60243 | 49 | |
cdfc4795 SR |
50 | my $uname = `uname -r`; |
51 | chomp $uname; | |
52 | ||
53 | my @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 | ||
93 | sub 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 | ||
114 | find_config; | |
115 | ||
4f4c51c9 SR |
116 | # Read in the entire config file into config_file |
117 | my @config_file = <CIN>; | |
118 | close CIN; | |
119 | ||
22d550ae AL |
120 | # Parse options |
121 | my $localmodconfig = 0; | |
122 | my $localyesconfig = 0; | |
123 | ||
124 | GetOptions("localmodconfig" => \$localmodconfig, | |
125 | "localyesconfig" => \$localyesconfig); | |
126 | ||
463bf900 SR |
127 | # Get the build source and top level Kconfig file (passed in) |
128 | my $ksource = $ARGV[0]; | |
129 | my $kconfig = $ARGV[1]; | |
f597a718 | 130 | my $lsmod_file = $ENV{'LSMOD'}; |
463bf900 | 131 | |
17431928 TF |
132 | my @makefiles = `find $ksource -name Makefile 2>/dev/null`; |
133 | chomp @makefiles; | |
134 | ||
dcc60243 SR |
135 | my %depends; |
136 | my %selects; | |
137 | my %prompts; | |
138 | my %objects; | |
139 | my $var; | |
13d7e938 SR |
140 | my $iflevel = 0; |
141 | my @ifdeps; | |
dcc60243 | 142 | |
dcc60243 SR |
143 | # prevent recursion |
144 | my %read_kconfigs; | |
145 | ||
146 | sub 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 | ||
255 | if ($kconfig) { | |
256 | read_kconfig($kconfig); | |
257 | } | |
258 | ||
0b58a99e | 259 | # Makefiles can use variables to define their dependencies |
364212fd SR |
260 | sub 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 |
283 | foreach 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 | ||
339 | my %modules; | |
340 | ||
615f0833 SR |
341 | if (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 |
375 | while (<LIN>) { |
376 | next if (/^Module/); # Skip the first line. | |
377 | if (/^(\S+)/) { | |
378 | $modules{$1} = 1; | |
379 | } | |
380 | } | |
381 | close (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 |
386 | my %configs; |
387 | foreach 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 | ||
402 | my %orig_configs; | |
dcc60243 | 403 | my $valid = "A-Za-z_0-9"; |
4f4c51c9 SR |
404 | |
405 | foreach my $line (@config_file) { | |
406 | $_ = $line; | |
407 | ||
408 | if (/(CONFIG_[$valid]*)=(m|y)/) { | |
409 | $orig_configs{$1} = $2; | |
410 | } | |
411 | } | |
412 | ||
dcc60243 SR |
413 | my $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 | # | |
423 | sub parse_config_dep_select | |
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 | ||
451 | while ($repeat) { | |
452 | $repeat = 0; | |
453 | ||
454 | foreach my $config (keys %configs) { | |
455 | $config =~ s/^CONFIG_//; | |
456 | ||
74398d32 SR |
457 | if (defined($depends{$config})) { |
458 | # This config has dependencies. Make sure they are also included | |
459 | parse_config_dep_select $depends{$config}; | |
dcc60243 SR |
460 | } |
461 | ||
ccece60a | 462 | if (defined($prompts{$config}) || !defined($selects{$config})) { |
dcc60243 SR |
463 | next; |
464 | } | |
465 | ||
466 | # config has no prompt and must be selected. | |
467 | parse_config_dep_select $selects{$config}; | |
468 | } | |
469 | } | |
470 | ||
471 | my %setconfigs; | |
472 | ||
473 | # Finally, read the .config file and turn off any module enabled that | |
474 | # we could not find a reason to keep enabled. | |
4f4c51c9 SR |
475 | foreach my $line (@config_file) { |
476 | $_ = $line; | |
744ffcbe SR |
477 | |
478 | if (/CONFIG_IKCONFIG/) { | |
479 | if (/# CONFIG_IKCONFIG is not set/) { | |
480 | # enable IKCONFIG at least as a module | |
481 | print "CONFIG_IKCONFIG=m\n"; | |
482 | # don't ask about PROC | |
d08ca277 | 483 | print "# CONFIG_IKCONFIG_PROC is not set\n"; |
744ffcbe SR |
484 | } else { |
485 | print; | |
486 | } | |
487 | next; | |
488 | } | |
489 | ||
490 | if (/^(CONFIG.*)=(m|y)/) { | |
491 | if (defined($configs{$1})) { | |
22d550ae AL |
492 | if ($localyesconfig) { |
493 | $setconfigs{$1} = 'y'; | |
494 | } else { | |
495 | $setconfigs{$1} = $2; | |
496 | } | |
744ffcbe SR |
497 | } elsif ($2 eq "m") { |
498 | print "# $1 is not set\n"; | |
d08ca277 | 499 | next; |
dcc60243 | 500 | } |
744ffcbe | 501 | } |
d08ca277 | 502 | print; |
dcc60243 | 503 | } |
dcc60243 SR |
504 | |
505 | # Integrity check, make sure all modules that we want enabled do | |
506 | # indeed have their configs set. | |
507 | loop: | |
508 | foreach my $module (keys(%modules)) { | |
509 | if (defined($objects{$module})) { | |
510 | my @arr = @{$objects{$module}}; | |
511 | foreach my $conf (@arr) { | |
512 | if (defined($setconfigs{$conf})) { | |
513 | next loop; | |
514 | } | |
515 | } | |
516 | print STDERR "module $module did not have configs"; | |
517 | foreach my $conf (@arr) { | |
518 | print STDERR " " , $conf; | |
519 | } | |
520 | print STDERR "\n"; | |
521 | } | |
522 | } |