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 | ||
22d550ae AL |
116 | # Parse options |
117 | my $localmodconfig = 0; | |
118 | my $localyesconfig = 0; | |
119 | ||
120 | GetOptions("localmodconfig" => \$localmodconfig, | |
121 | "localyesconfig" => \$localyesconfig); | |
122 | ||
463bf900 SR |
123 | # Get the build source and top level Kconfig file (passed in) |
124 | my $ksource = $ARGV[0]; | |
125 | my $kconfig = $ARGV[1]; | |
f597a718 | 126 | my $lsmod_file = $ENV{'LSMOD'}; |
463bf900 | 127 | |
17431928 TF |
128 | my @makefiles = `find $ksource -name Makefile 2>/dev/null`; |
129 | chomp @makefiles; | |
130 | ||
dcc60243 SR |
131 | my %depends; |
132 | my %selects; | |
133 | my %prompts; | |
134 | my %objects; | |
135 | my $var; | |
13d7e938 SR |
136 | my $iflevel = 0; |
137 | my @ifdeps; | |
dcc60243 | 138 | |
dcc60243 SR |
139 | # prevent recursion |
140 | my %read_kconfigs; | |
141 | ||
142 | sub read_kconfig { | |
143 | my ($kconfig) = @_; | |
144 | ||
145 | my $state = "NONE"; | |
146 | my $config; | |
147 | my @kconfigs; | |
148 | ||
20d19047 SR |
149 | my $cont = 0; |
150 | my $line; | |
151 | ||
4908980b SR |
152 | my $source = "$ksource/$kconfig"; |
153 | my $last_source = ""; | |
154 | ||
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}/; | |
160 | } | |
161 | ||
162 | open(KIN, "$source") || die "Can't open $kconfig"; | |
dcc60243 SR |
163 | while (<KIN>) { |
164 | chomp; | |
165 | ||
20d19047 SR |
166 | # Make sure that lines ending with \ continue |
167 | if ($cont) { | |
168 | $_ = $line . " " . $_; | |
169 | } | |
170 | ||
171 | if (s/\\$//) { | |
172 | $cont = 1; | |
173 | $line = $_; | |
174 | next; | |
175 | } | |
176 | ||
177 | $cont = 0; | |
178 | ||
dcc60243 SR |
179 | # collect any Kconfig sources |
180 | if (/^source\s*"(.*)"/) { | |
181 | $kconfigs[$#kconfigs+1] = $1; | |
182 | } | |
183 | ||
184 | # configs found | |
8ef17fa2 | 185 | if (/^\s*(menu)?config\s+(\S+)\s*$/) { |
dcc60243 | 186 | $state = "NEW"; |
8ef17fa2 | 187 | $config = $2; |
dcc60243 | 188 | |
0b58a99e | 189 | # Add depends for 'if' nesting |
13d7e938 SR |
190 | for (my $i = 0; $i < $iflevel; $i++) { |
191 | if ($i) { | |
192 | $depends{$config} .= " " . $ifdeps[$i]; | |
193 | } else { | |
194 | $depends{$config} = $ifdeps[$i]; | |
195 | } | |
196 | $state = "DEP"; | |
197 | } | |
198 | ||
dcc60243 SR |
199 | # collect the depends for the config |
200 | } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) { | |
201 | $state = "DEP"; | |
202 | $depends{$config} = $1; | |
203 | } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) { | |
204 | $depends{$config} .= " " . $1; | |
205 | ||
206 | # Get the configs that select this config | |
207 | } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) { | |
0b58a99e SR |
208 | my $conf = $1; |
209 | if (defined($selects{$conf})) { | |
210 | $selects{$conf} .= " " . $config; | |
dcc60243 | 211 | } else { |
0b58a99e | 212 | $selects{$conf} = $config; |
dcc60243 SR |
213 | } |
214 | ||
215 | # configs without prompts must be selected | |
216 | } elsif ($state ne "NONE" && /^\s*tristate\s\S/) { | |
217 | # note if the config has a prompt | |
ccece60a | 218 | $prompts{$config} = 1; |
dcc60243 | 219 | |
13d7e938 SR |
220 | # Check for if statements |
221 | } elsif (/^if\s+(.*\S)\s*$/) { | |
222 | my $deps = $1; | |
223 | # remove beginning and ending non text | |
224 | $deps =~ s/^[^a-zA-Z0-9_]*//; | |
225 | $deps =~ s/[^a-zA-Z0-9_]*$//; | |
226 | ||
227 | my @deps = split /[^a-zA-Z0-9_]+/, $deps; | |
228 | ||
229 | $ifdeps[$iflevel++] = join ':', @deps; | |
230 | ||
231 | } elsif (/^endif/) { | |
232 | ||
233 | $iflevel-- if ($iflevel); | |
234 | ||
dcc60243 SR |
235 | # stop on "help" |
236 | } elsif (/^\s*help\s*$/) { | |
237 | $state = "NONE"; | |
238 | } | |
239 | } | |
240 | close(KIN); | |
241 | ||
242 | # read in any configs that were found. | |
243 | foreach $kconfig (@kconfigs) { | |
244 | if (!defined($read_kconfigs{$kconfig})) { | |
245 | $read_kconfigs{$kconfig} = 1; | |
246 | read_kconfig($kconfig); | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | if ($kconfig) { | |
252 | read_kconfig($kconfig); | |
253 | } | |
254 | ||
0b58a99e | 255 | # Makefiles can use variables to define their dependencies |
364212fd SR |
256 | sub convert_vars { |
257 | my ($line, %vars) = @_; | |
258 | ||
259 | my $process = ""; | |
260 | ||
261 | while ($line =~ s/^(.*?)(\$\((.*?)\))//) { | |
262 | my $start = $1; | |
263 | my $variable = $2; | |
264 | my $var = $3; | |
265 | ||
266 | if (defined($vars{$var})) { | |
267 | $process .= $start . $vars{$var}; | |
268 | } else { | |
269 | $process .= $start . $variable; | |
270 | } | |
271 | } | |
272 | ||
273 | $process .= $line; | |
274 | ||
275 | return $process; | |
276 | } | |
277 | ||
dcc60243 SR |
278 | # Read all Makefiles to map the configs to the objects |
279 | foreach my $makefile (@makefiles) { | |
dcc60243 | 280 | |
d060d963 | 281 | my $line = ""; |
364212fd | 282 | my %make_vars; |
20d19047 | 283 | |
dcc60243 SR |
284 | open(MIN,$makefile) || die "Can't open $makefile"; |
285 | while (<MIN>) { | |
d060d963 SR |
286 | # if this line ends with a backslash, continue |
287 | chomp; | |
288 | if (/^(.*)\\$/) { | |
289 | $line .= $1; | |
290 | next; | |
dcc60243 | 291 | } |
d060d963 SR |
292 | |
293 | $line .= $_; | |
294 | $_ = $line; | |
295 | $line = ""; | |
296 | ||
297 | my $objs; | |
dcc60243 | 298 | |
0b58a99e | 299 | # Convert variables in a line (could define configs) |
364212fd SR |
300 | $_ = convert_vars($_, %make_vars); |
301 | ||
dcc60243 SR |
302 | # collect objects after obj-$(CONFIG_FOO_BAR) |
303 | if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) { | |
304 | $var = $1; | |
305 | $objs = $2; | |
364212fd SR |
306 | |
307 | # check if variables are set | |
308 | } elsif (/^\s*(\S+)\s*[:]?=\s*(.*\S)/) { | |
309 | $make_vars{$1} = $2; | |
dcc60243 SR |
310 | } |
311 | if (defined($objs)) { | |
dcc60243 SR |
312 | foreach my $obj (split /\s+/,$objs) { |
313 | $obj =~ s/-/_/g; | |
314 | if ($obj =~ /(.*)\.o$/) { | |
1d1d1fea | 315 | # Objects may be enabled by more than one config. |
dcc60243 SR |
316 | # Store configs in an array. |
317 | my @arr; | |
318 | ||
319 | if (defined($objects{$1})) { | |
320 | @arr = @{$objects{$1}}; | |
321 | } | |
322 | ||
323 | $arr[$#arr+1] = $var; | |
324 | ||
325 | # The objects have a hash mapping to a reference | |
326 | # of an array of configs. | |
327 | $objects{$1} = \@arr; | |
328 | } | |
329 | } | |
330 | } | |
331 | } | |
332 | close(MIN); | |
333 | } | |
334 | ||
335 | my %modules; | |
336 | ||
615f0833 SR |
337 | if (defined($lsmod_file)) { |
338 | if ( ! -f $lsmod_file) { | |
f597a718 AL |
339 | if ( -f $ENV{'objtree'}."/".$lsmod_file) { |
340 | $lsmod_file = $ENV{'objtree'}."/".$lsmod_file; | |
341 | } else { | |
342 | die "$lsmod_file not found"; | |
343 | } | |
615f0833 SR |
344 | } |
345 | if ( -x $lsmod_file) { | |
346 | # the file is executable, run it | |
347 | open(LIN, "$lsmod_file|"); | |
348 | } else { | |
349 | # Just read the contents | |
350 | open(LIN, "$lsmod_file"); | |
88f66ea9 | 351 | } |
615f0833 SR |
352 | } else { |
353 | ||
354 | # see what modules are loaded on this system | |
355 | my $lsmod; | |
356 | ||
cf5a189d | 357 | foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) { |
615f0833 SR |
358 | if ( -x "$dir/lsmod" ) { |
359 | $lsmod = "$dir/lsmod"; | |
360 | last; | |
361 | } | |
88f66ea9 | 362 | } |
615f0833 SR |
363 | if (!defined($lsmod)) { |
364 | # try just the path | |
365 | $lsmod = "lsmod"; | |
366 | } | |
367 | ||
368 | open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod"; | |
88f66ea9 SR |
369 | } |
370 | ||
dcc60243 SR |
371 | while (<LIN>) { |
372 | next if (/^Module/); # Skip the first line. | |
373 | if (/^(\S+)/) { | |
374 | $modules{$1} = 1; | |
375 | } | |
376 | } | |
377 | close (LIN); | |
378 | ||
379 | # add to the configs hash all configs that are needed to enable | |
0b58a99e SR |
380 | # a loaded module. This is a direct obj-${CONFIG_FOO} += bar.o |
381 | # where we know we need bar.o so we add FOO to the list. | |
dcc60243 SR |
382 | my %configs; |
383 | foreach my $module (keys(%modules)) { | |
384 | if (defined($objects{$module})) { | |
e5199edb | 385 | my @arr = @{$objects{$module}}; |
dcc60243 SR |
386 | foreach my $conf (@arr) { |
387 | $configs{$conf} = $module; | |
388 | } | |
389 | } else { | |
390 | # Most likely, someone has a custom (binary?) module loaded. | |
391 | print STDERR "$module config not found!!\n"; | |
392 | } | |
393 | } | |
394 | ||
395 | my $valid = "A-Za-z_0-9"; | |
396 | my $repeat = 1; | |
397 | ||
398 | # | |
399 | # Note, we do not care about operands (like: &&, ||, !) we want to add any | |
400 | # config that is in the depend list of another config. This script does | |
401 | # not enable configs that are not already enabled. If we come across a | |
402 | # config A that depends on !B, we can still add B to the list of depends | |
403 | # to keep on. If A was on in the original config, B would not have been | |
404 | # and B would not be turned on by this script. | |
405 | # | |
406 | sub parse_config_dep_select | |
407 | { | |
408 | my ($p) = @_; | |
409 | ||
410 | while ($p =~ /[$valid]/) { | |
411 | ||
412 | if ($p =~ /^[^$valid]*([$valid]+)/) { | |
413 | my $conf = "CONFIG_" . $1; | |
414 | ||
415 | $p =~ s/^[^$valid]*[$valid]+//; | |
416 | ||
417 | if (!defined($configs{$conf})) { | |
418 | # We must make sure that this config has its | |
419 | # dependencies met. | |
420 | $repeat = 1; # do again | |
421 | $configs{$conf} = 1; | |
422 | } | |
423 | } else { | |
424 | die "this should never happen"; | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
429 | while ($repeat) { | |
430 | $repeat = 0; | |
431 | ||
432 | foreach my $config (keys %configs) { | |
433 | $config =~ s/^CONFIG_//; | |
434 | ||
74398d32 SR |
435 | if (defined($depends{$config})) { |
436 | # This config has dependencies. Make sure they are also included | |
437 | parse_config_dep_select $depends{$config}; | |
dcc60243 SR |
438 | } |
439 | ||
ccece60a | 440 | if (defined($prompts{$config}) || !defined($selects{$config})) { |
dcc60243 SR |
441 | next; |
442 | } | |
443 | ||
444 | # config has no prompt and must be selected. | |
445 | parse_config_dep_select $selects{$config}; | |
446 | } | |
447 | } | |
448 | ||
449 | my %setconfigs; | |
450 | ||
451 | # Finally, read the .config file and turn off any module enabled that | |
452 | # we could not find a reason to keep enabled. | |
453 | while(<CIN>) { | |
744ffcbe SR |
454 | |
455 | if (/CONFIG_IKCONFIG/) { | |
456 | if (/# CONFIG_IKCONFIG is not set/) { | |
457 | # enable IKCONFIG at least as a module | |
458 | print "CONFIG_IKCONFIG=m\n"; | |
459 | # don't ask about PROC | |
d08ca277 | 460 | print "# CONFIG_IKCONFIG_PROC is not set\n"; |
744ffcbe SR |
461 | } else { |
462 | print; | |
463 | } | |
464 | next; | |
465 | } | |
466 | ||
467 | if (/^(CONFIG.*)=(m|y)/) { | |
468 | if (defined($configs{$1})) { | |
22d550ae AL |
469 | if ($localyesconfig) { |
470 | $setconfigs{$1} = 'y'; | |
471 | } else { | |
472 | $setconfigs{$1} = $2; | |
473 | } | |
744ffcbe SR |
474 | } elsif ($2 eq "m") { |
475 | print "# $1 is not set\n"; | |
d08ca277 | 476 | next; |
dcc60243 | 477 | } |
744ffcbe | 478 | } |
d08ca277 | 479 | print; |
dcc60243 SR |
480 | } |
481 | close(CIN); | |
482 | ||
483 | # Integrity check, make sure all modules that we want enabled do | |
484 | # indeed have their configs set. | |
485 | loop: | |
486 | foreach my $module (keys(%modules)) { | |
487 | if (defined($objects{$module})) { | |
488 | my @arr = @{$objects{$module}}; | |
489 | foreach my $conf (@arr) { | |
490 | if (defined($setconfigs{$conf})) { | |
491 | next loop; | |
492 | } | |
493 | } | |
494 | print STDERR "module $module did not have configs"; | |
495 | foreach my $conf (@arr) { | |
496 | print STDERR " " , $conf; | |
497 | } | |
498 | print STDERR "\n"; | |
499 | } | |
500 | } |