Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | # |
Uwe Kleine-König | cce1dac | 2011-01-24 21:12:01 +0100 | [diff] [blame] | 3 | # Copyright 2005-2009 - Steven Rostedt |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 | # |
hiromu | cf5a189 | 2010-08-17 19:49:18 +0900 | [diff] [blame] | 45 | use strict; |
Arnaud Lacombe | 22d550a | 2011-07-20 00:40:09 -0400 | [diff] [blame] | 46 | use Getopt::Long; |
hiromu | cf5a189 | 2010-08-17 19:49:18 +0900 | [diff] [blame] | 47 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 48 | my $config = ".config"; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 49 | |
Steven Rostedt | cdfc479 | 2009-04-30 14:39:48 -0400 | [diff] [blame] | 50 | my $uname = `uname -r`; |
| 51 | chomp $uname; |
| 52 | |
| 53 | my @searchconfigs = ( |
| 54 | { |
Steven Rostedt | a902483 | 2009-05-07 11:01:34 -0400 | [diff] [blame] | 55 | "file" => ".config", |
| 56 | "exec" => "cat", |
| 57 | }, |
| 58 | { |
Steven Rostedt | cdfc479 | 2009-04-30 14:39:48 -0400 | [diff] [blame] | 59 | "file" => "/proc/config.gz", |
| 60 | "exec" => "zcat", |
| 61 | }, |
| 62 | { |
Steven Rostedt | 810b2be | 2009-04-30 19:30:04 -0400 | [diff] [blame] | 63 | "file" => "/boot/config-$uname", |
| 64 | "exec" => "cat", |
| 65 | }, |
| 66 | { |
Steven Rostedt | cdfc479 | 2009-04-30 14:39:48 -0400 | [diff] [blame] | 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 | }, |
Steven Rostedt | cdfc479 | 2009-04-30 14:39:48 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | 4f4c51c | 2012-06-18 21:43:53 -0400 | [diff] [blame] | 116 | # Read in the entire config file into config_file |
| 117 | my @config_file = <CIN>; |
| 118 | close CIN; |
| 119 | |
Arnaud Lacombe | 22d550a | 2011-07-20 00:40:09 -0400 | [diff] [blame] | 120 | # Parse options |
| 121 | my $localmodconfig = 0; |
| 122 | my $localyesconfig = 0; |
| 123 | |
| 124 | GetOptions("localmodconfig" => \$localmodconfig, |
| 125 | "localyesconfig" => \$localyesconfig); |
| 126 | |
Steven Rostedt | 463bf90 | 2009-11-20 09:21:12 -0500 | [diff] [blame] | 127 | # Get the build source and top level Kconfig file (passed in) |
| 128 | my $ksource = $ARGV[0]; |
| 129 | my $kconfig = $ARGV[1]; |
Arnaud Lacombe | f597a71 | 2011-07-01 17:45:31 -0400 | [diff] [blame] | 130 | my $lsmod_file = $ENV{'LSMOD'}; |
Steven Rostedt | 463bf90 | 2009-11-20 09:21:12 -0500 | [diff] [blame] | 131 | |
Toralf Förster | 1743192 | 2010-05-26 20:22:02 +0200 | [diff] [blame] | 132 | my @makefiles = `find $ksource -name Makefile 2>/dev/null`; |
| 133 | chomp @makefiles; |
| 134 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 135 | my %depends; |
| 136 | my %selects; |
| 137 | my %prompts; |
| 138 | my %objects; |
| 139 | my $var; |
Steven Rostedt | 13d7e93 | 2010-01-06 17:56:12 -0500 | [diff] [blame] | 140 | my $iflevel = 0; |
| 141 | my @ifdeps; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 142 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | 20d1904 | 2010-10-28 22:13:51 -0400 | [diff] [blame] | 153 | my $cont = 0; |
| 154 | my $line; |
| 155 | |
Steven Rostedt | 4908980 | 2010-10-29 00:56:46 -0400 | [diff] [blame] | 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"; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 167 | while (<KIN>) { |
| 168 | chomp; |
| 169 | |
Steven Rostedt | 20d1904 | 2010-10-28 22:13:51 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 183 | # collect any Kconfig sources |
| 184 | if (/^source\s*"(.*)"/) { |
| 185 | $kconfigs[$#kconfigs+1] = $1; |
| 186 | } |
| 187 | |
| 188 | # configs found |
Steven Rostedt | 8ef17fa | 2010-10-28 22:21:57 -0400 | [diff] [blame] | 189 | if (/^\s*(menu)?config\s+(\S+)\s*$/) { |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 190 | $state = "NEW"; |
Steven Rostedt | 8ef17fa | 2010-10-28 22:21:57 -0400 | [diff] [blame] | 191 | $config = $2; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 192 | |
Steven Rostedt | 0b58a99 | 2012-06-18 14:09:22 -0400 | [diff] [blame] | 193 | # Add depends for 'if' nesting |
Steven Rostedt | 13d7e93 | 2010-01-06 17:56:12 -0500 | [diff] [blame] | 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 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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+)/) { |
Steven Rostedt | 0b58a99 | 2012-06-18 14:09:22 -0400 | [diff] [blame] | 212 | my $conf = $1; |
| 213 | if (defined($selects{$conf})) { |
| 214 | $selects{$conf} .= " " . $config; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 215 | } else { |
Steven Rostedt | 0b58a99 | 2012-06-18 14:09:22 -0400 | [diff] [blame] | 216 | $selects{$conf} = $config; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 |
hiromu yagura | ccece60 | 2010-08-15 13:13:17 +0900 | [diff] [blame] | 222 | $prompts{$config} = 1; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 223 | |
Steven Rostedt | 13d7e93 | 2010-01-06 17:56:12 -0500 | [diff] [blame] | 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 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | 0b58a99 | 2012-06-18 14:09:22 -0400 | [diff] [blame] | 259 | # Makefiles can use variables to define their dependencies |
Steven Rostedt | 364212f | 2012-01-13 17:53:40 -0500 | [diff] [blame] | 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 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 282 | # Read all Makefiles to map the configs to the objects |
| 283 | foreach my $makefile (@makefiles) { |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 284 | |
Steven Rostedt | d060d96 | 2012-01-13 17:50:39 -0500 | [diff] [blame] | 285 | my $line = ""; |
Steven Rostedt | 364212f | 2012-01-13 17:53:40 -0500 | [diff] [blame] | 286 | my %make_vars; |
Steven Rostedt | 20d1904 | 2010-10-28 22:13:51 -0400 | [diff] [blame] | 287 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 288 | open(MIN,$makefile) || die "Can't open $makefile"; |
| 289 | while (<MIN>) { |
Steven Rostedt | d060d96 | 2012-01-13 17:50:39 -0500 | [diff] [blame] | 290 | # if this line ends with a backslash, continue |
| 291 | chomp; |
| 292 | if (/^(.*)\\$/) { |
| 293 | $line .= $1; |
| 294 | next; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 295 | } |
Steven Rostedt | d060d96 | 2012-01-13 17:50:39 -0500 | [diff] [blame] | 296 | |
| 297 | $line .= $_; |
| 298 | $_ = $line; |
| 299 | $line = ""; |
| 300 | |
| 301 | my $objs; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 302 | |
Steven Rostedt | 0b58a99 | 2012-06-18 14:09:22 -0400 | [diff] [blame] | 303 | # Convert variables in a line (could define configs) |
Steven Rostedt | 364212f | 2012-01-13 17:53:40 -0500 | [diff] [blame] | 304 | $_ = convert_vars($_, %make_vars); |
| 305 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 306 | # collect objects after obj-$(CONFIG_FOO_BAR) |
| 307 | if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) { |
| 308 | $var = $1; |
| 309 | $objs = $2; |
Steven Rostedt | 364212f | 2012-01-13 17:53:40 -0500 | [diff] [blame] | 310 | |
| 311 | # check if variables are set |
| 312 | } elsif (/^\s*(\S+)\s*[:]?=\s*(.*\S)/) { |
| 313 | $make_vars{$1} = $2; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 314 | } |
| 315 | if (defined($objs)) { |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 316 | foreach my $obj (split /\s+/,$objs) { |
| 317 | $obj =~ s/-/_/g; |
| 318 | if ($obj =~ /(.*)\.o$/) { |
Toralf Foerster | 1d1d1fe | 2010-05-28 10:25:52 +0200 | [diff] [blame] | 319 | # Objects may be enabled by more than one config. |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | 615f083 | 2010-02-02 21:51:27 -0500 | [diff] [blame] | 341 | if (defined($lsmod_file)) { |
| 342 | if ( ! -f $lsmod_file) { |
Arnaud Lacombe | f597a71 | 2011-07-01 17:45:31 -0400 | [diff] [blame] | 343 | if ( -f $ENV{'objtree'}."/".$lsmod_file) { |
| 344 | $lsmod_file = $ENV{'objtree'}."/".$lsmod_file; |
| 345 | } else { |
| 346 | die "$lsmod_file not found"; |
| 347 | } |
Steven Rostedt | 88f66ea | 2010-01-06 18:49:44 -0500 | [diff] [blame] | 348 | } |
Steven Rostedt | 615f083 | 2010-02-02 21:51:27 -0500 | [diff] [blame] | 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"); |
| 355 | } |
| 356 | } else { |
| 357 | |
| 358 | # see what modules are loaded on this system |
| 359 | my $lsmod; |
| 360 | |
hiromu | cf5a189 | 2010-08-17 19:49:18 +0900 | [diff] [blame] | 361 | foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) { |
Steven Rostedt | 615f083 | 2010-02-02 21:51:27 -0500 | [diff] [blame] | 362 | if ( -x "$dir/lsmod" ) { |
| 363 | $lsmod = "$dir/lsmod"; |
| 364 | last; |
| 365 | } |
Steven Rostedt | 88f66ea | 2010-01-06 18:49:44 -0500 | [diff] [blame] | 366 | } |
Steven Rostedt | 615f083 | 2010-02-02 21:51:27 -0500 | [diff] [blame] | 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"; |
Steven Rostedt | 88f66ea | 2010-01-06 18:49:44 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 |
Steven Rostedt | 0b58a99 | 2012-06-18 14:09:22 -0400 | [diff] [blame] | 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. |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 386 | my %configs; |
| 387 | foreach my $module (keys(%modules)) { |
| 388 | if (defined($objects{$module})) { |
Toralf Foerster | e5199ed | 2010-05-28 10:24:59 +0200 | [diff] [blame] | 389 | my @arr = @{$objects{$module}}; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | 4f4c51c | 2012-06-18 21:43:53 -0400 | [diff] [blame] | 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; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 403 | my $valid = "A-Za-z_0-9"; |
Steven Rostedt | 4f4c51c | 2012-06-18 21:43:53 -0400 | [diff] [blame] | 404 | |
| 405 | foreach my $line (@config_file) { |
| 406 | $_ = $line; |
| 407 | |
| 408 | if (/(CONFIG_[$valid]*)=(m|y)/) { |
| 409 | $orig_configs{$1} = $2; |
| 410 | } |
| 411 | } |
| 412 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 | # |
Steven Rostedt | d4bb58b | 2012-06-18 22:22:33 -0400 | [diff] [blame^] | 423 | sub parse_config_depends |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | 4f4c51c | 2012-06-18 21:43:53 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | d4bb58b | 2012-06-18 22:22:33 -0400 | [diff] [blame^] | 451 | # Select is treated a bit differently than depends. We call this |
| 452 | # when a config has no prompt and requires another config to be |
| 453 | # selected. We use to just select all configs that selected this |
| 454 | # config, but found that that can balloon into enabling hundreds |
| 455 | # of configs that we do not care about. |
| 456 | # |
| 457 | # The idea is we look at all the configs that select it. If one |
| 458 | # is already in our list of configs to enable, then there's nothing |
| 459 | # else to do. If there isn't, we pick the first config that was |
| 460 | # enabled in the orignal config and use that. |
| 461 | sub parse_config_selects |
| 462 | { |
| 463 | my ($config, $p) = @_; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 464 | |
Steven Rostedt | d4bb58b | 2012-06-18 22:22:33 -0400 | [diff] [blame^] | 465 | my $next_config; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 466 | |
Steven Rostedt | d4bb58b | 2012-06-18 22:22:33 -0400 | [diff] [blame^] | 467 | while ($p =~ /[$valid]/) { |
| 468 | |
| 469 | if ($p =~ /^[^$valid]*([$valid]+)/) { |
| 470 | my $conf = "CONFIG_" . $1; |
| 471 | |
| 472 | $p =~ s/^[^$valid]*[$valid]+//; |
| 473 | |
| 474 | # Make sure that this config exists in the current .config file |
| 475 | if (!defined($orig_configs{$conf})) { |
| 476 | next; |
| 477 | } |
| 478 | |
| 479 | # Check if something other than a module selects this config |
| 480 | if (defined($orig_configs{$conf}) && $orig_configs{$conf} ne "m") { |
| 481 | # we are good with this |
| 482 | return; |
| 483 | } |
| 484 | if (defined($configs{$conf})) { |
| 485 | # A set config selects this config, we are good |
| 486 | return; |
| 487 | } |
| 488 | # Set this config to be selected |
| 489 | if (!defined($next_config)) { |
| 490 | $next_config = $conf; |
| 491 | } |
| 492 | } else { |
| 493 | die "this should never happen"; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 494 | } |
Steven Rostedt | d4bb58b | 2012-06-18 22:22:33 -0400 | [diff] [blame^] | 495 | } |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 496 | |
Steven Rostedt | d4bb58b | 2012-06-18 22:22:33 -0400 | [diff] [blame^] | 497 | # If no possible config selected this, then something happened. |
| 498 | if (!defined($next_config)) { |
| 499 | print STDERR "WARNING: $config is required, but nothing in the\n"; |
| 500 | print STDERR " current config selects it.\n"; |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | # If we are here, then we found no config that is set and |
| 505 | # selects this config. Repeat. |
| 506 | $repeat = 1; |
| 507 | # Make this config need to be selected |
| 508 | $configs{$next_config} = 1; |
| 509 | } |
| 510 | |
| 511 | my %process_selects; |
| 512 | |
| 513 | # loop through all configs, select their dependencies. |
| 514 | sub loop_depend { |
| 515 | $repeat = 1; |
| 516 | |
| 517 | while ($repeat) { |
| 518 | $repeat = 0; |
| 519 | |
| 520 | forloop: |
| 521 | foreach my $config (keys %configs) { |
| 522 | |
| 523 | # If this config is not a module, we do not need to process it |
| 524 | if (defined($orig_configs{$config}) && $orig_configs{$config} ne "m") { |
| 525 | next forloop; |
| 526 | } |
| 527 | |
| 528 | $config =~ s/^CONFIG_//; |
| 529 | |
| 530 | if (defined($depends{$config})) { |
| 531 | # This config has dependencies. Make sure they are also included |
| 532 | parse_config_depends $depends{$config}; |
| 533 | } |
| 534 | |
| 535 | # If the config has no prompt, then we need to check if a config |
| 536 | # that is enabled selected it. Or if we need to enable one. |
| 537 | if (!defined($prompts{$config}) && defined($selects{$config})) { |
| 538 | $process_selects{$config} = 1; |
| 539 | } |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 540 | } |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Steven Rostedt | d4bb58b | 2012-06-18 22:22:33 -0400 | [diff] [blame^] | 544 | sub loop_select { |
| 545 | |
| 546 | foreach my $config (keys %process_selects) { |
| 547 | $config =~ s/^CONFIG_//; |
| 548 | |
| 549 | # config has no prompt and must be selected. |
| 550 | parse_config_selects $config, $selects{$config}; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | while ($repeat) { |
| 555 | # Get the first set of configs and their dependencies. |
| 556 | loop_depend; |
| 557 | |
| 558 | $repeat = 0; |
| 559 | |
| 560 | # Now we need to see if we have to check selects; |
| 561 | loop_select; |
| 562 | } |
| 563 | |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 564 | my %setconfigs; |
| 565 | |
| 566 | # Finally, read the .config file and turn off any module enabled that |
| 567 | # we could not find a reason to keep enabled. |
Steven Rostedt | 4f4c51c | 2012-06-18 21:43:53 -0400 | [diff] [blame] | 568 | foreach my $line (@config_file) { |
| 569 | $_ = $line; |
Steven Rostedt | 744ffcb | 2009-04-30 12:15:10 -0400 | [diff] [blame] | 570 | |
| 571 | if (/CONFIG_IKCONFIG/) { |
| 572 | if (/# CONFIG_IKCONFIG is not set/) { |
| 573 | # enable IKCONFIG at least as a module |
| 574 | print "CONFIG_IKCONFIG=m\n"; |
| 575 | # don't ask about PROC |
Steven Rostedt | d08ca27 | 2009-04-30 19:24:00 -0400 | [diff] [blame] | 576 | print "# CONFIG_IKCONFIG_PROC is not set\n"; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 577 | } else { |
Steven Rostedt | 744ffcb | 2009-04-30 12:15:10 -0400 | [diff] [blame] | 578 | print; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 579 | } |
Steven Rostedt | 744ffcb | 2009-04-30 12:15:10 -0400 | [diff] [blame] | 580 | next; |
| 581 | } |
| 582 | |
| 583 | if (/^(CONFIG.*)=(m|y)/) { |
| 584 | if (defined($configs{$1})) { |
Arnaud Lacombe | 22d550a | 2011-07-20 00:40:09 -0400 | [diff] [blame] | 585 | if ($localyesconfig) { |
| 586 | $setconfigs{$1} = 'y'; |
| 587 | } else { |
| 588 | $setconfigs{$1} = $2; |
| 589 | } |
Steven Rostedt | 744ffcb | 2009-04-30 12:15:10 -0400 | [diff] [blame] | 590 | } elsif ($2 eq "m") { |
| 591 | print "# $1 is not set\n"; |
Steven Rostedt | d08ca27 | 2009-04-30 19:24:00 -0400 | [diff] [blame] | 592 | next; |
Steven Rostedt | 744ffcb | 2009-04-30 12:15:10 -0400 | [diff] [blame] | 593 | } |
Steven Rostedt | 744ffcb | 2009-04-30 12:15:10 -0400 | [diff] [blame] | 594 | } |
Steven Rostedt | d08ca27 | 2009-04-30 19:24:00 -0400 | [diff] [blame] | 595 | print; |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 596 | } |
Steven Rostedt | dcc6024 | 2009-04-29 22:52:21 -0400 | [diff] [blame] | 597 | |
| 598 | # Integrity check, make sure all modules that we want enabled do |
| 599 | # indeed have their configs set. |
| 600 | loop: |
| 601 | foreach my $module (keys(%modules)) { |
| 602 | if (defined($objects{$module})) { |
| 603 | my @arr = @{$objects{$module}}; |
| 604 | foreach my $conf (@arr) { |
| 605 | if (defined($setconfigs{$conf})) { |
| 606 | next loop; |
| 607 | } |
| 608 | } |
| 609 | print STDERR "module $module did not have configs"; |
| 610 | foreach my $conf (@arr) { |
| 611 | print STDERR " " , $conf; |
| 612 | } |
| 613 | print STDERR "\n"; |
| 614 | } |
| 615 | } |