ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / scripts / kconfig / streamline_config.pl
blob25f1e71c9bb507d490cf5152b51de21c1cc86c47
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;
47 my $config = ".config";
49 my $uname = `uname -r`;
50 chomp $uname;
52 my @searchconfigs = (
54 "file" => ".config",
55 "exec" => "cat",
58 "file" => "/proc/config.gz",
59 "exec" => "zcat",
62 "file" => "/boot/config-$uname",
63 "exec" => "cat",
66 "file" => "/boot/vmlinuz-$uname",
67 "exec" => "scripts/extract-ikconfig",
68 "test" => "scripts/extract-ikconfig",
71 "file" => "vmlinux",
72 "exec" => "scripts/extract-ikconfig",
73 "test" => "scripts/extract-ikconfig",
76 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
77 "exec" => "scripts/extract-ikconfig",
78 "test" => "scripts/extract-ikconfig",
81 "file" => "kernel/configs.ko",
82 "exec" => "scripts/extract-ikconfig",
83 "test" => "scripts/extract-ikconfig",
86 "file" => "kernel/configs.o",
87 "exec" => "scripts/extract-ikconfig",
88 "test" => "scripts/extract-ikconfig",
92 sub find_config {
93 foreach my $conf (@searchconfigs) {
94 my $file = $conf->{"file"};
96 next if ( ! -f "$file");
98 if (defined($conf->{"test"})) {
99 `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
100 next if ($?);
103 my $exec = $conf->{"exec"};
105 print STDERR "using config: '$file'\n";
107 open(CIN, "$exec $file |") || die "Failed to run $exec $file";
108 return;
110 die "No config file found";
113 find_config;
115 # Get the build source and top level Kconfig file (passed in)
116 my $ksource = $ARGV[0];
117 my $kconfig = $ARGV[1];
118 my $lsmod_file = $ARGV[2];
120 my @makefiles = `find $ksource -name Makefile 2>/dev/null`;
121 chomp @makefiles;
123 my %depends;
124 my %selects;
125 my %prompts;
126 my %objects;
127 my $var;
128 my $iflevel = 0;
129 my @ifdeps;
131 # prevent recursion
132 my %read_kconfigs;
134 sub read_kconfig {
135 my ($kconfig) = @_;
137 my $state = "NONE";
138 my $config;
139 my @kconfigs;
141 my $cont = 0;
142 my $line;
144 my $source = "$ksource/$kconfig";
145 my $last_source = "";
147 # Check for any environment variables used
148 while ($source =~ /\$(\w+)/ && $last_source ne $source) {
149 my $env = $1;
150 $last_source = $source;
151 $source =~ s/\$$env/$ENV{$env}/;
154 open(KIN, "$source") || die "Can't open $kconfig";
155 while (<KIN>) {
156 chomp;
158 # Make sure that lines ending with \ continue
159 if ($cont) {
160 $_ = $line . " " . $_;
163 if (s/\\$//) {
164 $cont = 1;
165 $line = $_;
166 next;
169 $cont = 0;
171 # collect any Kconfig sources
172 if (/^source\s*"(.*)"/) {
173 $kconfigs[$#kconfigs+1] = $1;
176 # configs found
177 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
178 $state = "NEW";
179 $config = $2;
181 for (my $i = 0; $i < $iflevel; $i++) {
182 if ($i) {
183 $depends{$config} .= " " . $ifdeps[$i];
184 } else {
185 $depends{$config} = $ifdeps[$i];
187 $state = "DEP";
190 # collect the depends for the config
191 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
192 $state = "DEP";
193 $depends{$config} = $1;
194 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
195 $depends{$config} .= " " . $1;
197 # Get the configs that select this config
198 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
199 if (defined($selects{$1})) {
200 $selects{$1} .= " " . $config;
201 } else {
202 $selects{$1} = $config;
205 # configs without prompts must be selected
206 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
207 # note if the config has a prompt
208 $prompts{$config} = 1;
210 # Check for if statements
211 } elsif (/^if\s+(.*\S)\s*$/) {
212 my $deps = $1;
213 # remove beginning and ending non text
214 $deps =~ s/^[^a-zA-Z0-9_]*//;
215 $deps =~ s/[^a-zA-Z0-9_]*$//;
217 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
219 $ifdeps[$iflevel++] = join ':', @deps;
221 } elsif (/^endif/) {
223 $iflevel-- if ($iflevel);
225 # stop on "help"
226 } elsif (/^\s*help\s*$/) {
227 $state = "NONE";
230 close(KIN);
232 # read in any configs that were found.
233 foreach $kconfig (@kconfigs) {
234 if (!defined($read_kconfigs{$kconfig})) {
235 $read_kconfigs{$kconfig} = 1;
236 read_kconfig($kconfig);
241 if ($kconfig) {
242 read_kconfig($kconfig);
245 sub convert_vars {
246 my ($line, %vars) = @_;
248 my $process = "";
250 while ($line =~ s/^(.*?)(\$\((.*?)\))//) {
251 my $start = $1;
252 my $variable = $2;
253 my $var = $3;
255 if (defined($vars{$var})) {
256 $process .= $start . $vars{$var};
257 } else {
258 $process .= $start . $variable;
262 $process .= $line;
264 return $process;
267 # Read all Makefiles to map the configs to the objects
268 foreach my $makefile (@makefiles) {
270 my $line = "";
271 my %make_vars;
273 open(MIN,$makefile) || die "Can't open $makefile";
274 while (<MIN>) {
275 # if this line ends with a backslash, continue
276 chomp;
277 if (/^(.*)\\$/) {
278 $line .= $1;
279 next;
282 $line .= $_;
283 $_ = $line;
284 $line = "";
286 my $objs;
288 $_ = convert_vars($_, %make_vars);
290 # collect objects after obj-$(CONFIG_FOO_BAR)
291 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
292 $var = $1;
293 $objs = $2;
295 # check if variables are set
296 } elsif (/^\s*(\S+)\s*[:]?=\s*(.*\S)/) {
297 $make_vars{$1} = $2;
299 if (defined($objs)) {
300 foreach my $obj (split /\s+/,$objs) {
301 $obj =~ s/-/_/g;
302 if ($obj =~ /(.*)\.o$/) {
303 # Objects may be enabled by more than one config.
304 # Store configs in an array.
305 my @arr;
307 if (defined($objects{$1})) {
308 @arr = @{$objects{$1}};
311 $arr[$#arr+1] = $var;
313 # The objects have a hash mapping to a reference
314 # of an array of configs.
315 $objects{$1} = \@arr;
320 close(MIN);
323 my %modules;
325 if (defined($lsmod_file)) {
326 if ( ! -f $lsmod_file) {
327 die "$lsmod_file not found";
329 if ( -x $lsmod_file) {
330 # the file is executable, run it
331 open(LIN, "$lsmod_file|");
332 } else {
333 # Just read the contents
334 open(LIN, "$lsmod_file");
336 } else {
338 # see what modules are loaded on this system
339 my $lsmod;
341 foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
342 if ( -x "$dir/lsmod" ) {
343 $lsmod = "$dir/lsmod";
344 last;
347 if (!defined($lsmod)) {
348 # try just the path
349 $lsmod = "lsmod";
352 open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod";
355 while (<LIN>) {
356 next if (/^Module/); # Skip the first line.
357 if (/^(\S+)/) {
358 $modules{$1} = 1;
361 close (LIN);
363 # add to the configs hash all configs that are needed to enable
364 # a loaded module.
365 my %configs;
366 foreach my $module (keys(%modules)) {
367 if (defined($objects{$module})) {
368 my @arr = @{$objects{$module}};
369 foreach my $conf (@arr) {
370 $configs{$conf} = $module;
372 } else {
373 # Most likely, someone has a custom (binary?) module loaded.
374 print STDERR "$module config not found!!\n";
378 my $valid = "A-Za-z_0-9";
379 my $repeat = 1;
382 # Note, we do not care about operands (like: &&, ||, !) we want to add any
383 # config that is in the depend list of another config. This script does
384 # not enable configs that are not already enabled. If we come across a
385 # config A that depends on !B, we can still add B to the list of depends
386 # to keep on. If A was on in the original config, B would not have been
387 # and B would not be turned on by this script.
389 sub parse_config_dep_select
391 my ($p) = @_;
393 while ($p =~ /[$valid]/) {
395 if ($p =~ /^[^$valid]*([$valid]+)/) {
396 my $conf = "CONFIG_" . $1;
398 $p =~ s/^[^$valid]*[$valid]+//;
400 if (!defined($configs{$conf})) {
401 # We must make sure that this config has its
402 # dependencies met.
403 $repeat = 1; # do again
404 $configs{$conf} = 1;
406 } else {
407 die "this should never happen";
412 while ($repeat) {
413 $repeat = 0;
415 foreach my $config (keys %configs) {
416 $config =~ s/^CONFIG_//;
418 if (defined($depends{$config})) {
419 # This config has dependencies. Make sure they are also included
420 parse_config_dep_select $depends{$config};
423 if (defined($prompts{$config}) || !defined($selects{$config})) {
424 next;
427 # config has no prompt and must be selected.
428 parse_config_dep_select $selects{$config};
432 my %setconfigs;
434 # Finally, read the .config file and turn off any module enabled that
435 # we could not find a reason to keep enabled.
436 while(<CIN>) {
438 if (/CONFIG_IKCONFIG/) {
439 if (/# CONFIG_IKCONFIG is not set/) {
440 # enable IKCONFIG at least as a module
441 print "CONFIG_IKCONFIG=m\n";
442 # don't ask about PROC
443 print "# CONFIG_IKCONFIG_PROC is not set\n";
444 } else {
445 print;
447 next;
450 if (/^(CONFIG.*)=(m|y)/) {
451 if (defined($configs{$1})) {
452 $setconfigs{$1} = $2;
453 } elsif ($2 eq "m") {
454 print "# $1 is not set\n";
455 next;
458 print;
460 close(CIN);
462 # Integrity check, make sure all modules that we want enabled do
463 # indeed have their configs set.
464 loop:
465 foreach my $module (keys(%modules)) {
466 if (defined($objects{$module})) {
467 my @arr = @{$objects{$module}};
468 foreach my $conf (@arr) {
469 if (defined($setconfigs{$conf})) {
470 next loop;
473 print STDERR "module $module did not have configs";
474 foreach my $conf (@arr) {
475 print STDERR " " , $conf;
477 print STDERR "\n";