1# This script runs `make config' to generate a Linux kernel
2# configuration file. For each question (i.e. kernel configuration
3# option), unless an override is provided, it answers "m" if possible,
4# and otherwise uses the default answer (as determined by the default
5# config for the architecture). Overrides are read from the file
6# $KERNEL_CONFIG, which on each line contains an option name and an
7# answer, e.g. "EXT2_FS_POSIX_ACL y". The script warns about ignored
8# options in $KERNEL_CONFIG, and barfs if `make config' selects
9# another answer for an option than the one provided in
10# $KERNEL_CONFIG.
11
12use strict;
13use IPC::Open2;
14use Cwd;
15
16# exported via nix
17my $debug = $ENV{'DEBUG'};
18my $autoModules = $ENV{'AUTO_MODULES'};
19my $preferBuiltin = $ENV{'PREFER_BUILTIN'};
20my $ignoreConfigErrors = $ENV{'ignoreConfigErrors'};
21my $buildRoot = $ENV{'BUILD_ROOT'};
22my $makeFlags = $ENV{'MAKE_FLAGS'};
23$SIG{PIPE} = 'IGNORE';
24
25# Read the answers.
26my %answers;
27my %requiredAnswers;
28open ANSWERS, "<$ENV{KERNEL_CONFIG}" or die "Could not open answer file";
29while (<ANSWERS>) {
30 chomp;
31 s/#.*//;
32 if (/^\s*([A-Za-z0-9_]+)(\?)?\s+(.*\S)\s*$/) {
33 $answers{$1} = $3;
34 $requiredAnswers{$1} = !(defined $2);
35 } elsif (!/^\s*$/) {
36 die "invalid config line: $_";
37 }
38}
39close ANSWERS;
40
41sub runConfig {
42
43 # Run `make config'.
44 my $pid = open2(\*IN, \*OUT, "make -C $ENV{SRC} O=$buildRoot config SHELL=bash ARCH=$ENV{ARCH} CC=$ENV{CC} HOSTCC=$ENV{HOSTCC} HOSTCXX=$ENV{HOSTCXX} $makeFlags");
45
46 # Parse the output, look for questions and then send an
47 # appropriate answer.
48 my $line = ""; my $s;
49 my %choices = ();
50
51 my ($prevQuestion, $prevName);
52
53 while (!eof IN) {
54 read IN, $s, 1 or next;
55 $line .= $s;
56
57 #print STDERR "LINE: $line\n";
58
59 if ($s eq "\n") {
60 print STDERR "GOT: $line" if $debug;
61
62 # Remember choice alternatives ("> 1. bla (FOO)" or " 2. bla (BAR) (NEW)").
63 if ($line =~ /^\s*>?\s*(\d+)\.\s+.*?\(([A-Za-z0-9_]+)\)(?:\s+\(NEW\))?\s*$/) {
64 $choices{$2} = $1;
65 } else {
66 # The list of choices has ended without us being
67 # asked. This happens for options where only one value
68 # is valid, for instance. The results can foul up
69 # later options, so forget about it.
70 %choices = ();
71 }
72
73 $line = "";
74 }
75
76 elsif ($line =~ /###$/) {
77 # The config program is waiting for an answer.
78
79 # Is this a regular question? ("bla bla (OPTION_NAME) [Y/n/m/...] ")
80 if ($line =~ /(.*) \(([A-Za-z0-9_]+)\) \[(.*)\].*###$/) {
81 my $question = $1; my $name = $2; my $alts = $3;
82 my $answer = "";
83 # Build everything as a module if possible.
84 $answer = "m" if $autoModules && $alts =~ qr{\A(\w/)+m/(\w/)*\?\z} && !($preferBuiltin && $alts =~ /Y/);
85 $answer = $answers{$name} if defined $answers{$name};
86 print STDERR "QUESTION: $question, NAME: $name, ALTS: $alts, ANSWER: $answer\n" if $debug;
87 print OUT "$answer\n";
88 die "repeated question: $question" if $prevQuestion && $prevQuestion eq $question && $name eq $prevName;
89 $prevQuestion = $question;
90 $prevName = $name;
91 }
92
93 # Is this a choice? ("choice[1-N]: ")
94 elsif ($line =~ /choice\[(.*)\]: ###$/) {
95 my $answer = "";
96 foreach my $name (keys %choices) {
97 $answer = $choices{$name} if ($answers{$name} || "") eq "y";
98 }
99 print STDERR "CHOICE: $1, ANSWER: $answer\n" if $debug;
100 print OUT "$answer\n" if $1 =~ /-/;
101 }
102
103 # Some questions lack the option name ("bla bla [Y/n/m/...] ").
104 elsif ($line =~ /(.*) \[(.*)\] ###$/) {
105 print OUT "\n";
106 }
107
108 else {
109 warn "don't know how to answer this question: $line\n";
110 print OUT "\n";
111 }
112
113 $line = "";
114 %choices = ();
115 }
116 }
117
118 close IN;
119 waitpid $pid, 0;
120}
121
122# Run `make config' several times to converge on the desired result.
123# (Some options may only become available after other options are
124# set in a previous run.)
125runConfig;
126runConfig;
127
128# Read the final .config file and check that our answers are in
129# there. `make config' often overrides answers if later questions
130# cause options to be selected.
131my %config;
132open CONFIG, "<$buildRoot/.config" or die "Could not read .config";
133while (<CONFIG>) {
134 chomp;
135 if (/^CONFIG_([A-Za-z0-9_]+)="(.*)"$/) {
136 # String options have double quotes, e.g. 'CONFIG_NLS_DEFAULT="utf8"' and allow escaping.
137 ($config{$1} = $2) =~ s/\\([\\"])/$1/g;
138 } elsif (/^CONFIG_([A-Za-z0-9_]+)=(.*)$/) {
139 $config{$1} = $2;
140 } elsif (/^# CONFIG_([A-Za-z0-9_]+) is not set$/) {
141 $config{$1} = "n";
142 }
143}
144close CONFIG;
145
146my $ret = 0;
147foreach my $name (sort (keys %answers)) {
148 my $f = $requiredAnswers{$name} && $ignoreConfigErrors ne "1"
149 ? sub { warn "error: " . $_[0]; $ret = -1; } : sub { warn "warning: " . $_[0]; };
150 &$f("unused option: $name\n") unless defined $config{$name};
151 &$f("option not set correctly: $name (wanted '$answers{$name}', got '$config{$name}')\n")
152 if $config{$name} && $config{$name} ne $answers{$name};
153}
154exit $ret;