?RCS: $Id: libpth.U 1 2006-08-24 12:32:52Z rmanfredi $
?RCS:
?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
?RCS:
?RCS: You may redistribute only under the terms of the Artistic License,
?RCS: as specified in the README file that comes with the distribution.
?RCS: You may reuse parts of this distribution only within the terms of
?RCS: that same Artistic License; a copy of which may be found at the root
?RCS: of the source tree for dist 4.0.
?RCS:
?RCS: $Log: libpth.U,v $
?RCS: Revision 3.0.1.6  1997/02/28  16:08:49  ram
?RCS: patch61: new loclibpth variable
?RCS:
?RCS: Revision 3.0.1.5  1995/01/11  15:31:30  ram
?RCS: patch45: call ./mips instead of just mips (WED)
?RCS:
?RCS: Revision 3.0.1.4  1994/08/29  16:29:15  ram
?RCS: patch32: added /lib/pa1.1 for HP-UX specially tuned PA-RISC libs (ADO)
?RCS: patch32: fixed information message, making it clearer (ADO)
?RCS:
?RCS: Revision 3.0.1.3  1994/06/20  07:03:54  ram
?RCS: patch30: added /usr/shlib to glibpth for shared-only libraries
?RCS:
?RCS: Revision 3.0.1.2  1994/05/13  15:26:57  ram
?RCS: patch27: fixed a typo (libpth -> glibpth)
?RCS:
?RCS: Revision 3.0.1.1  1994/05/06  15:07:53  ram
?RCS: patch23: now asks for library directories to be searched (ADO)
?RCS:
?RCS: Revision 3.0  1993/08/18  12:09:02  ram
?RCS: Baseline for dist 3.0 netwide release.
?RCS:
?X:
?X:	This unit initializes the path for C library lookup.
?X:
?MAKE:libpth glibpth xlibpth plibpth loclibpth incpth: \
	usrinc incpath test cat Myread Oldconfig sysroot osname \
	ccname echo cppstdin awk grep sed rm usecrosscompile
?MAKE:	-pick add $@ %<
?S:libpth:
?S:	This variable holds the general path (space-separated) used to find
?S:	libraries. It is intended to be used by other units.
?S:.
?S:glibpth:
?S:	This variable holds the general path (space-separated) used to
?S:	find libraries.  It may contain directories that do not exist on
?S:	this platform, libpth is the cleaned-up version.
?S:.
?S:xlibpth:
?S:	This variable holds extra path (space-separated) used to find
?S:	libraries on this platform, for example CPU-specific libraries
?S:	(on multi-CPU platforms) may be listed here.
?S:.
?S:loclibpth:
?S:	This variable holds the paths (space-separated) used to find local
?S:	libraries.  It is prepended to libpth, and is intended to be easily
?S:	set from the command line.
?S:.
?S:plibpth:
?S:	Holds the private path used by Configure to find out the libraries.
?S:	Its value is prepend to libpth. This variable takes care of special
?S:	machines, like the mips.  Usually, it should be empty.
?S:.
?S:incpth:
?S:	This variable must precede the normal include path to get the
?S:	right one, as in "$incpath/usr/include" or "$incpath/usr/lib".
?S:	Value can be "" or "/bsd43" on mips.
?S:.
?T: xxx dlist i j croak realpath inclibpth
?LINT:extern incpth
?LINT:change usrinc
?INIT:: change the next line if compiling for Xenix/286 on Xenix/386
?INIT:xlibpth='/usr/lib/386 /lib/386'
?INIT:: Possible local library directories to search.
?INIT:loclibpth="/usr/local/lib /opt/local/lib /usr/gnu/lib"
?INIT:loclibpth="$loclibpth /opt/gnu/lib /usr/GNU/lib /opt/GNU/lib"
?INIT:
?INIT:: general looking path for locating libraries
?INIT:glibpth="/lib /usr/lib $xlibpth"
?INIT:glibpth="$glibpth /usr/ccs/lib /usr/ucblib /usr/local/lib"
?X:	/shlib is for Digital Unix 4.0
?X:	/usr/shlib is for OSF/1 systems.
?INIT:test -f /usr/shlib/libc.so && glibpth="/usr/shlib $glibpth"
?INIT:test -f /shlib/libc.so     && glibpth="/shlib $glibpth"
?INIT:test -d /usr/lib64         && glibpth="$glibpth /lib64 /usr/lib64 /usr/local/lib64"
?INIT:
?INIT:: Private path used by Configure to find libraries.  Its value
?INIT:: is prepended to libpth. This variable takes care of special
?INIT:: machines, like the mips.  Usually, it should be empty.
?INIT:plibpth=''
?INIT:
?X:cppfilter is later used in Findhdr.U, but we not want to expose it to config.h
?T:cppfilter
: Adjust cppfilter for path component separator
case "$osname" in
vos) cppfilter="tr '\\\\>' '/' |" ;; # path component separator is >
os2) cppfilter="sed -e 's|\\\\\\\\|/|g' |" ;; # path component separator is \
*)   cppfilter='' ;;
esac

