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