This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
4f7be32e92c32823a2540b7180c93d162ac9a26d
[metaconfig.git] / U / modified / Options.U
1 ?RCS: $Id: Options.U,v 3.0.1.7 1997/02/28 15:08:15 ram Exp $
2 ?RCS:
3 ?RCS: Copyright (c) 1991-1993, Raphael Manfredi
4 ?RCS: 
5 ?RCS: You may redistribute only under the terms of the Artistic Licence,
6 ?RCS: as specified in the README file that comes with the distribution.
7 ?RCS: You may reuse parts of this distribution only within the terms of
8 ?RCS: that same Artistic Licence; a copy of which may be found at the root
9 ?RCS: of the source tree for dist 3.0.
10 ?RCS:
11 ?RCS: $Log: Options.U,v $
12 ?RCS: Revision 3.0.1.7  1997/02/28  15:08:15  ram
13 ?RCS: patch61: optdef.sh now starts with a "startsh"
14 ?RCS: patch61: moved some code from Head.U
15 ?RCS:
16 ?RCS: Revision 3.0.1.6  1995/09/25  09:14:46  ram
17 ?RCS: patch59: protected option parsing code against 'echo -*' option failure
18 ?RCS:
19 ?RCS: Revision 3.0.1.5  1995/05/12  12:04:52  ram
20 ?RCS: patch54: added -K option for experts
21 ?RCS:
22 ?RCS: Revision 3.0.1.4  1995/01/30  14:27:52  ram
23 ?RCS: patch49: this unit now exports file optdef.sh, not a variable
24 ?RCS:
25 ?RCS: Revision 3.0.1.3  1995/01/11  15:19:00  ram
26 ?RCS: patch45: new -O option allowing -D and -U to override config.sh setttings
27 ?RCS: patch45: file optdef.sh is no longer removed after sourcing
28 ?RCS:
29 ?RCS: Revision 3.0.1.2  1994/10/29  15:58:06  ram
30 ?RCS: patch36: ensure option definition file is removed before appending
31 ?RCS: patch36: protect variable definitions with spaces in them
32 ?RCS:
33 ?RCS: Revision 3.0.1.1  1994/06/20  06:55:44  ram
34 ?RCS: patch30: now uses new me symbol to tag error messages
35 ?RCS: patch30: new -D and -U options to define/undef symbols (JHI)
36 ?RCS:
37 ?RCS: Revision 3.0  1993/08/18  12:05:14  ram
38 ?RCS: Baseline for dist 3.0 netwide release.
39 ?RCS:
40 ?X:
41 ?X: Command line parsing. It is really important that the variables used here
42 ?X:     be not listed in the MAKE line, or they will be saved in config.sh and
43 ?X: loading this file to fetch default answers would clobber the values set
44 ?X:     herein.
45 ?X:
46 ?MAKE:Options: startsh
47 ?MAKE:  -pick wipe $@ %<
48 ?V:reuseval alldone error realsilent silent extractsh fastread \
49         override knowitall: config_sh
50 ?T:arg argn symbol config_arg0 config_args config_argc xxx yyy zzz uuu
51 ?T:args_exp args_sep arg_exp libswanted ccflags
52 ?F:./optdef.sh ./cmdline.opt ./posthint.sh ./cmdl.opt
53
54 : Save command line options in file UU/cmdline.opt for later use in
55 : generating config.sh.
56 ?X: This temporary file will be read by Oldsym.U.  I used a temporary
57 ?X: file to preserve all sorts of potential command line quotes and
58 ?X: also because we don't know in advance how many variables we'll
59 ?X: need, so I can't actually declare them on the MAKE line.
60 ?X: The config_args variable won't be quite correct if Configure is
61 ?X: fed something like    ./Configure -Dcc="gcc -B/usr/ccs/bin/"
62 ?X: since the quotes are gone by the time we see them.  You'd have to
63 ?X: reconstruct the command line from the config_arg? lines, but since
64 ?X: I don't imagine anyone actually having to do that, I'm not going
65 ?X: to worry too much.
66 cat > cmdline.opt <<EOSH
67 # Configure command line arguments.
68 config_arg0='$0'
69 config_args='$*'
70 config_argc=$#
71 EOSH
72 argn=1
73 args_exp=''
74 args_sep=''
75 for arg in "$@"; do
76         cat >>cmdline.opt <<EOSH
77 config_arg$argn='$arg'
78 EOSH
79         # Extreme backslashitis: replace each ' by '"'"'
80         cat <<EOC | sed -e "s/'/'"'"'"'"'"'"'/g" > cmdl.opt
81 $arg
82 EOC
83         arg_exp=`cat cmdl.opt`
84         args_exp="$args_exp$args_sep'$arg_exp'"
85         argn=`expr $argn + 1`
86         args_sep=' '
87 done
88 # args_exp is good for restarting self: eval "set X $args_exp"; shift; $0 "$@"
89 # used by ./hints/os2.sh
90 rm -f cmdl.opt
91
92 : produce awk script to parse command line options
93 cat >options.awk <<'EOF'
94 BEGIN {
95         optstr = "A:dD:eEf:hKOrsSU:V";  # getopt-style specification
96
97         len = length(optstr);
98         for (i = 1; i <= len; i++) {
99                 c = substr(optstr, i, 1);
100 ?X: some older awk's do not have the C ?: construct
101                 if (i < len) a = substr(optstr, i + 1, 1); else a = "";
102                 if (a == ":") {
103                         arg[c] = 1;
104                         i++;
105                 }
106                 opt[c] = 1;
107         }
108 }
109 {
110         expect = 0;
111         str = $0;
112         if (substr(str, 1, 1) != "-") {
113                 printf("'%s'\n", str);
114                 next;
115         }
116         len = length($0);
117         for (i = 2; i <= len; i++) {
118                 c = substr(str, i, 1);
119                 if (!opt[c]) {
120                         printf("-%s\n", substr(str, i));
121                         next;
122                 }
123                 printf("-%s\n", c);
124                 if (arg[c]) {
125                         if (i < len)
126                                 printf("'%s'\n", substr(str, i + 1));
127                         else
128                                 expect = 1;
129                         next;
130                 }
131         }
132 }
133 END {
134         if (expect)
135                 print "?";
136 }
137 EOF
138
139 : process the command line options
140 ?X: Use "$@" to keep arguments with spaces in them from being split apart.
141 ?X: For the same reason, awk will output quoted arguments and the final eval
142 ?X: removes them and sets a proper $* array. An 'X' is prependend to each
143 ?X: argument before being fed to echo to guard against 'echo -x', where -x
144 ?X: would be understood as an echo option! It is removed before feeding awk.
145 set X `for arg in "$@"; do echo "X$arg"; done |
146         sed -e s/X// | awk -f options.awk`
147 eval "set $*"
148 shift
149 rm -f options.awk
150
151 : set up default values
152 fastread=''
153 reuseval=false
154 config_sh=''
155 alldone=''
156 error=''
157 silent=''
158 extractsh=''
159 override=''
160 knowitall=''
161 rm -f optdef.sh posthint.sh
162 cat >optdef.sh <<EOS
163 $startsh
164 EOS
165
166 ?X:
167 ?X: Given that we now have the possibility to execute Configure remotely
168 ?X: thanks to the new src.U support, we have to face the possibility
169 ?X: of having to ask where the source lie, which means we need the Myread.U
170 ?X: stuff and possibly other things that might echo something on the
171 ?X: screen...
172 ?X:
173 ?X: That's not pretty, and might be confusing in 99% of the time. So...
174 ?X: We introduce a new realsilent variable which is set when -s is given,
175 ?X: and we force silent=true if -S is supplied. The Extractall.U unit
176 ?X: will then undo the >&4 redirection based on the value of the
177 ?X: realsilent variable... -- RAM, 18/93/96
178 ?X:
179
180 : option parsing
181 while test $# -gt 0; do
182         case "$1" in
183         -d) shift; fastread=yes;;
184         -e) shift; alldone=cont;;
185         -f)
186                 shift
187                 cd ..
188                 if test -r "$1"; then
189                         config_sh="$1"
190                 else
191                         echo "$me: cannot read config file $1." >&2
192                         error=true
193                 fi
194                 cd UU
195                 shift;;
196         --help|\
197         -h) shift; error=true;;
198         -r) shift; reuseval=true;;
199         -s) shift; silent=true; realsilent=true;;
200         -E) shift; alldone=exit;;
201         -K) shift; knowitall=true;;
202         -O) shift; override=true;;
203         -S) shift; silent=true; extractsh=true;;
204         -D)
205                 shift
206                 case "$1" in
207                 *=)
208                         echo "$me: use '-U symbol=', not '-D symbol='." >&2
209                         echo "$me: ignoring -D $1" >&2
210                         ;;
211                 *=*) echo "$1" | \
212                                 sed -e "s/'/'\"'\"'/g" -e "s/=\(.*\)/='\1'/" >> optdef.sh;;
213                 *) echo "$1='define'" >> optdef.sh;;
214                 esac
215                 shift
216                 ;;
217         -U)
218                 shift
219                 case "$1" in
220                 *=) echo "$1" >> optdef.sh;;
221                 *=*)
222                         echo "$me: use '-D symbol=val', not '-U symbol=val'." >&2
223                         echo "$me: ignoring -U $1" >&2
224                         ;;
225                 *) echo "$1='undef'" >> optdef.sh;;
226                 esac
227                 shift
228                 ;;
229         -A)
230             shift
231             xxx=''
232             yyy="$1"
233             zzz=''
234             uuu=undef
235             case "$yyy" in
236             *=*) zzz=`echo "$yyy"|sed 's!=.*!!'`
237                  case "$zzz" in
238                  *:*) zzz='' ;;
239                  *)   xxx=append
240                       zzz=" "`echo "$yyy"|sed 's!^[^=]*=!!'` 
241                       yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
242                  esac
243                  ;;
244             esac
245             case "$xxx" in
246             '')  case "$yyy" in
247                  *:*) xxx=`echo "$yyy"|sed 's!:.*!!'`
248                       yyy=`echo "$yyy"|sed 's!^[^:]*:!!'`
249                       zzz=`echo "$yyy"|sed 's!^[^=]*=!!'`
250                       yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
251                  *)   xxx=`echo "$yyy"|sed 's!:.*!!'`
252                       yyy=`echo "$yyy"|sed 's!^[^:]*:!!'` ;;
253                  esac
254                  ;;       
255             esac
256             case "$xxx" in
257             append)
258                 echo "$yyy=\"\${$yyy}$zzz\""    >> posthint.sh ;;
259             clear)
260                 echo "$yyy=''"                  >> posthint.sh ;;
261             define)
262                 case "$zzz" in
263                 '') zzz=define ;;
264                 esac
265                 echo "$yyy='$zzz'"              >> posthint.sh ;;
266             eval)
267                 echo "eval \"$yyy=$zzz\""       >> posthint.sh ;;
268             prepend)
269                 echo "$yyy=\"$zzz\${$yyy}\""    >> posthint.sh ;;
270             undef)
271                 case "$zzz" in
272                 '') zzz="$uuu" ;;
273                 esac
274                 echo "$yyy=$zzz"                >> posthint.sh ;;
275             *)  echo "$me: unknown -A command '$xxx', ignoring -A $1" >&2 ;;
276             esac
277             shift
278             ;;
279         -V) echo "$me generated by metaconfig <VERSION> PL<PATCHLEVEL>." >&2
280             exit 0;;
281         --) break;;
282         -*) echo "$me: unknown option $1" >&2; shift; error=true;;
283         *) break;;
284         esac
285 done
286
287 case "$error" in
288 true)
289         cat >&2 <<EOM
290 Usage: $me [-dehrsEKOSV] [-f config.sh] [-D symbol] [-D symbol=value]
291                  [-U symbol] [-U symbol=] [-A command:symbol...]
292   -d : use defaults for all answers.
293   -e : go on without questioning past the production of config.sh.
294   -f : specify an alternate default configuration file.
295   -h : print this help message and exit (with an error status).
296   -r : reuse C symbols value if possible (skips costly nm extraction).
297   -s : silent mode, only echoes questions and essential information.
298   -D : define symbol to have some value:
299          -D symbol         symbol gets the value 'define'
300          -D symbol=value   symbol gets the value 'value'
301        common used examples (see INSTALL for more info):
302          -Duse64bitint            use 64bit integers
303          -Duse64bitall            use 64bit integers and pointers
304          -Dusethreads             use thread support
305          -Dinc_version_list=none  do not include older perl trees in @INC
306          -DEBUGGING=none          DEBUGGING options
307          -Dcc=gcc                 choose your compiler
308          -Dprefix=/opt/perl5      choose your destination
309   -E : stop at the end of questions, after having produced config.sh.
310   -K : do not use unless you know what you are doing.
311   -O : let -D and -U override definitions from loaded configuration file.
312   -S : perform variable substitutions on all .SH files (can mix with -f)
313   -U : undefine symbol:
314          -U symbol    symbol gets the value 'undef'
315          -U symbol=   symbol gets completely empty
316        e.g.:  -Uversiononly
317   -A : manipulate symbol after the platform specific hints have been applied:
318          -A append:symbol=value   append value to symbol
319          -A symbol=value          like append:, but with a separating space
320          -A define:symbol=value   define symbol to have value
321          -A clear:symbol          define symbol to be ''
322          -A define:symbol         define symbol to be 'define'
323          -A eval:symbol=value     define symbol to be eval of value
324          -A prepend:symbol=value  prepend value to symbol
325          -A undef:symbol          define symbol to be 'undef'
326          -A undef:symbol=         define symbol to be ''
327        e.g.:  -A prepend:libswanted='cl pthread '
328               -A ccflags=-DSOME_MACRO
329   -V : print version number and exit (with a zero status).
330 EOM
331         exit 1
332         ;;
333 esac
334
335 ?X:
336 ?X: Unless they specified either -S or both -d and -e/E, make sure we're
337 ?X: running interactively, i.e. attached to a terminal. Moved from Head.U to
338 ?X: be able to handle batch configurations...
339 ?X:
340 ?X: We have to hardwire the Configure name and cannot use $me, since if they
341 ?X: said 'sh <Configure', then $me is 'sh'...
342 ?X:
343 : Sanity checks
344 case "$fastread$alldone" in
345 yescont|yesexit) ;;
346 *)
347         case "$extractsh" in
348         true) ;;
349         *)
350                 if test ! -t 0; then
351                         echo "Say 'sh Configure', not 'sh <Configure'"
352                         exit 1
353                 fi
354                 ;;
355         esac
356         ;;
357 esac
358
359 ?X: In silent mode, the standard output is closed. Questions are asked by
360 ?X: outputing on file descriptor #4, which is the original stdout descriptor.
361 ?X: This filters out all the "junk", since all the needed information is written
362 ?X: on #4. Note that ksh will not let us redirect output if the file descriptor
363 ?X: has not be defined yet, unlike sh, hence the following line...--RAM.
364 exec 4>&1
365 case "$silent" in
366 true) exec 1>/dev/null;;
367 esac
368
369 : run the defines and the undefines, if any, but leave the file out there...
370 touch optdef.sh
371 . ./optdef.sh
372 : create the posthint manipulation script and leave the file out there...
373 touch posthint.sh
374