configure.inc 34.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#   @(#) configure.inc 1.42@(#)
#   Copyright (c) 1999-2007 David Parsons. All rights reserved.
#   
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#  3. My name may not be used to endorse or promote products derived
#     from this software without specific prior written permission.
#     
#  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``AS IS'' AND ANY
#  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
#  PARSONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
#  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
#  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#  THE POSSIBILITY OF SUCH DAMAGE.
#


#
# this preamble code is executed when this file is sourced and it picks
# interesting things off the command line.
#
ac_default_path="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin"

ac_standard="--src=DIR		where the source lives (.)
--prefix=DIR		where to install the final product (/usr/local)
--execdir=DIR		where to put executables (prefix/bin)
--sbindir=DIR		where to put static executables (prefix/sbin)
--confdir=DIR		where to put configuration information (/etc)
--libdir=DIR		where to put libraries (prefix/lib)
--libexecdir=DIR	where to put private executables
--mandir=DIR		where to put manpages"

__fail=exit

if dirname B/A 2>/dev/null >/dev/null; then
__ac_dirname() {
    dirname "$1"
}
else
__ac_dirname() {
    echo "$1" | sed -e 's:/[^/]*$::'
}
fi

58 59 60 61 62 63 64
__remove() {
    if [ \( -x "$1" \) -a "$__MACOS_DSYM" ]; then
	rm -rf "$1".dSYM
    fi
    rm -f "$@"
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
ac_progname=$0
ac_configure_command=
Q=\'
for x in "$@"; do
    ac_configure_command="$ac_configure_command $Q$x$Q"
done
# ac_configure_command="$*"

__d=`__ac_dirname "$ac_progname"`
if [ "$__d" = "$ac_progname" ]; then
    AC_SRCDIR=`pwd`
else
    AC_SRCDIR=`cd $__d;pwd`
fi

__ac_dir() {
    if test -d "$1"; then
	(cd "$1";pwd)
    else
	echo "$1";
    fi
}

#
# echo w/o newline
#
echononl()
{
    ${ac_echo:-echo} "${@}$ac_echo_nonl"
}

#
# log something to the terminal and to a logfile.
#
LOG () {
    echo "$@"
    echo "$@" 1>&5
}

#
# log something to the terminal without a newline, and to a logfile with
# a newline
#
LOGN () {
    echononl "$@" 1>&5
    echo "$@"
}

#
# log something to the terminal
#
TLOG () {
    echo "$@" 1>&5
}

#
# log something to the terminal, no newline
#
TLOGN () {
    echononl "$@" 1>&5
}


#
# AC_CONTINUE tells configure not to bomb if something fails, but to
# continue blithely along
#
AC_CONTINUE () {
    __fail="return"
}


#
# generate a .o file from sources
#
__MAKEDOTO() {
    AC_PROG_CC

143 144
    if $AC_CC -c -o ngc$$.o "$@" $AC_LIBS 2>ngc$$.err; then
	__remove ngc$$.o ngc$$.err
145 146 147
	TLOG " (found)"
	return 0
    fi
148
    __remove ngc$$.o
149
    TLOG " (not found)"
150
    echo "test failed:  command was $AC_CC -c -o ngc$$.o" "$@" $AC_LIBS
151
    echo "output:"
152 153
    cat ngc$$.err
    __remove ngc$$.err
154 155 156 157 158 159 160 161 162 163 164 165 166 167
    echo "offending sources:"
    for x in "$@"; do
	echo "$x:"
	cat $x
    done
    return 1
}


#
# Emulate gnu autoconf's AC_CHECK_HEADERS() function
#
AC_CHECK_HEADERS () {

168
    echo "/* AC_CHECK_HEADERS */" > ngc$$.c
169
    for hdr in $*; do
170
	echo "#include <$hdr>" >> ngc$$.c
171
    done
172
    echo "int main() { }" >> ngc$$.c
173 174 175

    LOGN "looking for header $hdr"

176
    if __MAKEDOTO ngc$$.c; then
177 178 179 180 181
	AC_DEFINE 'HAVE_'`echo $hdr | $AC_UPPERCASE | tr './' '_'` 1
	rc=0
    else
	rc=1
    fi
182
    __remove ngc$$.c
183 184 185 186 187 188 189 190 191 192 193 194 195
    return $rc
}


#
# emulate GNU autoconf's AC_CHECK_FUNCS function
#
AC_CHECK_FUNCS () {
    AC_PROG_CC

    B=`echo "$1" | sed -e 's/(.*)//'`

    case "$B" in
196 197
    "$1") F="$1()"; need_proto=1 ;;
    *)    F="$1"  ; unset need_proto ;;
198 199 200
    esac

    shift
201
    __remove ngc$$.c
202 203

    while [ "$1" ]; do
204
	echo "#include <$1>" >> ngc$$.c
205 206 207
	shift
    done

208 209 210 211
    if [ "$need_proto" ]; then
	echo "void $F;" >> ngc$$.c
    fi

212
    cat >> ngc$$.c << EOF
213
int main()
214 215 216 217 218 219 220 221
{

    $F;
}
EOF

    LOGN "looking for the $B function"

222
    if $AC_CC -o ngc$$ ngc$$.c $LIBS; then
223 224 225 226 227
	AC_DEFINE `echo ${2:-HAVE_$B} | $AC_UPPERCASE` 1
	TLOG " (found)"
	rc=0
    else
	echo "offending command was:"
228 229
	cat ngc$$.c
	echo "$AC_CC -o ngc$$ ngc$$.c $LIBS"
230 231 232
	TLOG " (not found)"
	rc=1
    fi
233
    __remove ngc$$ ngc$$.c
234 235 236 237 238 239 240 241 242 243 244 245 246
    return $rc
}


#
# check to see if some structure exists
#
# usage: AC_CHECK_STRUCT structure {include ...}
#
AC_CHECK_STRUCT () {
    struct=$1
    shift

247
    __remove ngc$$.c
248 249

    for include in $*; do
250
	echo "#include <$include>" >> ngc$$.c
251 252
    done

253
    cat >> ngc$$.c << EOF
254
int main()
255 256 257 258 259 260 261
{
    struct $struct foo;
}
EOF

    LOGN "looking for struct $struct"

262
    if __MAKEDOTO ngc$$.c; then
263 264 265 266 267
	AC_DEFINE HAVE_STRUCT_`echo ${struct} | $AC_UPPERCASE`
	rc=0
    else
	rc=1
    fi
268
    __remove ngc$$.c
269 270 271 272 273 274 275 276 277 278 279 280 281
    return $rc
}


