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