OSDN Git Service

0bd52d5706eb069bbb0d2169108385e251725c54
[android-x86/external-mksh.git] / src / Build.sh
1 #!/bin/sh
2 srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.695 2016/01/02 20:11:31 tg Exp $'
3 #-
4 # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5 #               2011, 2012, 2013, 2014, 2015, 2016
6 #       mirabilos <m@mirbsd.org>
7 #
8 # Provided that these terms and disclaimer and all copyright notices
9 # are retained or reproduced in an accompanying document, permission
10 # is granted to deal in this work without restriction, including un-
11 # limited rights to use, publicly perform, distribute, sell, modify,
12 # merge, give away, or sublicence.
13 #
14 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 # the utmost extent permitted by applicable law, neither express nor
16 # implied; without malicious intent or gross negligence. In no event
17 # may a licensor, author or contributor be held liable for indirect,
18 # direct, other damage, loss, or other issues arising in any way out
19 # of dealing in the work, even if advised of the possibility of such
20 # damage or existence of a defect, except proven that it results out
21 # of said person's immediate fault when using the work as intended.
22 #-
23 # People analysing the output must whitelist conftest.c for any kind
24 # of compiler warning checks (mirtoconf is by design not quiet).
25 #
26 # Used environment documentation is at the end of this file.
27
28 LC_ALL=C
29 export LC_ALL
30
31 case $ZSH_VERSION:$VERSION in
32 :zsh*) ZSH_VERSION=2 ;;
33 esac
34
35 if test -n "${ZSH_VERSION+x}" && (emulate sh) >/dev/null 2>&1; then
36         emulate sh
37         NULLCMD=:
38 fi
39
40 if test -d /usr/xpg4/bin/. >/dev/null 2>&1; then
41         # Solaris: some of the tools have weird behaviour, use portable ones
42         PATH=/usr/xpg4/bin:$PATH
43         export PATH
44 fi
45
46 nl='
47 '
48 safeIFS='       '
49 safeIFS=" $safeIFS$nl"
50 IFS=$safeIFS
51 allu=QWERTYUIOPASDFGHJKLZXCVBNM
52 alll=qwertyuiopasdfghjklzxcvbnm
53 alln=0123456789
54 alls=______________________________________________________________
55
56 genopt_die() {
57         if test -n "$1"; then
58                 echo >&2 "E: $*"
59                 echo >&2 "E: in '$srcfile': '$line'"
60         else
61                 echo >&2 "E: invalid input in '$srcfile': '$line'"
62         fi
63         rm -f "$bn.gen"
64         exit 1
65 }
66
67 genopt_soptc() {
68         optc=`echo "$line" | sed 's/^[<>]\(.\).*$/\1/'`
69         test x"$optc" = x'|' && return
70         optclo=`echo "$optc" | tr $allu $alll`
71         if test x"$optc" = x"$optclo"; then
72                 islo=1
73         else
74                 islo=0
75         fi
76         sym=`echo "$line" | sed 's/^[<>]/|/'`
77         o_str=$o_str$nl"<$optclo$islo$sym"
78 }
79
80 genopt_scond() {
81         case x$cond in
82         x)
83                 cond=
84                 ;;
85         x*' '*)
86                 cond=`echo "$cond" | sed 's/^ //'`
87                 cond="#if $cond"
88                 ;;
89         x'!'*)
90                 cond=`echo "$cond" | sed 's/^!//'`
91                 cond="#ifndef $cond"
92                 ;;
93         x*)
94                 cond="#ifdef $cond"
95                 ;;
96         esac
97 }
98
99 do_genopt() {
100         srcfile=$1
101         test -f "$srcfile" || genopt_die Source file \$srcfile not set.
102         bn=`basename "$srcfile" | sed 's/.opt$//'`
103         o_hdr='/* +++ GENERATED FILE +++ DO NOT EDIT +++ */'
104         o_gen=
105         o_str=
106         o_sym=
107         ddefs=
108         state=0
109         exec <"$srcfile"
110         IFS=
111         while IFS= read line; do
112                 IFS=$safeIFS
113                 case $state:$line in
114                 2:'|'*)
115                         # end of input
116                         o_sym=`echo "$line" | sed 's/^.//'`
117                         o_gen=$o_gen$nl"#undef F0"
118                         o_gen=$o_gen$nl"#undef FN"
119                         o_gen=$o_gen$ddefs
120                         state=3
121                         ;;
122                 1:@@)
123                         # begin of data block
124                         o_gen=$o_gen$nl"#endif"
125                         o_gen=$o_gen$nl"#ifndef F0"
126                         o_gen=$o_gen$nl"#define F0 FN"
127                         o_gen=$o_gen$nl"#endif"
128                         state=2
129                         ;;
130                 *:@@*)
131                         genopt_die ;;
132                 0:/\*-|0:\ \**|0:)
133                         o_hdr=$o_hdr$nl$line
134                         ;;
135                 0:@*|1:@*)
136                         # begin of a definition block
137                         sym=`echo "$line" | sed 's/^@//'`
138                         if test $state = 0; then
139                                 o_gen=$o_gen$nl"#if defined($sym)"
140                         else
141                                 o_gen=$o_gen$nl"#elif defined($sym)"
142                         fi
143                         ddefs="$ddefs$nl#undef $sym"
144                         state=1
145                         ;;
146                 0:*|3:*)
147                         genopt_die ;;
148                 1:*)
149                         # definition line
150                         o_gen=$o_gen$nl$line
151                         ;;
152                 2:'<'*'|'*)
153                         genopt_soptc
154                         ;;
155                 2:'>'*'|'*)
156                         genopt_soptc
157                         cond=`echo "$line" | sed 's/^[^|]*|//'`
158                         genopt_scond
159                         case $optc in
160                         '|') optc=0 ;;
161                         *) optc=\'$optc\' ;;
162                         esac
163                         IFS= read line || genopt_die Unexpected EOF
164                         IFS=$safeIFS
165                         test -n "$cond" && o_gen=$o_gen$nl"$cond"
166                         o_gen=$o_gen$nl"$line, $optc)"
167                         test -n "$cond" && o_gen=$o_gen$nl"#endif"
168                         ;;
169                 esac
170         done
171         case $state:$o_sym in
172         3:) genopt_die Expected optc sym at EOF ;;
173         3:*) ;;
174         *) genopt_die Missing EOF marker ;;
175         esac
176         echo "$o_str" | sort | while IFS='|' read x opts cond; do
177                 IFS=$safeIFS
178                 test -n "$x" || continue
179                 genopt_scond
180                 test -n "$cond" && echo "$cond"
181                 echo "\"$opts\""
182                 test -n "$cond" && echo "#endif"
183         done | {
184                 echo "$o_hdr"
185                 echo "#ifndef $o_sym$o_gen"
186                 echo "#else"
187                 cat
188                 echo "#undef $o_sym"
189                 echo "#endif"
190         } >"$bn.gen"
191         IFS=$safeIFS
192         return 0
193 }
194
195 if test x"$BUILDSH_RUN_GENOPT" = x"1"; then
196         set x -G "$srcfile"
197         shift
198 fi
199 if test x"$1" = x"-G"; then
200         do_genopt "$2"
201         exit $?
202 fi
203
204 echo "For the build logs, demonstrate that /dev/null and /dev/tty exist:"
205 ls -l /dev/null /dev/tty
206
207 v() {
208         $e "$*"
209         eval "$@"
210 }
211
212 vv() {
213         _c=$1
214         shift
215         $e "\$ $*" 2>&1
216         eval "$@" >vv.out 2>&1
217         sed "s\a^\a${_c} \a" <vv.out
218 }
219
220 vq() {
221         eval "$@"
222 }
223
224 rmf() {
225         for _f in "$@"; do
226                 case $_f in
227                 Build.sh|check.pl|check.t|dot.mkshrc|*.1|*.c|*.h|*.ico|*.opt) ;;
228                 *) rm -f "$_f" ;;
229                 esac
230         done
231 }
232
233 tcfn=no
234 bi=
235 ui=
236 ao=
237 fx=
238 me=`basename "$0"`
239 orig_CFLAGS=$CFLAGS
240 phase=x
241 oldish_ed=stdout-ed,no-stderr-ed
242
243 if test -t 1; then
244         bi='\e[1m'
245         ui='\e[4m'
246         ao='\e[0m'
247 fi
248
249 upper() {
250         echo :"$@" | sed 's/^://' | tr $alll $allu
251 }
252
253 # clean up after ac_testrun()
254 ac_testdone() {
255         eval HAVE_$fu=$fv
256         fr=no
257         test 0 = $fv || fr=yes
258         $e "$bi==> $fd...$ao $ui$fr$ao$fx"
259         fx=
260 }
261
262 # ac_cache label: sets f, fu, fv?=0
263 ac_cache() {
264         f=$1
265         fu=`upper $f`
266         eval fv=\$HAVE_$fu
267         case $fv in
268         0|1)
269                 fx=' (cached)'
270                 return 0
271                 ;;
272         esac
273         fv=0
274         return 1
275 }
276
277 # ac_testinit label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
278 # returns 1 if value was cached/implied, 0 otherwise: call ac_testdone
279 ac_testinit() {
280         if ac_cache $1; then
281                 test x"$2" = x"!" && shift
282                 test x"$2" = x"" || shift
283                 fd=${3-$f}
284                 ac_testdone
285                 return 1
286         fi
287         fc=0
288         if test x"$2" = x""; then
289                 ft=1
290         else
291                 if test x"$2" = x"!"; then
292                         fc=1
293                         shift
294                 fi
295                 eval ft=\$HAVE_`upper $2`
296                 shift
297         fi
298         fd=${3-$f}
299         if test $fc = "$ft"; then
300                 fv=$2
301                 fx=' (implied)'
302                 ac_testdone
303                 return 1
304         fi
305         $e ... $fd
306         return 0
307 }
308
309 # pipe .c | ac_test[n] [!] label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
310 ac_testnnd() {
311         if test x"$1" = x"!"; then
312                 fr=1
313                 shift
314         else
315                 fr=0
316         fi
317         ac_testinit "$@" || return 1
318         cat >conftest.c
319         vv ']' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN conftest.c $LIBS $ccpr"
320         test $tcfn = no && test -f a.out && tcfn=a.out
321         test $tcfn = no && test -f a.exe && tcfn=a.exe
322         test $tcfn = no && test -f conftest.exe && tcfn=conftest.exe
323         test $tcfn = no && test -f conftest && tcfn=conftest
324         if test -f $tcfn; then
325                 test 1 = $fr || fv=1
326         else
327                 test 0 = $fr || fv=1
328         fi
329         vscan=
330         if test $phase = u; then
331                 test $ct = gcc && vscan='unrecogni[sz]ed'
332                 test $ct = hpcc && vscan='unsupported'
333                 test $ct = pcc && vscan='unsupported'
334                 test $ct = sunpro && vscan='-e ignored -e turned.off'
335         fi
336         test -n "$vscan" && grep $vscan vv.out >/dev/null 2>&1 && fv=$fr
337         return 0
338 }
339 ac_testn() {
340         ac_testnnd "$@" || return
341         rmf conftest.c conftest.o ${tcfn}* vv.out
342         ac_testdone
343 }
344
345 # ac_ifcpp cppexpr [!] label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
346 ac_ifcpp() {
347         expr=$1; shift
348         ac_testn "$@" <<-EOF
349                 #include <unistd.h>
350                 extern int thiswillneverbedefinedIhope(void);
351                 int main(void) { return (isatty(0) +
352                 #$expr
353                     0
354                 #else
355                 /* force a failure: expr is false */
356                     thiswillneverbedefinedIhope()
357                 #endif
358                     ); }
359 EOF
360         test x"$1" = x"!" && shift
361         f=$1
362         fu=`upper $f`
363         eval fv=\$HAVE_$fu
364         test x"$fv" = x"1"
365 }
366
367 add_cppflags() {
368         CPPFLAGS="$CPPFLAGS $*"
369 }
370
371 ac_cppflags() {
372         test x"$1" = x"" || fu=$1
373         fv=$2
374         test x"$2" = x"" && eval fv=\$HAVE_$fu
375         add_cppflags -DHAVE_$fu=$fv
376 }
377
378 ac_test() {
379         ac_testn "$@"
380         ac_cppflags
381 }
382
383 # ac_flags [-] add varname cflags [text] [ldflags]
384 ac_flags() {
385         if test x"$1" = x"-"; then
386                 shift
387                 hf=1
388         else
389                 hf=0
390         fi
391         fa=$1
392         vn=$2
393         f=$3
394         ft=$4
395         fl=$5
396         test x"$ft" = x"" && ft="if $f can be used"
397         save_CFLAGS=$CFLAGS
398         CFLAGS="$CFLAGS $f"
399         if test -n "$fl"; then
400                 save_LDFLAGS=$LDFLAGS
401                 LDFLAGS="$LDFLAGS $fl"
402         fi
403         if test 1 = $hf; then
404                 ac_testn can_$vn '' "$ft"
405         else
406                 ac_testn can_$vn '' "$ft" <<-'EOF'
407                         /* evil apo'stroph in comment test */
408                         #include <unistd.h>
409                         int main(void) { return (isatty(0)); }
410                 EOF
411         fi
412         eval fv=\$HAVE_CAN_`upper $vn`
413         if test -n "$fl"; then
414                 test 11 = $fa$fv || LDFLAGS=$save_LDFLAGS
415         fi
416         test 11 = $fa$fv || CFLAGS=$save_CFLAGS
417 }
418
419 # ac_header [!] header [prereq ...]
420 ac_header() {
421         if test x"$1" = x"!"; then
422                 na=1
423                 shift
424         else
425                 na=0
426         fi
427         hf=$1; shift
428         hv=`echo "$hf" | tr -d '\012\015' | tr -c $alll$allu$alln $alls`
429         echo "/* NeXTstep bug workaround */" >x
430         for i
431         do
432                 case $i in
433                 _time)
434                         echo '#if HAVE_BOTH_TIME_H' >>x
435                         echo '#include <sys/time.h>' >>x
436                         echo '#include <time.h>' >>x
437                         echo '#elif HAVE_SYS_TIME_H' >>x
438                         echo '#include <sys/time.h>' >>x
439                         echo '#elif HAVE_TIME_H' >>x
440                         echo '#include <time.h>' >>x
441                         echo '#endif' >>x
442                         ;;
443                 *)
444                         echo "#include <$i>" >>x
445                         ;;
446                 esac
447         done
448         echo "#include <$hf>" >>x
449         echo '#include <unistd.h>' >>x
450         echo 'int main(void) { return (isatty(0)); }' >>x
451         ac_testn "$hv" "" "<$hf>" <x
452         rmf x
453         test 1 = $na || ac_cppflags
454 }
455
456 addsrcs() {
457         if test x"$1" = x"!"; then
458                 fr=0
459                 shift
460         else
461                 fr=1
462         fi
463         eval i=\$$1
464         test $fr = "$i" && case " $SRCS " in
465         *\ $2\ *)       ;;
466         *)              SRCS="$SRCS $2" ;;
467         esac
468 }
469
470
471 curdir=`pwd` srcdir=`dirname "$0" 2>/dev/null`
472 case x$srcdir in
473 x)
474         srcdir=.
475         ;;
476 *\ *|*" "*|*"$nl"*)
477         echo >&2 Source directory should not contain space or tab or newline.
478         echo >&2 Errors may occur.
479         ;;
480 *"'"*)
481         echo Source directory must not contain single quotes.
482         exit 1
483         ;;
484 esac
485 dstversion=`sed -n '/define MKSH_VERSION/s/^.*"\([^"]*\)".*$/\1/p' "$srcdir/sh.h"`
486 add_cppflags -DMKSH_BUILDSH
487
488 e=echo
489 r=0
490 eq=0
491 pm=0
492 cm=normal
493 optflags=-std-compile-opts
494 check_categories=
495 last=
496 tfn=
497 legacy=0
498
499 for i
500 do
501         case $last:$i in
502         c:combine|c:dragonegg|c:llvm|c:lto)
503                 cm=$i
504                 last=
505                 ;;
506         c:*)
507                 echo "$me: Unknown option -c '$i'!" >&2
508                 exit 1
509                 ;;
510         o:*)
511                 optflags=$i
512                 last=
513                 ;;
514         t:*)
515                 tfn=$i
516                 last=
517                 ;;
518         :-c)
519                 last=c
520                 ;;
521         :-G)
522                 echo "$me: Do not call me with '-G'!" >&2
523                 exit 1
524                 ;;
525         :-g)
526                 # checker, debug, valgrind build
527                 add_cppflags -DDEBUG
528                 CFLAGS="$CFLAGS -g3 -fno-builtin"
529                 ;;
530         :-j)
531                 pm=1
532                 ;;
533         :-L)
534                 legacy=1
535                 ;;
536         :+L)
537                 legacy=0
538                 ;;
539         :-M)
540                 cm=makefile
541                 ;;
542         :-O)
543                 optflags=-std-compile-opts
544                 ;;
545         :-o)
546                 last=o
547                 ;;
548         :-Q)
549                 eq=1
550                 ;;
551         :-r)
552                 r=1
553                 ;;
554         :-t)
555                 last=t
556                 ;;
557         :-v)
558                 echo "Build.sh $srcversion"
559                 echo "for mksh $dstversion"
560                 exit 0
561                 ;;
562         :*)
563                 echo "$me: Unknown option '$i'!" >&2
564                 exit 1
565                 ;;
566         *)
567                 echo "$me: Unknown option -'$last' '$i'!" >&2
568                 exit 1
569                 ;;
570         esac
571 done
572 if test -n "$last"; then
573         echo "$me: Option -'$last' not followed by argument!" >&2
574         exit 1
575 fi
576
577 test -z "$tfn" && if test $legacy = 0; then
578         tfn=mksh
579 else
580         tfn=lksh
581 fi
582 if test -d $tfn || test -d $tfn.exe; then
583         echo "$me: Error: ./$tfn is a directory!" >&2
584         exit 1
585 fi
586 rmf a.exe* a.out* conftest.c conftest.exe* *core core.* ${tfn}* *.bc *.dbg \
587     *.ll *.o *.gen Rebuild.sh lft no signames.inc test.sh x vv.out
588
589 SRCS="lalloc.c eval.c exec.c expr.c funcs.c histrap.c jobs.c"
590 SRCS="$SRCS lex.c main.c misc.c shf.c syn.c tree.c var.c"
591
592 if test $legacy = 0; then
593         SRCS="$SRCS edit.c"
594         check_categories="$check_categories shell:legacy-no int:32"
595 else
596         check_categories="$check_categories shell:legacy-yes"
597         add_cppflags -DMKSH_LEGACY_MODE
598         HAVE_PERSISTENT_HISTORY=0
599         HAVE_ISSET_MKSH_CONSERVATIVE_FDS=1      # from sh.h
600 fi
601
602 if test x"$srcdir" = x"."; then
603         CPPFLAGS="-I. $CPPFLAGS"
604 else
605         CPPFLAGS="-I. -I'$srcdir' $CPPFLAGS"
606 fi
607 test -n "$LDSTATIC" && if test -n "$LDFLAGS"; then
608         LDFLAGS="$LDFLAGS $LDSTATIC"
609 else
610         LDFLAGS=$LDSTATIC
611 fi
612
613 if test -z "$TARGET_OS"; then
614         x=`uname -s 2>/dev/null || uname`
615         test x"$x" = x"`uname -n 2>/dev/null`" || TARGET_OS=$x
616 fi
617 if test -z "$TARGET_OS"; then
618         echo "$me: Set TARGET_OS, your uname is broken!" >&2
619         exit 1
620 fi
621 oswarn=
622 ccpc=-Wc,
623 ccpl=-Wl,
624 tsts=
625 ccpr='|| for _f in ${tcfn}*; do case $_f in Build.sh|check.pl|check.t|dot.mkshrc|*.1|*.c|*.h|*.ico|*.opt) ;; *) rm -f "$_f" ;; esac; done'
626
627 # Evil hack
628 if test x"$TARGET_OS" = x"Android"; then
629         check_categories="$check_categories android"
630         TARGET_OS=Linux
631 fi
632
633 # Evil OS
634 if test x"$TARGET_OS" = x"Minix"; then
635         echo >&2 "
636 WARNING: additional checks before running Build.sh required!
637 You can avoid these by calling Build.sh correctly, see below.
638 "
639         cat >conftest.c <<'EOF'
640 #include <sys/types.h>
641 const char *
642 #ifdef _NETBSD_SOURCE
643 ct="Ninix3"
644 #else
645 ct="Minix3"
646 #endif
647 ;
648 EOF
649         ct=unknown
650         vv ']' "${CC-cc} -E $CFLAGS $CPPFLAGS $NOWARN conftest.c | grep ct= | tr -d \\\\015 >x"
651         sed 's/^/[ /' x
652         eval `cat x`
653         rmf x vv.out
654         case $ct in
655         Minix3|Ninix3)
656                 echo >&2 "
657 Warning: you set TARGET_OS to $TARGET_OS but that is ambiguous.
658 Please set it to either Minix3 or Ninix3, whereas the latter is
659 all versions of Minix with even partial NetBSD(R) userland. The
660 value determined from your compiler for the current compilation
661 (which may be wrong) is: $ct
662 "
663                 TARGET_OS=$ct
664                 ;;
665         *)
666                 echo >&2 "
667 Warning: you set TARGET_OS to $TARGET_OS but that is ambiguous.
668 Please set it to either Minix3 or Ninix3, whereas the latter is
669 all versions of Minix with even partial NetBSD(R) userland. The
670 proper value couldn't be determined, continue at your own risk.
671 "
672                 ;;
673         esac
674 fi
675
676 # Configuration depending on OS revision, on OSes that need them
677 case $TARGET_OS in
678 NEXTSTEP)
679         test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`hostinfo 2>&1 | \
680             grep 'NeXT Mach [0-9][0-9.]*:' | \
681             sed 's/^.*NeXT Mach \([0-9][0-9.]*\):.*$/\1/'`
682         ;;
683 QNX|SCO_SV)
684         test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`uname -r`
685         ;;
686 esac
687
688 # Configuration depending on OS name
689 case $TARGET_OS in
690 386BSD)
691         : "${HAVE_CAN_OTWO=0}"
692         add_cppflags -DMKSH_NO_SIGSETJMP
693         add_cppflags -DMKSH_TYPEDEF_SIG_ATOMIC_T=int
694         add_cppflags -DMKSH_CONSERVATIVE_FDS
695         ;;
696 AIX)
697         add_cppflags -D_ALL_SOURCE
698         : "${HAVE_SETLOCALE_CTYPE=0}"
699         ;;
700 BeOS)
701         case $KSH_VERSION in
702         *MIRBSD\ KSH*)
703                 oswarn="; it has minor issues"
704                 ;;
705         *)
706                 oswarn="; you must recompile mksh with"
707                 oswarn="$oswarn${nl}itself in a second stage"
708                 ;;
709         esac
710         # BeOS has no real tty either
711         add_cppflags -DMKSH_UNEMPLOYED
712         add_cppflags -DMKSH_DISABLE_TTY_WARNING
713         # BeOS doesn't have different UIDs and GIDs
714         add_cppflags -DMKSH__NO_SETEUGID
715         ;;
716 BSD/OS)
717         : "${HAVE_SETLOCALE_CTYPE=0}"
718         ;;
719 Coherent)
720         oswarn="; it has major issues"
721         add_cppflags -DMKSH__NO_SYMLINK
722         check_categories="$check_categories nosymlink"
723         add_cppflags -DMKSH__NO_SETEUGID
724         add_cppflags -DMKSH_CONSERVATIVE_FDS
725         add_cppflags -DMKSH_DISABLE_TTY_WARNING
726         ;;
727 CYGWIN*)
728         : "${HAVE_SETLOCALE_CTYPE=0}"
729         ;;
730 Darwin)
731         add_cppflags -D_DARWIN_C_SOURCE
732         ;;
733 DragonFly)
734         ;;
735 FreeBSD)
736         ;;
737 FreeMiNT)
738         oswarn="; it has minor issues"
739         add_cppflags -D_GNU_SOURCE
740         add_cppflags -DMKSH_CONSERVATIVE_FDS
741         : "${HAVE_SETLOCALE_CTYPE=0}"
742         ;;
743 GNU)
744         case $CC in
745         *tendracc*) ;;
746         *) add_cppflags -D_GNU_SOURCE ;;
747         esac
748         add_cppflags -DSETUID_CAN_FAIL_WITH_EAGAIN
749         # define MKSH__NO_PATH_MAX to use Hurd-only functions
750         add_cppflags -DMKSH__NO_PATH_MAX
751         ;;
752 GNU/kFreeBSD)
753         case $CC in
754         *tendracc*) ;;
755         *) add_cppflags -D_GNU_SOURCE ;;
756         esac
757         add_cppflags -DSETUID_CAN_FAIL_WITH_EAGAIN
758         ;;
759 Haiku)
760         add_cppflags -DMKSH_ASSUME_UTF8; HAVE_ISSET_MKSH_ASSUME_UTF8=1
761         ;;
762 HP-UX)
763         ;;
764 Interix)
765         ccpc='-X '
766         ccpl='-Y '
767         add_cppflags -D_ALL_SOURCE
768         : "${LIBS=-lcrypt}"
769         : "${HAVE_SETLOCALE_CTYPE=0}"
770         ;;
771 IRIX*)
772         : "${HAVE_SETLOCALE_CTYPE=0}"
773         ;;
774 Linux)
775         case $CC in
776         *tendracc*) ;;
777         *) add_cppflags -D_GNU_SOURCE ;;
778         esac
779         add_cppflags -DSETUID_CAN_FAIL_WITH_EAGAIN
780         : "${HAVE_REVOKE=0}"
781         ;;
782 LynxOS)
783         oswarn="; it has minor issues"
784         ;;
785 MidnightBSD)
786         ;;
787 Minix-vmd)
788         add_cppflags -DMKSH__NO_SETEUGID
789         add_cppflags -DMKSH_UNEMPLOYED
790         add_cppflags -DMKSH_CONSERVATIVE_FDS
791         add_cppflags -D_MINIX_SOURCE
792         oldish_ed=no-stderr-ed          # no /bin/ed, maybe see below
793         : "${HAVE_SETLOCALE_CTYPE=0}"
794         ;;
795 Minix3)
796         add_cppflags -DMKSH_UNEMPLOYED
797         add_cppflags -DMKSH_CONSERVATIVE_FDS
798         add_cppflags -DMKSH_NO_LIMITS
799         add_cppflags -D_POSIX_SOURCE -D_POSIX_1_SOURCE=2 -D_MINIX
800         oldish_ed=no-stderr-ed          # /usr/bin/ed(!) is broken
801         : "${HAVE_SETLOCALE_CTYPE=0}"
802         ;;
803 MirBSD)
804         ;;
805 MSYS_*)
806         add_cppflags -DMKSH_ASSUME_UTF8=0; HAVE_ISSET_MKSH_ASSUME_UTF8=1
807         # almost same as CYGWIN* (from RT|Chatzilla)
808         : "${HAVE_SETLOCALE_CTYPE=0}"
809         # broken on this OE (from ir0nh34d)
810         : "${HAVE_STDINT_H=0}"
811         ;;
812 NetBSD)
813         ;;
814 NEXTSTEP)
815         add_cppflags -D_NEXT_SOURCE
816         add_cppflags -D_POSIX_SOURCE
817         : "${AWK=gawk}"
818         : "${CC=cc -posix}"
819         add_cppflags -DMKSH_NO_SIGSETJMP
820         # NeXTstep cannot get a controlling tty
821         add_cppflags -DMKSH_UNEMPLOYED
822         case $TARGET_OSREV in
823         4.2*)
824                 # OpenStep 4.2 is broken by default
825                 oswarn="; it needs libposix.a"
826                 ;;
827         esac
828         add_cppflags -DMKSH_CONSERVATIVE_FDS
829         ;;
830 Ninix3)
831         # similar to Minix3
832         add_cppflags -DMKSH_UNEMPLOYED
833         add_cppflags -DMKSH_CONSERVATIVE_FDS
834         add_cppflags -DMKSH_NO_LIMITS
835         # but no idea what else could be needed
836         oswarn="; it has unknown issues"
837         ;;
838 OpenBSD)
839         : "${HAVE_SETLOCALE_CTYPE=0}"
840         ;;
841 OS/2)
842         HAVE_TERMIOS_H=0
843         HAVE_MKNOD=0    # setmode() incompatible
844         oswarn="; it is currently being ported"
845         check_categories="$check_categories nosymlink"
846         : "${CC=gcc}"
847         : "${SIZE=: size}"
848         add_cppflags -DMKSH_UNEMPLOYED
849         add_cppflags -DMKSH_NOPROSPECTOFWORK
850         ;;
851 OSF1)
852         HAVE_SIG_T=0    # incompatible
853         add_cppflags -D_OSF_SOURCE
854         add_cppflags -D_POSIX_C_SOURCE=200112L
855         add_cppflags -D_XOPEN_SOURCE=600
856         add_cppflags -D_XOPEN_SOURCE_EXTENDED
857         : "${HAVE_SETLOCALE_CTYPE=0}"
858         ;;
859 Plan9)
860         add_cppflags -D_POSIX_SOURCE
861         add_cppflags -D_LIMITS_EXTENSION
862         add_cppflags -D_BSD_EXTENSION
863         add_cppflags -D_SUSV2_SOURCE
864         add_cppflags -DMKSH_ASSUME_UTF8; HAVE_ISSET_MKSH_ASSUME_UTF8=1
865         add_cppflags -DMKSH_NO_CMDLINE_EDITING
866         add_cppflags -DMKSH__NO_SETEUGID
867         oswarn=' and will currently not work'
868         add_cppflags -DMKSH_UNEMPLOYED
869         # this is for detecting kencc
870         add_cppflags -DMKSH_MAYBE_KENCC
871         ;;
872 PW32*)
873         HAVE_SIG_T=0    # incompatible
874         oswarn=' and will currently not work'
875         : "${HAVE_SETLOCALE_CTYPE=0}"
876         ;;
877 QNX)
878         add_cppflags -D__NO_EXT_QNX
879         add_cppflags -D__EXT_UNIX_MISC
880         case $TARGET_OSREV in
881         [012345].*|6.[0123].*|6.4.[01])
882                 oldish_ed=no-stderr-ed          # oldish /bin/ed is broken
883                 ;;
884         esac
885         : "${HAVE_SETLOCALE_CTYPE=0}"
886         ;;
887 SCO_SV)
888         case $TARGET_OSREV in
889         3.2*)
890                 # SCO OpenServer 5
891                 add_cppflags -DMKSH_UNEMPLOYED
892                 ;;
893         5*)
894                 # SCO OpenServer 6
895                 ;;
896         *)
897                 oswarn='; this is an unknown version of'
898                 oswarn="$oswarn$nl$TARGET_OS ${TARGET_OSREV}, please tell me what to do"
899                 ;;
900         esac
901         add_cppflags -DMKSH_CONSERVATIVE_FDS
902         : "${HAVE_SYS_SIGLIST=0}${HAVE__SYS_SIGLIST=0}"
903         ;;
904 skyos)
905         oswarn="; it has minor issues"
906         ;;
907 SunOS)
908         add_cppflags -D_BSD_SOURCE
909         add_cppflags -D__EXTENSIONS__
910         ;;
911 syllable)
912         add_cppflags -D_GNU_SOURCE
913         add_cppflags -DMKSH_NO_SIGSUSPEND
914         oswarn=' and will currently not work'
915         ;;
916 ULTRIX)
917         : "${CC=cc -YPOSIX}"
918         add_cppflags -DMKSH_TYPEDEF_SSIZE_T=int
919         add_cppflags -DMKSH_CONSERVATIVE_FDS
920         : "${HAVE_SETLOCALE_CTYPE=0}"
921         ;;
922 UnixWare|UNIX_SV)
923         # SCO UnixWare
924         add_cppflags -DMKSH_CONSERVATIVE_FDS
925         : "${HAVE_SYS_SIGLIST=0}${HAVE__SYS_SIGLIST=0}"
926         ;;
927 UWIN*)
928         ccpc='-Yc,'
929         ccpl='-Yl,'
930         tsts=" 3<>/dev/tty"
931         oswarn="; it will compile, but the target"
932         oswarn="$oswarn${nl}platform itself is very flakey/unreliable"
933         : "${HAVE_SETLOCALE_CTYPE=0}"
934         ;;
935 _svr4)
936         # generic target for SVR4 Unix with uname -s = uname -n
937         # this duplicates the * target below
938         oswarn='; it may or may not work'
939         test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`uname -r`
940         ;;
941 *)
942         oswarn='; it may or may not work'
943         test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`uname -r`
944         ;;
945 esac
946
947 : "${HAVE_MKNOD=0}"
948
949 : "${AWK=awk}${CC=cc}${NROFF=nroff}${SIZE=size}"
950 test 0 = $r && echo | $NROFF -v 2>&1 | grep GNU >/dev/null 2>&1 && \
951     echo | $NROFF -c >/dev/null 2>&1 && NROFF="$NROFF -c"
952
953 # this aids me in tracing FTBFSen without access to the buildd
954 $e "Hi from$ao $bi$srcversion$ao on:"
955 case $TARGET_OS in
956 AIX)
957         vv '|' "oslevel >&2"
958         vv '|' "uname -a >&2"
959         ;;
960 Darwin)
961         vv '|' "hwprefs machine_type os_type os_class >&2"
962         vv '|' "sw_vers >&2"
963         vv '|' "system_profiler SPSoftwareDataType SPHardwareDataType >&2"
964         vv '|' "/bin/sh --version >&2"
965         vv '|' "xcodebuild -version >&2"
966         vv '|' "uname -a >&2"
967         vv '|' "sysctl kern.version hw.machine hw.model hw.memsize hw.availcpu hw.cpufrequency hw.byteorder hw.cpu64bit_capable >&2"
968         ;;
969 IRIX*)
970         vv '|' "uname -a >&2"
971         vv '|' "hinv -v >&2"
972         ;;
973 OSF1)
974         vv '|' "uname -a >&2"
975         vv '|' "/usr/sbin/sizer -v >&2"
976         ;;
977 SCO_SV|UnixWare|UNIX_SV)
978         vv '|' "uname -a >&2"
979         vv '|' "uname -X >&2"
980         ;;
981 *)
982         vv '|' "uname -a >&2"
983         ;;
984 esac
985 test -z "$oswarn" || echo >&2 "
986 Warning: mksh has not yet been ported to or tested on your
987 operating system '$TARGET_OS'$oswarn. If you can provide
988 a shell account to the developer, this may improve; please
989 drop us a success or failure notice or even send in diffs.
990 "
991 $e "$bi$me: Building the MirBSD Korn Shell$ao $ui$dstversion$ao on $TARGET_OS ${TARGET_OSREV}..."
992
993 #
994 # Begin of mirtoconf checks
995 #
996 $e $bi$me: Scanning for functions... please ignore any errors.$ao
997
998 #
999 # Compiler: which one?
1000 #
1001 # notes:
1002 # - ICC defines __GNUC__ too
1003 # - GCC defines __hpux too
1004 # - LLVM+clang defines __GNUC__ too
1005 # - nwcc defines __GNUC__ too
1006 CPP="$CC -E"
1007 $e ... which compiler seems to be used
1008 cat >conftest.c <<'EOF'
1009 const char *
1010 #if defined(__ICC) || defined(__INTEL_COMPILER)
1011 ct="icc"
1012 #elif defined(__xlC__) || defined(__IBMC__)
1013 ct="xlc"
1014 #elif defined(__SUNPRO_C)
1015 ct="sunpro"
1016 #elif defined(__ACK__)
1017 ct="ack"
1018 #elif defined(__BORLANDC__)
1019 ct="bcc"
1020 #elif defined(__WATCOMC__)
1021 ct="watcom"
1022 #elif defined(__MWERKS__)
1023 ct="metrowerks"
1024 #elif defined(__HP_cc)
1025 ct="hpcc"
1026 #elif defined(__DECC) || (defined(__osf__) && !defined(__GNUC__))
1027 ct="dec"
1028 #elif defined(__PGI)
1029 ct="pgi"
1030 #elif defined(__DMC__)
1031 ct="dmc"
1032 #elif defined(_MSC_VER)
1033 ct="msc"
1034 #elif defined(__ADSPBLACKFIN__) || defined(__ADSPTS__) || defined(__ADSP21000__)
1035 ct="adsp"
1036 #elif defined(__IAR_SYSTEMS_ICC__)
1037 ct="iar"
1038 #elif defined(SDCC)
1039 ct="sdcc"
1040 #elif defined(__PCC__)
1041 ct="pcc"
1042 #elif defined(__TenDRA__)
1043 ct="tendra"
1044 #elif defined(__TINYC__)
1045 ct="tcc"
1046 #elif defined(__llvm__) && defined(__clang__)
1047 ct="clang"
1048 #elif defined(__NWCC__)
1049 ct="nwcc"
1050 #elif defined(__GNUC__)
1051 ct="gcc"
1052 #elif defined(_COMPILER_VERSION)
1053 ct="mipspro"
1054 #elif defined(__sgi)
1055 ct="mipspro"
1056 #elif defined(__hpux) || defined(__hpua)
1057 ct="hpcc"
1058 #elif defined(__ultrix)
1059 ct="ucode"
1060 #elif defined(__USLC__)
1061 ct="uslc"
1062 #elif defined(__LCC__)
1063 ct="lcc"
1064 #elif defined(MKSH_MAYBE_KENCC)
1065 /* and none of the above matches */
1066 ct="kencc"
1067 #else
1068 ct="unknown"
1069 #endif
1070 ;
1071 const char *
1072 #if defined(__KLIBC__) && !defined(__OS2__)
1073 et="klibc"
1074 #else
1075 et="unknown"
1076 #endif
1077 ;
1078 EOF
1079 ct=untested
1080 et=untested
1081 vv ']' "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c | \
1082     sed -n '/^ *[ce]t *= */s/^ *\([ce]t\) *= */\1=/p' | tr -d \\\\015 >x"
1083 sed 's/^/[ /' x
1084 eval `cat x`
1085 rmf x vv.out
1086 cat >conftest.c <<'EOF'
1087 #include <unistd.h>
1088 int main(void) { return (isatty(0)); }
1089 EOF
1090 case $ct in
1091 ack)
1092         # work around "the famous ACK const bug"
1093         CPPFLAGS="-Dconst= $CPPFLAGS"
1094         ;;
1095 adsp)
1096         echo >&2 'Warning: Analog Devices C++ compiler for Blackfin, TigerSHARC
1097     and SHARC (21000) DSPs detected. This compiler has not yet
1098     been tested for compatibility with mksh. Continue at your
1099     own risk, please report success/failure to the developers.'
1100         ;;
1101 bcc)
1102         echo >&2 "Warning: Borland C++ Builder detected. This compiler might
1103     produce broken executables. Continue at your own risk,
1104     please report success/failure to the developers."
1105         ;;
1106 clang)
1107         # does not work with current "ccc" compiler driver
1108         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
1109         # one of these two works, for now
1110         vv '|' "${CLANG-clang} -version"
1111         vv '|' "${CLANG-clang} --version"
1112         # ensure compiler and linker are in sync unless overridden
1113         case $CCC_CC:$CCC_LD in
1114         :*)     ;;
1115         *:)     CCC_LD=$CCC_CC; export CCC_LD ;;
1116         esac
1117         ;;
1118 dec)
1119         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
1120         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -Wl,-V conftest.c $LIBS"
1121         ;;
1122 dmc)
1123         echo >&2 "Warning: Digital Mars Compiler detected. When running under"
1124         echo >&2 "    UWIN, mksh tends to be unstable due to the limitations"
1125         echo >&2 "    of this platform. Continue at your own risk,"
1126         echo >&2 "    please report success/failure to the developers."
1127         ;;
1128 gcc)
1129         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1130         vv '|' 'echo `$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS \
1131             -dumpmachine` gcc`$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN \
1132             $LIBS -dumpversion`'
1133         ;;
1134 hpcc)
1135         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
1136         ;;
1137 iar)
1138         echo >&2 'Warning: IAR Systems (http://www.iar.com) compiler for embedded
1139     systems detected. This unsupported compiler has not yet
1140     been tested for compatibility with mksh. Continue at your
1141     own risk, please report success/failure to the developers.'
1142         ;;
1143 icc)
1144         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
1145         ;;
1146 kencc)
1147         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1148         ;;
1149 lcc)
1150         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1151         add_cppflags -D__inline__=__inline
1152         ;;
1153 metrowerks)
1154         echo >&2 'Warning: Metrowerks C compiler detected. This has not yet
1155     been tested for compatibility with mksh. Continue at your
1156     own risk, please report success/failure to the developers.'
1157         ;;
1158 mipspro)
1159         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
1160         ;;
1161 msc)
1162         ccpr=           # errorlevels are not reliable
1163         case $TARGET_OS in
1164         Interix)
1165                 if [[ -n $C89_COMPILER ]]; then
1166                         C89_COMPILER=`ntpath2posix -c "$C89_COMPILER"`
1167                 else
1168                         C89_COMPILER=CL.EXE
1169                 fi
1170                 if [[ -n $C89_LINKER ]]; then
1171                         C89_LINKER=`ntpath2posix -c "$C89_LINKER"`
1172                 else
1173                         C89_LINKER=LINK.EXE
1174                 fi
1175                 vv '|' "$C89_COMPILER /HELP >&2"
1176                 vv '|' "$C89_LINKER /LINK >&2"
1177                 ;;
1178         esac
1179         ;;
1180 nwcc)
1181         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
1182         ;;
1183 pcc)
1184         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -v"
1185         ;;
1186 pgi)
1187         echo >&2 'Warning: PGI detected. This unknown compiler has not yet
1188     been tested for compatibility with mksh. Continue at your
1189     own risk, please report success/failure to the developers.'
1190         ;;
1191 sdcc)
1192         echo >&2 'Warning: sdcc (http://sdcc.sourceforge.net), the small devices
1193     C compiler for embedded systems detected. This has not yet
1194     been tested for compatibility with mksh. Continue at your
1195     own risk, please report success/failure to the developers.'
1196         ;;
1197 sunpro)
1198         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
1199         ;;
1200 tcc)
1201         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -v"
1202         ;;
1203 tendra)
1204         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V 2>&1 | \
1205             grep -F -i -e version -e release"
1206         ;;
1207 ucode)
1208         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
1209         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -Wl,-V conftest.c $LIBS"
1210         ;;
1211 uslc)
1212         case $TARGET_OS:$TARGET_OSREV in
1213         SCO_SV:3.2*)
1214                 # SCO OpenServer 5
1215                 CFLAGS="$CFLAGS -g"
1216                 : "${HAVE_CAN_OTWO=0}${HAVE_CAN_OPTIMISE=0}"
1217                 ;;
1218         esac
1219         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
1220         ;;
1221 watcom)
1222         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1223         ;;
1224 xlc)
1225         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -qversion"
1226         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -qversion=verbose"
1227         vv '|' "ld -V"
1228         ;;
1229 *)
1230         test x"$ct" = x"untested" && $e "!!! detecting preprocessor failed"
1231         ct=unknown
1232         vv "$CC --version"
1233         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1234         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
1235         ;;
1236 esac
1237 case $cm in
1238 dragonegg|llvm)
1239         vv '|' "llc -version"
1240         ;;
1241 esac
1242 etd=" on $et"
1243 case $et in
1244 klibc)
1245         add_cppflags -DMKSH_NO_LIMITS
1246         ;;
1247 unknown)
1248         # nothing special detected, don’t worry
1249         etd=
1250         ;;
1251 *)
1252         # huh?
1253         ;;
1254 esac
1255 $e "$bi==> which compiler seems to be used...$ao $ui$ct$etd$ao"
1256 rmf conftest.c conftest.o conftest a.out* a.exe* conftest.exe* vv.out
1257
1258 #
1259 # Compiler: works as-is, with -Wno-error and -Werror
1260 #
1261 save_NOWARN=$NOWARN
1262 NOWARN=
1263 DOWARN=
1264 ac_flags 0 compiler_works '' 'if the compiler works'
1265 test 1 = $HAVE_CAN_COMPILER_WORKS || exit 1
1266 HAVE_COMPILER_KNOWN=0
1267 test $ct = unknown || HAVE_COMPILER_KNOWN=1
1268 if ac_ifcpp 'if 0' compiler_fails '' \
1269     'if the compiler does not fail correctly'; then
1270         save_CFLAGS=$CFLAGS
1271         : "${HAVE_CAN_DELEXE=x}"
1272         case $ct in
1273         dec)
1274                 CFLAGS="$CFLAGS ${ccpl}-non_shared"
1275                 ac_testn can_delexe compiler_fails 0 'for the -non_shared linker option' <<-EOF
1276                         #include <unistd.h>
1277                         int main(void) { return (isatty(0)); }
1278                 EOF
1279                 ;;
1280         dmc)
1281                 CFLAGS="$CFLAGS ${ccpl}/DELEXECUTABLE"
1282                 ac_testn can_delexe compiler_fails 0 'for the /DELEXECUTABLE linker option' <<-EOF
1283                         #include <unistd.h>
1284                         int main(void) { return (isatty(0)); }
1285                 EOF
1286                 ;;
1287         *)
1288                 exit 1
1289                 ;;
1290         esac
1291         test 1 = $HAVE_CAN_DELEXE || CFLAGS=$save_CFLAGS
1292         ac_testn compiler_still_fails '' 'if the compiler still does not fail correctly' <<-EOF
1293         EOF
1294         test 1 = $HAVE_COMPILER_STILL_FAILS && exit 1
1295 fi
1296 if ac_ifcpp 'ifdef __TINYC__' couldbe_tcc '!' compiler_known 0 \
1297     'if this could be tcc'; then
1298         ct=tcc
1299         CPP='cpp -D__TINYC__'
1300         HAVE_COMPILER_KNOWN=1
1301 fi
1302
1303 case $ct in
1304 bcc)
1305         save_NOWARN="${ccpc}-w"
1306         DOWARN="${ccpc}-w!"
1307         ;;
1308 dec)
1309         # -msg_* flags not used yet, or is -w2 correct?
1310         ;;
1311 dmc)
1312         save_NOWARN="${ccpc}-w"
1313         DOWARN="${ccpc}-wx"
1314         ;;
1315 hpcc)
1316         save_NOWARN=
1317         DOWARN=+We
1318         ;;
1319 kencc)
1320         save_NOWARN=
1321         DOWARN=
1322         ;;
1323 mipspro)
1324         save_NOWARN=
1325         DOWARN="-diag_error 1-10000"
1326         ;;
1327 msc)
1328         save_NOWARN="${ccpc}/w"
1329         DOWARN="${ccpc}/WX"
1330         ;;
1331 sunpro)
1332         test x"$save_NOWARN" = x"" && save_NOWARN='-errwarn=%none'
1333         ac_flags 0 errwarnnone "$save_NOWARN"
1334         test 1 = $HAVE_CAN_ERRWARNNONE || save_NOWARN=
1335         ac_flags 0 errwarnall "-errwarn=%all"
1336         test 1 = $HAVE_CAN_ERRWARNALL && DOWARN="-errwarn=%all"
1337         ;;
1338 tendra)
1339         save_NOWARN=-w
1340         ;;
1341 ucode)
1342         save_NOWARN=
1343         DOWARN=-w2
1344         ;;
1345 watcom)
1346         save_NOWARN=
1347         DOWARN=-Wc,-we
1348         ;;
1349 xlc)
1350         save_NOWARN=-qflag=i:e
1351         DOWARN=-qflag=i:i
1352         ;;
1353 *)
1354         test x"$save_NOWARN" = x"" && save_NOWARN=-Wno-error
1355         ac_flags 0 wnoerror "$save_NOWARN"
1356         test 1 = $HAVE_CAN_WNOERROR || save_NOWARN=
1357         ac_flags 0 werror -Werror
1358         test 1 = $HAVE_CAN_WERROR && DOWARN=-Werror
1359         test $ct = icc && DOWARN="$DOWARN -wd1419"
1360         ;;
1361 esac
1362 NOWARN=$save_NOWARN
1363
1364 #
1365 # Compiler: extra flags (-O2 -f* -W* etc.)
1366 #
1367 i=`echo :"$orig_CFLAGS" | sed 's/^://' | tr -c -d $alll$allu$alln`
1368 # optimisation: only if orig_CFLAGS is empty
1369 test x"$i" = x"" && case $ct in
1370 hpcc)
1371         phase=u
1372         ac_flags 1 otwo +O2
1373         phase=x
1374         ;;
1375 kencc|tcc|tendra)
1376         # no special optimisation
1377         ;;
1378 sunpro)
1379         cat >x <<-'EOF'
1380                 #include <unistd.h>
1381                 int main(void) { return (isatty(0)); }
1382                 #define __IDSTRING_CONCAT(l,p)  __LINTED__ ## l ## _ ## p
1383                 #define __IDSTRING_EXPAND(l,p)  __IDSTRING_CONCAT(l,p)
1384                 #define pad                     void __IDSTRING_EXPAND(__LINE__,x)(void) { }
1385         EOF
1386         yes pad | head -n 256 >>x
1387         ac_flags - 1 otwo -xO2 <x
1388         rmf x
1389         ;;
1390 xlc)
1391         ac_flags 1 othree "-O3 -qstrict"
1392         test 1 = $HAVE_CAN_OTHREE || ac_flags 1 otwo -O2
1393         ;;
1394 *)
1395         ac_flags 1 otwo -O2
1396         test 1 = $HAVE_CAN_OTWO || ac_flags 1 optimise -O
1397         ;;
1398 esac
1399 # other flags: just add them if they are supported
1400 i=0
1401 case $ct in
1402 bcc)
1403         ac_flags 1 strpool "${ccpc}-d" 'if string pooling can be enabled'
1404         ;;
1405 clang)
1406         i=1
1407         ;;
1408 dec)
1409         ac_flags 0 verb -verbose
1410         ac_flags 1 rodata -readonly_strings
1411         ;;
1412 dmc)
1413         ac_flags 1 decl "${ccpc}-r" 'for strict prototype checks'
1414         ac_flags 1 schk "${ccpc}-s" 'for stack overflow checking'
1415         ;;
1416 gcc)
1417         # The following tests run with -Werror (gcc only) if possible
1418         NOWARN=$DOWARN; phase=u
1419         ac_flags 1 wnodeprecateddecls -Wno-deprecated-declarations
1420         # mksh is not written in CFrustFrust!
1421         ac_flags 1 no_eh_frame -fno-asynchronous-unwind-tables
1422         ac_flags 1 fnostrictaliasing -fno-strict-aliasing
1423         ac_flags 1 fstackprotectorstrong -fstack-protector-strong
1424         test 1 = $HAVE_CAN_FSTACKPROTECTORSTRONG || \
1425             ac_flags 1 fstackprotectorall -fstack-protector-all
1426         test $cm = dragonegg && case " $CC $CFLAGS $LDFLAGS " in
1427         *\ -fplugin=*dragonegg*) ;;
1428         *) ac_flags 1 fplugin_dragonegg -fplugin=dragonegg ;;
1429         esac
1430         case $cm in
1431         combine)
1432                 fv=0
1433                 checks='7 8'
1434                 ;;
1435         lto)
1436                 fv=0
1437                 checks='1 2 3 4 5 6 7 8'
1438                 ;;
1439         *)
1440                 fv=1
1441                 ;;
1442         esac
1443         test $fv = 1 || for what in $checks; do
1444                 test $fv = 1 && break
1445                 case $what in
1446                 1)      t_cflags='-flto=jobserver'
1447                         t_ldflags='-fuse-linker-plugin'
1448                         t_use=1 t_name=fltojs_lp ;;
1449                 2)      t_cflags='-flto=jobserver' t_ldflags=''
1450                         t_use=1 t_name=fltojs_nn ;;
1451                 3)      t_cflags='-flto=jobserver'
1452                         t_ldflags='-fno-use-linker-plugin -fwhole-program'
1453                         t_use=1 t_name=fltojs_np ;;
1454                 4)      t_cflags='-flto'
1455                         t_ldflags='-fuse-linker-plugin'
1456                         t_use=1 t_name=fltons_lp ;;
1457                 5)      t_cflags='-flto' t_ldflags=''
1458                         t_use=1 t_name=fltons_nn ;;
1459                 6)      t_cflags='-flto'
1460                         t_ldflags='-fno-use-linker-plugin -fwhole-program'
1461                         t_use=1 t_name=fltons_np ;;
1462                 7)      t_cflags='-fwhole-program --combine' t_ldflags=''
1463                         t_use=0 t_name=combine cm=combine ;;
1464                 8)      fv=1 cm=normal ;;
1465                 esac
1466                 test $fv = 1 && break
1467                 ac_flags $t_use $t_name "$t_cflags" \
1468                     "if gcc supports $t_cflags $t_ldflags" "$t_ldflags"
1469         done
1470         i=1
1471         ;;
1472 hpcc)
1473         phase=u
1474         # probably not needed
1475         #ac_flags 1 agcc -Agcc 'for support of GCC extensions'
1476         phase=x
1477         ;;
1478 icc)
1479         ac_flags 1 fnobuiltinsetmode -fno-builtin-setmode
1480         ac_flags 1 fnostrictaliasing -fno-strict-aliasing
1481         ac_flags 1 fstacksecuritycheck -fstack-security-check
1482         i=1
1483         ;;
1484 mipspro)
1485         ac_flags 1 fullwarn -fullwarn 'for remark output support'
1486         ;;
1487 msc)
1488         ac_flags 1 strpool "${ccpc}/GF" 'if string pooling can be enabled'
1489         echo 'int main(void) { char test[64] = ""; return (*test); }' >x
1490         ac_flags - 1 stackon "${ccpc}/GZ" 'if stack checks can be enabled' <x
1491         ac_flags - 1 stckall "${ccpc}/Ge" 'stack checks for all functions' <x
1492         ac_flags - 1 secuchk "${ccpc}/GS" 'for compiler security checks' <x
1493         rmf x
1494         ac_flags 1 wall "${ccpc}/Wall" 'to enable all warnings'
1495         ac_flags 1 wp64 "${ccpc}/Wp64" 'to enable 64-bit warnings'
1496         ;;
1497 nwcc)
1498         i=1
1499         #broken# ac_flags 1 ssp -stackprotect
1500         ;;
1501 sunpro)
1502         phase=u
1503         ac_flags 1 v -v
1504         ac_flags 1 ipo -xipo 'for cross-module optimisation'
1505         phase=x
1506         ;;
1507 tcc)
1508         : #broken# ac_flags 1 boundschk -b
1509         ;;
1510 tendra)
1511         ac_flags 0 ysystem -Ysystem
1512         test 1 = $HAVE_CAN_YSYSTEM && CPPFLAGS="-Ysystem $CPPFLAGS"
1513         ac_flags 1 extansi -Xa
1514         ;;
1515 xlc)
1516         ac_flags 1 rodata "-qro -qroconst -qroptr"
1517         ac_flags 1 rtcheck -qcheck=all
1518         #ac_flags 1 rtchkc -qextchk     # reported broken
1519         ac_flags 1 wformat "-qformat=all -qformat=nozln"
1520         #ac_flags 1 wp64 -qwarn64       # too verbose for now
1521         ;;
1522 esac
1523 # flags common to a subset of compilers (run with -Werror on gcc)
1524 if test 1 = $i; then
1525         ac_flags 1 wall -Wall
1526         ac_flags 1 fwrapv -fwrapv
1527 fi
1528
1529 phase=x
1530 # The following tests run with -Werror or similar (all compilers) if possible
1531 NOWARN=$DOWARN
1532 test $ct = pcc && phase=u
1533
1534 #
1535 # Compiler: check for stuff that only generates warnings
1536 #
1537 ac_test attribute_bounded '' 'for __attribute__((__bounded__))' <<-'EOF'
1538         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1539         extern int thiswillneverbedefinedIhope(void);
1540         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1541         int main(void) { return (thiswillneverbedefinedIhope()); }
1542         #else
1543         #include <string.h>
1544         #undef __attribute__
1545         int xcopy(const void *, void *, size_t)
1546             __attribute__((__bounded__(__buffer__, 1, 3)))
1547             __attribute__((__bounded__(__buffer__, 2, 3)));
1548         int main(int ac, char *av[]) { return (xcopy(av[0], av[--ac], 1)); }
1549         int xcopy(const void *s, void *d, size_t n) {
1550                 /*
1551                  * if memmove does not exist, we are not on a system
1552                  * with GCC with __bounded__ attribute either so poo
1553                  */
1554                 memmove(d, s, n); return ((int)n);
1555         }
1556         #endif
1557 EOF
1558 ac_test attribute_format '' 'for __attribute__((__format__))' <<-'EOF'
1559         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1560         extern int thiswillneverbedefinedIhope(void);
1561         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1562         int main(void) { return (thiswillneverbedefinedIhope()); }
1563         #else
1564         #define fprintf printfoo
1565         #include <stdio.h>
1566         #undef __attribute__
1567         #undef fprintf
1568         extern int fprintf(FILE *, const char *format, ...)
1569             __attribute__((__format__(__printf__, 2, 3)));
1570         int main(int ac, char **av) { return (fprintf(stderr, "%s%d", *av, ac)); }
1571         #endif
1572 EOF
1573 ac_test attribute_noreturn '' 'for __attribute__((__noreturn__))' <<-'EOF'
1574         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1575         extern int thiswillneverbedefinedIhope(void);
1576         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1577         int main(void) { return (thiswillneverbedefinedIhope()); }
1578         #else
1579         #include <stdlib.h>
1580         #undef __attribute__
1581         void fnord(void) __attribute__((__noreturn__));
1582         int main(void) { fnord(); }
1583         void fnord(void) { exit(0); }
1584         #endif
1585 EOF
1586 ac_test attribute_pure '' 'for __attribute__((__pure__))' <<-'EOF'
1587         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1588         extern int thiswillneverbedefinedIhope(void);
1589         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1590         int main(void) { return (thiswillneverbedefinedIhope()); }
1591         #else
1592         #include <unistd.h>
1593         #undef __attribute__
1594         int foo(const char *) __attribute__((__pure__));
1595         int main(int ac, char **av) { return (foo(av[ac - 1]) + isatty(0)); }
1596         int foo(const char *s) { return ((int)s[0]); }
1597         #endif
1598 EOF
1599 ac_test attribute_unused '' 'for __attribute__((__unused__))' <<-'EOF'
1600         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1601         extern int thiswillneverbedefinedIhope(void);
1602         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1603         int main(void) { return (thiswillneverbedefinedIhope()); }
1604         #else
1605         #include <unistd.h>
1606         #undef __attribute__
1607         int main(int ac __attribute__((__unused__)), char **av
1608             __attribute__((__unused__))) { return (isatty(0)); }
1609         #endif
1610 EOF
1611 ac_test attribute_used '' 'for __attribute__((__used__))' <<-'EOF'
1612         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1613         extern int thiswillneverbedefinedIhope(void);
1614         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1615         int main(void) { return (thiswillneverbedefinedIhope()); }
1616         #else
1617         #include <unistd.h>
1618         #undef __attribute__
1619         static const char fnord[] __attribute__((__used__)) = "42";
1620         int main(void) { return (isatty(0)); }
1621         #endif
1622 EOF
1623
1624 # End of tests run with -Werror
1625 NOWARN=$save_NOWARN
1626 phase=x
1627
1628 #
1629 # mksh: flavours (full/small mksh, omit certain stuff)
1630 #
1631 if ac_ifcpp 'ifdef MKSH_SMALL' isset_MKSH_SMALL '' \
1632     "if a reduced-feature mksh is requested"; then
1633         : "${HAVE_NICE=0}"
1634         : "${HAVE_PERSISTENT_HISTORY=0}"
1635         check_categories="$check_categories smksh"
1636         HAVE_ISSET_MKSH_CONSERVATIVE_FDS=1      # from sh.h
1637 fi
1638 ac_ifcpp 'if defined(MKSH_BINSHPOSIX) || defined(MKSH_BINSHREDUCED)' \
1639     isset_MKSH_BINSH '' 'if invoking as sh should be handled specially' && \
1640     check_categories="$check_categories binsh"
1641 ac_ifcpp 'ifdef MKSH_UNEMPLOYED' isset_MKSH_UNEMPLOYED '' \
1642     "if mksh will be built without job control" && \
1643     check_categories="$check_categories arge"
1644 ac_ifcpp 'ifdef MKSH_NOPROSPECTOFWORK' isset_MKSH_NOPROSPECTOFWORK '' \
1645     "if mksh will be built without job signals" && \
1646     check_categories="$check_categories arge nojsig"
1647 ac_ifcpp 'ifdef MKSH_ASSUME_UTF8' isset_MKSH_ASSUME_UTF8 '' \
1648     'if the default UTF-8 mode is specified' && : "${HAVE_SETLOCALE_CTYPE=0}"
1649 ac_ifcpp 'ifdef MKSH_CONSERVATIVE_FDS' isset_MKSH_CONSERVATIVE_FDS '' \
1650     'if traditional/conservative fd use is requested' && \
1651     check_categories="$check_categories convfds"
1652 #ac_ifcpp 'ifdef MKSH_DISABLE_DEPRECATED' isset_MKSH_DISABLE_DEPRECATED '' \
1653 #    "if deprecated features are to be omitted" && \
1654 #    check_categories="$check_categories nodeprecated"
1655 #ac_ifcpp 'ifdef MKSH_DISABLE_EXPERIMENTAL' isset_MKSH_DISABLE_EXPERIMENTAL '' \
1656 #    "if experimental features are to be omitted" && \
1657 #    check_categories="$check_categories noexperimental"
1658 ac_ifcpp 'ifdef MKSH_MIDNIGHTBSD01ASH_COMPAT' isset_MKSH_MIDNIGHTBSD01ASH_COMPAT '' \
1659     'if the MidnightBSD 0.1 ash compatibility mode is requested' && \
1660     check_categories="$check_categories mnbsdash"
1661
1662 #
1663 # Environment: headers
1664 #
1665 ac_header sys/time.h sys/types.h
1666 ac_header time.h sys/types.h
1667 test "11" = "$HAVE_SYS_TIME_H$HAVE_TIME_H" || HAVE_BOTH_TIME_H=0
1668 ac_test both_time_h '' 'whether <sys/time.h> and <time.h> can both be included' <<-'EOF'
1669         #include <sys/types.h>
1670         #include <sys/time.h>
1671         #include <time.h>
1672         #include <unistd.h>
1673         int main(void) { struct tm tm; return ((int)sizeof(tm) + isatty(0)); }
1674 EOF
1675 ac_header sys/bsdtypes.h
1676 ac_header sys/file.h sys/types.h
1677 ac_header sys/mkdev.h sys/types.h
1678 ac_header sys/mman.h sys/types.h
1679 ac_header sys/param.h
1680 ac_header sys/resource.h sys/types.h _time
1681 ac_header sys/select.h sys/types.h
1682 ac_header sys/sysmacros.h
1683 ac_header bstring.h
1684 ac_header grp.h sys/types.h
1685 ac_header io.h
1686 ac_header libgen.h
1687 ac_header libutil.h sys/types.h
1688 ac_header paths.h
1689 ac_header stdint.h stdarg.h
1690 # include strings.h only if compatible with string.h
1691 ac_header strings.h sys/types.h string.h
1692 ac_header termios.h
1693 ac_header ulimit.h sys/types.h
1694 ac_header values.h
1695
1696 #
1697 # Environment: definitions
1698 #
1699 echo '#include <sys/types.h>
1700 #include <unistd.h>
1701 /* check that off_t can represent 2^63-1 correctly, thx FSF */
1702 #define LARGE_OFF_T ((((off_t)1 << 31) << 31) - 1 + (((off_t)1 << 31) << 31))
1703 int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 &&
1704     LARGE_OFF_T % 2147483647 == 1) ? 1 : -1];
1705 int main(void) { return (isatty(0)); }' >lft.c
1706 ac_testn can_lfs '' "for large file support" <lft.c
1707 save_CPPFLAGS=$CPPFLAGS
1708 add_cppflags -D_FILE_OFFSET_BITS=64
1709 ac_testn can_lfs_sus '!' can_lfs 0 "... with -D_FILE_OFFSET_BITS=64" <lft.c
1710 if test 0 = $HAVE_CAN_LFS_SUS; then
1711         CPPFLAGS=$save_CPPFLAGS
1712         add_cppflags -D_LARGE_FILES=1
1713         ac_testn can_lfs_aix '!' can_lfs 0 "... with -D_LARGE_FILES=1" <lft.c
1714         test 1 = $HAVE_CAN_LFS_AIX || CPPFLAGS=$save_CPPFLAGS
1715 fi
1716 rm -f lft.c
1717 rmf lft*        # end of large file support test
1718
1719 #
1720 # Environment: types
1721 #
1722 ac_test can_inttypes '!' stdint_h 1 "for standard 32-bit integer types" <<-'EOF'
1723         #include <sys/types.h>
1724         #include <stddef.h>
1725         int main(int ac, char **av) { return ((uint32_t)(size_t)*av + (int32_t)ac); }
1726 EOF
1727 ac_test can_ucbints '!' can_inttypes 1 "for UCB 32-bit integer types" <<-'EOF'
1728         #include <sys/types.h>
1729         #include <stddef.h>
1730         int main(int ac, char **av) { return ((u_int32_t)(size_t)*av + (int32_t)ac); }
1731 EOF
1732 ac_test can_int8type '!' stdint_h 1 "for standard 8-bit integer type" <<-'EOF'
1733         #include <sys/types.h>
1734         #include <stddef.h>
1735         int main(int ac, char **av) { return ((uint8_t)(size_t)av[ac]); }
1736 EOF
1737 ac_test can_ucbint8 '!' can_int8type 1 "for UCB 8-bit integer type" <<-'EOF'
1738         #include <sys/types.h>
1739         #include <stddef.h>
1740         int main(int ac, char **av) { return ((u_int8_t)(size_t)av[ac]); }
1741 EOF
1742
1743 ac_test rlim_t <<-'EOF'
1744         #include <sys/types.h>
1745         #if HAVE_BOTH_TIME_H
1746         #include <sys/time.h>
1747         #include <time.h>
1748         #elif HAVE_SYS_TIME_H
1749         #include <sys/time.h>
1750         #elif HAVE_TIME_H
1751         #include <time.h>
1752         #endif
1753         #if HAVE_SYS_RESOURCE_H
1754         #include <sys/resource.h>
1755         #endif
1756         #include <unistd.h>
1757         int main(void) { return (((int)(rlim_t)0) + isatty(0)); }
1758 EOF
1759
1760 # only testn: added later below
1761 ac_testn sig_t <<-'EOF'
1762         #include <sys/types.h>
1763         #include <signal.h>
1764         #include <stddef.h>
1765         volatile sig_t foo = (sig_t)0;
1766         int main(void) { return (foo == (sig_t)0); }
1767 EOF
1768
1769 ac_testn sighandler_t '!' sig_t 0 <<-'EOF'
1770         #include <sys/types.h>
1771         #include <signal.h>
1772         #include <stddef.h>
1773         volatile sighandler_t foo = (sighandler_t)0;
1774         int main(void) { return (foo == (sighandler_t)0); }
1775 EOF
1776 if test 1 = $HAVE_SIGHANDLER_T; then
1777         add_cppflags -Dsig_t=sighandler_t
1778         HAVE_SIG_T=1
1779 fi
1780
1781 ac_testn __sighandler_t '!' sig_t 0 <<-'EOF'
1782         #include <sys/types.h>
1783         #include <signal.h>
1784         #include <stddef.h>
1785         volatile __sighandler_t foo = (__sighandler_t)0;
1786         int main(void) { return (foo == (__sighandler_t)0); }
1787 EOF
1788 if test 1 = $HAVE___SIGHANDLER_T; then
1789         add_cppflags -Dsig_t=__sighandler_t
1790         HAVE_SIG_T=1
1791 fi
1792
1793 test 1 = $HAVE_SIG_T || add_cppflags -Dsig_t=nosig_t
1794 ac_cppflags SIG_T
1795
1796 #
1797 # check whether whatever we use for the final link will succeed
1798 #
1799 if test $cm = makefile; then
1800         : nothing to check
1801 else
1802         HAVE_LINK_WORKS=x
1803         ac_testinit link_works '' 'checking if the final link command may succeed'
1804         fv=1
1805         cat >conftest.c <<-EOF
1806                 #define EXTERN
1807                 #define MKSH_INCLUDES_ONLY
1808                 #include "sh.h"
1809                 __RCSID("$srcversion");
1810                 int main(void) { printf("Hello, World!\\n"); return (isatty(0)); }
1811 EOF
1812         case $cm in
1813         llvm)
1814                 v "$CC $CFLAGS $CPPFLAGS $NOWARN -emit-llvm -c conftest.c" || fv=0
1815                 rmf $tfn.s
1816                 test $fv = 0 || v "llvm-link -o - conftest.o | opt $optflags | llc -o $tfn.s" || fv=0
1817                 test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn $tfn.s $LIBS $ccpr"
1818                 ;;
1819         dragonegg)
1820                 v "$CC $CFLAGS $CPPFLAGS $NOWARN -S -flto conftest.c" || fv=0
1821                 test $fv = 0 || v "mv conftest.s conftest.ll"
1822                 test $fv = 0 || v "llvm-as conftest.ll" || fv=0
1823                 rmf $tfn.s
1824                 test $fv = 0 || v "llvm-link -o - conftest.bc | opt $optflags | llc -o $tfn.s" || fv=0
1825                 test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn $tfn.s $LIBS $ccpr"
1826                 ;;
1827         combine)
1828                 v "$CC $CFLAGS $CPPFLAGS $LDFLAGS -fwhole-program --combine $NOWARN -o $tcfn conftest.c $LIBS $ccpr"
1829                 ;;
1830         lto|normal)
1831                 cm=normal
1832                 v "$CC $CFLAGS $CPPFLAGS $NOWARN -c conftest.c" || fv=0
1833                 test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn conftest.o $LIBS $ccpr"
1834                 ;;
1835         esac
1836         test -f $tcfn || fv=0
1837         ac_testdone
1838         test $fv = 1 || exit 1
1839 fi
1840
1841 #
1842 # Environment: errors and signals
1843 #
1844 test x"NetBSD" = x"$TARGET_OS" && $e Ignore the compatibility warning.
1845
1846 ac_testn sys_errlist '' "the sys_errlist[] array and sys_nerr" <<-'EOF'
1847         extern const int sys_nerr;
1848         extern const char * const sys_errlist[];
1849         extern int isatty(int);
1850         int main(void) { return (*sys_errlist[sys_nerr - 1] + isatty(0)); }
1851 EOF
1852 ac_testn _sys_errlist '!' sys_errlist 0 "the _sys_errlist[] array and _sys_nerr" <<-'EOF'
1853         extern const int _sys_nerr;
1854         extern const char * const _sys_errlist[];
1855         extern int isatty(int);
1856         int main(void) { return (*_sys_errlist[_sys_nerr - 1] + isatty(0)); }
1857 EOF
1858 if test 1 = "$HAVE__SYS_ERRLIST"; then
1859         add_cppflags -Dsys_nerr=_sys_nerr
1860         add_cppflags -Dsys_errlist=_sys_errlist
1861         HAVE_SYS_ERRLIST=1
1862 fi
1863 ac_cppflags SYS_ERRLIST
1864
1865 for what in name list; do
1866         uwhat=`upper $what`
1867         ac_testn sys_sig$what '' "the sys_sig${what}[] array" <<-EOF
1868                 extern const char * const sys_sig${what}[];
1869                 extern int isatty(int);
1870                 int main(void) { return (sys_sig${what}[0][0] + isatty(0)); }
1871         EOF
1872         ac_testn _sys_sig$what '!' sys_sig$what 0 "the _sys_sig${what}[] array" <<-EOF
1873                 extern const char * const _sys_sig${what}[];
1874                 extern int isatty(int);
1875                 int main(void) { return (_sys_sig${what}[0][0] + isatty(0)); }
1876         EOF
1877         eval uwhat_v=\$HAVE__SYS_SIG$uwhat
1878         if test 1 = "$uwhat_v"; then
1879                 add_cppflags -Dsys_sig$what=_sys_sig$what
1880                 eval HAVE_SYS_SIG$uwhat=1
1881         fi
1882         ac_cppflags SYS_SIG$uwhat
1883 done
1884
1885 #
1886 # Environment: library functions
1887 #
1888 ac_test flock <<-'EOF'
1889         #include <sys/types.h>
1890         #include <fcntl.h>
1891         #undef flock
1892         #if HAVE_SYS_FILE_H
1893         #include <sys/file.h>
1894         #endif
1895         int main(void) { return (flock(0, LOCK_EX | LOCK_UN)); }
1896 EOF
1897
1898 ac_test lock_fcntl '!' flock 1 'whether we can lock files with fcntl' <<-'EOF'
1899         #include <fcntl.h>
1900         #undef flock
1901         int main(void) {
1902                 struct flock lks;
1903                 lks.l_type = F_WRLCK | F_UNLCK;
1904                 return (fcntl(0, F_SETLKW, &lks));
1905         }
1906 EOF
1907
1908 ac_test getrusage <<-'EOF'
1909         #define MKSH_INCLUDES_ONLY
1910         #include "sh.h"
1911         int main(void) {
1912                 struct rusage ru;
1913                 return (getrusage(RUSAGE_SELF, &ru) +
1914                     getrusage(RUSAGE_CHILDREN, &ru));
1915         }
1916 EOF
1917
1918 ac_test getsid <<-'EOF'
1919         #include <unistd.h>
1920         int main(void) { return ((int)getsid(0)); }
1921 EOF
1922
1923 ac_test gettimeofday <<-'EOF'
1924         #define MKSH_INCLUDES_ONLY
1925         #include "sh.h"
1926         int main(void) { struct timeval tv; return (gettimeofday(&tv, NULL)); }
1927 EOF
1928
1929 ac_test killpg <<-'EOF'
1930         #include <signal.h>
1931         int main(int ac, char *av[]) { return (av[0][killpg(123, ac)]); }
1932 EOF
1933
1934 ac_test memmove <<-'EOF'
1935         #include <sys/types.h>
1936         #include <stddef.h>
1937         #include <string.h>
1938         #if HAVE_STRINGS_H
1939         #include <strings.h>
1940         #endif
1941         int main(int ac, char *av[]) {
1942                 return (*(int *)(void *)memmove(av[0], av[1], (size_t)ac));
1943         }
1944 EOF
1945
1946 ac_test mknod '' 'if to use mknod(), makedev() and friends' <<-'EOF'
1947         #define MKSH_INCLUDES_ONLY
1948         #include "sh.h"
1949         int main(int ac, char *av[]) {
1950                 dev_t dv;
1951                 dv = makedev((unsigned int)ac, (unsigned int)av[0][0]);
1952                 return (mknod(av[0], (mode_t)0, dv) ? (int)major(dv) :
1953                     (int)minor(dv));
1954         }
1955 EOF
1956
1957 ac_test mmap lock_fcntl 0 'for mmap and munmap' <<-'EOF'
1958         #include <sys/types.h>
1959         #if HAVE_SYS_FILE_H
1960         #include <sys/file.h>
1961         #endif
1962         #if HAVE_SYS_MMAN_H
1963         #include <sys/mman.h>
1964         #endif
1965         #include <stddef.h>
1966         #include <stdlib.h>
1967         int main(void) { return ((void *)mmap(NULL, (size_t)0,
1968             PROT_READ, MAP_PRIVATE, 0, (off_t)0) == (void *)NULL ? 1 :
1969             munmap(NULL, 0)); }
1970 EOF
1971
1972 ac_test nice <<-'EOF'
1973         #include <unistd.h>
1974         int main(void) { return (nice(4)); }
1975 EOF
1976
1977 ac_test revoke <<-'EOF'
1978         #include <sys/types.h>
1979         #if HAVE_LIBUTIL_H
1980         #include <libutil.h>
1981         #endif
1982         #include <unistd.h>
1983         int main(int ac, char *av[]) { return (ac + revoke(av[0])); }
1984 EOF
1985
1986 ac_test setlocale_ctype '' 'setlocale(LC_CTYPE, "")' <<-'EOF'
1987         #include <locale.h>
1988         #include <stddef.h>
1989         int main(void) { return ((int)(size_t)(void *)setlocale(LC_CTYPE, "")); }
1990 EOF
1991
1992 ac_test langinfo_codeset setlocale_ctype 0 'nl_langinfo(CODESET)' <<-'EOF'
1993         #include <langinfo.h>
1994         #include <stddef.h>
1995         int main(void) { return ((int)(size_t)(void *)nl_langinfo(CODESET)); }
1996 EOF
1997
1998 ac_test select <<-'EOF'
1999         #include <sys/types.h>
2000         #if HAVE_BOTH_TIME_H
2001         #include <sys/time.h>
2002         #include <time.h>
2003         #elif HAVE_SYS_TIME_H
2004         #include <sys/time.h>
2005         #elif HAVE_TIME_H
2006         #include <time.h>
2007         #endif
2008         #if HAVE_SYS_BSDTYPES_H
2009         #include <sys/bsdtypes.h>
2010         #endif
2011         #if HAVE_SYS_SELECT_H
2012         #include <sys/select.h>
2013         #endif
2014         #if HAVE_BSTRING_H
2015         #include <bstring.h>
2016         #endif
2017         #include <stddef.h>
2018         #include <stdlib.h>
2019         #include <string.h>
2020         #if HAVE_STRINGS_H
2021         #include <strings.h>
2022         #endif
2023         #include <unistd.h>
2024         int main(void) {
2025                 struct timeval tv = { 1, 200000 };
2026                 fd_set fds; FD_ZERO(&fds); FD_SET(0, &fds);
2027                 return (select(FD_SETSIZE, &fds, NULL, NULL, &tv));
2028         }
2029 EOF
2030
2031 ac_test setresugid <<-'EOF'
2032         #include <sys/types.h>
2033         #include <unistd.h>
2034         int main(void) { return (setresuid(0,0,0) + setresgid(0,0,0)); }
2035 EOF
2036
2037 ac_test setgroups setresugid 0 <<-'EOF'
2038         #include <sys/types.h>
2039         #if HAVE_GRP_H
2040         #include <grp.h>
2041         #endif
2042         #include <unistd.h>
2043         int main(void) { gid_t gid = 0; return (setgroups(0, &gid)); }
2044 EOF
2045
2046 if test x"$et" = x"klibc"; then
2047
2048         ac_testn __rt_sigsuspend '' 'whether klibc uses RT signals' <<-'EOF'
2049                 #define MKSH_INCLUDES_ONLY
2050                 #include "sh.h"
2051                 extern int __rt_sigsuspend(const sigset_t *, size_t);
2052                 int main(void) { return (__rt_sigsuspend(NULL, 0)); }
2053 EOF
2054
2055         # no? damn! legacy crap ahead!
2056
2057         ac_testn __sigsuspend_s '!' __rt_sigsuspend 1 \
2058             'whether sigsuspend is usable (1/2)' <<-'EOF'
2059                 #define MKSH_INCLUDES_ONLY
2060                 #include "sh.h"
2061                 extern int __sigsuspend_s(sigset_t);
2062                 int main(void) { return (__sigsuspend_s(0)); }
2063 EOF
2064         ac_testn __sigsuspend_xxs '!' __sigsuspend_s 1 \
2065             'whether sigsuspend is usable (2/2)' <<-'EOF'
2066                 #define MKSH_INCLUDES_ONLY
2067                 #include "sh.h"
2068                 extern int __sigsuspend_xxs(int, int, sigset_t);
2069                 int main(void) { return (__sigsuspend_xxs(0, 0, 0)); }
2070 EOF
2071
2072         if test "000" = "$HAVE___RT_SIGSUSPEND$HAVE___SIGSUSPEND_S$HAVE___SIGSUSPEND_XXS"; then
2073                 # no usable sigsuspend(), use pause() *ugh*
2074                 add_cppflags -DMKSH_NO_SIGSUSPEND
2075         fi
2076 fi
2077
2078 ac_test strerror '!' sys_errlist 0 <<-'EOF'
2079         extern char *strerror(int);
2080         int main(int ac, char *av[]) { return (*strerror(*av[ac])); }
2081 EOF
2082
2083 ac_test strsignal '!' sys_siglist 0 <<-'EOF'
2084         #include <string.h>
2085         #include <signal.h>
2086         int main(void) { return (strsignal(1)[0]); }
2087 EOF
2088
2089 ac_test strlcpy <<-'EOF'
2090         #include <string.h>
2091         int main(int ac, char *av[]) { return (strlcpy(*av, av[1],
2092             (size_t)ac)); }
2093 EOF
2094
2095 #
2096 # check headers for declarations
2097 #
2098 ac_test flock_decl flock 1 'for declaration of flock()' <<-'EOF'
2099         #define MKSH_INCLUDES_ONLY
2100         #include "sh.h"
2101         #if HAVE_SYS_FILE_H
2102         #include <sys/file.h>
2103         #endif
2104         int main(void) { return ((flock)(0, 0)); }
2105 EOF
2106 ac_test revoke_decl revoke 1 'for declaration of revoke()' <<-'EOF'
2107         #define MKSH_INCLUDES_ONLY
2108         #include "sh.h"
2109         int main(void) { return ((revoke)("")); }
2110 EOF
2111 ac_test sys_errlist_decl sys_errlist 0 "for declaration of sys_errlist[] and sys_nerr" <<-'EOF'
2112         #define MKSH_INCLUDES_ONLY
2113         #include "sh.h"
2114         int main(void) { return (*sys_errlist[sys_nerr - 1] + isatty(0)); }
2115 EOF
2116 ac_test sys_siglist_decl sys_siglist 0 'for declaration of sys_siglist[]' <<-'EOF'
2117         #define MKSH_INCLUDES_ONLY
2118         #include "sh.h"
2119         int main(void) { return (sys_siglist[0][0] + isatty(0)); }
2120 EOF
2121
2122 #
2123 # other checks
2124 #
2125 fd='if to use persistent history'
2126 ac_cache PERSISTENT_HISTORY || case $HAVE_MMAP$HAVE_FLOCK$HAVE_LOCK_FCNTL in
2127 11*|101) fv=1 ;;
2128 esac
2129 test 1 = $fv || check_categories="$check_categories no-histfile"
2130 ac_testdone
2131 ac_cppflags
2132
2133 save_CFLAGS=$CFLAGS
2134 ac_testn compile_time_asserts_$$ '' 'whether compile-time assertions pass' <<-'EOF'
2135         #define MKSH_INCLUDES_ONLY
2136         #include "sh.h"
2137         #ifndef CHAR_BIT
2138         #define CHAR_BIT 8      /* defuse this test on really legacy systems */
2139         #endif
2140         struct ctasserts {
2141         #define cta(name, assertion) char name[(assertion) ? 1 : -1]
2142 /* this one should be defined by the standard */
2143 cta(char_is_1_char, (sizeof(char) == 1) && (sizeof(signed char) == 1) &&
2144     (sizeof(unsigned char) == 1));
2145 cta(char_is_8_bits, ((CHAR_BIT) == 8) && ((int)(unsigned char)0xFF == 0xFF) &&
2146     ((int)(unsigned char)0x100 == 0) && ((int)(unsigned char)(int)-1 == 0xFF));
2147 /* the next assertion is probably not really needed */
2148 cta(short_is_2_char, sizeof(short) == 2);
2149 cta(short_size_no_matter_of_signedness, sizeof(short) == sizeof(unsigned short));
2150 /* the next assertion is probably not really needed */
2151 cta(int_is_4_char, sizeof(int) == 4);
2152 cta(int_size_no_matter_of_signedness, sizeof(int) == sizeof(unsigned int));
2153
2154 cta(long_ge_int, sizeof(long) >= sizeof(int));
2155 cta(long_size_no_matter_of_signedness, sizeof(long) == sizeof(unsigned long));
2156
2157 #ifndef MKSH_LEGACY_MODE
2158 /* the next assertion is probably not really needed */
2159 cta(ari_is_4_char, sizeof(mksh_ari_t) == 4);
2160 /* but this is */
2161 cta(ari_has_31_bit, 0 < (mksh_ari_t)(((((mksh_ari_t)1 << 15) << 15) - 1) * 2 + 1));
2162 /* the next assertion is probably not really needed */
2163 cta(uari_is_4_char, sizeof(mksh_uari_t) == 4);
2164 /* but the next three are; we REQUIRE unsigned integer wraparound */
2165 cta(uari_has_31_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 2 + 1));
2166 cta(uari_has_32_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3));
2167 cta(uari_wrap_32_bit,
2168     (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3) >
2169     (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 4));
2170 #define NUM 22
2171 #else
2172 #define NUM 16
2173 #endif
2174 /* these are always required */
2175 cta(ari_is_signed, (mksh_ari_t)-1 < (mksh_ari_t)0);
2176 cta(uari_is_unsigned, (mksh_uari_t)-1 > (mksh_uari_t)0);
2177 /* we require these to have the precisely same size and assume 2s complement */
2178 cta(ari_size_no_matter_of_signedness, sizeof(mksh_ari_t) == sizeof(mksh_uari_t));
2179
2180 cta(sizet_size_no_matter_of_signedness, sizeof(ssize_t) == sizeof(size_t));
2181 cta(sizet_voidptr_same_size, sizeof(size_t) == sizeof(void *));
2182 cta(sizet_funcptr_same_size, sizeof(size_t) == sizeof(void (*)(void)));
2183 /* our formatting routines assume this */
2184 cta(ptr_fits_in_long, sizeof(size_t) <= sizeof(long));
2185 cta(ari_fits_in_long, sizeof(mksh_ari_t) <= sizeof(long));
2186 /* for struct alignment people */
2187                 char padding[64 - NUM];
2188         };
2189 char ctasserts_dblcheck[sizeof(struct ctasserts) == 64 ? 1 : -1];
2190         int main(void) { return (sizeof(ctasserts_dblcheck) + isatty(0)); }
2191 EOF
2192 CFLAGS=$save_CFLAGS
2193 eval test 1 = \$HAVE_COMPILE_TIME_ASSERTS_$$ || exit 1
2194
2195 #
2196 # extra checks for legacy mksh
2197 #
2198 if test $legacy = 1; then
2199         ac_test long_32bit '' 'whether long is 32 bit wide' <<-'EOF'
2200                 #define MKSH_INCLUDES_ONLY
2201                 #include "sh.h"
2202                 #ifndef CHAR_BIT
2203                 #define CHAR_BIT 0
2204                 #endif
2205                 struct ctasserts {
2206                 #define cta(name, assertion) char name[(assertion) ? 1 : -1]
2207                         cta(char_is_8_bits, (CHAR_BIT) == 8);
2208                         cta(long_is_32_bits, sizeof(long) == 4);
2209                 };
2210                 int main(void) { return (sizeof(struct ctasserts)); }
2211 EOF
2212
2213         ac_test long_64bit '!' long_32bit 0 'whether long is 64 bit wide' <<-'EOF'
2214                 #define MKSH_INCLUDES_ONLY
2215                 #include "sh.h"
2216                 #ifndef CHAR_BIT
2217                 #define CHAR_BIT 0
2218                 #endif
2219                 struct ctasserts {
2220                 #define cta(name, assertion) char name[(assertion) ? 1 : -1]
2221                         cta(char_is_8_bits, (CHAR_BIT) == 8);
2222                         cta(long_is_64_bits, sizeof(long) == 8);
2223                 };
2224                 int main(void) { return (sizeof(struct ctasserts)); }
2225 EOF
2226
2227         case $HAVE_LONG_32BIT$HAVE_LONG_64BIT in
2228         10) check_categories="$check_categories int:32" ;;
2229         01) check_categories="$check_categories int:64" ;;
2230         *) check_categories="$check_categories int:u" ;;
2231         esac
2232 fi
2233
2234 #
2235 # Compiler: Praeprocessor (only if needed)
2236 #
2237 test 0 = $HAVE_SYS_SIGNAME && if ac_testinit cpp_dd '' \
2238     'checking if the C Preprocessor supports -dD'; then
2239         echo '#define foo bar' >conftest.c
2240         vv ']' "$CPP $CFLAGS $CPPFLAGS $NOWARN -dD conftest.c >x"
2241         grep '#define foo bar' x >/dev/null 2>&1 && fv=1
2242         rmf conftest.c x vv.out
2243         ac_testdone
2244 fi
2245
2246 #
2247 # End of mirtoconf checks
2248 #
2249 $e ... done.
2250
2251 # Some operating systems have ancient versions of ed(1) writing
2252 # the character count to standard output; cope for that
2253 echo wq >x
2254 ed x <x 2>/dev/null | grep 3 >/dev/null 2>&1 && \
2255     check_categories="$check_categories $oldish_ed"
2256 rmf x vv.out
2257
2258 if test 0 = $HAVE_SYS_SIGNAME; then
2259         if test 1 = $HAVE_CPP_DD; then
2260                 $e Generating list of signal names...
2261         else
2262                 $e No list of signal names available via cpp. Falling back...
2263         fi
2264         sigseenone=:
2265         sigseentwo=:
2266         echo '#include <signal.h>
2267 #if defined(NSIG_MAX)
2268 #define cfg_NSIG NSIG_MAX
2269 #elif defined(NSIG)
2270 #define cfg_NSIG NSIG
2271 #elif defined(_NSIG)
2272 #define cfg_NSIG _NSIG
2273 #elif defined(SIGMAX)
2274 #define cfg_NSIG (SIGMAX + 1)
2275 #elif defined(_SIGMAX)
2276 #define cfg_NSIG (_SIGMAX + 1)
2277 #else
2278 /*XXX better error out, see sh.h */
2279 #define cfg_NSIG 64
2280 #endif
2281 int
2282 mksh_cfg= cfg_NSIG
2283 ;' >conftest.c
2284         # GNU sed 2.03 segfaults when optimising this to sed -n
2285         NSIG=`vq "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c" | \
2286             grep -v '^#' | \
2287             sed '/mksh_cfg.*= *$/{
2288                 N
2289                 s/\n/ /
2290                 }' | \
2291             grep '^ *mksh_cfg *=' | \
2292             sed 's/^ *mksh_cfg *=[       ]*\([()0-9x+-][()0-9x+  -]*\).*$/\1/'`
2293         case $NSIG in
2294         *mksh_cfg*) $e "Error: NSIG='$NSIG'"; NSIG=0 ;;
2295         *[\ \(\)+-]*) NSIG=`"$AWK" "BEGIN { print $NSIG }" </dev/null` ;;
2296         esac
2297         printf=printf
2298         (printf hallo) >/dev/null 2>&1 || printf=echo
2299         test $printf = echo || test "`printf %d 42`" = 42 || printf=echo
2300         test $printf = echo || NSIG=`printf %d "$NSIG" 2>/dev/null`
2301         $printf "NSIG=$NSIG ... "
2302         sigs="ABRT FPE ILL INT SEGV TERM ALRM BUS CHLD CONT HUP KILL PIPE QUIT"
2303         sigs="$sigs STOP TSTP TTIN TTOU USR1 USR2 POLL PROF SYS TRAP URG VTALRM"
2304         sigs="$sigs XCPU XFSZ INFO WINCH EMT IO DIL LOST PWR SAK CLD IOT STKFLT"
2305         sigs="$sigs ABND DCE DUMP IOERR TRACE DANGER THCONT THSTOP RESV UNUSED"
2306         test 1 = $HAVE_CPP_DD && test $NSIG -gt 1 && sigs="$sigs "`vq \
2307             "$CPP $CFLAGS $CPPFLAGS $NOWARN -dD conftest.c" | \
2308             grep '[      ]SIG[A-Z0-9][A-Z0-9]*[  ]' | \
2309             sed 's/^.*[  ]SIG\([A-Z0-9][A-Z0-9]*\)[      ].*$/\1/' | sort`
2310         test $NSIG -gt 1 || sigs=
2311         for name in $sigs; do
2312                 case $sigseenone in
2313                 *:$name:*) continue ;;
2314                 esac
2315                 sigseenone=$sigseenone$name:
2316                 echo '#include <signal.h>' >conftest.c
2317                 echo int >>conftest.c
2318                 echo mksh_cfg= SIG$name >>conftest.c
2319                 echo ';' >>conftest.c
2320                 # GNU sed 2.03 croaks on optimising this, too
2321                 vq "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c" | \
2322                     grep -v '^#' | \
2323                     sed '/mksh_cfg.*= *$/{
2324                         N
2325                         s/\n/ /
2326                         }' | \
2327                     grep '^ *mksh_cfg *=' | \
2328                     sed 's/^ *mksh_cfg *=[       ]*\([0-9][0-9x]*\).*$/:\1 '$name/
2329         done | sed -n '/^:[^ ]/s/^://p' | while read nr name; do
2330                 test $printf = echo || nr=`printf %d "$nr" 2>/dev/null`
2331                 test $nr -gt 0 && test $nr -lt $NSIG || continue
2332                 case $sigseentwo in
2333                 *:$nr:*) ;;
2334                 *)      echo "          { \"$name\", $nr },"
2335                         sigseentwo=$sigseentwo$nr:
2336                         $printf "$name=$nr " >&2
2337                         ;;
2338                 esac
2339         done 2>&1 >signames.inc
2340         rmf conftest.c
2341         $e done.
2342 fi
2343
2344 addsrcs '!' HAVE_STRLCPY strlcpy.c
2345 addsrcs USE_PRINTF_BUILTIN printf.c
2346 test 1 = "$USE_PRINTF_BUILTIN" && add_cppflags -DMKSH_PRINTF_BUILTIN
2347 test 1 = "$HAVE_CAN_VERB" && CFLAGS="$CFLAGS -verbose"
2348 add_cppflags -DMKSH_BUILD_R=521
2349
2350 $e $bi$me: Finished configuration testing, now producing output.$ao
2351
2352 files=
2353 objs=
2354 sp=
2355 case $tcfn in
2356 a.exe|conftest.exe)
2357         mkshexe=$tfn.exe
2358         add_cppflags -DMKSH_EXE_EXT
2359         ;;
2360 *)
2361         mkshexe=$tfn
2362         ;;
2363 esac
2364 case $curdir in
2365 *\ *)   mkshshebang="#!./$mkshexe" ;;
2366 *)      mkshshebang="#!$curdir/$mkshexe" ;;
2367 esac
2368 cat >test.sh <<-EOF
2369         $mkshshebang
2370         LC_ALL=C PATH='$PATH'; export LC_ALL PATH
2371         test -n "\$KSH_VERSION" || exit 1
2372         set -A check_categories -- $check_categories
2373         pflag='$curdir/$mkshexe'
2374         sflag='$srcdir/check.t'
2375         usee=0 Pflag=0 Sflag=0 uset=0 vflag=1 xflag=0
2376         while getopts "C:e:fPp:QSs:t:v" ch; do case \$ch {
2377         (C)     check_categories[\${#check_categories[*]}]=\$OPTARG ;;
2378         (e)     usee=1; eflag=\$OPTARG ;;
2379         (f)     check_categories[\${#check_categories[*]}]=fastbox ;;
2380         (P)     Pflag=1 ;;
2381         (+P)    Pflag=0 ;;
2382         (p)     pflag=\$OPTARG ;;
2383         (Q)     vflag=0 ;;
2384         (+Q)    vflag=1 ;;
2385         (S)     Sflag=1 ;;
2386         (+S)    Sflag=0 ;;
2387         (s)     sflag=\$OPTARG ;;
2388         (t)     uset=1; tflag=\$OPTARG ;;
2389         (v)     vflag=1 ;;
2390         (+v)    vflag=0 ;;
2391         (*)     xflag=1 ;;
2392         }
2393         done
2394         shift \$((OPTIND - 1))
2395         set -A args -- '$srcdir/check.pl' -p "\$pflag"
2396         x=
2397         for y in "\${check_categories[@]}"; do
2398                 x=\$x,\$y
2399         done
2400         if [[ -n \$x ]]; then
2401                 args[\${#args[*]}]=-C
2402                 args[\${#args[*]}]=\${x#,}
2403         fi
2404         if (( usee )); then
2405                 args[\${#args[*]}]=-e
2406                 args[\${#args[*]}]=\$eflag
2407         fi
2408         (( Pflag )) && args[\${#args[*]}]=-P
2409         if (( uset )); then
2410                 args[\${#args[*]}]=-t
2411                 args[\${#args[*]}]=\$tflag
2412         fi
2413         (( vflag )) && args[\${#args[*]}]=-v
2414         (( xflag )) && args[\${#args[*]}]=-x    # force usage by synerr
2415         if [[ -n \$TMPDIR && -d \$TMPDIR/. ]]; then
2416                 args[\${#args[*]}]=-T
2417                 args[\${#args[*]}]=\$TMPDIR
2418         fi
2419         print Testing mksh for conformance:
2420         grep -F -e Mir''OS: -e MIRBSD "\$sflag"
2421         print "This shell is actually:\\n\\t\$KSH_VERSION"
2422         print 'test.sh built for mksh $dstversion'
2423         cstr='\$os = defined \$^O ? \$^O : "unknown";'
2424         cstr="\$cstr"'print \$os . ", Perl version " . \$];'
2425         for perli in \$PERL perl5 perl no; do
2426                 if [[ \$perli = no ]]; then
2427                         print Cannot find a working Perl interpreter, aborting.
2428                         exit 1
2429                 fi
2430                 print "Trying Perl interpreter '\$perli'..."
2431                 perlos=\$(\$perli -e "\$cstr")
2432                 rv=\$?
2433                 print "Errorlevel \$rv, running on '\$perlos'"
2434                 if (( rv )); then
2435                         print "=> not using"
2436                         continue
2437                 fi
2438                 if [[ -n \$perlos ]]; then
2439                         print "=> using it"
2440                         break
2441                 fi
2442         done
2443         (( Sflag )) || echo + \$perli "\${args[@]}" -s "\$sflag" "\$@"
2444         (( Sflag )) || exec \$perli "\${args[@]}" -s "\$sflag" "\$@"$tsts
2445         # use of the -S option for check.t split into multiple chunks
2446         rv=0
2447         for s in "\$sflag".*; do
2448                 echo + \$perli "\${args[@]}" -s "\$s" "\$@"
2449                 \$perli "\${args[@]}" -s "\$s" "\$@"$tsts
2450                 rc=\$?
2451                 (( rv = rv ? rv : rc ))
2452         done
2453         exit \$rv
2454 EOF
2455 chmod 755 test.sh
2456 case $cm in
2457 dragonegg)
2458         emitbc="-S -flto"
2459         ;;
2460 llvm)
2461         emitbc="-emit-llvm -c"
2462         ;;
2463 *)
2464         emitbc=-c
2465         ;;
2466 esac
2467 echo ": # work around NeXTstep bug" >Rebuild.sh
2468 cd "$srcdir"
2469 optfiles=`echo *.opt`
2470 cd "$curdir"
2471 for file in $optfiles; do
2472         echo "echo + Running genopt on '$file'..."
2473         echo "(srcfile='$srcdir/$file'; BUILDSH_RUN_GENOPT=1; . '$srcdir/Build.sh')"
2474 done >>Rebuild.sh
2475 echo set -x >>Rebuild.sh
2476 for file in $SRCS; do
2477         op=`echo x"$file" | sed 's/^x\(.*\)\.c$/\1./'`
2478         test -f $file || file=$srcdir/$file
2479         files="$files$sp$file"
2480         sp=' '
2481         echo "$CC $CFLAGS $CPPFLAGS $emitbc $file || exit 1" >>Rebuild.sh
2482         if test $cm = dragonegg; then
2483                 echo "mv ${op}s ${op}ll" >>Rebuild.sh
2484                 echo "llvm-as ${op}ll || exit 1" >>Rebuild.sh
2485                 objs="$objs$sp${op}bc"
2486         else
2487                 objs="$objs$sp${op}o"
2488         fi
2489 done
2490 case $cm in
2491 dragonegg|llvm)
2492         echo "rm -f $tfn.s" >>Rebuild.sh
2493         echo "llvm-link -o - $objs | opt $optflags | llc -o $tfn.s" >>Rebuild.sh
2494         lobjs=$tfn.s
2495         ;;
2496 *)
2497         lobjs=$objs
2498         ;;
2499 esac
2500 echo tcfn=$mkshexe >>Rebuild.sh
2501 echo "$CC $CFLAGS $LDFLAGS -o \$tcfn $lobjs $LIBS $ccpr" >>Rebuild.sh
2502 echo "test -f \$tcfn || exit 1; $SIZE \$tcfn" >>Rebuild.sh
2503 if test $cm = makefile; then
2504         extras='emacsfn.h rlimits.opt sh.h sh_flags.opt var_spec.h'
2505         test 0 = $HAVE_SYS_SIGNAME && extras="$extras signames.inc"
2506         gens= genq=
2507         for file in $optfiles; do
2508                 genf=`basename "$file" | sed 's/.opt$/.gen/'`
2509                 gens="$gens $genf"
2510                 genq="$genq$nl$genf: $srcdir/Build.sh $srcdir/$file
2511                         srcfile=$srcdir/$file; BUILDSH_RUN_GENOPT=1; . $srcdir/Build.sh"
2512         done
2513         cat >Makefrag.inc <<EOF
2514 # Makefile fragment for building mksh $dstversion
2515
2516 PROG=           $mkshexe
2517 MAN=            mksh.1
2518 SRCS=           $SRCS
2519 SRCS_FP=        $files
2520 OBJS_BP=        $objs
2521 INDSRCS=        $extras
2522 NONSRCS_INST=   dot.mkshrc \$(MAN)
2523 NONSRCS_NOINST= Build.sh Makefile Rebuild.sh check.pl check.t test.sh
2524 CC=             $CC
2525 CFLAGS=         $CFLAGS
2526 CPPFLAGS=       $CPPFLAGS
2527 LDFLAGS=        $LDFLAGS
2528 LIBS=           $LIBS
2529
2530 .depend \$(OBJS_BP):$gens$genq
2531
2532 # not BSD make only:
2533 #VPATH=         $srcdir
2534 #all: \$(PROG)
2535 #\$(PROG): \$(OBJS_BP)
2536 #       \$(CC) \$(CFLAGS) \$(LDFLAGS) -o \$@ \$(OBJS_BP) \$(LIBS)
2537 #\$(OBJS_BP): \$(SRCS_FP) \$(NONSRCS)
2538 #.c.o:
2539 #       \$(CC) \$(CFLAGS) \$(CPPFLAGS) -c \$<
2540
2541 # for all make variants:
2542 #REGRESS_FLAGS= -f
2543 #regress:
2544 #       ./test.sh \$(REGRESS_FLAGS)
2545 check_categories=$check_categories
2546
2547 # for BSD make only:
2548 #.PATH: $srcdir
2549 #.include <bsd.prog.mk>
2550 EOF
2551         $e
2552         $e Generated Makefrag.inc successfully.
2553         exit 0
2554 fi
2555 for file in $optfiles; do
2556         $e "+ Running genopt on '$file'..."
2557         do_genopt "$srcdir/$file" || exit 1
2558 done
2559 if test $cm = combine; then
2560         objs="-o $mkshexe"
2561         for file in $SRCS; do
2562                 test -f $file || file=$srcdir/$file
2563                 objs="$objs $file"
2564         done
2565         emitbc="-fwhole-program --combine"
2566         v "$CC $CFLAGS $CPPFLAGS $LDFLAGS $emitbc $objs $LIBS $ccpr"
2567 elif test 1 = $pm; then
2568         for file in $SRCS; do
2569                 test -f $file || file=$srcdir/$file
2570                 v "$CC $CFLAGS $CPPFLAGS $emitbc $file" &
2571         done
2572         wait
2573 else
2574         for file in $SRCS; do
2575                 test $cm = dragonegg && \
2576                     op=`echo x"$file" | sed 's/^x\(.*\)\.c$/\1./'`
2577                 test -f $file || file=$srcdir/$file
2578                 v "$CC $CFLAGS $CPPFLAGS $emitbc $file" || exit 1
2579                 if test $cm = dragonegg; then
2580                         v "mv ${op}s ${op}ll"
2581                         v "llvm-as ${op}ll" || exit 1
2582                 fi
2583         done
2584 fi
2585 case $cm in
2586 dragonegg|llvm)
2587         rmf $tfn.s
2588         v "llvm-link -o - $objs | opt $optflags | llc -o $tfn.s"
2589         ;;
2590 esac
2591 tcfn=$mkshexe
2592 test $cm = combine || v "$CC $CFLAGS $LDFLAGS -o $tcfn $lobjs $LIBS $ccpr"
2593 test -f $tcfn || exit 1
2594 test 1 = $r || v "$NROFF -mdoc <'$srcdir/mksh.1' >$tfn.cat1" || \
2595     rmf $tfn.cat1
2596 test 0 = $eq && v $SIZE $tcfn
2597 i=install
2598 test -f /usr/ucb/$i && i=/usr/ucb/$i
2599 test 1 = $eq && e=:
2600 $e
2601 $e Installing the shell:
2602 $e "# $i -c -s -o root -g bin -m 555 $tfn /bin/$tfn"
2603 if test $legacy = 0; then
2604         $e "# grep -x /bin/$tfn /etc/shells >/dev/null || echo /bin/$tfn >>/etc/shells"
2605         $e "# $i -c -o root -g bin -m 444 dot.mkshrc /usr/share/doc/mksh/examples/"
2606 fi
2607 $e
2608 $e Installing the manual:
2609 if test -f $tfn.cat1; then
2610         $e "# $i -c -o root -g bin -m 444 $tfn.cat1" \
2611             "/usr/share/man/cat1/$tfn.0"
2612         $e or
2613 fi
2614 $e "# $i -c -o root -g bin -m 444 $tfn.1 /usr/share/man/man1/$tfn.1"
2615 $e
2616 $e Run the regression test suite: ./test.sh
2617 $e Please also read the sample file dot.mkshrc and the fine manual.
2618 exit 0
2619
2620 : <<'EOD'
2621
2622 === Environment used ===
2623
2624 ==== build environment ====
2625 AWK                             default: awk
2626 CC                              default: cc
2627 CFLAGS                          if empty, defaults to -xO2 or +O2
2628                                 or -O3 -qstrict or -O2, per compiler
2629 CPPFLAGS                        default empty
2630 LDFLAGS                         default empty; added before sources
2631 LDSTATIC                        set this to '-static'; default unset
2632 LIBS                            default empty; added after sources
2633                                 [Interix] default: -lcrypt (XXX still needed?)
2634 NOWARN                          -Wno-error or similar
2635 NROFF                           default: nroff
2636 TARGET_OS                       default: $(uname -s || uname)
2637 TARGET_OSREV                    [QNX] default: $(uname -r)
2638
2639 ==== feature selectors ====
2640 USE_PRINTF_BUILTIN              1 to include (unsupported) printf(1) as builtin
2641 ===== general format =====
2642 HAVE_STRLEN                     ac_test
2643 HAVE_STRING_H                   ac_header
2644 HAVE_CAN_FSTACKPROTECTORALL     ac_flags
2645
2646 ==== cpp definitions ====
2647 DEBUG                           dont use in production, wants gcc, implies:
2648 DEBUG_LEAKS                     enable freeing resources before exiting
2649 MKSHRC_PATH                     "~/.mkshrc" (do not change)
2650 MKSH_A4PB                       force use of arc4random_pushb
2651 MKSH_ASSUME_UTF8                (0=disabled, 1=enabled; default: unset)
2652 MKSH_BINSHPOSIX                 if */sh or */-sh, enable set -o posix
2653 MKSH_BINSHREDUCED               if */sh or */-sh, enable set -o sh
2654 MKSH_CLRTOEOL_STRING            "\033[K"
2655 MKSH_CLS_STRING                 "\033[;H\033[J"
2656 MKSH_CONSERVATIVE_FDS           fd 0-9 for scripts, shell only up to 31
2657 MKSH_DEFAULT_EXECSHELL          "/bin/sh" (do not change)
2658 MKSH_DEFAULT_PROFILEDIR         "/etc" (do not change)
2659 MKSH_DEFAULT_TMPDIR             "/tmp" (do not change)
2660 MKSH_DISABLE_DEPRECATED         disable code paths scheduled for later removal
2661 MKSH_DISABLE_EXPERIMENTAL       disable code not yet comfy for (LTS) snapshots
2662 MKSH_DISABLE_TTY_WARNING        shut up warning about ctty if OS cant be fixed
2663 MKSH_DONT_EMIT_IDSTRING         omit RCS IDs from binary
2664 MKSH_MIDNIGHTBSD01ASH_COMPAT    set -o sh: additional compatibility quirk
2665 MKSH_NOPROSPECTOFWORK           disable jobs, co-processes, etc. (do not use)
2666 MKSH_NOPWNAM                    skip PAM calls, for -static on glibc or Solaris
2667 MKSH_NO_CMDLINE_EDITING         disable command line editing code entirely
2668 MKSH_NO_DEPRECATED_WARNING      omit warning when deprecated stuff is run
2669 MKSH_NO_LIMITS                  omit ulimit code
2670 MKSH_NO_SIGSETJMP               define if sigsetjmp is broken or not available
2671 MKSH_NO_SIGSUSPEND              use sigprocmask+pause instead of sigsuspend
2672 MKSH_SMALL                      omit some code, optimise hard for size (slower)
2673 MKSH_SMALL_BUT_FAST             disable some hard-for-size optim. (modern sys.)
2674 MKSH_S_NOVI=1                   disable Vi editing mode (default if MKSH_SMALL)
2675 MKSH_TYPEDEF_SIG_ATOMIC_T       define to e.g. 'int' if sig_atomic_t is missing
2676 MKSH_TYPEDEF_SSIZE_T            define to e.g. 'long' if your OS has no ssize_t
2677 MKSH_UNEMPLOYED                 disable job control (but not jobs/co-processes)
2678
2679 === generic installation instructions ===
2680
2681 Set CC and possibly CFLAGS, CPPFLAGS, LDFLAGS, LIBS. If cross-compiling,
2682 also set TARGET_OS. To disable tests, set e.g. HAVE_STRLCPY=0; to enable
2683 them, set to a value other than 0 or 1. Ensure /bin/ed is installed. For
2684 MKSH_SMALL but with Vi mode, add -DMKSH_S_NOVI=0 to CPPFLAGS as well.
2685
2686 Normally, the following command is what you want to run, then:
2687 $ (sh Build.sh -r -c lto && ./test.sh -f) 2>&1 | tee log
2688
2689 Copy dot.mkshrc to /etc/skel/.mkshrc; install mksh into $prefix/bin; or
2690 /bin; install the manpage, if omitting the -r flag a catmanpage is made
2691 using $NROFF. Consider using a forward script as /etc/skel/.mkshrc like
2692 http://anonscm.debian.org/cgit/collab-maint/mksh.git/plain/debian/.mkshrc
2693 and put dot.mkshrc as /etc/mkshrc so users need not keep up their HOME.
2694
2695 You may also want to install the lksh binary (also as /bin/sh) built by:
2696 $ CPPFLAGS="$CPPFLAGS -DMKSH_BINSHPOSIX" sh Build.sh -L -r -c lto
2697
2698 EOD