#
# check to see if some type exists
#
# usage: AC_CHECK_TYPE type {include ...}
#
AC_CHECK_TYPE () {
    type=$1
    shift

282
    __remove ngc$$.c
283 284

    for include in $*; do
285
	echo "#include <$include>" >> ngc$$.c
286 287
    done

288
    cat >> ngc$$.c << EOF
289
int main()
290 291 292 293 294 295 296
{
    $type foo;
}
EOF

    LOGN "looking for $type type"

297
    if __MAKEDOTO ngc$$.c; then
298 299 300 301 302
	AC_DEFINE HAVE_TYPE_`echo ${type} | $AC_UPPERCASE`
	rc=0
    else
	rc=1
    fi
303
    __remove ngc$$.c
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    return $rc
}


#
# check to see if some structure contains a field
#
# usage: AC_CHECK_FIELD structure field {include ...}
#
AC_CHECK_FIELD () {

    struct=$1
    field=$2
    shift 2

319
    __remove ngc$$.c
320 321

    for include in $*;do
322
	echo "#include <$include>" >> ngc$$.c
323 324
    done

325
    cat >> ngc$$.c << EOF
326
int main()
327 328 329 330 331 332 333 334 335
{
    struct $struct foo;

    foo.$field;
}
EOF

    LOGN "checking that struct $struct has a $field field"

336
    if __MAKEDOTO ngc$$.c; then
337 338 339 340 341
	AC_DEFINE HAVE_`echo ${struct}_$field | $AC_UPPERCASE`
	rc=0
    else
	rc=1
    fi
342
    __remove ngc$$.c
343 344 345 346 347 348 349 350 351 352
    return $rc
}


#
# check that the C compiler works
#
AC_PROG_CC () {
    test "$AC_CC" && return 0

353
    cat > ngc$$.c << \EOF
354
#include <stdio.h>
355
int main()
356 357 358 359 360 361 362
{
    puts("hello, sailor");
}
EOF

    TLOGN "checking the C compiler"

363
    unset AC_CFLAGS AC_LDFLAGS __MACOS_DSYM
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

    if [ "$CC" ] ; then
	AC_CC="$CC"
    elif [ "$WITH_PATH" ]; then
	AC_CC=`acLookFor cc`
    elif [ "`acLookFor cc`" ]; then
	# don't specify the full path if the user is looking in their $PATH
	# for a C compiler.
	AC_CC=cc
    fi

    # finally check for POSIX c89
    test "$AC_CC" || AC_CC=`acLookFor c89`

    if [ ! "$AC_CC" ]; then
	TLOG " (no C compiler found)"
	$__fail 1
    fi
    echo "checking out the C compiler"

384
    $AC_CC -o ngc$$ ngc$$.c
385 386 387
    status=$?

    TLOGN " ($AC_CC)"
388

389
    if [ $status -eq 0 ]; then
390 391
	if $AC_CC -x c /dev/null -dM -E 2>&1 | grep '__clang__' >/dev/null; then
	    TLOG " yuck, you're using clang"
392
	    IS_CLANG=T
393 394
	    IS_BROKEN_CC=T
	elif $AC_CC -v 2>&1 | grep 'gcc version' >/dev/null; then
395
	    TLOG " oh ick, it looks like gcc"
396
	    IS_GCC=T
397 398 399 400 401 402 403 404 405 406 407
	    IS_BROKEN_CC=T
	else
	    TLOG " ok"
	fi

	# check that the CFLAGS and LDFLAGS aren't bogus

	unset AC_CFLAGS AC_LDFLAGS

	if [ "$CFLAGS" ]; then
	    test "$CFLAGS" && echo "validating CFLAGS=${CFLAGS}"
408
	    if $AC_CC $CFLAGS -o ngc$$.o ngc$$.c ; then
409 410 411 412 413 414 415 416 417 418
		AC_CFLAGS=${CFLAGS:-"-g"}
		test "$CFLAGS" && echo "CFLAGS=\"${CFLAGS}\" are okay"
	    elif [ "$CFLAGS" ]; then
		echo "ignoring bogus CFLAGS=\"${CFLAGS}\""
	    fi
	else
	    AC_CFLAGS=-g
	fi
	if [ "$LDFLAGS" ]; then
	    test "$LDFLAGS" && echo "validating LDFLAGS=${LDFLAGS}"
419
	    if $AC_CC $LDFLAGS -o ngc$$ ngc$$.o; then
420 421 422 423 424 425 426 427
		AC_LDFLAGS=${LDFLAGS:-"-g"}
		test "$LDFLAGS" && TLOG "LDFLAGS=\"${LDFLAGS}\" are okay"
	    elif [ "$LDFLAGS" ]; then
		TLOG "ignoring bogus LDFLAGS=\"${LDFLAGS}\""
	    fi
	else
	    AC_LDFLAGS=${CFLAGS:-"-g"}
	fi
428 429 430 431 432 433
	
	# macos-specific(?) test for .dSYM resource directories
	$AC_CC $AC_CFLAGS $AC_LDFLAGS -o ngc$$ ngc$$.c
	ls -dl ngc$$*
	test -d ngc$$.dSYM && __MACOS_DSYM=1
	
434 435 436 437
    else
	AC_FAIL " does not compile code properly"
    fi

438
    __remove ngc$$ ngc$$.c ngc$$.o
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

    return $status
}


#
# acLookFor actually looks for a program, without setting anything.
#
acLookFor () {
    path=${AC_PATH:-$ac_default_path}
    case "X$1" in
    X-[rx]) __mode=$1
	    shift
	    ;;
    *)	    __mode=-x
	    ;;
    esac
    oldifs="$IFS"
    for program in $*; do
	IFS=":"
	for x in $path; do
	    if [ $__mode $x/$program -a -f $x/$program ]; then
		echo $x/$program
		break 2
	    fi
	done
    done
    IFS="$oldifs"
    unset __mode
}


#
# check that a program exists and set its path
#
MF_PATH_INCLUDE () {
    SYM=$1; shift

    case X$1 in
    X-[rx]) __mode=$1
	    shift
	    ;;
    *)      unset __mode
	    ;;
    esac

    TLOGN "looking for $1"

    DEST=`acLookFor $__mode $*`

    __sym=`echo "$SYM" | $AC_UPPERCASE`
    if [ "$DEST" ]; then
	TLOG " ($DEST)"
	echo "$1 is $DEST"
	AC_MAK $SYM
	AC_DEFINE PATH_$__sym \""$DEST"\"
	AC_SUB $__sym "$DEST"
	eval CF_$SYM=$DEST
	return 0
    else
	#AC_SUB $__sym ''
	echo "$1 is not found"
	TLOG " (not found)"
	return 1
    fi
}

