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