• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.459 2010/08/24 15:46:06 tg Exp $'
3#-
4# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5#	Thorsten Glaser <tg@mirbsd.org>
6#
7# Provided that these terms and disclaimer and all copyright notices
8# are retained or reproduced in an accompanying document, permission
9# is granted to deal in this work without restriction, including un-
10# limited rights to use, publicly perform, distribute, sell, modify,
11# merge, give away, or sublicence.
12#
13# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14# the utmost extent permitted by applicable law, neither express nor
15# implied; without malicious intent or gross negligence. In no event
16# may a licensor, author or contributor be held liable for indirect,
17# direct, other damage, loss, or other issues arising in any way out
18# of dealing in the work, even if advised of the possibility of such
19# damage or existence of a defect, except proven that it results out
20# of said person's immediate fault when using the work as intended.
21#-
22# People analysing the output must whitelist conftest.c for any kind
23# of compiler warning checks (mirtoconf is by design not quiet).
24#
25# Environment used:	CC CFLAGS CPPFLAGS LDFLAGS LIBS NOWARN NROFF
26#			TARGET_OS TARGET_OSREV
27# Feature selectors:	USE_PRINTF_BUILTIN
28# CPPFLAGS recognised:	MKSH_ASSUME_UTF8 MKSH_BINSHREDUCED MKSH_CLS_STRING
29#			MKSH_CONSERVATIVE_FDS MKSH_MIDNIGHTBSD01ASH_COMPAT
30#			MKSH_NOPWNAM MKSH_NO_LIMITS MKSH_SMALL MKSH_S_NOVI
31#			MKSH_UNEMPLOYED MKSH_DEFAULT_EXECSHELL MKSHRC_PATH
32#			MKSH_DEFAULT_TMPDIR MKSH_CLRTOEOL_STRING MKSH_A4PB
33
34LC_ALL=C
35export LC_ALL
36
37v() {
38	$e "$*"
39	eval "$@"
40}
41
42vv() {
43	_c=$1
44	shift
45	$e "\$ $*" 2>&1
46	eval "$@" >vv.out 2>&1
47	sed "s^${_c} " <vv.out
48}
49
50vq() {
51	eval "$@"
52}
53
54rmf() {
55	for _f in "$@"; do
56		case ${_f} in
57		mksh.1) ;;
58		*) rm -f "${_f}" ;;
59		esac
60	done
61}
62
63if test -d /usr/xpg4/bin/. >/dev/null 2>&1; then
64	# Solaris: some of the tools have weird behaviour, use portable ones
65	PATH=/usr/xpg4/bin:$PATH
66	export PATH
67fi
68
69if test -n "${ZSH_VERSION+x}" && (emulate sh) >/dev/null 2>&1; then
70	emulate sh
71	NULLCMD=:
72fi
73
74allu=QWERTYUIOPASDFGHJKLZXCVBNM
75alll=qwertyuiopasdfghjklzxcvbnm
76alln=0123456789
77alls=______________________________________________________________
78nl='
79'
80tcfn=no
81bi=
82ui=
83ao=
84fx=
85me=`basename "$0"`
86orig_CFLAGS=$CFLAGS
87phase=x
88oldish_ed=stdout-ed,no-stderr-ed
89
90if test -t 1; then
91	bi=''
92	ui=''
93	ao=''
94fi
95
96upper() {
97	echo :"$@" | sed 's/^://' | tr $alll $allu
98}
99
100# clean up after ac_testrun()
101ac_testdone() {
102	eval HAVE_$fu=$fv
103	fr=no
104	test 0 = $fv || fr=yes
105	$e "$bi==> $fd...$ao $ui$fr$ao$fx"
106	fx=
107}
108
109# ac_cache label: sets f, fu, fv?=0
110ac_cache() {
111	f=$1
112	fu=`upper $f`
113	eval fv=\$HAVE_$fu
114	case $fv in
115	0|1)
116		fx=' (cached)'
117		return 0
118		;;
119	esac
120	fv=0
121	return 1
122}
123
124# ac_testinit label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
125# returns 1 if value was cached/implied, 0 otherwise: call ac_testdone
126ac_testinit() {
127	if ac_cache $1; then
128		test x"$2" = x"!" && shift
129		test x"$2" = x"" || shift
130		fd=${3-$f}
131		ac_testdone
132		return 1
133	fi
134	fc=0
135	if test x"$2" = x""; then
136		ft=1
137	else
138		if test x"$2" = x"!"; then
139			fc=1
140			shift
141		fi
142		eval ft=\$HAVE_`upper $2`
143		shift
144	fi
145	fd=${3-$f}
146	if test $fc = "$ft"; then
147		fv=$2
148		fx=' (implied)'
149		ac_testdone
150		return 1
151	fi
152	$e ... $fd
153	return 0
154}
155
156# pipe .c | ac_test[n] [!] label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
157ac_testn() {
158	if test x"$1" = x"!"; then
159		fr=1
160		shift
161	else
162		fr=0
163	fi
164	ac_testinit "$@" || return
165	cat >conftest.c
166	vv ']' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN conftest.c $LIBS $ccpr"
167	test $tcfn = no && test -f a.out && tcfn=a.out
168	test $tcfn = no && test -f a.exe && tcfn=a.exe
169	test $tcfn = no && test -f conftest && tcfn=conftest
170	if test -f $tcfn; then
171		test 1 = $fr || fv=1
172	else
173		test 0 = $fr || fv=1
174	fi
175	vscan=
176	if test $phase = u; then
177		test $ct = gcc && vscan='unrecogni[sz]ed'
178		test $ct = hpcc && vscan='unsupported'
179		test $ct = pcc && vscan='unsupported'
180		test $ct = sunpro && vscan='-e ignored -e turned.off'
181	fi
182	test -n "$vscan" && grep $vscan vv.out >/dev/null 2>&1 && fv=$fr
183	rmf conftest.c conftest.o ${tcfn}* vv.out
184	ac_testdone
185}
186
187# ac_ifcpp cppexpr [!] label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
188ac_ifcpp() {
189	expr=$1; shift
190	ac_testn "$@" <<-EOF
191		int main(void) { return (
192		#$expr
193		    0
194		#else
195		/* force a failure: expr is false */
196		    thiswillneverbedefinedIhope()
197		#endif
198		    ); }
199EOF
200	test x"$1" = x"!" && shift
201	f=$1
202	fu=`upper $f`
203	eval fv=\$HAVE_$fu
204	test x"$fv" = x"1"
205}
206
207ac_cppflags() {
208	test x"$1" = x"" || fu=$1
209	fv=$2
210	test x"$2" = x"" && eval fv=\$HAVE_$fu
211	CPPFLAGS="$CPPFLAGS -DHAVE_$fu=$fv"
212}
213
214ac_test() {
215	ac_testn "$@"
216	ac_cppflags
217}
218
219# ac_flags [-] add varname flags [text]
220ac_flags() {
221	if test x"$1" = x"-"; then
222		shift
223		hf=1
224	else
225		hf=0
226	fi
227	fa=$1
228	vn=$2
229	f=$3
230	ft=$4
231	test x"$ft" = x"" && ft="if $f can be used"
232	save_CFLAGS=$CFLAGS
233	CFLAGS="$CFLAGS $f"
234	if test 1 = $hf; then
235		ac_testn can_$vn '' "$ft"
236	else
237		ac_testn can_$vn '' "$ft" <<-'EOF'
238			/* evil apo'stroph in comment test */
239			int main(void) { return (0); }
240		EOF
241	fi
242	eval fv=\$HAVE_CAN_`upper $vn`
243	test 11 = $fa$fv || CFLAGS=$save_CFLAGS
244}
245
246# ac_header [!] header [prereq ...]
247ac_header() {
248	if test x"$1" = x"!"; then
249		na=1
250		shift
251	else
252		na=0
253	fi
254	hf=$1; shift
255	hv=`echo "$hf" | tr -d '\012\015' | tr -c $alll$allu$alln $alls`
256	for i
257	do
258		echo "#include <$i>" >>x
259	done
260	echo "#include <$hf>" >>x
261	echo 'int main(void) { return (0); }' >>x
262	ac_testn "$hv" "" "<$hf>" <x
263	rmf x
264	test 1 = $na || ac_cppflags
265}
266
267addsrcs() {
268	if test x"$1" = x"!"; then
269		fr=0
270		shift
271	else
272		fr=1
273	fi
274	eval i=\$$1
275	test $fr = "$i" && case " $SRCS " in
276	*\ $2\ *)	;;
277	*)		SRCS="$SRCS $2" ;;
278	esac
279}
280
281
282if test -d mksh || test -d mksh.exe; then
283	echo "$me: Error: ./mksh is a directory!" >&2
284	exit 1
285fi
286rmf a.exe* a.out* conftest.c *core lft mksh* no *.bc *.ll *.o \
287    Rebuild.sh signames.inc test.sh x vv.out
288
289curdir=`pwd` srcdir=`dirname "$0"` check_categories=
290test -n "$dirname" || dirname=.
291dstversion=`sed -n '/define MKSH_VERSION/s/^.*"\(.*\)".*$/\1/p' $srcdir/sh.h`
292
293e=echo
294r=0
295eq=0
296pm=0
297cm=normal
298optflags=-std-compile-opts
299last=
300
301for i
302do
303	case $last:$i in
304	c:combine|c:dragonegg|c:llvm)
305		cm=$i
306		last=
307		;;
308	c:*)
309		echo "$me: Unknown option -c '$i'!" >&2
310		exit 1
311		;;
312	o:*)
313		optflags=$i
314		last=
315		;;
316	:-c)
317		last=c
318		;;
319	:-combine)
320		cm=combine
321		echo "$me: Warning: '$i' is deprecated, use '-c combine' instead!" >&2
322		;;
323	:-g)
324		# checker, debug, valgrind build
325		CPPFLAGS="$CPPFLAGS -DDEBUG"
326		CFLAGS="$CFLAGS -g3 -fno-builtin"
327		;;
328	:-j)
329		pm=1
330		;;
331	:-llvm)
332		cm=llvm
333		optflags=-std-compile-opts
334		echo "$me: Warning: '$i' is deprecated, use '-c llvm -O' instead!" >&2
335		;;
336	:-llvm=*)
337		cm=llvm
338		optflags=`echo "x$i" | sed 's/^x-llvm=//'`
339		echo "$me: Warning: '$i' is deprecated, use '-c llvm -o $llvm' instead!" >&2
340		;;
341	:-M)
342		cm=makefile
343		;;
344	:-O)
345		optflags=-std-compile-opts
346		;;
347	:-o)
348		last=o
349		;;
350	:-Q)
351		eq=1
352		;;
353	:-r)
354		r=1
355		;;
356	:-v)
357		echo "Build.sh $srcversion"
358		echo "for mksh $dstversion"
359		exit 0
360		;;
361	:*)
362		echo "$me: Unknown option '$i'!" >&2
363		exit 1
364		;;
365	*)
366		echo "$me: Unknown option -'$last' '$i'!" >&2
367		exit 1
368		;;
369	esac
370done
371if test -n "$last"; then
372	echo "$me: Option -'$last' not followed by argument!" >&2
373	exit 1
374fi
375
376SRCS="lalloc.c edit.c eval.c exec.c expr.c funcs.c histrap.c"
377SRCS="$SRCS jobs.c lex.c main.c misc.c shf.c syn.c tree.c var.c"
378
379if test x"$srcdir" = x"."; then
380	CPPFLAGS="-I. $CPPFLAGS"
381else
382	CPPFLAGS="-I. -I'$srcdir' $CPPFLAGS"
383fi
384
385test x"$TARGET_OS" = x"" && TARGET_OS=`uname -s 2>/dev/null || uname`
386oswarn=
387ccpc=-Wc,
388ccpl=-Wl,
389tsts=
390ccpr='|| for _f in ${tcfn}*; do test x"${_f}" = x"mksh.1" || rm -f "${_f}"; done'
391
392# Configuration depending on OS revision, on OSes that need them
393case $TARGET_OS in
394QNX)
395	test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`uname -r`
396	;;
397esac
398
399# Configuration depending on OS name
400case $TARGET_OS in
401AIX)
402	CPPFLAGS="$CPPFLAGS -D_ALL_SOURCE"
403	: ${HAVE_SETLOCALE_CTYPE=0}
404	;;
405BeOS)
406	oswarn=' and will currently not work'
407	;;
408BSD/OS)
409	: ${HAVE_SETLOCALE_CTYPE=0}
410	;;
411CYGWIN*)
412	: ${HAVE_SETLOCALE_CTYPE=0}
413	;;
414Darwin)
415	;;
416DragonFly)
417	;;
418FreeBSD)
419	;;
420GNU)
421	# define NO_PATH_MAX to use Hurd-only functions
422	CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE -DNO_PATH_MAX"
423	;;
424GNU/kFreeBSD)
425	CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
426	;;
427Haiku)
428	CPPFLAGS="$CPPFLAGS -DMKSH_ASSUME_UTF8"
429	;;
430HP-UX)
431	;;
432Interix)
433	ccpc='-X '
434	ccpl='-Y '
435	CPPFLAGS="$CPPFLAGS -D_ALL_SOURCE"
436	: ${LIBS='-lcrypt'}
437	: ${HAVE_SETLOCALE_CTYPE=0}
438	;;
439IRIX*)
440	: ${HAVE_SETLOCALE_CTYPE=0}
441	;;
442Linux)
443	CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
444	: ${HAVE_REVOKE=0}
445	;;
446MidnightBSD)
447	;;
448Minix)
449	CPPFLAGS="$CPPFLAGS -DMKSH_UNEMPLOYED -DMKSH_CONSERVATIVE_FDS"
450	CPPFLAGS="$CPPFLAGS -D_POSIX_SOURCE -D_POSIX_1_SOURCE=2 -D_MINIX"
451	oldish_ed=no-stderr-ed		# /usr/bin/ed(!) is broken
452	: ${HAVE_SETLOCALE_CTYPE=0}
453	;;
454MirBSD)
455	;;
456NetBSD)
457	;;
458OpenBSD)
459	: ${HAVE_SETLOCALE_CTYPE=0}
460	;;
461OSF1)
462	HAVE_SIG_T=0	# incompatible
463	CPPFLAGS="$CPPFLAGS -D_OSF_SOURCE -D_POSIX_C_SOURCE=200112L"
464	CPPFLAGS="$CPPFLAGS -D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED"
465	: ${HAVE_SETLOCALE_CTYPE=0}
466	;;
467Plan9)
468	CPPFLAGS="$CPPFLAGS -D_POSIX_SOURCE -D_LIMITS_EXTENSION"
469	CPPFLAGS="$CPPFLAGS -D_BSD_EXTENSION -D_SUSV2_SOURCE"
470	oswarn=' and will currently not work'
471	CPPFLAGS="$CPPFLAGS -DMKSH_ASSUME_UTF8 -DMKSH_UNEMPLOYED"
472	;;
473PW32*)
474	HAVE_SIG_T=0	# incompatible
475	oswarn=' and will currently not work'
476	: ${HAVE_SETLOCALE_CTYPE=0}
477	;;
478QNX)
479	CPPFLAGS="$CPPFLAGS -D__NO_EXT_QNX"
480	case $TARGET_OSREV in
481	[012345].*|6.[0123].*|6.4.[01])
482		oldish_ed=no-stderr-ed		# oldish /bin/ed is broken
483		;;
484	esac
485	: ${HAVE_SETLOCALE_CTYPE=0}
486	;;
487SunOS)
488	CPPFLAGS="$CPPFLAGS -D_BSD_SOURCE -D__EXTENSIONS__"
489	;;
490syllable)
491	CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
492	oswarn=' and will currently not work'
493	;;
494ULTRIX)
495	: ${CC=cc -YPOSIX}
496	CPPFLAGS="$CPPFLAGS -Dssize_t=int"
497	: ${HAVE_SETLOCALE_CTYPE=0}
498	;;
499UWIN*)
500	ccpc='-Yc,'
501	ccpl='-Yl,'
502	tsts=" 3<>/dev/tty"
503	oswarn="; it will compile, but the target"
504	oswarn="$oswarn${nl}platform itself is very flakey/unreliable"
505	: ${HAVE_SETLOCALE_CTYPE=0}
506	;;
507*)
508	oswarn='; it may or may not work'
509	;;
510esac
511
512: ${CC=cc} ${NROFF=nroff}
513test 0 = $r && echo | $NROFF -v 2>&1 | grep GNU >/dev/null 2>&1 && \
514    NROFF="$NROFF -c"
515
516# this aids me in tracing FTBFSen without access to the buildd
517$e "Hi from$ao $bi$srcversion$ao on:"
518case $TARGET_OS in
519Darwin)
520	vv '|' "hwprefs machine_type os_type os_class >&2"
521	vv '|' "uname -a >&2"
522	;;
523IRIX*)
524	vv '|' "uname -a >&2"
525	vv '|' "hinv -v >&2"
526	;;
527OSF1)
528	vv '|' "uname -a >&2"
529	vv '|' "/usr/sbin/sizer -v >&2"
530	;;
531*)
532	vv '|' "uname -a >&2"
533	;;
534esac
535test -z "$oswarn" || echo >&2 "
536Warning: mksh has not yet been ported to or tested on your
537operating system '$TARGET_OS'$oswarn. If you can provide
538a shell account to the developer, this may improve; please
539drop us a success or failure notice or even send in diffs.
540"
541$e "$bi$me: Building the MirBSD Korn Shell$ao $ui$dstversion$ao"
542
543#
544# Begin of mirtoconf checks
545#
546$e $bi$me: Scanning for functions... please ignore any errors.$ao
547
548#
549# Compiler: which one?
550#
551# notes:
552# - ICC defines __GNUC__ too
553# - GCC defines __hpux too
554# - LLVM+clang defines __GNUC__ too
555# - nwcc defines __GNUC__ too
556CPP="$CC -E"
557$e ... which compiler seems to be used
558cat >conftest.c <<'EOF'
559#if defined(__ICC) || defined(__INTEL_COMPILER)
560ct=icc
561#elif defined(__xlC__) || defined(__IBMC__)
562ct=xlc
563#elif defined(__SUNPRO_C)
564ct=sunpro
565#elif defined(__ACK__)
566ct=ack
567#elif defined(__BORLANDC__)
568ct=bcc
569#elif defined(__WATCOMC__)
570ct=watcom
571#elif defined(__MWERKS__)
572ct=metrowerks
573#elif defined(__HP_cc)
574ct=hpcc
575#elif defined(__DECC) || (defined(__osf__) && !defined(__GNUC__))
576ct=dec
577#elif defined(__PGI)
578ct=pgi
579#elif defined(__DMC__)
580ct=dmc
581#elif defined(_MSC_VER)
582ct=msc
583#elif defined(__ADSPBLACKFIN__) || defined(__ADSPTS__) || defined(__ADSP21000__)
584ct=adsp
585#elif defined(__IAR_SYSTEMS_ICC__)
586ct=iar
587#elif defined(SDCC)
588ct=sdcc
589#elif defined(__PCC__)
590ct=pcc
591#elif defined(__TenDRA__)
592ct=tendra
593#elif defined(__TINYC__)
594ct=tcc
595#elif defined(__llvm__) && defined(__clang__)
596ct=clang
597#elif defined(__NWCC__)
598ct=nwcc
599#elif defined(__GNUC__)
600ct=gcc
601#elif defined(_COMPILER_VERSION)
602ct=mipspro
603#elif defined(__sgi)
604ct=mipspro
605#elif defined(__hpux) || defined(__hpua)
606ct=hpcc
607#elif defined(__ultrix)
608ct=ucode
609#else
610ct=unknown
611#endif
612EOF
613ct=unknown
614vv ']' "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c | grep ct= | tr -d \\\\015 >x"
615sed 's/^/[ /' x
616eval `cat x`
617rmf x vv.out
618echo 'int main(void) { return (0); }' >conftest.c
619case $ct in
620ack)
621	# work around "the famous ACK const bug"
622	CPPFLAGS="-Dconst= $CPPFLAGS"
623	;;
624adsp)
625	echo >&2 'Warning: Analog Devices C++ compiler for Blackfin, TigerSHARC
626    and SHARC (21000) DSPs detected. This compiler has not yet
627    been tested for compatibility with mksh. Continue at your
628    own risk, please report success/failure to the developers.'
629	;;
630bcc)
631	echo >&2 "Warning: Borland C++ Builder detected. This compiler might
632    produce broken executables. Continue at your own risk,
633    please report success/failure to the developers."
634	;;
635clang)
636	# does not work with current "ccc" compiler driver
637	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
638	# this works, for now
639	vv '|' "${CLANG-clang} -version"
640	# ensure compiler and linker are in sync unless overridden
641	case $CCC_CC:$CCC_LD in
642	:*)	;;
643	*:)	CCC_LD=$CCC_CC; export CCC_LD ;;
644	esac
645	;;
646dec)
647	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
648	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -Wl,-V conftest.c $LIBS"
649	;;
650dmc)
651	echo >&2 "Warning: Digital Mars Compiler detected. When running under"
652	echo >&2 "    UWIN, mksh tends to be unstable due to the limitations"
653	echo >&2 "    of this platform. Continue at your own risk,"
654	echo >&2 "    please report success/failure to the developers."
655	;;
656gcc)
657	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
658	vv '|' 'echo `$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS \
659	    -dumpmachine` gcc`$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN \
660	    $LIBS -dumpversion`'
661	;;
662hpcc)
663	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
664	;;
665iar)
666	echo >&2 'Warning: IAR Systems (http://www.iar.com) compiler for embedded
667    systems detected. This unsupported compiler has not yet
668    been tested for compatibility with mksh. Continue at your
669    own risk, please report success/failure to the developers.'
670	;;
671icc)
672	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
673	;;
674metrowerks)
675	echo >&2 'Warning: Metrowerks C compiler detected. This has not yet
676    been tested for compatibility with mksh. Continue at your
677    own risk, please report success/failure to the developers.'
678	;;
679mipspro)
680	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
681	;;
682msc)
683	ccpr=		# errorlevels are not reliable
684	case $TARGET_OS in
685	Interix)
686		if [[ -n $C89_COMPILER ]]; then
687			C89_COMPILER=`ntpath2posix -c "$C89_COMPILER"`
688		else
689			C89_COMPILER=CL.EXE
690		fi
691		if [[ -n $C89_LINKER ]]; then
692			C89_LINKER=`ntpath2posix -c "$C89_LINKER"`
693		else
694			C89_LINKER=LINK.EXE
695		fi
696		vv '|' "$C89_COMPILER /HELP >&2"
697		vv '|' "$C89_LINKER /LINK >&2"
698		;;
699	esac
700	;;
701nwcc)
702	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
703	;;
704pcc)
705	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -v"
706	;;
707pgi)
708	echo >&2 'Warning: PGI detected. This unknown compiler has not yet
709    been tested for compatibility with mksh. Continue at your
710    own risk, please report success/failure to the developers.'
711	;;
712sdcc)
713	echo >&2 'Warning: sdcc (http://sdcc.sourceforge.net), the small devices
714    C compiler for embedded systems detected. This has not yet
715    been tested for compatibility with mksh. Continue at your
716    own risk, please report success/failure to the developers.'
717	;;
718sunpro)
719	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
720	;;
721tcc)
722	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -v"
723	;;
724tendra)
725	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V 2>&1 | \
726	    fgrep -i -e version -e release"
727	;;
728ucode)
729	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
730	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -Wl,-V conftest.c $LIBS"
731	;;
732watcom)
733	echo >&2 'Warning: Watcom C Compiler detected. This compiler has not yet
734    been tested for compatibility with mksh. Continue at your
735    own risk, please report success/failure to the developers.'
736	;;
737xlc)
738	vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -qversion=verbose"
739	vv '|' "ld -V"
740	;;
741*)
742	ct=unknown
743	;;
744esac
745case $cm in
746dragonegg|llvm)
747	vv '|' "llc -version"
748	;;
749esac
750$e "$bi==> which compiler seems to be used...$ao $ui$ct$ao"
751rmf conftest.c conftest.o conftest a.out* a.exe* vv.out
752
753#
754# Compiler: works as-is, with -Wno-error and -Werror
755#
756save_NOWARN=$NOWARN
757NOWARN=
758DOWARN=
759ac_flags 0 compiler_works '' 'if the compiler works'
760test 1 = $HAVE_CAN_COMPILER_WORKS || exit 1
761HAVE_COMPILER_KNOWN=0
762test $ct = unknown || HAVE_COMPILER_KNOWN=1
763if ac_ifcpp 'if 0' compiler_fails '' \
764    'if the compiler does not fail correctly'; then
765	save_CFLAGS=$CFLAGS
766	: ${HAVE_CAN_DELEXE=x}
767	if test $ct = dmc; then
768		CFLAGS="$CFLAGS ${ccpl}/DELEXECUTABLE"
769		ac_testn can_delexe compiler_fails 0 'for the /DELEXECUTABLE linker option' <<-EOF
770			int main(void) { return (0); }
771		EOF
772	elif test $ct = dec; then
773		CFLAGS="$CFLAGS ${ccpl}-non_shared"
774		ac_testn can_delexe compiler_fails 0 'for the -non_shared linker option' <<-EOF
775			int main(void) { return (0); }
776		EOF
777	else
778		exit 1
779	fi
780	test 1 = $HAVE_CAN_DELEXE || CFLAGS=$save_CFLAGS
781	ac_testn compiler_still_fails '' 'if the compiler still does not fail correctly' <<-EOF
782	EOF
783	test 1 = $HAVE_COMPILER_STILL_FAILS && exit 1
784fi
785if ac_ifcpp 'ifdef __TINYC__' couldbe_tcc '!' compiler_known 0 \
786    'if this could be tcc'; then
787	ct=tcc
788	CPP='cpp -D__TINYC__'
789fi
790
791if test $ct = sunpro; then
792	test x"$save_NOWARN" = x"" && save_NOWARN='-errwarn=%none'
793	ac_flags 0 errwarnnone "$save_NOWARN"
794	test 1 = $HAVE_CAN_ERRWARNNONE || save_NOWARN=
795	ac_flags 0 errwarnall "-errwarn=%all"
796	test 1 = $HAVE_CAN_ERRWARNALL && DOWARN="-errwarn=%all"
797elif test $ct = hpcc; then
798	save_NOWARN=
799	DOWARN=+We
800elif test $ct = mipspro; then
801	save_NOWARN=
802	DOWARN="-diag_error 1-10000"
803elif test $ct = msc; then
804	save_NOWARN="${ccpc}/w"
805	DOWARN="${ccpc}/WX"
806elif test $ct = dmc; then
807	save_NOWARN="${ccpc}-w"
808	DOWARN="${ccpc}-wx"
809elif test $ct = bcc; then
810	save_NOWARN="${ccpc}-w"
811	DOWARN="${ccpc}-w!"
812elif test $ct = dec; then
813	: -msg_* flags not used yet, or is -w2 correct?
814elif test $ct = xlc; then
815	save_NOWARN=-qflag=i:e
816	DOWARN=-qflag=i:i
817elif test $ct = tendra; then
818	save_NOWARN=-w
819elif test $ct = ucode; then
820	save_NOWARN=
821	DOWARN=-w2
822else
823	test x"$save_NOWARN" = x"" && save_NOWARN=-Wno-error
824	ac_flags 0 wnoerror "$save_NOWARN"
825	test 1 = $HAVE_CAN_WNOERROR || save_NOWARN=
826	ac_flags 0 werror -Werror
827	test 1 = $HAVE_CAN_WERROR && DOWARN=-Werror
828fi
829
830test $ct = icc && DOWARN="$DOWARN -wd1419"
831NOWARN=$save_NOWARN
832
833#
834# Compiler: extra flags (-O2 -f* -W* etc.)
835#
836i=`echo :"$orig_CFLAGS" | sed 's/^://' | tr -c -d $alll$allu$alln`
837# optimisation: only if orig_CFLAGS is empty
838test x"$i" = x"" && if test $ct = sunpro; then
839	cat >x <<-'EOF'
840		int main(void) { return (0); }
841		#define __IDSTRING_CONCAT(l,p)	__LINTED__ ## l ## _ ## p
842		#define __IDSTRING_EXPAND(l,p)	__IDSTRING_CONCAT(l,p)
843		#define pad			void __IDSTRING_EXPAND(__LINE__,x)(void) { }
844	EOF
845	yes pad | head -n 256 >>x
846	ac_flags - 1 otwo -xO2 <x
847	rmf x
848elif test $ct = hpcc; then
849	phase=u
850	ac_flags 1 otwo +O2
851	phase=x
852elif test $ct = xlc; then
853	ac_flags 1 othree "-O3 -qstrict"
854	test 1 = $HAVE_CAN_OTHREE || ac_flags 1 otwo -O2
855elif test $ct = tcc || test $ct = tendra; then
856	: no special optimisation
857else
858	ac_flags 1 otwo -O2
859	test 1 = $HAVE_CAN_OTWO || ac_flags 1 optimise -O
860fi
861# other flags: just add them if they are supported
862i=0
863if test $ct = gcc; then
864	# The following tests run with -Werror (gcc only) if possible
865	NOWARN=$DOWARN; phase=u
866	ac_flags 1 fnostrictaliasing -fno-strict-aliasing
867	ac_flags 1 fstackprotectorall -fstack-protector-all
868	ac_flags 1 fwrapv -fwrapv
869	test $cm = combine && ac_flags 0 combine \
870	    '-fwhole-program --combine' \
871	    'if gcc supports -fwhole-program --combine'
872	i=1
873elif test $ct = icc; then
874	ac_flags 1 fnobuiltinsetmode -fno-builtin-setmode
875	ac_flags 1 fnostrictaliasing -fno-strict-aliasing
876	ac_flags 1 fstacksecuritycheck -fstack-security-check
877	i=1
878elif test $ct = sunpro; then
879	phase=u
880	ac_flags 1 v -v
881	ac_flags 1 xc99 -xc99 'for support of ISO C99'
882	ac_flags 1 ipo -xipo 'for cross-module optimisation'
883	phase=x
884elif test $ct = hpcc; then
885	phase=u
886	ac_flags 1 agcc -Agcc 'for support of GCC extensions'
887	ac_flags 1 ac99 -AC99 'for support of ISO C99'
888	phase=x
889elif test $ct = dec; then
890	ac_flags 0 verb -verbose
891	ac_flags 1 rodata -readonly_strings
892elif test $ct = dmc; then
893	ac_flags 1 decl "${ccpc}-r" 'for strict prototype checks'
894	ac_flags 1 schk "${ccpc}-s" 'for stack overflow checking'
895elif test $ct = bcc; then
896	ac_flags 1 strpool "${ccpc}-d" 'if string pooling can be enabled'
897elif test $ct = mipspro; then
898	ac_flags 1 xc99 -c99 'for support of ISO C99'
899	ac_flags 1 fullwarn -fullwarn 'for remark output support'
900elif test $ct = msc; then
901	ac_flags 1 strpool "${ccpc}/GF" 'if string pooling can be enabled'
902	echo 'int main(void) { char test[64] = ""; return (*test); }' >x
903	ac_flags - 1 stackon "${ccpc}/GZ" 'if stack checks can be enabled' <x
904	ac_flags - 1 stckall "${ccpc}/Ge" 'stack checks for all functions' <x
905	ac_flags - 1 secuchk "${ccpc}/GS" 'for compiler security checks' <x
906	rmf x
907	ac_flags 1 wall "${ccpc}/Wall" 'to enable all warnings'
908	ac_flags 1 wp64 "${ccpc}/Wp64" 'to enable 64-bit warnings'
909elif test $ct = xlc; then
910	ac_flags 1 x99 -qlanglvl=extc99
911	test 1 = $HAVE_CAN_X99 || ac_flags 1 c99 -qlanglvl=stdc99
912	ac_flags 1 rodata "-qro -qroconst -qroptr"
913	ac_flags 1 rtcheck -qcheck=all
914	ac_flags 1 rtchkc -qextchk
915	ac_flags 1 wformat "-qformat=all -qformat=nozln"
916	#ac_flags 1 wp64 -qwarn64	# too verbose for now
917elif test $ct = tendra; then
918	ac_flags 0 ysystem -Ysystem
919	test 1 = $HAVE_CAN_YSYSTEM && CPPFLAGS="-Ysystem $CPPFLAGS"
920	ac_flags 1 extansi -Xa
921elif test $ct = tcc; then
922	ac_flags 1 boundschk -b
923elif test $ct = clang; then
924	i=1
925elif test $ct = nwcc; then
926	i=1
927	#broken# ac_flags 1 ssp -stackprotect
928fi
929# flags common to a subset of compilers (run with -Werror on gcc)
930if test 1 = $i; then
931	ac_flags 1 stdg99 -std=gnu99 'for support of ISO C99 + GCC extensions'
932	test 1 = $HAVE_CAN_STDG99 || \
933	    ac_flags 1 stdc99 -std=c99 'for support of ISO C99'
934	ac_flags 1 wall -Wall
935fi
936phase=x
937
938# The following tests run with -Werror or similar (all compilers) if possible
939NOWARN=$DOWARN
940test $ct = pcc && phase=u
941
942#
943# Compiler: check for stuff that only generates warnings
944#
945ac_test attribute_bounded '' 'for __attribute__((bounded))' <<-'EOF'
946	#if defined(__GNUC__) && (__GNUC__ < 2)
947	/* force a failure: gcc 1.42 has a false positive here */
948	int main(void) { return (thiswillneverbedefinedIhope()); }
949	#else
950	#include <string.h>
951	#undef __attribute__
952	int xcopy(const void *, void *, size_t)
953	    __attribute__((bounded (buffer, 1, 3)))
954	    __attribute__((bounded (buffer, 2, 3)));
955	int main(int ac, char *av[]) { return (xcopy(av[0], av[--ac], 1)); }
956	int xcopy(const void *s, void *d, size_t n) {
957		memmove(d, s, n); return ((int)n);
958	}
959	#endif
960EOF
961ac_test attribute_format '' 'for __attribute__((format))' <<-'EOF'
962	#if defined(__GNUC__) && (__GNUC__ < 2)
963	/* force a failure: gcc 1.42 has a false positive here */
964	int main(void) { return (thiswillneverbedefinedIhope()); }
965	#else
966	#include <stdio.h>
967	#undef __attribute__
968	#undef printf
969	extern int printf(const char *format, ...)
970	    __attribute__((format (printf, 1, 2)));
971	int main(int ac, char **av) { return (printf("%s%d", *av, ac)); }
972	#endif
973EOF
974ac_test attribute_nonnull '' 'for __attribute__((nonnull))' <<-'EOF'
975	#if defined(__GNUC__) && (__GNUC__ < 2)
976	/* force a failure: gcc 1.42 has a false positive here */
977	int main(void) { return (thiswillneverbedefinedIhope()); }
978	#else
979	int foo(char *s1, char *s2) __attribute__((nonnull));
980	int bar(char *s1, char *s2) __attribute__((nonnull (1, 2)));
981	int baz(char *s) __attribute__((nonnull (1)));
982	int foo(char *s1, char *s2) { return (bar(s2, s1)); }
983	int bar(char *s1, char *s2) { return (baz(s1) - baz(s2)); }
984	int baz(char *s) { return (*s); }
985	int main(int ac, char **av) { return (ac == foo(av[0], av[ac-1])); }
986	#endif
987EOF
988ac_test attribute_noreturn '' 'for __attribute__((noreturn))' <<-'EOF'
989	#if defined(__GNUC__) && (__GNUC__ < 2)
990	/* force a failure: gcc 1.42 has a false positive here */
991	int main(void) { return (thiswillneverbedefinedIhope()); }
992	#else
993	#include <stdlib.h>
994	#undef __attribute__
995	void fnord(void) __attribute__((noreturn));
996	int main(void) { fnord(); }
997	void fnord(void) { exit(0); }
998	#endif
999EOF
1000ac_test attribute_unused '' 'for __attribute__((unused))' <<-'EOF'
1001	#if defined(__GNUC__) && (__GNUC__ < 2)
1002	/* force a failure: gcc 1.42 has a false positive here */
1003	int main(void) { return (thiswillneverbedefinedIhope()); }
1004	#else
1005	int main(int ac __attribute__((unused)), char **av
1006	    __attribute__((unused))) { return (0); }
1007	#endif
1008EOF
1009ac_test attribute_used '' 'for __attribute__((used))' <<-'EOF'
1010	#if defined(__GNUC__) && (__GNUC__ < 2)
1011	/* force a failure: gcc 1.42 has a false positive here */
1012	int main(void) { return (thiswillneverbedefinedIhope()); }
1013	#else
1014	static const char fnord[] __attribute__((used)) = "42";
1015	int main(void) { return (0); }
1016	#endif
1017EOF
1018
1019# End of tests run with -Werror
1020NOWARN=$save_NOWARN
1021phase=x
1022
1023#
1024# mksh: flavours (full/small mksh, omit certain stuff)
1025#
1026if ac_ifcpp 'ifdef MKSH_SMALL' isset_MKSH_SMALL '' \
1027    "if a reduced-feature mksh is requested"; then
1028	#XXX this sucks; fix it for *all* compilers
1029	case $ct in
1030	clang|icc|nwcc)
1031		ac_flags 1 fnoinline -fno-inline
1032		;;
1033	gcc)
1034		NOWARN=$DOWARN; phase=u
1035		ac_flags 1 fnoinline -fno-inline
1036		NOWARN=$save_NOWARN; phase=x
1037		;;
1038	sunpro)
1039		ac_flags 1 fnoinline -xinline=
1040		;;
1041	xlc)
1042		ac_flags 1 fnoinline -qnoinline
1043		;;
1044	esac
1045
1046	: ${HAVE_MKNOD=0}
1047	: ${HAVE_NICE=0}
1048	: ${HAVE_REVOKE=0}
1049	: ${HAVE_PERSISTENT_HISTORY=0}
1050	check_categories=$check_categories,smksh
1051	HAVE_ISSET_MKSH_CONSERVATIVE_FDS=1	# from sh.h
1052fi
1053ac_ifcpp 'ifdef MKSH_BINSHREDUCED' isset_MKSH_BINSHREDUCED '' \
1054    "if a reduced-feature sh is requested" && \
1055    check_categories=$check_categories,binsh
1056ac_ifcpp 'ifdef MKSH_UNEMPLOYED' isset_MKSH_UNEMPLOYED '' \
1057    "if mksh will be built without job control" && \
1058    check_categories=$check_categories,arge
1059ac_ifcpp 'ifdef MKSH_ASSUME_UTF8' isset_MKSH_ASSUME_UTF8 '' \
1060    'if the default UTF-8 mode is specified' && : ${HAVE_SETLOCALE_CTYPE=0}
1061ac_ifcpp 'ifdef MKSH_CONSERVATIVE_FDS' isset_MKSH_CONSERVATIVE_FDS '' \
1062    'if traditional/conservative fd use is requested' && \
1063    check_categories=$check_categories,convfds
1064
1065#
1066# Environment: headers
1067#
1068ac_header sys/param.h
1069ac_header sys/mkdev.h sys/types.h
1070ac_header sys/mman.h sys/types.h
1071ac_header sys/sysmacros.h
1072ac_header grp.h sys/types.h
1073ac_header libgen.h
1074ac_header libutil.h sys/types.h
1075ac_header paths.h
1076ac_header stdbool.h
1077ac_header stdint.h stdarg.h
1078ac_header strings.h sys/types.h
1079ac_header ulimit.h sys/types.h
1080ac_header values.h
1081
1082#
1083# Environment: definitions
1084#
1085echo '#include <sys/types.h>
1086/* check that off_t can represent 2^63-1 correctly, thx FSF */
1087#define LARGE_OFF_T (((off_t)1 << 62) - 1 + ((off_t)1 << 62))
1088int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 &&
1089    LARGE_OFF_T % 2147483647 == 1) ? 1 : -1];
1090int main(void) { return (0); }' >lft.c
1091ac_testn can_lfs '' "for large file support" <lft.c
1092save_CPPFLAGS=$CPPFLAGS
1093CPPFLAGS="$CPPFLAGS -D_FILE_OFFSET_BITS=64"
1094ac_testn can_lfs_sus '!' can_lfs 0 "... with -D_FILE_OFFSET_BITS=64" <lft.c
1095if test 0 = $HAVE_CAN_LFS_SUS; then
1096	CPPFLAGS="$save_CPPFLAGS -D_LARGE_FILES=1"
1097	ac_testn can_lfs_aix '!' can_lfs 0 "... with -D_LARGE_FILES=1" <lft.c
1098	test 1 = $HAVE_CAN_LFS_AIX || CPPFLAGS=$save_CPPFLAGS
1099fi
1100rmf lft*	# end of large file support test
1101
1102#
1103# Environment: types
1104#
1105ac_test can_inttypes '!' stdint_h 1 "for standard 32-bit integer types" <<-'EOF'
1106	#include <sys/types.h>
1107	#include <stddef.h>
1108	int main(int ac, char **av) { return ((uint32_t)(ptrdiff_t)*av + (int32_t)ac); }
1109EOF
1110ac_test can_ucbints '!' can_inttypes 1 "for UCB 32-bit integer types" <<-'EOF'
1111	#include <sys/types.h>
1112	#include <stddef.h>
1113	int main(int ac, char **av) { return ((u_int32_t)(ptrdiff_t)*av + (int32_t)ac); }
1114EOF
1115ac_test can_int8type '!' stdint_h 1 "for standard 8-bit integer type" <<-'EOF'
1116	#include <sys/types.h>
1117	#include <stddef.h>
1118	int main(int ac, char **av) { return ((uint8_t)(ptrdiff_t)av[ac]); }
1119EOF
1120ac_test can_ucbint8 '!' can_int8type 1 "for UCB 8-bit integer type" <<-'EOF'
1121	#include <sys/types.h>
1122	#include <stddef.h>
1123	int main(int ac, char **av) { return ((u_int8_t)(ptrdiff_t)av[ac]); }
1124EOF
1125
1126ac_test rlim_t <<-'EOF'
1127	#include <sys/types.h>
1128	#include <sys/time.h>
1129	#include <sys/resource.h>
1130	#include <unistd.h>
1131	int main(void) { return ((int)(rlim_t)0); }
1132EOF
1133
1134# only testn: added later below
1135ac_testn sig_t <<-'EOF'
1136	#include <sys/types.h>
1137	#include <signal.h>
1138	#include <stddef.h>
1139	int main(void) { return ((int)(ptrdiff_t)(sig_t)kill(0,0)); }
1140EOF
1141
1142ac_testn sighandler_t '!' sig_t 0 <<-'EOF'
1143	#include <sys/types.h>
1144	#include <signal.h>
1145	#include <stddef.h>
1146	int main(void) { return ((int)(ptrdiff_t)(sighandler_t)kill(0,0)); }
1147EOF
1148if test 1 = $HAVE_SIGHANDLER_T; then
1149	CPPFLAGS="$CPPFLAGS -Dsig_t=sighandler_t"
1150	HAVE_SIG_T=1
1151fi
1152
1153ac_testn __sighandler_t '!' sig_t 0 <<-'EOF'
1154	#include <sys/types.h>
1155	#include <signal.h>
1156	#include <stddef.h>
1157	int main(void) { return ((int)(ptrdiff_t)(__sighandler_t)kill(0,0)); }
1158EOF
1159if test 1 = $HAVE___SIGHANDLER_T; then
1160	CPPFLAGS="$CPPFLAGS -Dsig_t=__sighandler_t"
1161	HAVE_SIG_T=1
1162fi
1163
1164test 1 = $HAVE_SIG_T || CPPFLAGS="$CPPFLAGS -Dsig_t=nosig_t"
1165ac_cppflags SIG_T
1166
1167#
1168# Environment: signals
1169#
1170test x"NetBSD" = x"$TARGET_OS" && $e Ignore the compatibility warning.
1171
1172for what in name list; do
1173	uwhat=`upper $what`
1174	ac_testn sys_sig$what '' "the sys_sig${what}[] array" <<-EOF
1175		extern const char *const sys_sig${what}[];
1176		int main(void) { return (sys_sig${what}[0][0]); }
1177	EOF
1178	ac_testn _sys_sig$what '!' sys_sig$what 0 "the _sys_sig${what}[] array" <<-EOF
1179		extern const char *const _sys_sig${what}[];
1180		int main(void) { return (_sys_sig${what}[0][0]); }
1181	EOF
1182	if eval "test 1 = \$HAVE__SYS_SIG$uwhat"; then
1183		CPPFLAGS="$CPPFLAGS -Dsys_sig$what=_sys_sig$what"
1184		eval "HAVE_SYS_SIG$uwhat=1"
1185	fi
1186	ac_cppflags SYS_SIG$uwhat
1187done
1188
1189ac_test strsignal '!' sys_siglist 0 <<-'EOF'
1190	#include <string.h>
1191	#include <signal.h>
1192	int main(void) { return (strsignal(1)[0]); }
1193EOF
1194
1195#
1196# Environment: library functions
1197#
1198ac_testn flock_ex '' 'flock and mmap' <<-'EOF'
1199	#include <sys/types.h>
1200	#include <sys/file.h>
1201	#include <sys/mman.h>
1202	#include <fcntl.h>
1203	#include <stdlib.h>
1204	int main(void) { return ((void *)mmap(NULL, (size_t)flock(0, LOCK_EX),
1205	    PROT_READ, MAP_PRIVATE, 0, (off_t)0) == (void *)NULL ? 1 :
1206	    munmap(NULL, 0)); }
1207EOF
1208
1209ac_test getrusage <<-'EOF'
1210	#define MKSH_INCLUDES_ONLY
1211	#include "sh.h"
1212	int main(void) {
1213		struct rusage ru;
1214		return (getrusage(RUSAGE_SELF, &ru) +
1215		    getrusage(RUSAGE_CHILDREN, &ru));
1216	}
1217EOF
1218
1219ac_test killpg <<-'EOF'
1220	#include <signal.h>
1221	int main(int ac, char *av[]) { return (av[0][killpg(123, ac)]); }
1222EOF
1223
1224ac_test mknod '' 'if to use mknod(), makedev() and friends' <<-'EOF'
1225	#define MKSH_INCLUDES_ONLY
1226	#include "sh.h"
1227	int main(int ac, char *av[]) {
1228		dev_t dv;
1229		dv = makedev((unsigned int)ac, (unsigned int)av[0][0]);
1230		return (mknod(av[0], (mode_t)0, dv) ? (int)major(dv) :
1231		    (int)minor(dv));
1232	}
1233EOF
1234
1235ac_test mkstemp <<-'EOF'
1236	#include <stdlib.h>
1237	#include <unistd.h>
1238	int main(void) { char tmpl[] = "X"; return (mkstemp(tmpl)); }
1239EOF
1240
1241ac_test nice <<-'EOF'
1242	#include <unistd.h>
1243	int main(void) { return (nice(4)); }
1244EOF
1245
1246ac_test revoke <<-'EOF'
1247	#include <sys/types.h>
1248	#if HAVE_LIBUTIL_H
1249	#include <libutil.h>
1250	#endif
1251	#include <unistd.h>
1252	int main(int ac, char *av[]) { return (ac + revoke(av[0])); }
1253EOF
1254
1255ac_test setlocale_ctype '' 'setlocale(LC_CTYPE, "")' <<-'EOF'
1256	#include <locale.h>
1257	#include <stddef.h>
1258	int main(void) { return ((int)(ptrdiff_t)(void *)setlocale(LC_CTYPE, "")); }
1259EOF
1260
1261ac_test langinfo_codeset setlocale_ctype 0 'nl_langinfo(CODESET)' <<-'EOF'
1262	#include <langinfo.h>
1263	#include <stddef.h>
1264	int main(void) { return ((int)(ptrdiff_t)(void *)nl_langinfo(CODESET)); }
1265EOF
1266
1267ac_test setmode mknod 1 <<-'EOF'
1268	/* XXX imake style */
1269	/* XXX conditions correct? */
1270	#if defined(__MSVCRT__) || defined(__CYGWIN__)
1271	/* force a failure: Win32 setmode() is not what we want... */
1272	int main(void) { return (thiswillneverbedefinedIhope()); }
1273	#else
1274	#include <sys/types.h>
1275	#include <unistd.h>
1276	int main(int ac, char *av[]) { return (getmode(setmode(av[0]),
1277	    (mode_t)ac)); }
1278	#endif
1279EOF
1280
1281ac_test setresugid <<-'EOF'
1282	#include <sys/types.h>
1283	#include <unistd.h>
1284	int main(void) { setresuid(0,0,0); return (setresgid(0,0,0)); }
1285EOF
1286
1287ac_test setgroups setresugid 0 <<-'EOF'
1288	#include <sys/types.h>
1289	#if HAVE_GRP_H
1290	#include <grp.h>
1291	#endif
1292	#include <unistd.h>
1293	int main(void) { gid_t gid = 0; return (setgroups(0, &gid)); }
1294EOF
1295
1296ac_test strcasestr <<-'EOF'
1297	#include <sys/types.h>
1298	#include <stddef.h>
1299	#include <string.h>
1300	#if HAVE_STRINGS_H
1301	#include <strings.h>
1302	#endif
1303	int main(int ac, char *av[]) {
1304		return ((int)(ptrdiff_t)(void *)strcasestr(*av, av[ac]));
1305	}
1306EOF
1307
1308ac_test strlcpy <<-'EOF'
1309	#include <string.h>
1310	int main(int ac, char *av[]) { return (strlcpy(*av, av[1],
1311	    (size_t)ac)); }
1312EOF
1313
1314#
1315# check headers for declarations
1316#
1317save_CC=$CC; save_LDFLAGS=$LDFLAGS; save_LIBS=$LIBS
1318CC="$CC -c -o $tcfn"; LDFLAGS=; LIBS=
1319ac_test '!' flock_decl flock_ex 1 'if flock() does not need to be declared' <<-'EOF'
1320	#define MKSH_INCLUDES_ONLY
1321	#include "sh.h"
1322	long flock(void);		/* this clashes if defined before */
1323	int main(void) { return ((int)flock()); }
1324EOF
1325ac_test '!' revoke_decl revoke 1 'if revoke() does not need to be declared' <<-'EOF'
1326	#define MKSH_INCLUDES_ONLY
1327	#include "sh.h"
1328	long revoke(void);		/* this clashes if defined before */
1329	int main(void) { return ((int)revoke()); }
1330EOF
1331ac_test sys_siglist_decl sys_siglist 1 'if sys_siglist[] does not need to be declared' <<-'EOF'
1332	#define MKSH_INCLUDES_ONLY
1333	#include "sh.h"
1334	int main(void) { return (sys_siglist[0][0]); }
1335EOF
1336CC=$save_CC; LDFLAGS=$save_LDFLAGS; LIBS=$save_LIBS
1337
1338#
1339# other checks
1340#
1341fd='if to use persistent history'
1342ac_cache PERSISTENT_HISTORY || test 0 = $HAVE_FLOCK_EX || fv=1
1343test 1 = $fv || check_categories=$check_categories,no-histfile
1344ac_testdone
1345ac_cppflags
1346
1347#
1348# Compiler: Praeprocessor (only if needed)
1349#
1350test 0 = $HAVE_SYS_SIGNAME && if ac_testinit cpp_dd '' \
1351    'checking if the C Preprocessor supports -dD'; then
1352	echo '#define foo bar' >conftest.c
1353	vv ']' "$CPP $CFLAGS $CPPFLAGS $NOWARN -dD conftest.c >x"
1354	grep '#define foo bar' x >/dev/null 2>&1 && fv=1
1355	rmf conftest.c x vv.out
1356	ac_testdone
1357fi
1358
1359#
1360# End of mirtoconf checks
1361#
1362$e ... done.
1363
1364# Some operating systems have ancient versions of ed(1) writing
1365# the character count to standard output; cope for that
1366echo wq >x
1367ed x <x 2>/dev/null | grep 3 >/dev/null 2>&1 && \
1368    check_categories=$check_categories,$oldish_ed
1369rmf x vv.out
1370
1371if test 0 = $HAVE_SYS_SIGNAME; then
1372	if test 1 = $HAVE_CPP_DD; then
1373		$e Generating list of signal names...
1374	else
1375		$e No list of signal names available via cpp. Falling back...
1376	fi
1377	sigseen=:
1378	echo '#include <signal.h>
1379#ifndef NSIG
1380#if defined(_NSIG)
1381#define NSIG _NSIG
1382#elif defined(SIGMAX)
1383#define NSIG (SIGMAX+1)
1384#endif
1385#endif
1386mksh_cfg: NSIG' >conftest.c
1387	NSIG=`vq "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c" | \
1388	    grep mksh_cfg: | sed 's/^mksh_cfg:[	 ]*\([0-9x ()+-]*\).*$/\1/'`
1389	case $NSIG in
1390	*[\ \(\)+-]*) NSIG=`awk "BEGIN { print $NSIG }"` ;;
1391	esac
1392	printf=printf
1393	(printf hallo) >/dev/null 2>&1 || printf=echo
1394	test $printf = echo || NSIG=`printf %d "$NSIG" 2>/dev/null`
1395	$printf "NSIG=$NSIG ... "
1396	sigs="ABRT ALRM BUS CHLD CLD CONT DIL EMT FPE HUP ILL INFO INT IO IOT"
1397	sigs="$sigs KILL LOST PIPE PROF PWR QUIT RESV SAK SEGV STOP SYS TERM"
1398	sigs="$sigs TRAP TSTP TTIN TTOU URG USR1 USR2 VTALRM WINCH XCPU XFSZ"
1399	test 1 = $HAVE_CPP_DD && test $NSIG -gt 1 && sigs="$sigs "`vq \
1400	    "$CPP $CFLAGS $CPPFLAGS $NOWARN -dD conftest.c" | \
1401	    grep '[	 ]SIG[A-Z0-9]*[	 ]' | \
1402	    sed 's/^\(.*[	 ]SIG\)\([A-Z0-9]*\)\([	 ].*\)$/\2/' | sort`
1403	test $NSIG -gt 1 || sigs=
1404	for name in $sigs; do
1405		echo '#include <signal.h>' >conftest.c
1406		echo mksh_cfg: SIG$name >>conftest.c
1407		vq "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c" | \
1408		    grep mksh_cfg: | \
1409		    sed 's/^mksh_cfg:[	 ]*\([0-9x]*\).*$/\1:'$name/
1410	done | grep -v '^:' | while IFS=: read nr name; do
1411		test $printf = echo || nr=`printf %d "$nr" 2>/dev/null`
1412		test $nr -gt 0 && test $nr -le $NSIG || continue
1413		case $sigseen in
1414		*:$nr:*) ;;
1415		*)	echo "		{ \"$name\", $nr },"
1416			sigseen=$sigseen$nr:
1417			$printf "$name=$nr " >&2
1418			;;
1419		esac
1420	done 2>&1 >signames.inc
1421	rmf conftest.c
1422	$e done.
1423fi
1424
1425addsrcs '!' HAVE_SETMODE setmode.c
1426addsrcs '!' HAVE_STRLCPY strlcpy.c
1427addsrcs USE_PRINTF_BUILTIN printf.c
1428test 1 = "$USE_PRINTF_BUILTIN" && CPPFLAGS="$CPPFLAGS -DMKSH_PRINTF_BUILTIN"
1429test 0 = "$HAVE_SETMODE" && CPPFLAGS="$CPPFLAGS -DHAVE_CONFIG_H -DCONFIG_H_FILENAME=\\\"sh.h\\\""
1430test 1 = "$HAVE_CAN_VERB" && CFLAGS="$CFLAGS -verbose"
1431
1432$e $bi$me: Finished configuration testing, now producing output.$ao
1433
1434files=
1435objs=
1436sp=
1437case $curdir in
1438*\ *)	echo "#!./mksh" >test.sh ;;
1439*)	echo "#!$curdir/mksh" >test.sh ;;
1440esac
1441cat >>test.sh <<-EOF
1442	LC_ALL=C PATH='$PATH'; export LC_ALL PATH
1443	test -n "\$KSH_VERSION" || exit 1
1444	check_categories=$check_categories
1445	print Testing mksh for conformance:
1446	fgrep MirOS: '$srcdir/check.t'
1447	fgrep MIRBSD '$srcdir/check.t'
1448	print "This shell is actually:\\n\\t\$KSH_VERSION"
1449	print 'test.sh built for mksh $dstversion'
1450	cstr='\$os = defined \$^O ? \$^O : "unknown";'
1451	cstr="\$cstr"'print \$os . ", Perl version " . \$];'
1452	for perli in \$PERL perl5 perl no; do
1453		[[ \$perli = no ]] && exit 1
1454		perlos=\$(\$perli -e "\$cstr") 2>&- || continue
1455		print "Perl interpreter '\$perli' running on '\$perlos'"
1456		[[ -n \$perlos ]] && break
1457	done
1458	exec \$perli '$srcdir/check.pl' -s '$srcdir/check.t' -p '$curdir/mksh' \${check_categories:+-C} \${check_categories#,} \$*$tsts
1459EOF
1460chmod 755 test.sh
1461test $HAVE_CAN_COMBINE$cm = 0combine && cm=normal
1462if test $cm = llvm; then
1463	emitbc="-emit-llvm -c"
1464elif test $cm = dragonegg; then
1465	emitbc="-S -flto"
1466else
1467	emitbc=-c
1468fi
1469echo set -x >Rebuild.sh
1470for file in $SRCS; do
1471	op=`echo x"$file" | sed 's/^x\(.*\)\.c$/\1./'`
1472	test -f $file || file=$srcdir/$file
1473	files="$files$sp$file"
1474	sp=' '
1475	echo "$CC $CFLAGS $CPPFLAGS $emitbc $file || exit 1" >>Rebuild.sh
1476	if test $cm = dragonegg; then
1477		echo "mv ${op}s ${op}ll" >>Rebuild.sh
1478		echo "llvm-as ${op}ll || exit 1" >>Rebuild.sh
1479		objs="$objs$sp${op}bc"
1480	else
1481		objs="$objs$sp${op}o"
1482	fi
1483done
1484case $cm in
1485dragonegg|llvm)
1486	echo "rm -f mksh.s" >>Rebuild.sh
1487	echo "llvm-link -o - $objs | opt $optflags | llc -o mksh.s" >>Rebuild.sh
1488	lobjs=mksh.s
1489	;;
1490*)
1491	lobjs=$objs
1492	;;
1493esac
1494case $tcfn in
1495a.exe)	mkshexe=mksh.exe ;;
1496*)	mkshexe=mksh ;;
1497esac
1498echo tcfn=$mkshexe >>Rebuild.sh
1499echo "$CC $CFLAGS $LDFLAGS -o \$tcfn $lobjs $LIBS $ccpr" >>Rebuild.sh
1500echo 'test -f $tcfn || exit 1; size $tcfn' >>Rebuild.sh
1501if test $cm = makefile; then
1502	extras='emacsfn.h sh.h sh_flags.h var_spec.h'
1503	test 0 = $HAVE_SYS_SIGNAME && extras="$extras signames.inc"
1504	cat >Makefrag.inc <<EOF
1505# Makefile fragment for building mksh $dstversion
1506
1507PROG=		$mkshexe
1508MAN=		mksh.1
1509SRCS=		$SRCS
1510SRCS_FP=	$files
1511OBJS_BP=	$objs
1512INDSRCS=	$extras
1513NONSRCS_INST=	dot.mkshrc \$(MAN)
1514NONSRCS_NOINST=	Build.sh Makefile Rebuild.sh check.pl check.t test.sh
1515CC=		$CC
1516CFLAGS=		$CFLAGS
1517CPPFLAGS=	$CPPFLAGS
1518LDFLAGS=	$LDFLAGS
1519LIBS=		$LIBS
1520
1521# not BSD make only:
1522#VPATH=		$srcdir
1523#all: \$(PROG)
1524#\$(PROG): \$(OBJS_BP)
1525#	\$(CC) \$(CFLAGS) \$(LDFLAGS) -o \$@ \$(OBJS_BP) \$(LIBS)
1526#\$(OBJS_BP): \$(SRCS_FP) \$(NONSRCS)
1527#.c.o:
1528#	\$(CC) \$(CFLAGS) \$(CPPFLAGS) -c \$<
1529
1530# for all make variants:
1531#REGRESS_FLAGS=	-v
1532#regress:
1533#	./test.sh \$(REGRESS_FLAGS)
1534
1535# for BSD make only:
1536#.PATH: $srcdir
1537#.include <bsd.prog.mk>
1538EOF
1539	$e
1540	$e Generated Makefrag.inc successfully.
1541	exit 0
1542fi
1543if test $cm = combine; then
1544	objs="-o $mkshexe"
1545	for file in $SRCS; do
1546		test -f $file || file=$srcdir/$file
1547		objs="$objs $file"
1548	done
1549	emitbc="-fwhole-program --combine"
1550	v "$CC $CFLAGS $CPPFLAGS $LDFLAGS $emitbc $objs $LIBS $ccpr"
1551elif test 1 = $pm; then
1552	for file in $SRCS; do
1553		test -f $file || file=$srcdir/$file
1554		v "$CC $CFLAGS $CPPFLAGS $emitbc $file" &
1555	done
1556	wait
1557else
1558	for file in $SRCS; do
1559		test $cm = dragonegg && \
1560		    op=`echo x"$file" | sed 's/^x\(.*\)\.c$/\1./'`
1561		test -f $file || file=$srcdir/$file
1562		v "$CC $CFLAGS $CPPFLAGS $emitbc $file" || exit 1
1563		if test $cm = dragonegg; then
1564			v "mv ${op}s ${op}ll"
1565			v "llvm-as ${op}ll" || exit 1
1566		fi
1567	done
1568fi
1569case $cm in
1570dragonegg|llvm)
1571	rmf mksh.s
1572	v "llvm-link -o - $objs | opt $optflags | llc -o mksh.s"
1573	;;
1574esac
1575tcfn=$mkshexe
1576test $cm = combine || v "$CC $CFLAGS $LDFLAGS -o $tcfn $lobjs $LIBS $ccpr"
1577test -f $tcfn || exit 1
1578test 1 = $r || v "$NROFF -mdoc <'$srcdir/mksh.1' >mksh.cat1" || \
1579    rmf mksh.cat1
1580test 0 = $eq && v size $tcfn
1581i=install
1582test -f /usr/ucb/$i && i=/usr/ucb/$i
1583test 1 = $eq && e=:
1584$e
1585$e Installing the shell:
1586$e "# $i -c -s -o root -g bin -m 555 mksh /bin/mksh"
1587$e "# grep -x /bin/mksh /etc/shells >/dev/null || echo /bin/mksh >>/etc/shells"
1588$e "# $i -c -o root -g bin -m 444 dot.mkshrc /usr/share/doc/mksh/examples/"
1589$e
1590$e Installing the manual:
1591if test -f mksh.cat1; then
1592	$e "# $i -c -o root -g bin -m 444 mksh.cat1" \
1593	    "/usr/share/man/cat1/mksh.0"
1594	$e or
1595fi
1596$e "# $i -c -o root -g bin -m 444 mksh.1 /usr/share/man/man1/mksh.1"
1597$e
1598$e Run the regression test suite: ./test.sh
1599$e Please also read the sample file dot.mkshrc and the fine manual.
1600exit 0
1601