#
# AC_INIT starts the ball rolling
#
# After AC_INIT, fd's 1 and 2 point to config.log
# and fd 5 points to what used to be fd 1
#
AC_INIT () {
    __config_files="config.cmd config.sub config.h config.mak config.log"
    rm -f $__config_files
    __cwd=`pwd`
516
    exec 5>&1 1>"$__cwd"/config.log 2>&1
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    AC_CONFIGURE_FOR=__AC_`echo $1 | sed -e 's/\..$//' | $AC_UPPERCASE  | tr ' ' '_'`_D

    # check to see whether to use echo -n or echo ...\c
    #
    echo -n hello > $$
    echo world >> $$
    if grep "helloworld" $$ >/dev/null; then
	ac_echo="echo -n"
	echo "[echo -n] works"
    else
	ac_echo="echo"
	echo 'hello\c' > $$
	echo 'world' >> $$
	if grep "helloworld" $$ >/dev/null; then
	    ac_echo_nonl='\c'
	    echo "[echo ...\\c] works"
	fi
    fi
    rm -f $$

    LOG "Configuring for [$1]"

539
    cat > "$__cwd"/config.h << EOF
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
/*
 * configuration for $1${2:+" ($2)"}, generated `date`
 * by ${LOGNAME:-`whoami`}@`hostname`
 */
#ifndef $AC_CONFIGURE_FOR
#define $AC_CONFIGURE_FOR 1


EOF

    unset __share
    if [ -d $AC_PREFIX/share/man ]; then
	for t in 1 2 3 4 5 6 7 8 9; do
	    if [ -d $AC_PREFIX/share/man/man$t ]; then
		__share=/share
	    elif [ -d $AC_PREFIX/share/man/cat$t ]; then
		__share=/share
	    fi
	done
    else
	__share=
    fi

    if [ -d $AC_PREFIX/libexec ]; then
	__libexec=libexec
    else
	__libexec=lib
    fi


    AC_PREFIX=${AC_PREFIX:-/usr/local}
    AC_EXECDIR=${AC_EXECDIR:-$AC_PREFIX/bin}
    AC_SBINDIR=${AC_SBINDIR:-$AC_PREFIX/sbin}
    AC_LIBDIR=${AC_LIBDIR:-$AC_PREFIX/lib}
    AC_MANDIR=${AC_MANDIR:-$AC_PREFIX$__share/man}
    AC_LIBEXEC=${AC_LIBEXEC:-$AC_PREFIX/$__libexec}
    AC_CONFDIR=${AC_CONFDIR:-/etc}

    AC_PATH=${WITH_PATH:-$PATH}
    AC_PROG_CPP
    AC_PROG_INSTALL

    ac_os=`uname -s`
    _os=`echo $ac_os | $AC_UPPERCASE | sed -e 's/[^A-Z0-9_].*$//'`
    AC_DEFINE OS_$_os	1
    eval OS_${_os}=1
    unset _os
}


#
# AC_LIBRARY checks to see if a given library exists and contains the
# given function.
# usage: AC_LIBRARY function library [alternate ...]
#
AC_LIBRARY() {
    SRC=$1
    shift

    __acllibs=
    __aclhdrs=

    for x in "$@"; do
	case X"$x" in
	X-l*) __acllibs="$__acllibs $x" ;;
	*)    __aclhdrs="$__aclhdrs $x" ;;
	esac
    done

    # first see if the function can be found in any of the
    # current libraries
    AC_QUIET AC_CHECK_FUNCS $SRC $__aclhdrs && return 0

    # then search through the list of libraries
    __libs="$LIBS"
    for x in $__acllibs; do
	LIBS="$__libs $x"
	if AC_QUIET AC_CHECK_FUNCS $SRC $__aclhdrs; then
	    AC_LIBS="$AC_LIBS $x"
	    return 0
	fi
    done
    return 1
}


#
# AC_PROG_LEX checks to see if LEX exists, and if it's lex or flex.
#
AC_PROG_LEX() {
    TLOGN "looking for lex "

    DEST=`acLookFor lex`
    if [ "$DEST" ]; then
	AC_MAK LEX
	AC_DEFINE PATH_LEX \"$DEST\"
	AC_SUB 'LEX' "$DEST"
	echo "lex is $DEST"
    else
	DEST=`acLookFor flex`
	if [ "$DEST" ]; then
	    AC_MAK FLEX
	    AC_DEFINE 'LEX' \"$DEST\"
	    AC_SUB 'LEX', "$DEST"
	    echo "lex is $DEST"
	else
	    AC_SUB LEX ''
	    echo "neither lex or flex found"
	    TLOG " (not found)"
	    return 1
	fi
    fi

    if AC_LIBRARY yywrap -ll -lfl; then
	TLOG "($DEST)"
	return 0
    fi
    TLOG "(no lex library found)"
    return 1
}


#
# AC_PROG_YACC checks to see if YACC exists, and if it's bison or
# not.
#
AC_PROG_YACC () {

    TLOGN "looking for yacc "

    DEST=`acLookFor yacc`
    if [ "$DEST" ]; then
	AC_MAK YACC
	AC_DEFINE PATH_YACC \"$DEST\"
	AC_SUB 'YACC' "$DEST"
	TLOG "($DEST)"
	echo "yacc is $DEST"
    else
	DEST=`acLookFor bison`
	if [ "$DEST" ]; then
	    AC_MAK BISON
	    AC_DEFINE 'YACC' \"$DEST\"
	    AC_SUB 'YACC' "$DEST -y"
	    echo "yacc is $DEST -y"
	    TLOG "($DEST -y)"
	else
	    AC_SUB 'YACC' ''
	    echo "neither yacc or bison found"
	    TLOG " (not found)"
	    return 1
	fi
    fi
    return 0
}


#
# AC_PROG looks for a program
#
AC_PROG () {
    PN=`basename $1 | $AC_UPPERCASE | tr -dc $AC_UPPER_PAT`

    if set | grep -v PROG_$PN >/dev/null; then
	TLOGN "looking for $1"
	DEST=`acLookFor $1`
	if [ "$DEST" ]; then
	    eval PROG_$PN="$DEST"
	    AC_SUB $PN $DEST
	    TLOG " ($DEST)"
	    return 0
	fi
	AC_SUB $PN true
	TLOG " (not found)"
	return 1
    fi
}


#
# AC_PROG_LN_S checks to see if ln exists, and, if so, if ln -s works
#
AC_PROG_LN_S () {
    test "$AC_FIND_PROG" || AC_PROG_FIND

    test "$AC_FIND_PROG" || return 1
    
    TLOGN "looking for \"ln -s\""
    DEST=`acLookFor ln`

    if [ "$DEST" ]; then
	rm -f /tmp/b$$
	$DEST -s /tmp/a$$ /tmp/b$$
	if [ "`$AC_FIND_PROG /tmp/b$$ -type l -print`" ]; then
	    TLOG " ($DEST)"
	    echo "$DEST exists, and ln -s works"
	    PROG_LN_S="$DEST -s"
	    AC_SUB 'LN_S' "$DEST -s"
	    rm -f /tmp/b$$
	else
	    AC_SUB 'LN_S' ''
	    TLOG " ($DEST exists, but -s does not seem to work)"
	    echo "$DEST exists, but ln -s doesn't seem to work"
	    rm -f /tmp/b$$
	    return 1
	fi
    else
	AC_SUB 'LN_S' ''
	echo "ln not found"
	TLOG " (not found)"
	return 1
    fi
}


