unfortunately, we cannot use time.h, there is a conflict name with std headers
[aversive.git] / config / scripts / Configure
1 #! /bin/sh
2 #
3 # This script is used to configure an Aversive project. It is mainly
4 # coming from the Linux Kernel Configure script. Thanks to the authors.
5 #
6
7
8
9 # Make sure we're really running bash.
10 #
11 # I would really have preferred to write this script in a language with
12 # better string handling, but alas, bash is the only scripting language
13 # that I can be reasonable sure everybody has on their linux machine.
14 #
15 [ -z "$BASH" ] && { echo "Configure requires bash" 1>&2; exit 1; }
16
17 # Disable filename globbing once and for all.
18 # Enable function cacheing.
19 set -f -h
20
21 #
22 # Dummy functions for use with a config.in modified for menuconf
23 #
24 function mainmenu_option () {
25         :
26 }
27 function mainmenu_name () {
28         :
29 }
30 function endmenu () {
31         :
32 }
33
34 #
35 # help prints the corresponding help text from ${HELP_FILE} to stdout
36 #
37 #       help variable
38 #
39 function help () {
40   if [ -f ${HELP_FILE} ]
41   then
42      #first escape regexp special characters in the argument:
43      var=$(echo "$1"|sed 's/[][\/.^$*]/\\&/g')
44      #now pick out the right help text:
45      text=$(sed -n "/^$var[     ]*\$/,\${
46                         /^$var[         ]*\$/c\\
47 ${var}:\\
48
49                         /^#/b
50                         /^[^    ]/q
51                         p
52                     }" ${HELP_FILE})
53      if [ -z "$text" ]
54      then
55           echo; echo "  Sorry, no help available for this option yet.";echo
56      else
57           (echo; echo "$text") | ${PAGER:-more}
58      fi
59   else
60      echo;
61      echo "  Can't access the file ${HELP_FILE} which"
62      echo "  should contain the help texts."
63      echo
64   fi
65 }
66
67
68 #
69 # readln reads a line into $ans.
70 #
71 #       readln prompt default oldval
72 #
73 function readln () {
74         if [ "$DEFAULT" = "-d" ]; then
75                 echo "$1"
76                 ans=$2
77         else
78                 echo -n "$1"
79                 [ -z "$3" ] && echo -n "(NEW) "
80                 IFS='@' read ans || exit 1
81                 [ -z "$ans" ] && ans=$2
82         fi
83 }
84
85 #
86 # comment does some pretty-printing
87 #
88 #       comment 'xxx'
89
90 function comment () {
91         echo "*"; echo "* $1" ; echo "*"
92         (echo "" ; echo "#"; echo "# $1" ; echo "#") >>$CONFIG
93         (echo "" ; echo "/*"; echo " * $1" ; echo " */") >>$CONFIG_H
94 }
95
96 #
97 # define_bool sets the value of a boolean argument
98 #
99 #       define_bool define value
100 #
101
102 function define_bool () {
103         case "$2" in
104          "y")
105                 echo "$1=y" >>$CONFIG
106                 echo "#define $1 1" >>$CONFIG_H
107                 ;;
108
109          "n")
110                 echo "# $1 is not set" >>$CONFIG
111                 echo "#undef  $1" >>$CONFIG_H
112                 ;;
113         esac
114         eval "$1=$2"
115 }
116
117 #
118 # bool processes a boolean argument
119 #
120 #       bool question define
121 #
122 function bool () {
123         old=$(eval echo "\${$2}")
124         def=${old:-'n'}
125         case "$def" in
126          "y" | "m") defprompt="Y/n/?"
127               def="y"
128               ;;
129          "n") defprompt="N/y/?"
130               ;;
131         esac
132         while :; do
133           readln "$1 ($2) [$defprompt] " "$def" "$old"
134           case "$ans" in
135             [yY] | [yY]es ) define_bool "$2" "y"
136                             break;;
137             [nN] | [nN]o )  define_bool "$2" "n"
138                             break;;
139             * )             help "$2"
140                             ;;
141           esac
142         done
143 }
144
145 function dep_bool () {
146         ques=$1
147         var=$2
148         shift 2
149         while [ $# -gt 0 ]; do
150           case "$1" in
151             n)
152               define_bool "$var" "n"
153               return
154               ;;
155           esac
156           shift
157         done
158
159         bool "$ques" "$var"
160 }
161
162
163 #
164 # define_int sets the value of a integer argument
165 #
166 #       define_int define value
167 #
168 function define_int () {
169         echo "$1=$2" >>$CONFIG
170         echo "#define $1 ($2)" >>$CONFIG_H
171         eval "$1=$2"
172 }
173
174 #
175 # int processes an integer argument with optional limits
176 #
177 #       int question define default [min max]
178 #
179 function int () {
180         old=$(eval echo "\${$2}")
181         def=${old:-$3}
182         if [ $# -gt 3 ]; then
183           min=$4
184         else
185           min=-1000000000    # !!
186         fi
187         if [ $# -gt 4 ]; then
188           max=$5
189         else
190           max=1000000000     # !!
191         fi
192         while :; do
193           readln "$1 ($2) [$def] " "$def" "$old"
194           echo [$ans]
195           if expr \( \( $ans + 0 \) \>= $min \) \& \( $ans \<= $max \) >/dev/null 2>&1 ; then
196             define_int "$2" "$ans"
197             break
198           else
199             help "$2"
200           fi
201         done
202 }
203
204 #
205 # define_hex sets the value of a hexadecimal argument
206 #
207 #       define_hex define value
208 #
209 function define_hex () {
210         echo "$1=$2" >>$CONFIG
211         echo "#define $1 0x${2#*[x,X]}" >>$CONFIG_H
212         eval "$1=$2"
213 }
214
215 #
216 # hex processes an hexadecimal argument
217 #
218 #       hex question define default
219 #
220 function hex () {
221         old=$(eval echo "\${$2}")
222         def=${old:-$3}
223         def=${def#*[x,X]}
224         while :; do
225           readln "$1 ($2) [$def] " "$def" "$old"
226           ans=${ans#*[x,X]}
227           if expr "$ans" : '[0-9a-fA-F][0-9a-fA-F]*$' > /dev/null; then
228             define_hex "$2" "$ans"
229             break
230           else
231             help "$2"
232           fi
233         done
234 }
235
236 #
237 # define_string sets the value of a string argument
238 #
239 #       define_string define value
240 #
241 function define_string () {
242         echo "$1=\"$2\"" >>$CONFIG
243         echo "#define $1 \"$2\"" >>$CONFIG_H
244         eval "$1=\"$2\""
245 }
246
247 #
248 # string processes a string argument
249 #
250 #       string question define default
251 #
252 function string () {
253         old=$(eval echo "\${$2}")
254         def=${old:-$3}
255         while :; do
256           if [ "$old" = "?" ]; then
257              readln "$1 ($2) [$def] " "$def" ""
258           else
259              readln "$1 ($2) [$def] " "$def" "$old"
260           fi
261           if [ "$ans" = "?" ]; then
262             help "$2"
263           else
264             break
265           fi
266         done
267         define_string "$2" "$ans"
268 }
269 #
270 # choice processes a choice list (1-out-of-n)
271 #
272 #       choice question choice-list default
273 #
274 # The choice list has a syntax of:
275 #       NAME WHITESPACE VALUE { WHITESPACE NAME WHITESPACE VALUE }
276 # The user may enter any unique prefix of one of the NAMEs and
277 # choice will define VALUE as if it were a boolean option.
278 # VALUE must be in all uppercase.  Normally, VALUE is of the
279 # form CONFIG_<something>.  Thus, if the user selects <something>,
280 # the CPP symbol CONFIG_<something> will be defined and the
281 # shell variable CONFIG_<something> will be set to "y".
282 #
283 function choice () {
284         question="$1"
285         choices="$2"
286         old=
287         def=$3
288
289         # determine default answer:
290         names=""
291         set -- $choices
292         firstvar=$2
293         while [ -n "$2" ]; do
294                 if [ -n "$names" ]; then
295                         names="$names, $1"
296                 else
297                         names="$1"
298                 fi
299                 if [ "$(eval echo \"\${$2}\")" = "y" ]; then
300                         old=$1
301                         def=$1
302                 fi
303                 shift; shift
304         done
305
306         val=""
307         while [ -z "$val" ]; do
308                 ambg=n
309                 readln "$question ($names) [$def] " "$def" "$old"
310                 ans=$(echo $ans | tr a-z A-Z)
311                 set -- $choices
312                 while [ -n "$1" ]; do
313                         name=$(echo $1 | tr a-z A-Z)
314                         case "$name" in
315                                 "$ans"* | */"$ans"* )
316                                         case "$name" in
317                                                 "$ans" | */"$ans"/* | \
318                                                 "$ans"/* | */"$ans" )
319                                                         val="$2"
320                                                         break # exact match
321                                                 ;;
322                                         esac
323                                         if [ -n "$val" ]; then
324                                                 echo;echo \
325                 "  Sorry, \"$ans\" is ambiguous; please enter a longer string."
326                                                 echo
327                                                 val=""
328                                                 ambg=y
329                                                 break
330                                         else
331                                                 val="$2"
332                                         fi;;
333                         esac
334                         shift; shift
335                 done
336                 if [ "$val" = "" -a "$ambg" = "n" ]; then
337                         help "$firstvar"
338                 fi
339         done
340         set -- $choices
341         while [ -n "$2" ]; do
342                 if [ "$2" = "$val" ]; then
343                         echo "  defined $val"
344                         define_bool "$2" "y"
345                 else
346                         define_bool "$2" "n"
347                 fi
348                 shift; shift
349         done
350 }
351
352 CONFIG=.tmpconfig
353 CONFIG_H=.tmpconfig.h
354 trap "rm -f $CONFIG $CONFIG_H ; exit 1" 1 2
355 if [ -z "$HELP_FILE" ]
356 then
357         HELP_FILE="Documentation/Configure.help"
358 fi
359
360 #
361 # Make sure we start out with a clean slate.
362 #
363 echo "#" > $CONFIG
364 echo "# Automatically generated make config: don't edit" >> $CONFIG
365 echo "#" >> $CONFIG
366
367 echo "/*" > $CONFIG_H
368 echo " * Automatically generated C config: don't edit" >> $CONFIG_H
369 echo " */" >> $CONFIG_H
370 echo "#define AUTOCONF_INCLUDED" >> $CONFIG_H
371
372 DEFAULT=""
373 if [ "$1" = "-d" ] ; then
374         DEFAULT="-d"
375         shift
376 fi
377
378 CONFIG_IN=./config.in
379 if [ "$1" != "" ] ; then
380         CONFIG_IN=$1
381 fi
382
383 DEFAULTS=$AVERSIVE_DIR/config/.config
384 if [ -f .config ]; then
385   DEFAULTS=.config
386 fi
387
388 if [ -f $DEFAULTS ]; then
389   echo "#"
390   echo "# Using defaults found in" $DEFAULTS
391   echo "#"
392   . $DEFAULTS
393   sed -e 's/# \(CONFIG_[^ ]*\) is not.*/\1=n/' <$DEFAULTS >.config-is-not.$$
394   . .config-is-not.$$
395   rm .config-is-not.$$
396 else
397   echo "#"
398   echo "# No defaults found"
399   echo "#"
400 fi
401
402 . $CONFIG_IN
403
404 rm -f .config.old
405 if [ -f .config ]; then
406         mv .config .config.old
407 fi
408 mv .tmpconfig .config
409 if [ -w "`dirname ${AUTOCONF_FILE:=include/linux/autoconf.h}`" ]; then
410         mv .tmpconfig.h ${AUTOCONF_FILE:-include/linux/autoconf.h}
411 fi
412
413 $AVERSIVE_DIR/config/generate_aversive_config .config .aversive_conf
414
415 echo
416 echo "*** End of Aversive project configuration."
417 echo "*** Next, you should run 'make clean, then make'."
418 echo
419
420
421 exit 0