kconfig: Fix missing declaration of variable $dir in streamline_config.pl
[firefly-linux-kernel-4.4.55.git] / scripts / kconfig / streamline_config.pl
1 #!/usr/bin/perl -w
2 #
3 # Copywrite 2005-2009 - Steven Rostedt
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 #
45 use strict;
46
47 my $config = ".config";
48
49 my $uname = `uname -r`;
50 chomp $uname;
51
52 my @searchconfigs = (
53         {
54             "file" => ".config",
55             "exec" => "cat",
56         },
57         {
58             "file" => "/proc/config.gz",
59             "exec" => "zcat",
60         },
61         {
62             "file" => "/boot/config-$uname",
63             "exec" => "cat",
64         },
65         {
66             "file" => "/boot/vmlinuz-$uname",
67             "exec" => "scripts/extract-ikconfig",
68             "test" => "scripts/extract-ikconfig",
69         },
70         {
71             "file" => "vmlinux",
72             "exec" => "scripts/extract-ikconfig",
73             "test" => "scripts/extract-ikconfig",
74         },
75         {
76             "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
77             "exec" => "scripts/extract-ikconfig",
78             "test" => "scripts/extract-ikconfig",
79         },
80         {
81             "file" => "kernel/configs.ko",
82             "exec" => "scripts/extract-ikconfig",
83             "test" => "scripts/extract-ikconfig",
84         },
85         {
86             "file" => "kernel/configs.o",
87             "exec" => "scripts/extract-ikconfig",
88             "test" => "scripts/extract-ikconfig",
89         },
90 );
91
92 sub find_config {
93     foreach my $conf (@searchconfigs) {
94         my $file = $conf->{"file"};
95
96         next if ( ! -f "$file");
97
98         if (defined($conf->{"test"})) {
99             `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
100             next if ($?);
101         }
102
103         my $exec = $conf->{"exec"};
104
105         print STDERR "using config: '$file'\n";
106
107         open(CIN, "$exec $file |") || die "Failed to run $exec $file";
108         return;
109     }
110     die "No config file found";
111 }
112
113 find_config;
114
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];
119
120 my @makefiles = `find $ksource -name Makefile 2>/dev/null`;
121 chomp @makefiles;
122
123 my %depends;
124 my %selects;
125 my %prompts;
126 my %objects;
127 my $var;
128 my $cont = 0;
129 my $iflevel = 0;
130 my @ifdeps;
131
132 # prevent recursion
133 my %read_kconfigs;
134
135 sub read_kconfig {
136     my ($kconfig) = @_;
137
138     my $state = "NONE";
139     my $config;
140     my @kconfigs;
141
142     my $source = "$ksource/$kconfig";
143     my $last_source = "";
144
145     # Check for any environment variables used
146     while ($source =~ /\$(\w+)/ && $last_source ne $source) {
147         my $env = $1;
148         $last_source = $source;
149         $source =~ s/\$$env/$ENV{$env}/;
150     }
151
152     open(KIN, "$source") || die "Can't open $kconfig";
153     while (<KIN>) {
154         chomp;
155
156         # collect any Kconfig sources
157         if (/^source\s*"(.*)"/) {
158             $kconfigs[$#kconfigs+1] = $1;
159         }
160
161         # configs found
162         if (/^\s*config\s+(\S+)\s*$/) {
163             $state = "NEW";
164             $config = $1;
165
166             for (my $i = 0; $i < $iflevel; $i++) {
167                 if ($i) {
168                     $depends{$config} .= " " . $ifdeps[$i];
169                 } else {
170                     $depends{$config} = $ifdeps[$i];
171                 }
172                 $state = "DEP";
173             }
174
175         # collect the depends for the config
176         } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
177             $state = "DEP";
178             $depends{$config} = $1;
179         } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
180             $depends{$config} .= " " . $1;
181
182         # Get the configs that select this config
183         } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
184             if (defined($selects{$1})) {
185                 $selects{$1} .= " " . $config;
186             } else {
187                 $selects{$1} = $config;
188             }
189
190         # configs without prompts must be selected
191         } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
192             # note if the config has a prompt
193             $prompts{$config} = 1;
194
195         # Check for if statements
196         } elsif (/^if\s+(.*\S)\s*$/) {
197             my $deps = $1;
198             # remove beginning and ending non text
199             $deps =~ s/^[^a-zA-Z0-9_]*//;
200             $deps =~ s/[^a-zA-Z0-9_]*$//;
201
202             my @deps = split /[^a-zA-Z0-9_]+/, $deps;
203
204             $ifdeps[$iflevel++] = join ':', @deps;
205
206         } elsif (/^endif/) {
207
208             $iflevel-- if ($iflevel);
209
210         # stop on "help"
211         } elsif (/^\s*help\s*$/) {
212             $state = "NONE";
213         }
214     }
215     close(KIN);
216
217     # read in any configs that were found.
218     foreach $kconfig (@kconfigs) {
219         if (!defined($read_kconfigs{$kconfig})) {
220             $read_kconfigs{$kconfig} = 1;
221             read_kconfig($kconfig);
222         }
223     }
224 }
225
226 if ($kconfig) {
227     read_kconfig($kconfig);
228 }
229
230 # Read all Makefiles to map the configs to the objects
231 foreach my $makefile (@makefiles) {
232
233     open(MIN,$makefile) || die "Can't open $makefile";
234     while (<MIN>) {
235         my $objs;
236
237         # is this a line after a line with a backslash?
238         if ($cont && /(\S.*)$/) {
239             $objs = $1;
240         }
241         $cont = 0;
242
243         # collect objects after obj-$(CONFIG_FOO_BAR)
244         if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
245             $var = $1;
246             $objs = $2;
247         }
248         if (defined($objs)) {
249             # test if the line ends with a backslash
250             if ($objs =~ m,(.*)\\$,) {
251                 $objs = $1;
252                 $cont = 1;
253             }
254
255             foreach my $obj (split /\s+/,$objs) {
256                 $obj =~ s/-/_/g;
257                 if ($obj =~ /(.*)\.o$/) {
258                     # Objects may be enabled by more than one config.
259                     # Store configs in an array.
260                     my @arr;
261
262                     if (defined($objects{$1})) {
263                         @arr = @{$objects{$1}};
264                     }
265
266                     $arr[$#arr+1] = $var;
267
268                     # The objects have a hash mapping to a reference
269                     # of an array of configs.
270                     $objects{$1} = \@arr;
271                 }
272             }
273         }
274     }
275     close(MIN);
276 }
277
278 my %modules;
279
280 if (defined($lsmod_file)) {
281     if ( ! -f $lsmod_file) {
282         die "$lsmod_file not found";
283     }
284     if ( -x $lsmod_file) {
285         # the file is executable, run it
286         open(LIN, "$lsmod_file|");
287     } else {
288         # Just read the contents
289         open(LIN, "$lsmod_file");
290     }
291 } else {
292
293     # see what modules are loaded on this system
294     my $lsmod;
295
296     foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
297         if ( -x "$dir/lsmod" ) {
298             $lsmod = "$dir/lsmod";
299             last;
300         }
301 }
302     if (!defined($lsmod)) {
303         # try just the path
304         $lsmod = "lsmod";
305     }
306
307     open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod";
308 }
309
310 while (<LIN>) {
311         next if (/^Module/);  # Skip the first line.
312         if (/^(\S+)/) {
313                 $modules{$1} = 1;
314         }
315 }
316 close (LIN);
317
318 # add to the configs hash all configs that are needed to enable
319 # a loaded module.
320 my %configs;
321 foreach my $module (keys(%modules)) {
322     if (defined($objects{$module})) {
323         my @arr = @{$objects{$module}};
324         foreach my $conf (@arr) {
325             $configs{$conf} = $module;
326         }
327     } else {
328         # Most likely, someone has a custom (binary?) module loaded.
329         print STDERR "$module config not found!!\n";
330     }
331 }
332
333 my $valid = "A-Za-z_0-9";
334 my $repeat = 1;
335
336 #
337 # Note, we do not care about operands (like: &&, ||, !) we want to add any
338 # config that is in the depend list of another config. This script does
339 # not enable configs that are not already enabled. If we come across a
340 # config A that depends on !B, we can still add B to the list of depends
341 # to keep on. If A was on in the original config, B would not have been
342 # and B would not be turned on by this script.
343 #
344 sub parse_config_dep_select
345 {
346     my ($p) = @_;
347
348     while ($p =~ /[$valid]/) {
349
350         if ($p =~ /^[^$valid]*([$valid]+)/) {
351             my $conf = "CONFIG_" . $1;
352
353             $p =~ s/^[^$valid]*[$valid]+//;
354
355             if (!defined($configs{$conf})) {
356                 # We must make sure that this config has its
357                 # dependencies met.
358                 $repeat = 1; # do again
359                 $configs{$conf} = 1;
360             }
361         } else {
362             die "this should never happen";
363         }
364     }
365 }
366
367 while ($repeat) {
368     $repeat = 0;
369
370     foreach my $config (keys %configs) {
371         $config =~ s/^CONFIG_//;
372
373         if (defined($depends{$config})) {
374             # This config has dependencies. Make sure they are also included
375             parse_config_dep_select $depends{$config};
376         }
377
378         if (defined($prompts{$config}) || !defined($selects{$config})) {
379             next;
380         }
381
382         # config has no prompt and must be selected.
383         parse_config_dep_select $selects{$config};
384     }
385 }
386
387 my %setconfigs;
388
389 # Finally, read the .config file and turn off any module enabled that
390 # we could not find a reason to keep enabled.
391 while(<CIN>) {
392
393     if (/CONFIG_IKCONFIG/) {
394         if (/# CONFIG_IKCONFIG is not set/) {
395             # enable IKCONFIG at least as a module
396             print "CONFIG_IKCONFIG=m\n";
397             # don't ask about PROC
398             print "# CONFIG_IKCONFIG_PROC is not set\n";
399         } else {
400             print;
401         }
402         next;
403     }
404
405     if (/^(CONFIG.*)=(m|y)/) {
406         if (defined($configs{$1})) {
407             $setconfigs{$1} = $2;
408         } elsif ($2 eq "m") {
409             print "# $1 is not set\n";
410             next;
411         }
412     }
413     print;
414 }
415 close(CIN);
416
417 # Integrity check, make sure all modules that we want enabled do
418 # indeed have their configs set.
419 loop:
420 foreach my $module (keys(%modules)) {
421     if (defined($objects{$module})) {
422         my @arr = @{$objects{$module}};
423         foreach my $conf (@arr) {
424             if (defined($setconfigs{$conf})) {
425                 next loop;
426             }
427         }
428         print STDERR "module $module did not have configs";
429         foreach my $conf (@arr) {
430             print STDERR " " , $conf;
431         }
432         print STDERR "\n";
433     }
434 }