#
# AC_PROG_FIND looks for the find program and sets the FIND environment
# variable
#
AC_PROG_FIND () {
    if test -z "$AC_FIND_PROG"; then
	MF_PATH_INCLUDE FIND find
	rc=$?
	AC_FIND_PROG=$DEST
	return $rc
    fi
    return 0
}


#
# AC_PROG_AWK looks for the awk program and sets the AWK environment
# variable
#
AC_PROG_AWK () {
    if test -z "$AC_AWK_PROG"; then
	MF_PATH_INCLUDE AWK awk
	rc=$?
	AC_AWK_PROG=$DEST
	return $rc
    fi
    return 0
}


#
# AC_PROG_SED looks for the sed program and sets the SED environment
# variable
#
AC_PROG_SED () {
    if test -z "$AC_SED_PROG"; then
	MF_PATH_INCLUDE SED sed
	rc=$?
	AC_SED_PROG=$DEST
	return $rc
    fi
    return 0
}


#
# AC_HEADER_SYS_WAIT looks for sys/wait.h
#
AC_HEADER_SYS_WAIT () {
    AC_CHECK_HEADERS sys/wait.h || return 1
}

#
# AC_TYPE_PID_T checks to see if the pid_t type exists
#
AC_TYPE_PID_T () {

    AC_CHECK_TYPE pid_t sys/types.h
    return $?
}


#
# AC_C_CONST checks to see if the compiler supports the const keyword
#
AC_C_CONST () {
820
    cat > ngc$$.c << EOF
821 822 823 824
const char me=1;
EOF
    LOGN "checking for \"const\" keyword"

825
    if __MAKEDOTO ngc$$.c; then
826 827 828 829 830
	rc=0
    else
	AC_DEFINE 'const' '/**/'
	rc=1
    fi
831
    __remove ngc$$.c
832 833 834 835 836 837 838 839
    return $rc
}


#
# AC_C_VOLATILE checks to see if the compiler supports the volatile keyword
#
AC_C_VOLATILE () {
840
    echo 'f() { volatile char me=1; }' > ngc$$.c
841 842
    LOGN "checking for \"volatile\" keyword"

843
    if __MAKEDOTO ngc$$.c; then
844 845 846 847 848
	rc=0
    else
	AC_DEFINE 'volatile' '/**/'
	rc=1
    fi
849
    __remove ngc$$.c
850 851 852 853
    return $rc
}


854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
#
# AC_C_INLINE checks to see if compiler supports the inline keyword
#
AC_C_INLINE() {
    echo 'inline int foo() { return 1; }' > ngc$$.c
    LOGN 'Checking for "inline" keyword'
    if __MAKEDOTO ngc$$.c; then
	rc=0
    else
	AC_DEFINE inline '/**/'
	rc=1
    fi
    __remove ngc$$.c
    return $rc
}


871 872 873 874
#
# AC_SCALAR_TYPES checks to see if the compiler can generate 2 and 4 byte ints.
#
AC_SCALAR_TYPES () {
875 876 877 878

    rc=1
    LOGN "defining WORD & DWORD scalar types"
    
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
# --- is this needed anymore? ---
#    if AC_QUIET AC_CHECK_HEADERS WinDef.h; then
#	# windows machine; BYTE, WORD, DWORD already
#	# defined
#
#	for x in $@; do
#	    case "$x" in
#	    sub)AC_SUB DWORD DWORD
#		AC_SUB WORD WORD
#		AC_SUB BYTE BYTE
#		;;
#	    *)
#		echo "#include <WinDef.h>" >> "$__cwd"/config.h
#		;;
#	    esac
#	done
#	TLOG " (defined in WinDef.h)"
#	return 0
#    fi
898
	
899
    cat > ngc$$.c << EOF
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
#include <stdio.h>
#include <string.h>

int pound_define = 1;

void
say(char *w, char *v)
{
    printf(pound_define ? "#define %s %s\n"
			: "s:@%s@:%s:g\n", w, v);
}

main(argc, argv)
char **argv;
{
    unsigned long v_long;
    unsigned int v_int;
    unsigned short v_short;

    if ( argc > 1 && strcmp(argv[1], "sub") == 0 )
	pound_define = 0;
	
    if (sizeof v_long == 4)
	say("DWORD", "unsigned long");
    else if (sizeof v_int == 4)
	say("DWORD", "unsigned int");
    else
	exit(1);

    if (sizeof v_int == 2)
	say("WORD", "unsigned int");
    else if (sizeof v_short == 2)
	say("WORD", "unsigned short");
    else
	exit(2);
    say("BYTE", "unsigned char");
    exit(0);
}
EOF
939
    if $AC_CC ngc$$.c -o ngc$$; then
940 941
	while [ "$1" ]; do
	    case "$1" in
942
	    sub)if ./ngc$$ sub >> "$__cwd"/config.sub; then
943 944
		    rc=0
		fi;;
945
	    *)  if ./ngc$$ >> "$__cwd"/config.h; then
946 947 948 949 950 951
		    rc=0
		fi ;;
	    esac
	    shift
	done
	if [ "$rc" != 0 ]; then
952
	    if ./ngc$$ >> "$__cwd"/config.h; then
953 954 955 956
		rc=1
	    fi
	fi
    fi
957
    __remove ngc$$ ngc$$.c
958 959 960 961
    case "$rc" in
    0) TLOG "" ;;
    *) AC_FAIL " ** FAILED **" ;;
    esac
962
    return $rc
963 964 965 966 967 968 969
}


