• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.669.2.4 2015/04/19 19:18:08 tg Exp $'
3#-
4# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5#		2011, 2012, 2013, 2014, 2015
6#	Thorsten “mirabilos” 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=''
240	ui=''
241	ao=''
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
1695rm -f lft.c
1696rmf lft*	# end of large file support test
1697
1698#
1699# Environment: types
1700#
1701ac_test can_inttypes '!' stdint_h 1 "for standard 32-bit integer types" <<-'EOF'
1702	#include <sys/types.h>
1703	#include <stddef.h>
1704	int main(int ac, char **av) { return ((uint32_t)(size_t)*av + (int32_t)ac); }
1705EOF
1706ac_test can_ucbints '!' can_inttypes 1 "for UCB 32-bit integer types" <<-'EOF'
1707	#include <sys/types.h>
1708	#include <stddef.h>
1709	int main(int ac, char **av) { return ((u_int32_t)(size_t)*av + (int32_t)ac); }
1710EOF
1711ac_test can_int8type '!' stdint_h 1 "for standard 8-bit integer type" <<-'EOF'
1712	#include <sys/types.h>
1713	#include <stddef.h>
1714	int main(int ac, char **av) { return ((uint8_t)(size_t)av[ac]); }
1715EOF
1716ac_test can_ucbint8 '!' can_int8type 1 "for UCB 8-bit integer type" <<-'EOF'
1717	#include <sys/types.h>
1718	#include <stddef.h>
1719	int main(int ac, char **av) { return ((u_int8_t)(size_t)av[ac]); }
1720EOF
1721
1722ac_test rlim_t <<-'EOF'
1723	#include <sys/types.h>
1724	#if HAVE_BOTH_TIME_H
1725	#include <sys/time.h>
1726	#include <time.h>
1727	#elif HAVE_SYS_TIME_H
1728	#include <sys/time.h>
1729	#elif HAVE_TIME_H
1730	#include <time.h>
1731	#endif
1732	#if HAVE_SYS_RESOURCE_H
1733	#include <sys/resource.h>
1734	#endif
1735	#include <unistd.h>
1736	int main(void) { return (((int)(rlim_t)0) + isatty(0)); }
1737EOF
1738
1739# only testn: added later below
1740ac_testn sig_t <<-'EOF'
1741	#include <sys/types.h>
1742	#include <signal.h>
1743	#include <stddef.h>
1744	volatile sig_t foo = (sig_t)0;
1745	int main(void) { return (foo == (sig_t)0); }
1746EOF
1747
1748ac_testn sighandler_t '!' sig_t 0 <<-'EOF'
1749	#include <sys/types.h>
1750	#include <signal.h>
1751	#include <stddef.h>
1752	volatile sighandler_t foo = (sighandler_t)0;
1753	int main(void) { return (foo == (sighandler_t)0); }
1754EOF
1755if test 1 = $HAVE_SIGHANDLER_T; then
1756	add_cppflags -Dsig_t=sighandler_t
1757	HAVE_SIG_T=1
1758fi
1759
1760ac_testn __sighandler_t '!' sig_t 0 <<-'EOF'
1761	#include <sys/types.h>
1762	#include <signal.h>
1763	#include <stddef.h>
1764	volatile __sighandler_t foo = (__sighandler_t)0;
1765	int main(void) { return (foo == (__sighandler_t)0); }
1766EOF
1767if test 1 = $HAVE___SIGHANDLER_T; then
1768	add_cppflags -Dsig_t=__sighandler_t
1769	HAVE_SIG_T=1
1770fi
1771
1772test 1 = $HAVE_SIG_T || add_cppflags -Dsig_t=nosig_t
1773ac_cppflags SIG_T
1774
1775#
1776# check whether whatever we use for the final link will succeed
1777#
1778if test $cm = makefile; then
1779	: nothing to check
1780else
1781	HAVE_LINK_WORKS=x
1782	ac_testinit link_works '' 'checking if the final link command may succeed'
1783	fv=1
1784	cat >conftest.c <<-EOF
1785		#define EXTERN
1786		#define MKSH_INCLUDES_ONLY
1787		#include "sh.h"
1788		__RCSID("$srcversion");
1789		int main(void) { printf("Hello, World!\\n"); return (isatty(0)); }
1790EOF
1791	case $cm in
1792	llvm)
1793		v "$CC $CFLAGS $CPPFLAGS $NOWARN -emit-llvm -c conftest.c" || fv=0
1794		rmf $tfn.s
1795		test $fv = 0 || v "llvm-link -o - conftest.o | opt $optflags | llc -o $tfn.s" || fv=0
1796		test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn $tfn.s $LIBS $ccpr"
1797		;;
1798	dragonegg)
1799		v "$CC $CFLAGS $CPPFLAGS $NOWARN -S -flto conftest.c" || fv=0
1800		test $fv = 0 || v "mv conftest.s conftest.ll"
1801		test $fv = 0 || v "llvm-as conftest.ll" || fv=0
1802		rmf $tfn.s
1803		test $fv = 0 || v "llvm-link -o - conftest.bc | opt $optflags | llc -o $tfn.s" || fv=0
1804		test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn $tfn.s $LIBS $ccpr"
1805		;;
1806	combine)
1807		v "$CC $CFLAGS $CPPFLAGS $LDFLAGS -fwhole-program --combine $NOWARN -o $tcfn conftest.c $LIBS $ccpr"
1808		;;
1809	lto|normal)
1810		cm=normal
1811		v "$CC $CFLAGS $CPPFLAGS $NOWARN -c conftest.c" || fv=0
1812		test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn conftest.o $LIBS $ccpr"
1813		;;
1814	esac
1815	test -f $tcfn || fv=0
1816	ac_testdone
1817	test $fv = 1 || exit 1
1818fi
1819
1820#
1821# Environment: errors and signals
1822#
1823test x"NetBSD" = x"$TARGET_OS" && $e Ignore the compatibility warning.
1824
1825ac_testn sys_errlist '' "the sys_errlist[] array and sys_nerr" <<-'EOF'
1826	extern const int sys_nerr;
1827	extern const char * const sys_errlist[];
1828	extern int isatty(int);
1829	int main(void) { return (*sys_errlist[sys_nerr - 1] + isatty(0)); }
1830EOF
1831ac_testn _sys_errlist '!' sys_errlist 0 "the _sys_errlist[] array and _sys_nerr" <<-'EOF'
1832	extern const int _sys_nerr;
1833	extern const char * const _sys_errlist[];
1834	extern int isatty(int);
1835	int main(void) { return (*_sys_errlist[_sys_nerr - 1] + isatty(0)); }
1836EOF
1837if test 1 = "$HAVE__SYS_ERRLIST"; then
1838	add_cppflags -Dsys_nerr=_sys_nerr
1839	add_cppflags -Dsys_errlist=_sys_errlist
1840	HAVE_SYS_ERRLIST=1
1841fi
1842ac_cppflags SYS_ERRLIST
1843
1844for what in name list; do
1845	uwhat=`upper $what`
1846	ac_testn sys_sig$what '' "the sys_sig${what}[] array" <<-EOF
1847		extern const char * const sys_sig${what}[];
1848		extern int isatty(int);
1849		int main(void) { return (sys_sig${what}[0][0] + isatty(0)); }
1850	EOF
1851	ac_testn _sys_sig$what '!' sys_sig$what 0 "the _sys_sig${what}[] array" <<-EOF
1852		extern const char * const _sys_sig${what}[];
1853		extern int isatty(int);
1854		int main(void) { return (_sys_sig${what}[0][0] + isatty(0)); }
1855	EOF
1856	eval uwhat_v=\$HAVE__SYS_SIG$uwhat
1857	if test 1 = "$uwhat_v"; then
1858		add_cppflags -Dsys_sig$what=_sys_sig$what
1859		eval HAVE_SYS_SIG$uwhat=1
1860	fi
1861	ac_cppflags SYS_SIG$uwhat
1862done
1863
1864#
1865# Environment: library functions
1866#
1867ac_test flock <<-'EOF'
1868	#include <sys/types.h>
1869	#include <fcntl.h>
1870	#undef flock
1871	#if HAVE_SYS_FILE_H
1872	#include <sys/file.h>
1873	#endif
1874	int main(void) { return (flock(0, LOCK_EX | LOCK_UN)); }
1875EOF
1876
1877ac_test lock_fcntl '!' flock 1 'whether we can lock files with fcntl' <<-'EOF'
1878	#include <fcntl.h>
1879	#undef flock
1880	int main(void) {
1881		struct flock lks;
1882		lks.l_type = F_WRLCK | F_UNLCK;
1883		return (fcntl(0, F_SETLKW, &lks));
1884	}
1885EOF
1886
1887ac_test getrusage <<-'EOF'
1888	#define MKSH_INCLUDES_ONLY
1889	#include "sh.h"
1890	int main(void) {
1891		struct rusage ru;
1892		return (getrusage(RUSAGE_SELF, &ru) +
1893		    getrusage(RUSAGE_CHILDREN, &ru));
1894	}
1895EOF
1896
1897ac_test getsid <<-'EOF'
1898	#include <unistd.h>
1899	int main(void) { return ((int)getsid(0)); }
1900EOF
1901
1902ac_test gettimeofday <<-'EOF'
1903	#define MKSH_INCLUDES_ONLY
1904	#include "sh.h"
1905	int main(void) { struct timeval tv; return (gettimeofday(&tv, NULL)); }
1906EOF
1907
1908ac_test issetugid <<-'EOF'
1909	#include <unistd.h>
1910	int main(void) { return (issetugid()); }
1911EOF
1912
1913ac_test killpg <<-'EOF'
1914	#include <signal.h>
1915	int main(int ac, char *av[]) { return (av[0][killpg(123, ac)]); }
1916EOF
1917
1918ac_test memmove <<-'EOF'
1919	#include <sys/types.h>
1920	#include <stddef.h>
1921	#include <string.h>
1922	#if HAVE_STRINGS_H
1923	#include <strings.h>
1924	#endif
1925	int main(int ac, char *av[]) {
1926		return (*(int *)(void *)memmove(av[0], av[1], (size_t)ac));
1927	}
1928EOF
1929
1930ac_test mknod '' 'if to use mknod(), makedev() and friends' <<-'EOF'
1931	#define MKSH_INCLUDES_ONLY
1932	#include "sh.h"
1933	int main(int ac, char *av[]) {
1934		dev_t dv;
1935		dv = makedev((unsigned int)ac, (unsigned int)av[0][0]);
1936		return (mknod(av[0], (mode_t)0, dv) ? (int)major(dv) :
1937		    (int)minor(dv));
1938	}
1939EOF
1940
1941ac_test mmap lock_fcntl 0 'for mmap and munmap' <<-'EOF'
1942	#include <sys/types.h>
1943	#if HAVE_SYS_FILE_H
1944	#include <sys/file.h>
1945	#endif
1946	#if HAVE_SYS_MMAN_H
1947	#include <sys/mman.h>
1948	#endif
1949	#include <stddef.h>
1950	#include <stdlib.h>
1951	int main(void) { return ((void *)mmap(NULL, (size_t)0,
1952	    PROT_READ, MAP_PRIVATE, 0, (off_t)0) == (void *)NULL ? 1 :
1953	    munmap(NULL, 0)); }
1954EOF
1955
1956ac_test nice <<-'EOF'
1957	#include <unistd.h>
1958	int main(void) { return (nice(4)); }
1959EOF
1960
1961ac_test revoke <<-'EOF'
1962	#include <sys/types.h>
1963	#if HAVE_LIBUTIL_H
1964	#include <libutil.h>
1965	#endif
1966	#include <unistd.h>
1967	int main(int ac, char *av[]) { return (ac + revoke(av[0])); }
1968EOF
1969
1970ac_test setlocale_ctype '' 'setlocale(LC_CTYPE, "")' <<-'EOF'
1971	#include <locale.h>
1972	#include <stddef.h>
1973	int main(void) { return ((int)(size_t)(void *)setlocale(LC_CTYPE, "")); }
1974EOF
1975
1976ac_test langinfo_codeset setlocale_ctype 0 'nl_langinfo(CODESET)' <<-'EOF'
1977	#include <langinfo.h>
1978	#include <stddef.h>
1979	int main(void) { return ((int)(size_t)(void *)nl_langinfo(CODESET)); }
1980EOF
1981
1982ac_test select <<-'EOF'
1983	#include <sys/types.h>
1984	#if HAVE_BOTH_TIME_H
1985	#include <sys/time.h>
1986	#include <time.h>
1987	#elif HAVE_SYS_TIME_H
1988	#include <sys/time.h>
1989	#elif HAVE_TIME_H
1990	#include <time.h>
1991	#endif
1992	#if HAVE_SYS_BSDTYPES_H
1993	#include <sys/bsdtypes.h>
1994	#endif
1995	#if HAVE_SYS_SELECT_H
1996	#include <sys/select.h>
1997	#endif
1998	#if HAVE_BSTRING_H
1999	#include <bstring.h>
2000	#endif
2001	#include <stddef.h>
2002	#include <stdlib.h>
2003	#include <string.h>
2004	#if HAVE_STRINGS_H
2005	#include <strings.h>
2006	#endif
2007	#include <unistd.h>
2008	int main(void) {
2009		struct timeval tv = { 1, 200000 };
2010		fd_set fds; FD_ZERO(&fds); FD_SET(0, &fds);
2011		return (select(FD_SETSIZE, &fds, NULL, NULL, &tv));
2012	}
2013EOF
2014
2015ac_test setresugid <<-'EOF'
2016	#include <sys/types.h>
2017	#include <unistd.h>
2018	int main(void) { return (setresuid(0,0,0) + setresgid(0,0,0)); }
2019EOF
2020
2021ac_test setgroups setresugid 0 <<-'EOF'
2022	#include <sys/types.h>
2023	#if HAVE_GRP_H
2024	#include <grp.h>
2025	#endif
2026	#include <unistd.h>
2027	int main(void) { gid_t gid = 0; return (setgroups(0, &gid)); }
2028EOF
2029
2030if test x"$et" = x"klibc"; then
2031
2032	ac_testn __rt_sigsuspend '' 'whether klibc uses RT signals' <<-'EOF'
2033		#define MKSH_INCLUDES_ONLY
2034		#include "sh.h"
2035		extern int __rt_sigsuspend(const sigset_t *, size_t);
2036		int main(void) { return (__rt_sigsuspend(NULL, 0)); }
2037EOF
2038
2039	# no? damn! legacy crap ahead!
2040
2041	ac_testn __sigsuspend_s '!' __rt_sigsuspend 1 \
2042	    'whether sigsuspend is usable (1/2)' <<-'EOF'
2043		#define MKSH_INCLUDES_ONLY
2044		#include "sh.h"
2045		extern int __sigsuspend_s(sigset_t);
2046		int main(void) { return (__sigsuspend_s(0)); }
2047EOF
2048	ac_testn __sigsuspend_xxs '!' __sigsuspend_s 1 \
2049	    'whether sigsuspend is usable (2/2)' <<-'EOF'
2050		#define MKSH_INCLUDES_ONLY
2051		#include "sh.h"
2052		extern int __sigsuspend_xxs(int, int, sigset_t);
2053		int main(void) { return (__sigsuspend_xxs(0, 0, 0)); }
2054EOF
2055
2056	if test "000" = "$HAVE___RT_SIGSUSPEND$HAVE___SIGSUSPEND_S$HAVE___SIGSUSPEND_XXS"; then
2057		# no usable sigsuspend(), use pause() *ugh*
2058		add_cppflags -DMKSH_NO_SIGSUSPEND
2059	fi
2060fi
2061
2062ac_test strerror '!' sys_errlist 0 <<-'EOF'
2063	extern char *strerror(int);
2064	int main(int ac, char *av[]) { return (*strerror(*av[ac])); }
2065EOF
2066
2067ac_test strsignal '!' sys_siglist 0 <<-'EOF'
2068	#include <string.h>
2069	#include <signal.h>
2070	int main(void) { return (strsignal(1)[0]); }
2071EOF
2072
2073ac_test strlcpy <<-'EOF'
2074	#include <string.h>
2075	int main(int ac, char *av[]) { return (strlcpy(*av, av[1],
2076	    (size_t)ac)); }
2077EOF
2078
2079#
2080# check headers for declarations
2081#
2082ac_test flock_decl flock 1 'for declaration of flock()' <<-'EOF'
2083	#define MKSH_INCLUDES_ONLY
2084	#include "sh.h"
2085	#if HAVE_SYS_FILE_H
2086	#include <sys/file.h>
2087	#endif
2088	int main(void) { return ((flock)(0, 0)); }
2089EOF
2090ac_test revoke_decl revoke 1 'for declaration of revoke()' <<-'EOF'
2091	#define MKSH_INCLUDES_ONLY
2092	#include "sh.h"
2093	int main(void) { return ((revoke)("")); }
2094EOF
2095ac_test sys_errlist_decl sys_errlist 0 "for declaration of sys_errlist[] and sys_nerr" <<-'EOF'
2096	#define MKSH_INCLUDES_ONLY
2097	#include "sh.h"
2098	int main(void) { return (*sys_errlist[sys_nerr - 1] + isatty(0)); }
2099EOF
2100ac_test sys_siglist_decl sys_siglist 0 'for declaration of sys_siglist[]' <<-'EOF'
2101	#define MKSH_INCLUDES_ONLY
2102	#include "sh.h"
2103	int main(void) { return (sys_siglist[0][0] + isatty(0)); }
2104EOF
2105
2106#
2107# other checks
2108#
2109fd='if to use persistent history'
2110ac_cache PERSISTENT_HISTORY || case $HAVE_MMAP$HAVE_FLOCK$HAVE_LOCK_FCNTL in
211111*|101) fv=1 ;;
2112esac
2113test 1 = $fv || check_categories="$check_categories no-histfile"
2114ac_testdone
2115ac_cppflags
2116
2117save_CFLAGS=$CFLAGS
2118ac_testn compile_time_asserts_$$ '' 'whether compile-time assertions pass' <<-'EOF'
2119	#define MKSH_INCLUDES_ONLY
2120	#include "sh.h"
2121	#ifndef CHAR_BIT
2122	#define CHAR_BIT 8	/* defuse this test on really legacy systems */
2123	#endif
2124	struct ctasserts {
2125	#define cta(name, assertion) char name[(assertion) ? 1 : -1]
2126/* this one should be defined by the standard */
2127cta(char_is_1_char, (sizeof(char) == 1) && (sizeof(signed char) == 1) &&
2128    (sizeof(unsigned char) == 1));
2129cta(char_is_8_bits, ((CHAR_BIT) == 8) && ((int)(unsigned char)0xFF == 0xFF) &&
2130    ((int)(unsigned char)0x100 == 0) && ((int)(unsigned char)(int)-1 == 0xFF));
2131/* the next assertion is probably not really needed */
2132cta(short_is_2_char, sizeof(short) == 2);
2133cta(short_size_no_matter_of_signedness, sizeof(short) == sizeof(unsigned short));
2134/* the next assertion is probably not really needed */
2135cta(int_is_4_char, sizeof(int) == 4);
2136cta(int_size_no_matter_of_signedness, sizeof(int) == sizeof(unsigned int));
2137
2138cta(long_ge_int, sizeof(long) >= sizeof(int));
2139cta(long_size_no_matter_of_signedness, sizeof(long) == sizeof(unsigned long));
2140
2141#ifndef MKSH_LEGACY_MODE
2142/* the next assertion is probably not really needed */
2143cta(ari_is_4_char, sizeof(mksh_ari_t) == 4);
2144/* but this is */
2145cta(ari_has_31_bit, 0 < (mksh_ari_t)(((((mksh_ari_t)1 << 15) << 15) - 1) * 2 + 1));
2146/* the next assertion is probably not really needed */
2147cta(uari_is_4_char, sizeof(mksh_uari_t) == 4);
2148/* but the next three are; we REQUIRE unsigned integer wraparound */
2149cta(uari_has_31_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 2 + 1));
2150cta(uari_has_32_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3));
2151cta(uari_wrap_32_bit,
2152    (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3) >
2153    (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 4));
2154#define NUM 22
2155#else
2156#define NUM 16
2157#endif
2158/* these are always required */
2159cta(ari_is_signed, (mksh_ari_t)-1 < (mksh_ari_t)0);
2160cta(uari_is_unsigned, (mksh_uari_t)-1 > (mksh_uari_t)0);
2161/* we require these to have the precisely same size and assume 2s complement */
2162cta(ari_size_no_matter_of_signedness, sizeof(mksh_ari_t) == sizeof(mksh_uari_t));
2163
2164cta(sizet_size_no_matter_of_signedness, sizeof(ssize_t) == sizeof(size_t));
2165cta(sizet_voidptr_same_size, sizeof(size_t) == sizeof(void *));
2166cta(sizet_funcptr_same_size, sizeof(size_t) == sizeof(void (*)(void)));
2167/* our formatting routines assume this */
2168cta(ptr_fits_in_long, sizeof(size_t) <= sizeof(long));
2169cta(ari_fits_in_long, sizeof(mksh_ari_t) <= sizeof(long));
2170/* for struct alignment people */
2171		char padding[64 - NUM];
2172	};
2173char ctasserts_dblcheck[sizeof(struct ctasserts) == 64 ? 1 : -1];
2174	int main(void) { return (sizeof(ctasserts_dblcheck) + isatty(0)); }
2175EOF
2176CFLAGS=$save_CFLAGS
2177eval test 1 = \$HAVE_COMPILE_TIME_ASSERTS_$$ || exit 1
2178
2179#
2180# extra checks for legacy mksh
2181#
2182if test $legacy = 1; then
2183	ac_test long_32bit '' 'whether long is 32 bit wide' <<-'EOF'
2184		#define MKSH_INCLUDES_ONLY
2185		#include "sh.h"
2186		#ifndef CHAR_BIT
2187		#define CHAR_BIT 0
2188		#endif
2189		struct ctasserts {
2190		#define cta(name, assertion) char name[(assertion) ? 1 : -1]
2191			cta(char_is_8_bits, (CHAR_BIT) == 8);
2192			cta(long_is_32_bits, sizeof(long) == 4);
2193		};
2194		int main(void) { return (sizeof(struct ctasserts)); }
2195EOF
2196
2197	ac_test long_64bit '!' long_32bit 0 'whether long is 64 bit wide' <<-'EOF'
2198		#define MKSH_INCLUDES_ONLY
2199		#include "sh.h"
2200		#ifndef CHAR_BIT
2201		#define CHAR_BIT 0
2202		#endif
2203		struct ctasserts {
2204		#define cta(name, assertion) char name[(assertion) ? 1 : -1]
2205			cta(char_is_8_bits, (CHAR_BIT) == 8);
2206			cta(long_is_64_bits, sizeof(long) == 8);
2207		};
2208		int main(void) { return (sizeof(struct ctasserts)); }
2209EOF
2210
2211	case $HAVE_LONG_32BIT$HAVE_LONG_64BIT in
2212	10) check_categories="$check_categories int:32" ;;
2213	01) check_categories="$check_categories int:64" ;;
2214	*) check_categories="$check_categories int:u" ;;
2215	esac
2216fi
2217
2218#
2219# Compiler: Praeprocessor (only if needed)
2220#
2221test 0 = $HAVE_SYS_SIGNAME && if ac_testinit cpp_dd '' \
2222    'checking if the C Preprocessor supports -dD'; then
2223	echo '#define foo bar' >conftest.c
2224	vv ']' "$CPP $CFLAGS $CPPFLAGS $NOWARN -dD conftest.c >x"
2225	grep '#define foo bar' x >/dev/null 2>&1 && fv=1
2226	rmf conftest.c x vv.out
2227	ac_testdone
2228fi
2229
2230#
2231# End of mirtoconf checks
2232#
2233$e ... done.
2234
2235# Some operating systems have ancient versions of ed(1) writing
2236# the character count to standard output; cope for that
2237echo wq >x
2238ed x <x 2>/dev/null | grep 3 >/dev/null 2>&1 && \
2239    check_categories="$check_categories $oldish_ed"
2240rmf x vv.out
2241
2242if test 0 = $HAVE_SYS_SIGNAME; then
2243	if test 1 = $HAVE_CPP_DD; then
2244		$e Generating list of signal names...
2245	else
2246		$e No list of signal names available via cpp. Falling back...
2247	fi
2248	sigseenone=:
2249	sigseentwo=:
2250	echo '#include <signal.h>
2251#ifndef NSIG
2252#if defined(_NSIG)
2253#define NSIG _NSIG
2254#elif defined(SIGMAX)
2255#define NSIG (SIGMAX+1)
2256#elif defined(_SIGMAX)
2257#define NSIG (_SIGMAX+1)
2258#else
2259/* XXX better error out, see sh.h */
2260#define NSIG 64
2261#endif
2262#endif
2263int
2264mksh_cfg= NSIG
2265;' >conftest.c
2266	# GNU sed 2.03 segfaults when optimising this to sed -n
2267	NSIG=`vq "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c" | \
2268	    grep -v '^#' | \
2269	    sed '/mksh_cfg.*= *$/{
2270		N
2271		s/\n/ /
2272		}' | \
2273	    grep '^ *mksh_cfg *=' | \
2274	    sed 's/^ *mksh_cfg *=[	 ]*\([()0-9x+-][()0-9x+	 -]*\).*$/\1/'`
2275	case $NSIG in
2276	*mksh_cfg*) $e "Error: NSIG='$NSIG'"; NSIG=0 ;;
2277	*[\ \(\)+-]*) NSIG=`"$AWK" "BEGIN { print $NSIG }" </dev/null` ;;
2278	esac
2279	printf=printf
2280	(printf hallo) >/dev/null 2>&1 || printf=echo
2281	test $printf = echo || test "`printf %d 42`" = 42 || printf=echo
2282	test $printf = echo || NSIG=`printf %d "$NSIG" 2>/dev/null`
2283	$printf "NSIG=$NSIG ... "
2284	sigs="ABRT FPE ILL INT SEGV TERM ALRM BUS CHLD CONT HUP KILL PIPE QUIT"
2285	sigs="$sigs STOP TSTP TTIN TTOU USR1 USR2 POLL PROF SYS TRAP URG VTALRM"
2286	sigs="$sigs XCPU XFSZ INFO WINCH EMT IO DIL LOST PWR SAK CLD IOT RESV"
2287	sigs="$sigs STKFLT UNUSED"
2288	test 1 = $HAVE_CPP_DD && test $NSIG -gt 1 && sigs="$sigs "`vq \
2289	    "$CPP $CFLAGS $CPPFLAGS $NOWARN -dD conftest.c" | \
2290	    grep '[	 ]SIG[A-Z0-9][A-Z0-9]*[	 ]' | \
2291	    sed 's/^.*[	 ]SIG\([A-Z0-9][A-Z0-9]*\)[	 ].*$/\1/' | sort`
2292	test $NSIG -gt 1 || sigs=
2293	for name in $sigs; do
2294		case $sigseenone in
2295		*:$name:*) continue ;;
2296		esac
2297		sigseenone=$sigseenone$name:
2298		echo '#include <signal.h>' >conftest.c
2299		echo int >>conftest.c
2300		echo mksh_cfg= SIG$name >>conftest.c
2301		echo ';' >>conftest.c
2302		# GNU sed 2.03 croaks on optimising this, too
2303		vq "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c" | \
2304		    grep -v '^#' | \
2305		    sed '/mksh_cfg.*= *$/{
2306			N
2307			s/\n/ /
2308			}' | \
2309		    grep '^ *mksh_cfg *=' | \
2310		    sed 's/^ *mksh_cfg *=[	 ]*\([0-9][0-9x]*\).*$/:\1 '$name/
2311	done | sed -n '/^:[^ ]/s/^://p' | while read nr name; do
2312		test $printf = echo || nr=`printf %d "$nr" 2>/dev/null`
2313		test $nr -gt 0 && test $nr -le $NSIG || continue
2314		case $sigseentwo in
2315		*:$nr:*) ;;
2316		*)	echo "		{ \"$name\", $nr },"
2317			sigseentwo=$sigseentwo$nr:
2318			$printf "$name=$nr " >&2
2319			;;
2320		esac
2321	done 2>&1 >signames.inc
2322	rmf conftest.c
2323	$e done.
2324fi
2325
2326addsrcs '!' HAVE_STRLCPY strlcpy.c
2327addsrcs USE_PRINTF_BUILTIN printf.c
2328test 1 = "$USE_PRINTF_BUILTIN" && add_cppflags -DMKSH_PRINTF_BUILTIN
2329test 1 = "$HAVE_CAN_VERB" && CFLAGS="$CFLAGS -verbose"
2330add_cppflags -DMKSH_BUILD_R=506
2331
2332$e $bi$me: Finished configuration testing, now producing output.$ao
2333
2334files=
2335objs=
2336sp=
2337case $tcfn in
2338a.exe)	mkshexe=$tfn.exe ;;
2339*)	mkshexe=$tfn ;;
2340esac
2341case $curdir in
2342*\ *)	mkshshebang="#!./$mkshexe" ;;
2343*)	mkshshebang="#!$curdir/$mkshexe" ;;
2344esac
2345cat >test.sh <<-EOF
2346	$mkshshebang
2347	LC_ALL=C PATH='$PATH'; export LC_ALL PATH
2348	test -n "\$KSH_VERSION" || exit 1
2349	set -A check_categories -- $check_categories
2350	pflag='$curdir/$mkshexe'
2351	sflag='$srcdir/check.t'
2352	usee=0 Pflag=0 Sflag=0 uset=0 vflag=1 xflag=0
2353	while getopts "C:e:fPp:QSs:t:v" ch; do case \$ch {
2354	(C)	check_categories[\${#check_categories[*]}]=\$OPTARG ;;
2355	(e)	usee=1; eflag=\$OPTARG ;;
2356	(f)	check_categories[\${#check_categories[*]}]=fastbox ;;
2357	(P)	Pflag=1 ;;
2358	(+P)	Pflag=0 ;;
2359	(p)	pflag=\$OPTARG ;;
2360	(Q)	vflag=0 ;;
2361	(+Q)	vflag=1 ;;
2362	(S)	Sflag=1 ;;
2363	(+S)	Sflag=0 ;;
2364	(s)	sflag=\$OPTARG ;;
2365	(t)	uset=1; tflag=\$OPTARG ;;
2366	(v)	vflag=1 ;;
2367	(+v)	vflag=0 ;;
2368	(*)	xflag=1 ;;
2369	}
2370	done
2371	shift \$((OPTIND - 1))
2372	set -A args -- '$srcdir/check.pl' -p "\$pflag"
2373	x=
2374	for y in "\${check_categories[@]}"; do
2375		x=\$x,\$y
2376	done
2377	if [[ -n \$x ]]; then
2378		args[\${#args[*]}]=-C
2379		args[\${#args[*]}]=\${x#,}
2380	fi
2381	if (( usee )); then
2382		args[\${#args[*]}]=-e
2383		args[\${#args[*]}]=\$eflag
2384	fi
2385	(( Pflag )) && args[\${#args[*]}]=-P
2386	if (( uset )); then
2387		args[\${#args[*]}]=-t
2388		args[\${#args[*]}]=\$tflag
2389	fi
2390	(( vflag )) && args[\${#args[*]}]=-v
2391	(( xflag )) && args[\${#args[*]}]=-x	# force usage by synerr
2392	if [[ -n \$TMPDIR && -d \$TMPDIR/. ]]; then
2393		args[\${#args[*]}]=-T
2394		args[\${#args[*]}]=\$TMPDIR
2395	fi
2396	print Testing mksh for conformance:
2397	fgrep -e MirOS: -e MIRBSD "\$sflag"
2398	print "This shell is actually:\\n\\t\$KSH_VERSION"
2399	print 'test.sh built for mksh $dstversion'
2400	cstr='\$os = defined \$^O ? \$^O : "unknown";'
2401	cstr="\$cstr"'print \$os . ", Perl version " . \$];'
2402	for perli in \$PERL perl5 perl no; do
2403		if [[ \$perli = no ]]; then
2404			print Cannot find a working Perl interpreter, aborting.
2405			exit 1
2406		fi
2407		print "Trying Perl interpreter '\$perli'..."
2408		perlos=\$(\$perli -e "\$cstr")
2409		rv=\$?
2410		print "Errorlevel \$rv, running on '\$perlos'"
2411		if (( rv )); then
2412			print "=> not using"
2413			continue
2414		fi
2415		if [[ -n \$perlos ]]; then
2416			print "=> using it"
2417			break
2418		fi
2419	done
2420	(( Sflag )) || echo + \$perli "\${args[@]}" -s "\$sflag" "\$@"
2421	(( Sflag )) || exec \$perli "\${args[@]}" -s "\$sflag" "\$@"$tsts
2422	# use of the -S option for check.t split into multiple chunks
2423	rv=0
2424	for s in "\$sflag".*; do
2425		echo + \$perli "\${args[@]}" -s "\$s" "\$@"
2426		\$perli "\${args[@]}" -s "\$s" "\$@"$tsts
2427		rc=\$?
2428		(( rv = rv ? rv : rc ))
2429	done
2430	exit \$rv
2431EOF
2432chmod 755 test.sh
2433case $cm in
2434dragonegg)
2435	emitbc="-S -flto"
2436	;;
2437llvm)
2438	emitbc="-emit-llvm -c"
2439	;;
2440*)
2441	emitbc=-c
2442	;;
2443esac
2444echo ": # work around NeXTstep bug" >Rebuild.sh
2445cd "$srcdir"
2446optfiles=`echo *.opt`
2447cd "$curdir"
2448for file in $optfiles; do
2449	echo "echo + Running genopt on '$file'..."
2450	echo "(srcfile='$srcdir/$file'; BUILDSH_RUN_GENOPT=1; . '$srcdir/Build.sh')"
2451done >>Rebuild.sh
2452echo set -x >>Rebuild.sh
2453for file in $SRCS; do
2454	op=`echo x"$file" | sed 's/^x\(.*\)\.c$/\1./'`
2455	test -f $file || file=$srcdir/$file
2456	files="$files$sp$file"
2457	sp=' '
2458	echo "$CC $CFLAGS $CPPFLAGS $emitbc $file || exit 1" >>Rebuild.sh
2459	if test $cm = dragonegg; then
2460		echo "mv ${op}s ${op}ll" >>Rebuild.sh
2461		echo "llvm-as ${op}ll || exit 1" >>Rebuild.sh
2462		objs="$objs$sp${op}bc"
2463	else
2464		objs="$objs$sp${op}o"
2465	fi
2466done
2467case $cm in
2468dragonegg|llvm)
2469	echo "rm -f $tfn.s" >>Rebuild.sh
2470	echo "llvm-link -o - $objs | opt $optflags | llc -o $tfn.s" >>Rebuild.sh
2471	lobjs=$tfn.s
2472	;;
2473*)
2474	lobjs=$objs
2475	;;
2476esac
2477echo tcfn=$mkshexe >>Rebuild.sh
2478echo "$CC $CFLAGS $LDFLAGS -o \$tcfn $lobjs $LIBS $ccpr" >>Rebuild.sh
2479echo "test -f \$tcfn || exit 1; $SIZE \$tcfn" >>Rebuild.sh
2480if test $cm = makefile; then
2481	extras='emacsfn.h rlimits.opt sh.h sh_flags.opt var_spec.h'
2482	test 0 = $HAVE_SYS_SIGNAME && extras="$extras signames.inc"
2483	gens= genq=
2484	for file in $optfiles; do
2485		genf=`basename "$file" | sed 's/.opt$/.gen/'`
2486		gens="$gens $genf"
2487		genq="$genq$nl$genf: $srcdir/Build.sh $srcdir/$file
2488			srcfile=$srcdir/$file; BUILDSH_RUN_GENOPT=1; . $srcdir/Build.sh"
2489	done
2490	cat >Makefrag.inc <<EOF
2491# Makefile fragment for building mksh $dstversion
2492
2493PROG=		$mkshexe
2494MAN=		mksh.1
2495SRCS=		$SRCS
2496SRCS_FP=	$files
2497OBJS_BP=	$objs
2498INDSRCS=	$extras
2499NONSRCS_INST=	dot.mkshrc \$(MAN)
2500NONSRCS_NOINST=	Build.sh Makefile Rebuild.sh check.pl check.t test.sh
2501CC=		$CC
2502CFLAGS=		$CFLAGS
2503CPPFLAGS=	$CPPFLAGS
2504LDFLAGS=	$LDFLAGS
2505LIBS=		$LIBS
2506
2507.depend \$(OBJS_BP):$gens$genq
2508
2509# not BSD make only:
2510#VPATH=		$srcdir
2511#all: \$(PROG)
2512#\$(PROG): \$(OBJS_BP)
2513#	\$(CC) \$(CFLAGS) \$(LDFLAGS) -o \$@ \$(OBJS_BP) \$(LIBS)
2514#\$(OBJS_BP): \$(SRCS_FP) \$(NONSRCS)
2515#.c.o:
2516#	\$(CC) \$(CFLAGS) \$(CPPFLAGS) -c \$<
2517
2518# for all make variants:
2519#REGRESS_FLAGS=	-f
2520#regress:
2521#	./test.sh \$(REGRESS_FLAGS)
2522check_categories=$check_categories
2523
2524# for BSD make only:
2525#.PATH: $srcdir
2526#.include <bsd.prog.mk>
2527EOF
2528	$e
2529	$e Generated Makefrag.inc successfully.
2530	exit 0
2531fi
2532for file in $optfiles; do
2533	$e "+ Running genopt on '$file'..."
2534	do_genopt "$srcdir/$file" || exit 1
2535done
2536if test $cm = combine; then
2537	objs="-o $mkshexe"
2538	for file in $SRCS; do
2539		test -f $file || file=$srcdir/$file
2540		objs="$objs $file"
2541	done
2542	emitbc="-fwhole-program --combine"
2543	v "$CC $CFLAGS $CPPFLAGS $LDFLAGS $emitbc $objs $LIBS $ccpr"
2544elif test 1 = $pm; then
2545	for file in $SRCS; do
2546		test -f $file || file=$srcdir/$file
2547		v "$CC $CFLAGS $CPPFLAGS $emitbc $file" &
2548	done
2549	wait
2550else
2551	for file in $SRCS; do
2552		test $cm = dragonegg && \
2553		    op=`echo x"$file" | sed 's/^x\(.*\)\.c$/\1./'`
2554		test -f $file || file=$srcdir/$file
2555		v "$CC $CFLAGS $CPPFLAGS $emitbc $file" || exit 1
2556		if test $cm = dragonegg; then
2557			v "mv ${op}s ${op}ll"
2558			v "llvm-as ${op}ll" || exit 1
2559		fi
2560	done
2561fi
2562case $cm in
2563dragonegg|llvm)
2564	rmf $tfn.s
2565	v "llvm-link -o - $objs | opt $optflags | llc -o $tfn.s"
2566	;;
2567esac
2568tcfn=$mkshexe
2569test $cm = combine || v "$CC $CFLAGS $LDFLAGS -o $tcfn $lobjs $LIBS $ccpr"
2570test -f $tcfn || exit 1
2571test 1 = $r || v "$NROFF -mdoc <'$srcdir/mksh.1' >$tfn.cat1" || \
2572    rmf $tfn.cat1
2573test 0 = $eq && v $SIZE $tcfn
2574i=install
2575test -f /usr/ucb/$i && i=/usr/ucb/$i
2576test 1 = $eq && e=:
2577$e
2578$e Installing the shell:
2579$e "# $i -c -s -o root -g bin -m 555 $tfn /bin/$tfn"
2580if test $legacy = 0; then
2581	$e "# grep -x /bin/$tfn /etc/shells >/dev/null || echo /bin/$tfn >>/etc/shells"
2582	$e "# $i -c -o root -g bin -m 444 dot.mkshrc /usr/share/doc/mksh/examples/"
2583fi
2584$e
2585$e Installing the manual:
2586if test -f $tfn.cat1; then
2587	$e "# $i -c -o root -g bin -m 444 $tfn.cat1" \
2588	    "/usr/share/man/cat1/$tfn.0"
2589	$e or
2590fi
2591$e "# $i -c -o root -g bin -m 444 $tfn.1 /usr/share/man/man1/$tfn.1"
2592$e
2593$e Run the regression test suite: ./test.sh
2594$e Please also read the sample file dot.mkshrc and the fine manual.
2595exit 0
2596
2597: <<'EOD'
2598
2599=== Environment used ===
2600
2601==== build environment ====
2602AWK				default: awk
2603CC				default: cc
2604CFLAGS				if empty, defaults to -xO2 or +O2
2605				or -O3 -qstrict or -O2, per compiler
2606CPPFLAGS			default empty
2607LDFLAGS				default empty; added before sources
2608LDSTATIC			set this to '-static'; default unset
2609LIBS				default empty; added after sources
2610				[Interix] default: -lcrypt (XXX still needed?)
2611NOWARN				-Wno-error or similar
2612NROFF				default: nroff
2613TARGET_OS			default: $(uname -s || uname)
2614TARGET_OSREV			[QNX] default: $(uname -r)
2615
2616==== feature selectors ====
2617USE_PRINTF_BUILTIN		1 to include (unsupported) printf(1) as builtin
2618===== general format =====
2619HAVE_STRLEN			ac_test
2620HAVE_STRING_H			ac_header
2621HAVE_CAN_FSTACKPROTECTORALL	ac_flags
2622
2623==== cpp definitions ====
2624DEBUG				dont use in production, wants gcc, implies:
2625DEBUG_LEAKS			enable freeing resources before exiting
2626MKSHRC_PATH			"~/.mkshrc" (do not change)
2627MKSH_A4PB			force use of arc4random_pushb
2628MKSH_ASSUME_UTF8		(0=disabled, 1=enabled; default: unset)
2629MKSH_BINSHPOSIX			if */sh or */-sh, enable set -o posix
2630MKSH_BINSHREDUCED		if */sh or */-sh, enable set -o sh
2631MKSH_CLRTOEOL_STRING		"\033[K"
2632MKSH_CLS_STRING			"\033[;H\033[J"
2633MKSH_CONSERVATIVE_FDS		fd 0-9 for scripts, shell only up to 31
2634MKSH_DEFAULT_EXECSHELL		"/bin/sh" (do not change)
2635MKSH_DEFAULT_PROFILEDIR		"/etc" (do not change)
2636MKSH_DEFAULT_TMPDIR		"/tmp" (do not change)
2637MKSH_DISABLE_DEPRECATED		disable code paths scheduled for later removal
2638MKSH_DISABLE_EXPERIMENTAL	disable code not yet comfy for (LTS) snapshots
2639MKSH_DISABLE_TTY_WARNING	shut up warning about ctty if OS cant be fixed
2640MKSH_DONT_EMIT_IDSTRING		omit RCS IDs from binary
2641MKSH_MIDNIGHTBSD01ASH_COMPAT	set -o sh: additional compatibility quirk
2642MKSH_NOPROSPECTOFWORK		disable jobs, co-processes, etc. (do not use)
2643MKSH_NOPWNAM			skip PAM calls, for -static on glibc or Solaris
2644MKSH_NO_CMDLINE_EDITING		disable command line editing code entirely
2645MKSH_NO_DEPRECATED_WARNING	omit warning when deprecated stuff is run
2646MKSH_NO_EXTERNAL_CAT		omit hack to skip cat builtin when flags passed
2647MKSH_NO_LIMITS			omit ulimit code
2648MKSH_NO_SIGSETJMP		define if sigsetjmp is broken or not available
2649MKSH_NO_SIGSUSPEND		use sigprocmask+pause instead of sigsuspend
2650MKSH_SMALL			omit some code, optimise hard for size (slower)
2651MKSH_SMALL_BUT_FAST		disable some hard-for-size optim. (modern sys.)
2652MKSH_S_NOVI=1			disable Vi editing mode (default if MKSH_SMALL)
2653MKSH_TYPEDEF_SIG_ATOMIC_T	define to e.g. 'int' if sig_atomic_t is missing
2654MKSH_TYPEDEF_SSIZE_T		define to e.g. 'long' if your OS has no ssize_t
2655MKSH_UNEMPLOYED			disable job control (but not jobs/co-processes)
2656
2657=== generic installation instructions ===
2658
2659Set CC and possibly CFLAGS, CPPFLAGS, LDFLAGS, LIBS. If cross-compiling,
2660also set TARGET_OS. To disable tests, set e.g. HAVE_STRLCPY=0; to enable
2661them, set to a value other than 0 or 1. Ensure /bin/ed is installed. For
2662MKSH_SMALL but with Vi mode, add -DMKSH_S_NOVI=0 to CPPFLAGS as well.
2663
2664Normally, the following command is what you want to run, then:
2665$ (sh Build.sh -r -c lto && ./test.sh -f) 2>&1 | tee log
2666
2667Copy dot.mkshrc to /etc/skel/.mkshrc; install mksh into $prefix/bin; or
2668/bin; install the manpage, if omitting the -r flag a catmanpage is made
2669using $NROFF. Consider using a forward script as /etc/skel/.mkshrc like
2670http://anonscm.debian.org/cgit/collab-maint/mksh.git/plain/debian/.mkshrc
2671and put dot.mkshrc as /etc/mkshrc so users need not keep up their HOME.
2672
2673EOD
2674