: Use gcc to determine libpth and incpth
# If using gcc or clang, we can get better values for libpth, incpth
# and usrinc directly from the compiler.
# Note that ccname for clang is also gcc.
case "$ccname" in
    gcc)
	realpath=`which realpath 2>/dev/null | sed 's/no realpath in.*//'`
	$echo 'extern int foo;' > try.c
	set X `$cppstdin -v try.c 2>&1 | $awk '/^#include </,/^End of search /'|$cppfilter $grep '/include'`
	shift
	inclibpth=""
	if $test $# -gt 0; then
	    for i in $*; do
		case $realpath in
		    */realpath) i=`$realpath $i` ;;
		esac
		incpth="$incpth $i"
		j="`$echo $i | $sed 's,/include[^/]*,/lib,'`"
		if $test -d $j; then
		    inclibpth="$inclibpth $j"
		fi
	    done
	    incpth="`$echo $incpth | $sed 's/^ //'`"
	    for xxx in $inclibpth $loclibpth $plibpth $glibpth; do
		if $test -d $xxx; then
		    case $realpath in
			*/realpath) xxx=`$realpath $xxx` ;;
		    esac
		    case " $libpth " in
		    *" $xxx "*) ;;
		    *) libpth="$libpth $xxx";;
		    esac
		fi
	    done
	    libpth="`$echo $libpth | $sed 's/^ //'`"
	fi
	$rm -f try.c
	case "$usrinc" in
	'') for i in $incpth; do
		if $test -f $i/errno.h -a -f $i/stdio.h -a -f $i/time.h; then
		    usrinc="$i"
		    break
		fi
	    done
	    ;;
	esac

	case "$usecrosscompile" in
	$define|true|[yY]*)
	    case "$incpth" in
		'') echo "Incpth not defined." >&4; croak=y ;;
		*)  echo "Using incpth '$incpth'." >&4 ;;
	    esac
	    case "$libpth" in
		'') echo "Libpth not defined." >&4; croak=y ;;
		*)  echo "Using libpth '$libpth'." >&4 ;;
	    esac
	    case "$usrinc" in
		'') echo "Usrinc not defined." >&4; croak=y ;;
		*)  echo "Using usrinc $usrinc." >&4 ;;
	    esac
	    case "$croak" in
		y)
		if test "X$sysroot" = X; then
		    echo "Cannot continue, aborting." >&4; exit 1
		else
		    echo "Cross-compiling using sysroot $sysroot, failing to guess inc/lib paths is not fatal" >&4
		fi
		;;
	    esac
	    ;;
	esac
    ;;
esac

: Default value for incpth is just usrinc
case "$incpth" in
'') incpth="$usrinc";;
esac

: Set private lib path
case "$plibpth" in
'') if ./mips; then
?X: on mips, we DO NOT want /lib, and we want $incpath/usr/lib
	plibpth="$incpath/usr/lib $sysroot/usr/local/lib $sysroot/usr/ccs/lib"
    fi;;
esac
case "$libpth" in
' ') dlist='';;
'') dlist="$loclibpth $plibpth $glibpth";;
*) dlist="$libpth";;
esac

: Now check and see which directories actually exist, avoiding duplicates
for xxx in $dlist
do
    if $test -d $xxx; then
		case " $libpth " in
		*" $xxx "*) ;;
		*) libpth="$libpth $xxx";;
		esac
    fi
done
$cat <<'EOM'

Some systems have incompatible or broken versions of libraries.  Among
the directories listed in the question below, please remove any you
know not to be holding relevant libraries, and add any that are needed.
Say "none" for none.

EOM

if test "X$sysroot" != X; then
    $cat <<EOM
You have set sysroot to $sysroot, please supply the directories excluding sysroot

EOM
fi

case "$libpth" in
'') dflt='none';;
*)
?X: strip leading space
	set X $libpth
	shift
	dflt=${1+"$@"}
	;;
esac
rp="Directories to use for library searches?"
. ./myread
case "$ans" in
none) libpth=' ';;
*) libpth="$ans";;
esac