#
# AC_OUTPUT generates makefiles from makefile.in's
#
AC_OUTPUT () {
970 971
    
    cd "$__cwd"
972 973
    AC_SUB 'LIBS'    "$AC_LIBS"
    AC_SUB 'CONFIGURE_FILES' "$__config_files"
974 975 976 977 978 979 980 981
    
    if test "$__MACOS_DSYM"; then
	# deal with extra OSX droppings, if they exist
	AC_SUB 'GENERATED_FILES' "-r *.dSYM $*"
    else
	AC_SUB 'GENERATED_FILES' "$*"
    fi
    
982
    AC_SUB 'CC'      "$AC_CC"
983 984 985 986 987 988 989 990 991 992 993 994 995
    AC_SUB 'CFLAGS'  "$AC_CFLAGS"
    AC_SUB 'LDFLAGS' "$AC_LDFLAGS"
    AC_SUB 'srcdir'  "$AC_SRCDIR"
    AC_SUB 'prefix'  "$AC_PREFIX"
    AC_SUB 'exedir'  "$AC_EXECDIR"
    AC_SUB 'sbindir' "$AC_SBINDIR"
    AC_SUB 'libdir'  "$AC_LIBDIR"
    AC_SUB 'libexec' "$AC_LIBEXEC"
    AC_SUB 'confdir' "$AC_CONFDIR"
    AC_SUB 'mandir'  "$AC_MANDIR"

    if echo "$__config_files" | grep -v librarian.sh >/dev/null; then
	# write a librarian that works with static libraries
996 997 998 999 1000 1001 1002 1003 1004
	if AC_PROG_LN_S ; then
	    __dolink=$PROG_LN_S
	elif AC_PROG ln; then
	    __dolink=$PROG_LN
	elif AC_PROG cp; then
	    __dolink=$PROG_CP
	else
	    __dolink=:
	fi
1005 1006 1007
	AC_PROG ar
	AC_PROG ranlib
	AC_SUB LD_LIBRARY_PATH HERE
1008

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	__config_files="$__config_files librarian.sh"
	cat > librarian.sh << EOF
#! /bin/sh
#
#  Build static libraries, hiding (some) ickiness from the makefile

ACTION=\$1; shift
LIBRARY=\$1; shift
VERSION=\$1; shift

case "\$ACTION" in
make)   ${PROG_AR} crv \$LIBRARY.a "\$@"
	${PROG_RANLIB} \$LIBRARY.a
	rm -f \$LIBRARY
1023
	${__dolink} \$LIBRARY.a \$LIBRARY
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	;;
files)  echo "\${LIBRARY}.a"
	;;
install)$PROG_INSTALL -m 644 \${LIBRARY}.a \$1
	;;
esac
EOF
    fi
    chmod +x librarian.sh

    if [ -r config.sub ]; then
	test "$AC_SED_PROG" || AC_PROG_SED
	test "$AC_SED_PROG" || return 1

	echo                                   >> config.h
	echo "#endif/* ${AC_CONFIGURE_FOR} */" >> config.h

	rm -f config.cmd
	Q=\'
	cat - > config.cmd << EOF
#! /bin/sh
${CC:+CC=${Q}${CC}${Q}} ${CFLAGS:+CFLAGS=${Q}${CFLAGS}${Q}} $ac_progname $ac_configure_command
EOF
	chmod +x config.cmd

	__d=$AC_SRCDIR
	for makefile in $*;do
1051
	    if test -r "$__d/${makefile}.in"; then
1052 1053
		LOG "generating $makefile"
		./config.md `__ac_dirname ./$makefile` 2>/dev/null
1054
		$AC_SED_PROG -f config.sub < "$__d/${makefile}.in" > $makefile
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
		__config_files="$__config_files $makefile"
	    else
		LOG "WARNING: ${makefile}.in does not exist!"
	    fi
	done
	unset __d

    else
	echo 
    fi
}

#
# AC_CHECK_FLOCK checks to see if flock() exists and if the LOCK_NB argument
# works properly.
#
AC_CHECK_FLOCK() {

    AC_CHECK_HEADERS sys/types.h sys/file.h fcntl.h

1075
    cat << EOF > ngc$$.c
1076 1077 1078 1079 1080
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <fcntl.h>

1081
int main()
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
{
    int x = open("$$.c", O_RDWR, 0666);
    int y = open("$$.c", O_RDWR, 0666);

    if (flock(x, LOCK_EX) != 0)
	exit(1);
    if (flock(y, LOCK_EX|LOCK_NB) == 0)
	exit(1);
    exit(0);
}
EOF

    LOGN "checking flock() sanity"
    HAS_FLOCK=0
1096
    if $AC_CC -o ngc$$ ngc$$.c ; then
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	if ./flock ; then
	    LOG " (good)"
	    HAS_FLOCK=1
	    AC_DEFINE HAS_FLOCK
	else
	    LOG " (bad)"
	fi
    else
	LOG " (not found)"
    fi

1108
    __remove ngc$$ ngc$$.c
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

    case "$HAS_FLOCK" in
    0) return 1 ;;
    *) return 0 ;;
    esac
}


#
# AC_CHECK_RESOLVER finds out whether the berkeley resolver is
# present on this system.
#
AC_CHECK_RESOLVER () {
    AC_PROG_CC

    TLOGN "looking for the Berkeley resolver library"

    __ACR_rc=0

1128
    cat > ngc$$.c << EOF
1129 1130 1131 1132 1133
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>

1134
int main()
1135 1136 1137 1138 1139 1140 1141 1142
{
    char bfr[256];

    res_init();
    res_query("hello", C_IN, T_A, bfr, sizeof bfr);
}
EOF

1143
    if $AC_CC -o ngc$$ ngc$$.c; then
1144
	TLOG " (found)"
1145
    elif $AC_CC -o ngc$$ ngc$$.c -lresolv; then
1146
	TLOG " (yes, needs -lresolv)"
1147
	AC_LIBS="$AC_LIBS -lresolv"
1148
    elif $AC_CC -DBIND_8_COMPAT -o ngc$$ ngc$$.c; then
1149
	TLOG " (fyes, needs BIND_8_COMPAT)"
1150
	AC_DEFINE BIND_8_COMPAT 1
1151
    elif $AC_CC -DBIND_8_COMPAT -o ngc$$ ngc$$.c -lresolv; then
1152
	TLOG " (yes, needs BIND_8_COMPAT & -lresolv)"
1153
	AC_DEFINE BIND_8_COMPAT 1
1154
	AC_LIBS="$AC_LIBS -lresolv"
1155 1156 1157 1158
    else
	TLOG " (not found)"
	__ACR_rc=1
    fi
1159
    __remove ngc$$ ngc$$.c
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    return $__ACR_rc
}


#
# AC_CHECK_ALLOCA looks for alloca
#
AC_CHECK_ALLOCA () {

    AC_PROG_CC
    AC_CHECK_HEADERS stdlib.h

1172
    cat - > ngc$$.c << EOF
1173 1174 1175 1176 1177
#if T
# include <alloca.h>
#else
# include <stdlib.h>
#endif
1178
int main()
1179 1180 1181 1182 1183 1184
{
	alloca(10);
}
EOF

    LOGN "looking for the alloca function"
1185
    if $AC_CC -DT ngc$$.c -o ngc$$; then
1186 1187 1188
	AC_DEFINE 'HAVE_ALLOCA_H' 1
	status=0
	TLOG " (found in alloca.h)"
1189
    elif $AC_CC ngc$$.c -o ngc$$; then
1190 1191 1192 1193 1194 1195
	TLOG " (found)"
	status=0
    else
	TLOG " (not found)"
	status=1
    fi
1196
    __remove ngc$$ ngc$$.c
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    return $status

}


#
# AC_CHECK_BASENAME looks for a copy of basename that does NOT use
# a local static buffer to hold results in.
#
AC_CHECK_BASENAME() {
    TLOGN "looking for a reentrant basename "

1209
    cat > ngc$$.c << EOF
1210 1211
#include <string.h>

1212 1213
extern char *basename(char*);

1214
int main()
1215 1216 1217 1218
{
    char *a = basename("/a/test");
    char *b = basename("/a/nother");

1219 1220 1221
    if ( a == 0 || b == 0 || a == b )
	return 0;

1222 1223 1224 1225 1226
    return (strcmp(a,b) != 0) ? 0 : 1;

}
EOF

1227
    if $AC_CC -o ngc$$ ngc$$.c $LIBS; then
1228
	if ./ngc$$; then
1229 1230 1231 1232 1233 1234 1235 1236 1237
	    TLOG "(found)"
	    AC_DEFINE 'HAVE_BASENAME' 1
	    AC_CHECK_HEADERS libgen.h
	else
	    TLOG "(broken)"
	fi
    else
	TLOG "(not found)"
    fi
1238
    __remove ngc$$ ngc$$.c
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
}

#
# AC_COMPILER_PIC checks for the compiler option to produce position independent
# code.  At the moment we assume gcc semantics.
#
AC_COMPILER_PIC () {
    AC_PROG_CC

    LOGN "checking for C compiler option to produce PIC "
1249
    echo "int some_variable = 0;" > ngc$$.c 
1250

1251
    if $AC_CC -c -fPIC -o ngc$$ ngc$$.c $LIBS; then
1252 1253 1254 1255 1256 1257 1258
	AC_CFLAGS="$AC_CFLAGS -fPIC"
        LOG "(-fPIC)"
	__rc=0
    else
        LOG "(none)"
	__rc=1
    fi
1259
    __remove ngc$$ ngc$$.c
1260 1261 1262 1263
    return $__rc
}


1264 1265 1266 1267 1268 1269
# generate a macosX librarian
#
__AC_MACOS_LIBRARIAN() {
    AC_SUB LD_LIBRARY_PATH DYLD_LIBRARY_PATH
    __config_files="$__config_files librarian.sh"
    cat > librarian.sh << EOF
1270 1271
#! /bin/sh
#
1272
#  Build MacOS shared libraries, hiding (some) ickiness from the makefile
1273 1274 1275 1276 1277 1278 1279 1280

ACTION=\$1; shift
LIBRARY=\$1; shift
	
eval \`awk -F. '{ printf "MAJOR=%d\n", \$1;
		  printf "VERSION=%d.%d.%d\n", \$1, \$2, \$3; }' \$1\`
shift

1281 1282
LIBNAME=\$LIBRARY.dylib
FULLNAME=\$LIBNAME
1283 1284

case "\$ACTION" in
1285 1286
make)   FLAGS="$AC_CFLAGS -dynamiclib"
	VFLAGS="-current_version \$VERSION -compatibility_version \$MAJOR"
1287

1288
	rm -f \$LIBRARY
1289 1290 1291 1292
	if $AC_CC \$FLAGS \$VFLAGS -o \$FULLNAME "\$@"; then
	    $PROG_LN_S \$FULLNAME \$LIBRARY
	fi
	;;
1293
files)  echo "\$FULLNAME"
1294 1295 1296 1297 1298
	;;
install)$PROG_INSTALL -c \$FULLNAME "\$1"
	;;
esac
EOF
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
    chmod +x librarian.sh
}


# Generate an ELF librarian (for Linux, freebsd)
#
__AC_ELF_LIBRARIAN() {
    AC_SUB LD_LIBRARY_PATH LD_LIBRARY_PATH
    # -Wl option probably works, but be paranoid anyway
    _VFLAGS="$AC_PICFLAG -shared -Wl,-soname,ngc$$.so.1"
    if $AC_CC $_VFLAGS -o ngc$$.so ngc$$.c; then
	USE_SONAME=T
    fi
    LDCONFIG=`AC_PATH=/sbin:/usr/sbin:/usr/local/sbin acLookFor ldconfig`
    __config_files="$__config_files librarian.sh"
    cat > librarian.sh << EOF
1315 1316
#! /bin/sh
#
1317
#  Build ELF shared libraries, hiding (some) ickiness from the makefile
1318 1319 1320 1321 1322 1323 1324 1325

ACTION=\$1; shift
LIBRARY=\$1; shift
	
eval \`awk -F. '{ printf "MAJOR=%d\n", \$1;
		  printf "VERSION=%d.%d.%d\n", \$1, \$2, \$3; }' \$1\`
shift

1326 1327
LIBNAME=\$LIBRARY.so
FULLNAME=\$LIBNAME.\$VERSION
1328 1329

case "\$ACTION" in
1330 1331 1332
make)   FLAGS="$AC_CFLAGS -shared"
	unset VFLAGS
	test "$USE_SONAME" && VFLAGS="-Wl,-soname,\$LIBNAME.\$MAJOR"
1333

1334
	rm -f \$LIBRARY \$LIBNAME \$LIBNAME.\$MAJOR
1335 1336
	if $AC_CC \$FLAGS \$VFLAGS -o \$FULLNAME "\$@"; then
	    $PROG_LN_S \$FULLNAME \$LIBRARY
1337 1338
	    $PROG_LN_S \$FULLNAME \$LIBNAME
	    $PROG_LN_S \$FULLNAME \$LIBNAME.\$MAJOR
1339 1340
	fi
	;;
1341
files)  echo "\$FULLNAME" "\$LIBNAME" "\$LIBNAME.\$MAJOR"
1342 1343
	;;
install)$PROG_INSTALL -c \$FULLNAME "\$1"
1344 1345
	$PROG_LN_S -f \$FULLNAME \$1/\$LIBNAME.\$MAJOR
	$PROG_LN_S -f \$FULLNAME \$1/\$LIBNAME
1346 1347 1348
EOF
    test "$LDCONFIG" && echo '	'$LDCONFIG '"$1"' >> librarian.sh
    cat >> librarian.sh << EOF
1349 1350 1351
	;;
esac
EOF
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
    chmod +x librarian.sh
}


#
# AC_CC_SHLIBS checks if the C compiler can produce shared libraries
# and if it can writes a librarian that handles those libraries for us.
#
AC_CC_SHLIBS () {
    AC_PROG_CC || AC_FAIL "Need a C compiler to build shared libraries"
    AC_PROG_LN_S || AC_FAIL "Need to be able to make symbolic links for shared libraries"
    AC_PROG_INSTALL || AC_FAIL "Need an install program to install shared libraries"
    LOGN "checking whether the C compiler can build shared libraries "

    echo "int some_variable = 0;" > ngc$$.c 

    if uname -a | grep Darwin >/dev/null; then
	# Claims to be macos?
	if $AC_CC $AC_PICFLAG  -dynamiclib -o ngc$$.so ngc$$.c; then
	    __AC_MACOS_LIBRARIAN

	    LOG "(yes; macos dylib)"
	    __rc=0
	else
	    LOG "(no)"
	    __rc=1
	fi
    elif $AC_CC $AC_PICFLAG -shared -o ngc$$.so ngc$$.c; then
	__AC_ELF_LIBRARIAN
        LOG "(yes; -shared)"
1382 1383 1384 1385 1386 1387
	__rc=0
    else
        LOG "(no)"
	__rc=1
    fi

1388
    __remove ngc$$.so ngc$$.c
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454

    return $__rc
}


#
# AC_PROG_INSTALL finds the install program and guesses whether it's a 
# Berkeley or GNU install program
#
AC_PROG_INSTALL () {

    DEST=`acLookFor install`

    LOGN "looking for install"
    unset IS_BSD
    if [ "$DEST" ]; then
	# BSD install or GNU install?  Let's find out...
	touch /tmp/a$$

	$DEST /tmp/a$$ /tmp/b$$

	if test -r /tmp/a$$; then
	    LOG " ($DEST)"
	else
	    IS_BSD=1
	    LOG " ($DEST) bsd install"
	fi
	rm -f /tmp/a$$ /tmp/b$$
    else
	DEST=`acLookFor ginstall`
	if [ "$DEST" ]; then
	    LOG " ($DEST)"
	else
	    DEST="false"
	    LOG " (not found)"
	fi
    fi

    if [ "$IS_BSD" ]; then
	PROG_INSTALL="$DEST -c"
    else
	PROG_INSTALL="$DEST"
    fi

    AC_SUB 'INSTALL' "$PROG_INSTALL"
    AC_SUB 'INSTALL_PROGRAM' "$PROG_INSTALL -s -m 755"
    AC_SUB 'INSTALL_DATA' "$PROG_INSTALL -m 444"

    # finally build a little directory installer
    # if mkdir -p works, use that, otherwise use install -d,
    # otherwise build a script to do it by hand.
    # in every case, test to see if the directory exists before
    # making it.

    if mkdir -p $$a/b; then
	# I like this method best.
	__mkdir="mkdir -p"
	rmdir $$a/b
	rmdir $$a
    elif $PROG_INSTALL -d $$a/b; then
	__mkdir="$PROG_INSTALL -d"
	rmdir $$a/b
	rmdir $$a
    fi

    __config_files="$__config_files config.md"
1455 1456 1457 1458
    AC_SUB 'INSTALL_DIR' "./config.md"
    echo "#! /bin/sh"                                   > ""$__cwd"/config.md"
    echo "# script generated" `date` "by configure.sh" >> ""$__cwd"/config.md"
    echo                                               >> ""$__cwd"/config.md"
1459
    if [ "$__mkdir" ]; then
1460 1461
	echo "test -d \"\$1\" || $__mkdir \"\$1\""     >> ""$__cwd"/config.md"
	echo "exit $?"                                 >> ""$__cwd"/config.md"
1462
    else
1463
	cat - >> ""$__cwd"/config.md" << \EOD
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
pieces=`IFS=/; for x in $1; do echo $x; done`
dir=
for x in $pieces; do
    dir="$dir$x"
    mkdir $dir || exit 1
    dir="$dir/"
done
exit 0
EOD
    fi
1474
    chmod +x "$__cwd"/config.md
1475 1476 1477 1478 1479 1480 1481
}

#
# acCheckCPP is a local that runs a C preprocessor with a given set of
# compiler options
#
acCheckCPP () {
1482
	cat > ngc$$.c << EOF
1483 1484 1485 1486 1487
#define FOO BAR

FOO
EOF

1488 1489
    if $1 $2 ngc$$.c > ngc$$.o; then
	if grep -v '#define' ngc$$.o | grep -s BAR >/dev/null; then
1490 1491 1492
	    echo "CPP=[$1], CPPFLAGS=[$2]"
	    AC_SUB 'CPP' "$1"
	    AC_SUB 'CPPFLAGS' "$2"
1493
	    rm ngc$$.c ngc$$.o
1494 1495 1496
	    return 0
	fi
    fi
1497
    rm ngc$$.c ngc$$.o
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
    return 1
}


#
# AC_PROG_CPP checks for cpp, then checks to see which CPPFLAGS are needed
# to run it as a filter.
#
AC_PROG_CPP () {
    if [ "$AC_CPP_PROG" ]; then
	DEST=$AC_CPP_PROG
    else
	__ac_path="$AC_PATH"
	AC_PATH="/lib:/usr/lib:${__ac_path:-$ac_default_path}"
	DEST=`acLookFor cpp`
	AC_PATH="$__ac_path"
    fi

    unset fail
    LOGN "Looking for cpp"
    if [ "$DEST" ]; then
	TLOGN " ($DEST)"
	acCheckCPP $DEST "$CPPFLAGS" || \
		 acCheckCPP $DEST -traditional-cpp -E || \
		 acCheckCPP $DEST -E || \
		 acCheckCPP $DEST -traditional-cpp -pipe || \
	         acCheckCPP $DEST -pipe || fail=1

	if [ "$fail" ]; then
	    AC_FAIL " (can't run cpp as a pipeline)"
	else
	    TLOG " ok"
	    return 0
	fi
    fi
    AC_FAIL " (not found)"
}

#
# AC_FAIL spits out an error message, then __fail's 
AC_FAIL() {
    LOG "$*" 
    $__fail 1
}

#
# AC_SUB writes a substitution into config.sub
AC_SUB() {
1546 1547 1548 1549
    (   _target="$1"
	shift
	_subst=`echo "$*" | sed -e 's/;/\\;/g'`
	echo "s;@$_target@;$_subst;g" ) >> "$__cwd"/config.sub
1550 1551 1552 1553 1554
}

#
# AC_MAK writes a define into config.mak
AC_MAK() {
1555
    echo "HAVE_$1 = 1" >> "$__cwd"/config.mak
1556 1557 1558 1559 1560
}

#
# AC_DEFINE adds a #define to config.h
AC_DEFINE() {
1561
    echo "#define $1 ${2:-1}" >> "$__cwd"/config.h
1562 1563 1564 1565 1566
}

#
# AC_INCLUDE adds a #include to config.h
AC_INCLUDE() {
1567
    echo "#include \"$1\"" >> "$__cwd"/config.h
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
}

#
# AC_CONFIG adds a configuration setting to all the config files
AC_CONFIG() {
    AC_DEFINE "PATH_$1" \""$2"\"
    AC_MAK "$1"
    AC_SUB "$1" "$2"
}

#
# AC_QUIET does something quietly
AC_QUIET() {
    eval $* 5>/dev/null
}
    

AC_TR=`acLookFor tr`
if [ "$AC_TR" ]; then
    # try posix-style tr
    ABC=`echo abc | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
    if [ "$ABC" = "ABC" ]; then
	AC_UPPERCASE="$AC_TR abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	AC_UPPER_PAT="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    else
	ABC=`echo abc | tr a-z A-Z`
	if [ "$ABC" = "ABC" ]; then
	    AC_UPPERCASE="$AC_TR a-z A-Z"
	    AC_UPPER_PAT="A-Z"
	else
	    ABC=`echo abc | tr '[a-z]' '[A-Z]'`
	    if [ "$ABC" = "ABC" ]; then
		AC_UPPERCASE="$AC_TR '[a-z]' '[A-Z]'"
		AC_UPPER_PAT="'[A-Z]'"
	    else
		AC_FAIL "$AC_TR cannot translate lowercase to uppercase"
		return 0
	    fi
	fi
    fi
else
    AC_FAIL "configure requires a functional version of tr"
fi

while [ $# -gt 0 ]; do
    unset matched

    case X"$1" in
    X--src|X--srcdir)
	AC_SRCDIR=`__ac_dir "$2"`
	_set_srcdir=1
	shift 2;;

    X--src=*|X--srcdir=*)
	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
	AC_SRCDIR=`__ac_dir "$__d"`
	_set_srcdir=1
	shift 1 ;;

    X--prefix)
	AC_PREFIX=`__ac_dir "$2"`
	_set_prefix=1
	shift 2;;

    X--prefix=*)
	__d=`echo "$1"| sed -e 's/^[^=]*=//'`
	AC_PREFIX=`__ac_dir "$__d"`
	_set_prefix=1
	shift 1;;

    X--confdir)
	AC_CONFDIR=`__ac_dir "$2"`
	_set_confdir=1
	shift 2;;

    X--confdir=*)
	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
	AC_CONFDIR=`__ac_dir "$__d"`
	_set_confdir=1
	shift 1;;

    X--libexec|X--libexecdir)
	AC_LIBEXEC=`__ac_dir "$2"`
	_set_libexec=1
	shift 2;;

    X--libexec=*|X--libexecdir=*)
	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
	AC_LIBEXEC=`__ac_dir "$__d"`
	_set_libexec=1
	shift 1;;

    X--lib|X--libdir)
	AC_LIBDIR=`__ac_dir "$2"`
	_set_libdir=1
	shift 2;;

    X--lib=*|X--libdir=*)
	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
	AC_LIBDIR=`__ac_dir "$__d"`
	_set_libdir=1
	shift 1;;

    X--exec|X--execdir)
	AC_EXECDIR=`__ac_dir "$2"`
	_set_execdir=1
	shift 2;;

    X--exec=*|X--execdir=*)
	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
	AC_EXECDIR=`__ac_dir "$__d"`
	_set_execdir=1
	shift 1;;

    X--sbin|X--sbindir)
	AC_SBINDIR=`__ac_dir "$2"`
	_set_sbindir=1
	shift 2;;

    X--sbin=*|X--sbindir=*)
	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
	AC_SBINDIR=`__ac_dir "$__d"`
	_set_sbindir=1
	shift 1;;

    X--man|X--mandir)
	AC_MANDIR=`__ac_dir "$2"`
	_set_mandir=1
	shift 2;;

    X--man=*|X--mandir=*)
	__d=`echo "$1" | sed -e 's/^[^=]*=//'`
	AC_MANDIR=`__ac_dir "$__d"`
	_set_mandir=1
	shift 1;;

    X--use-*=*)
	_var=`echo "$1"| sed -n 's/^--use-\([A-Za-z][-A-Za-z0-9_]*\)=.*$/\1/p'`
	if [ "$_var" ]; then
	    _val=`echo "$1" | sed -e 's/^--use-[^=]*=\(.*\)$/\1/'`
	    _v=`echo $_var | $AC_UPPERCASE | tr '-' '_'`
	    case X"$_val" in
	    X[Yy][Ee][Ss]|X[Tt][Rr][Uu][Ee]) eval USE_${_v}=T ;;
	    X[Nn][Oo]|X[Ff][Aa][Ll][Ss][Ee]) eval unset USE_${_v} ;;
	    *) echo "Bad value for --use-$_var ; must be yes or no"
	       exit 1 ;;
	    esac
	else
	    echo "Bad option $1.   Use --help to show options" 1>&2
	    exit 1
	fi
	shift 1 ;;

    X--use-*)
        _var=`echo "$1"|sed -n 's/^--use-\([A-Za-z][-A-Za-z0-9_]*\)$/\1/p'`
	_v=`echo $_var | $AC_UPPERCASE | tr '-' '_'`
	eval USE_${_v}=T
	shift 1;;

    X--with-*=*)
	_var=`echo "$1"| sed -n 's/^--with-\([A-Za-z][-A-Za-z0-9_]*\)=.*$/\1/p'`
	if [ "$_var" ]; then
	    _val=`echo "$1" | sed -e 's/^--with-[^=]*=\(.*\)$/\1/'`
	    _v=`echo $_var | $AC_UPPERCASE | tr '-' '_'`
	    eval WITH_${_v}=\"$_val\"
	else
	    echo "Bad option $1.   Use --help to show options" 1>&2
	    exit 1
	fi
	shift 1 ;;

    X--with-*)
	_var=`echo "$1" | sed -n 's/^--with-\([A-Za-z][A-Za-z0-9_-]*\)$/\1/p'`
	if [ "$_var" ]; then
	    _v=`echo $_var | $AC_UPPERCASE | tr '-' '_'`
	    eval WITH_${_v}=1
	else
	    echo "Bad option $1.   Use --help to show options" 1>&2
	    exit 1
	fi
	shift 1 ;;

    X--help)
	echo "$ac_standard"
	test "$ac_help" && echo "$ac_help"
	exit 0;;

    *)	if [ "$LOCAL_AC_OPTIONS" ]; then
	    eval "$LOCAL_AC_OPTIONS"
	else
	    ac_error=T
	fi
	if [ "$ac_error" ]; then
	    echo "Bad option $1.   Use --help to show options" 1>&2
	    exit 1
	fi ;;
    esac
done