2003-11-23 18:29:08 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2022-08-17 16:57:04 +00:00
|
|
|
[ "$1" = "" ] && exit 1
|
|
|
|
|
2023-12-18 07:12:48 +00:00
|
|
|
test -f "$PLAN9/config" && . "$PLAN9/config"
|
2005-01-04 21:13:58 +00:00
|
|
|
libsl=""
|
2005-10-19 03:15:30 +00:00
|
|
|
frameworks=""
|
2005-01-07 06:40:17 +00:00
|
|
|
doautolib=true
|
2005-10-19 03:15:30 +00:00
|
|
|
doautoframework=true
|
2005-01-07 06:40:17 +00:00
|
|
|
verbose=false
|
2005-01-04 21:13:58 +00:00
|
|
|
|
2009-07-15 20:08:09 +00:00
|
|
|
nmflags=""
|
|
|
|
extralibs="-lm"
|
2020-12-30 12:39:16 +00:00
|
|
|
tag="${SYSNAME:-`uname`}"
|
2009-07-15 20:08:09 +00:00
|
|
|
case "$tag" in
|
2014-02-28 04:17:47 +00:00
|
|
|
*DragonFly*|*BSD*)
|
2020-05-18 00:07:52 +00:00
|
|
|
ld="${CC9:-gcc} $CC9FLAGS"
|
2009-07-15 20:08:09 +00:00
|
|
|
userpath=true
|
|
|
|
extralibs="$extralibs -lutil"
|
|
|
|
;;
|
|
|
|
*OSF1*)
|
2020-05-18 00:07:52 +00:00
|
|
|
ld="${CC9:-cc} $CC9FLAGS"
|
2009-07-15 20:08:09 +00:00
|
|
|
userpath=true
|
|
|
|
extralibs="$extralibs -lutil"
|
|
|
|
nmflags="-B"
|
|
|
|
;;
|
|
|
|
*Linux*)
|
2020-05-18 00:07:52 +00:00
|
|
|
ld="${CC9:-gcc} $CC9FLAGS"
|
2009-07-15 20:08:09 +00:00
|
|
|
userpath=true
|
2020-01-20 02:10:11 +00:00
|
|
|
extralibs="$extralibs -lutil -lresolv -lpthread"
|
2009-07-15 20:08:09 +00:00
|
|
|
;;
|
|
|
|
*Darwin*)
|
2020-12-30 12:39:16 +00:00
|
|
|
ld="${CC9:-gcc} -m64 $CC9FLAGS"
|
2009-07-15 20:08:09 +00:00
|
|
|
;;
|
|
|
|
*SunOS*)
|
2020-05-18 00:07:52 +00:00
|
|
|
ld="${CC9:-cc} -g $CC9FLAGS"
|
2009-07-15 20:08:09 +00:00
|
|
|
extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"
|
2020-01-20 02:10:11 +00:00
|
|
|
# Record paths to shared libraries to avoid needing LD_LIBRARY_PATH
|
2009-07-15 20:08:09 +00:00
|
|
|
for i in "$libsl $@"
|
|
|
|
do
|
|
|
|
case "$i" in
|
|
|
|
-L*)
|
|
|
|
s=`echo $i | sed 's/-L/-R/'`
|
|
|
|
extralibs="$extralibs $s"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
case "${SYSVERSION:-`uname -r`}" in
|
|
|
|
5.[67])
|
|
|
|
echo do not know how to link right thread library on "$tag" 1>&2
|
|
|
|
;;
|
|
|
|
5.8)
|
|
|
|
# Some trickery is needed to force use of
|
|
|
|
# alternate thread lib from /usr/lib/lwp
|
|
|
|
# Likely, this only works with sun cc,
|
|
|
|
# for other compiler/loader we would need other flags.
|
|
|
|
ld="$ld -i"
|
|
|
|
extralibs="$extralibs /usr/lib/lwp/libthread.so -R/usr/lib/lwp:/usr/lib"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
*AIX*)
|
2021-08-29 20:36:30 +00:00
|
|
|
ld="${CC9:-xlc_r} $CC9FLAGS"
|
2020-05-05 02:52:02 +00:00
|
|
|
nmflags="-A -B"
|
2009-07-15 20:08:09 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo do not know how to link on "$tag" 1>&2
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
|
2005-01-04 21:13:58 +00:00
|
|
|
if [ "x$1" = "x-l" ]
|
|
|
|
then
|
|
|
|
shift
|
2005-01-07 06:40:17 +00:00
|
|
|
doautolib=false
|
2005-10-19 03:15:30 +00:00
|
|
|
doautoframework=false
|
2005-01-04 21:13:58 +00:00
|
|
|
elif [ "x$1" = "x-v" ]
|
|
|
|
then
|
|
|
|
shift
|
2005-01-07 06:40:17 +00:00
|
|
|
verbose=true
|
2005-01-04 21:13:58 +00:00
|
|
|
fi
|
|
|
|
|
2005-01-07 18:51:26 +00:00
|
|
|
target=a.out
|
|
|
|
if [ "x$1" = "x-o" ]
|
|
|
|
then
|
|
|
|
target=$2
|
|
|
|
fi
|
|
|
|
|
2005-01-07 06:40:17 +00:00
|
|
|
if $doautolib
|
2005-01-04 21:13:58 +00:00
|
|
|
then
|
|
|
|
ofiles=""
|
2008-12-03 07:52:55 +00:00
|
|
|
lpaths="$PLAN9/lib"
|
2005-01-04 21:13:58 +00:00
|
|
|
for i
|
|
|
|
do
|
|
|
|
case "$i" in
|
2005-01-14 04:24:09 +00:00
|
|
|
*.[ao])
|
2005-01-04 21:13:58 +00:00
|
|
|
ofiles="$ofiles $i"
|
|
|
|
;;
|
2008-12-03 07:52:55 +00:00
|
|
|
-L*)
|
|
|
|
l=`echo $i | sed 's/-L//'`
|
|
|
|
lpaths="$lpaths $l"
|
2005-01-04 21:13:58 +00:00
|
|
|
esac
|
|
|
|
done
|
2020-01-20 02:10:11 +00:00
|
|
|
|
2008-12-03 07:52:55 +00:00
|
|
|
if $verbose
|
|
|
|
then
|
|
|
|
echo "ofiles $ofiles"
|
|
|
|
echo "lpaths $lpaths"
|
|
|
|
fi
|
|
|
|
|
2005-01-04 21:13:58 +00:00
|
|
|
autolibs=""
|
|
|
|
if [ "x$ofiles" != "x" ]
|
|
|
|
then
|
2005-01-07 06:40:17 +00:00
|
|
|
a=`
|
2009-07-15 20:08:09 +00:00
|
|
|
nm $nmflags $ofiles |
|
2020-05-05 02:52:02 +00:00
|
|
|
grep '__p9l_autolib_[a-zA-Z0-9+-]*' |
|
|
|
|
sed 's/.*__p9l_autolib_//; s/:.*//' |
|
2005-01-04 21:13:58 +00:00
|
|
|
sort -u
|
|
|
|
`
|
2005-01-07 06:40:17 +00:00
|
|
|
for i in $a
|
|
|
|
do
|
|
|
|
autolibs="$autolibs $i"
|
|
|
|
eval "need$i=true"
|
|
|
|
done
|
2005-01-04 21:13:58 +00:00
|
|
|
fi
|
2006-06-25 23:48:51 +00:00
|
|
|
if $verbose
|
|
|
|
then
|
|
|
|
echo "autolibs1 $autolibs"
|
|
|
|
fi
|
2005-01-07 06:40:17 +00:00
|
|
|
|
|
|
|
# fetch dependencies out of libraries
|
|
|
|
workq="$autolibs"
|
|
|
|
while [ "x$workq" != "x" ]
|
2005-01-04 21:13:58 +00:00
|
|
|
do
|
2005-01-07 06:40:17 +00:00
|
|
|
w="$workq"
|
|
|
|
workq=""
|
|
|
|
for i in $w
|
|
|
|
do
|
2005-01-14 04:24:09 +00:00
|
|
|
# can't trust the libraries about using
|
2006-02-14 02:00:06 +00:00
|
|
|
# libthread or libdraw - we might not be linking with
|
2005-01-14 04:24:09 +00:00
|
|
|
# those object files.
|
2008-12-03 07:52:55 +00:00
|
|
|
a=""
|
|
|
|
for lpath in $lpaths
|
|
|
|
do
|
|
|
|
b=`
|
|
|
|
nm $lpath/lib$i.a 2>/dev/null |
|
2020-05-05 02:52:02 +00:00
|
|
|
grep '__p9l_autolib_[a-zA-Z0-9+-]*' |
|
|
|
|
sed 's/.*__p9l_autolib_//; s/:.*//' |
|
2008-12-03 07:52:55 +00:00
|
|
|
sort -u |
|
|
|
|
egrep -v '^(thread|draw)$'
|
|
|
|
`
|
|
|
|
a="$a $b"
|
|
|
|
done
|
2006-06-25 23:48:51 +00:00
|
|
|
# fix up libraries that really need draw
|
|
|
|
if [ "x$i" = "xmemdraw" -o "x$i" = "xmemlayer" -o "x$i" = "xframe" ]
|
|
|
|
then
|
|
|
|
a="$a draw"
|
|
|
|
fi
|
2005-01-07 06:40:17 +00:00
|
|
|
okayfn="true"
|
|
|
|
for j in $a
|
|
|
|
do
|
|
|
|
if eval "[ x\$need$j = x ]"
|
|
|
|
then
|
|
|
|
autolibs="$autolibs $j"
|
|
|
|
workq="$workq $j"
|
|
|
|
eval "need$j=true"
|
|
|
|
fi
|
|
|
|
if [ $j != $i ]
|
|
|
|
then
|
|
|
|
okayfn="$okayfn && have$j"
|
|
|
|
fi
|
|
|
|
done
|
2006-06-25 23:48:51 +00:00
|
|
|
if $verbose
|
|
|
|
then
|
|
|
|
echo "can$i: $okayfn"
|
|
|
|
fi
|
2005-01-07 06:40:17 +00:00
|
|
|
eval "can$i() { $okayfn; }"
|
|
|
|
done
|
2005-01-04 21:13:58 +00:00
|
|
|
done
|
2005-01-07 06:40:17 +00:00
|
|
|
if $verbose
|
|
|
|
then
|
|
|
|
echo "autolibs $autolibs"
|
|
|
|
fi
|
|
|
|
|
2005-01-04 21:13:58 +00:00
|
|
|
for i in $autolibs
|
|
|
|
do
|
2005-01-07 06:40:17 +00:00
|
|
|
eval "have$i() { false; }"
|
2005-01-04 21:13:58 +00:00
|
|
|
done
|
2005-01-13 04:49:19 +00:00
|
|
|
havethread() { false; }
|
2005-02-11 16:52:08 +00:00
|
|
|
havesec() { false; }
|
2006-06-25 23:48:51 +00:00
|
|
|
canmemlayer() { havedraw; }
|
2005-01-07 06:40:17 +00:00
|
|
|
|
|
|
|
# now find correct order
|
|
|
|
libsl=""
|
|
|
|
while [ "x$autolibs" != x ]
|
2005-01-04 21:13:58 +00:00
|
|
|
do
|
2005-01-07 06:40:17 +00:00
|
|
|
stillneed=""
|
|
|
|
didnothing=true
|
|
|
|
for i in $autolibs
|
|
|
|
do
|
|
|
|
if eval "can$i"
|
|
|
|
then
|
|
|
|
libsl="-l$i $libsl"
|
|
|
|
eval "have$i() { true; }"
|
|
|
|
didnothing=false
|
|
|
|
else
|
|
|
|
stillneed="$stillneed $i"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# break cycle by setting the last library on the list
|
|
|
|
# to have no dependencies
|
|
|
|
if $didnothing
|
2005-01-04 21:13:58 +00:00
|
|
|
then
|
2005-01-07 06:40:17 +00:00
|
|
|
j="xxx"
|
|
|
|
for i in $autolibs
|
|
|
|
do
|
|
|
|
j=$i
|
|
|
|
done
|
|
|
|
echo "dependency cycle: $autolibs; breaking with $j"
|
|
|
|
eval "can$j() { true; }"
|
2005-01-04 21:13:58 +00:00
|
|
|
fi
|
2005-01-07 06:40:17 +00:00
|
|
|
autolibs="$stillneed"
|
2005-01-04 21:13:58 +00:00
|
|
|
done
|
2005-01-07 06:40:17 +00:00
|
|
|
if $verbose
|
|
|
|
then
|
|
|
|
echo "liborder $libsl"
|
|
|
|
fi
|
2005-01-04 21:13:58 +00:00
|
|
|
libsl="$libsl -l9"
|
2005-01-07 06:40:17 +00:00
|
|
|
|
2005-01-13 04:49:19 +00:00
|
|
|
# cycle: lib9 expects p9main, which is defined in libthread. oops.
|
2005-01-14 04:24:09 +00:00
|
|
|
if havethread
|
2005-01-13 04:49:19 +00:00
|
|
|
then
|
2005-01-14 04:24:09 +00:00
|
|
|
libsl="$libsl -lthread -l9"
|
2005-01-13 04:49:19 +00:00
|
|
|
fi
|
|
|
|
|
2005-02-11 16:52:08 +00:00
|
|
|
# cycle: lib9 netcrypt uses libsec
|
|
|
|
if havesec
|
|
|
|
then
|
|
|
|
libsl="$libsl -lsec -l9"
|
|
|
|
fi
|
|
|
|
|
2006-04-04 18:09:05 +00:00
|
|
|
if [ "x$needndb" = xtrue -a '(' -f /usr/lib/libresolv.a -o -f /usr/lib/libresolv.dylib ')' ]
|
2006-02-14 02:00:06 +00:00
|
|
|
then
|
|
|
|
libsl="$libsl -lresolv"
|
|
|
|
fi
|
|
|
|
|
2007-02-22 14:27:45 +00:00
|
|
|
if [ "x$needX11" = xtrue -a "x$WSYSTYPE" != xnowsys ]
|
2005-01-04 21:13:58 +00:00
|
|
|
then
|
|
|
|
if [ "x$X11" = "x" ]
|
|
|
|
then
|
|
|
|
X11=/usr/X11R6
|
|
|
|
fi
|
2012-05-29 19:11:14 +00:00
|
|
|
# Don't say -L with a non-existent directory: Xcode complains.
|
2005-01-18 05:59:34 +00:00
|
|
|
# x86_64 seems to put its 64-bit libraries in lib64.
|
2020-12-30 12:39:16 +00:00
|
|
|
if [ "`uname -m`" = "x86_64" -a -d "$X11/lib64" ]
|
2005-01-18 05:59:34 +00:00
|
|
|
then
|
|
|
|
libsl="$libsl -L$X11/lib64"
|
|
|
|
fi
|
2012-05-29 19:11:14 +00:00
|
|
|
if [ -d "$X11/lib" ]
|
|
|
|
then
|
|
|
|
libsl="$libsl -L$X11/lib"
|
|
|
|
fi
|
|
|
|
libsl="$libsl -lX11"
|
2005-01-04 21:13:58 +00:00
|
|
|
fi
|
|
|
|
fi
|
2005-10-19 03:15:30 +00:00
|
|
|
if $doautoframework
|
|
|
|
then
|
|
|
|
ofiles=""
|
|
|
|
for i
|
|
|
|
do
|
|
|
|
case "$i" in
|
|
|
|
*.[ao])
|
|
|
|
ofiles="$ofiles $i"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# echo "ofiles $ofiles"
|
|
|
|
autoframeworks=""
|
|
|
|
if [ "x$ofiles" != "x" ]
|
|
|
|
then
|
|
|
|
a=`
|
|
|
|
nm $ofiles |
|
|
|
|
grep '__p9l_autoframework_[a-zA-Z0-9+-]*$' |
|
|
|
|
sed 's/.*__p9l_autoframework_//' |
|
|
|
|
sort -u
|
|
|
|
`
|
|
|
|
for i in $a
|
|
|
|
do
|
|
|
|
autoframeworks="$autoframeworks $i"
|
|
|
|
eval "need$i=true"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
if $verbose
|
|
|
|
then
|
|
|
|
echo "autoframeworks $autoframeworks"
|
|
|
|
fi
|
|
|
|
|
|
|
|
for i in $autoframeworks
|
|
|
|
do
|
|
|
|
eval "have$i() { false; }"
|
|
|
|
done
|
|
|
|
|
|
|
|
frameworks=""
|
|
|
|
for i in $autoframeworks
|
2020-01-20 02:10:11 +00:00
|
|
|
do
|
2005-10-19 03:15:30 +00:00
|
|
|
frameworks="-framework $i $frameworks"
|
|
|
|
done
|
|
|
|
fi
|
2005-01-04 21:13:58 +00:00
|
|
|
|
2005-01-19 00:25:38 +00:00
|
|
|
case "$userpath" in
|
|
|
|
true)
|
2005-01-16 20:56:40 +00:00
|
|
|
for i in "$libsl $@"
|
|
|
|
do
|
|
|
|
case "$i" in
|
|
|
|
-L*)
|
|
|
|
s=`echo $i | sed 's/-L/-Wl,-rpath,/'`
|
|
|
|
extralibs="$extralibs $s"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2005-01-07 06:40:17 +00:00
|
|
|
if $verbose
|
2005-01-04 21:13:58 +00:00
|
|
|
then
|
2023-12-18 07:12:48 +00:00
|
|
|
echo $ld -L"$PLAN9/lib" "$@" $libsl $extralibs $frameworks
|
2005-01-04 21:13:58 +00:00
|
|
|
fi
|
2005-03-18 18:50:33 +00:00
|
|
|
|
2023-12-18 07:12:48 +00:00
|
|
|
quiet()
|
|
|
|
{
|
|
|
|
ignore='^$'
|
|
|
|
ignore=$ignore'|is (often|almost always) misused'
|
|
|
|
ignore=$ignore'|is dangerous, better use'
|
|
|
|
ignore=$ignore'|text-based stub'
|
2023-12-18 07:13:23 +00:00
|
|
|
# macOS linker is incessant about reoccurring -l9, -lsec, -lthread:
|
|
|
|
ignore=$ignore'|ld: warning: ignoring duplicate libraries:'
|
2023-12-18 07:12:48 +00:00
|
|
|
|
|
|
|
sed 's/.*: In function `[^:]*: *//' "$1" |
|
|
|
|
egrep -v "$ignore"
|
2005-03-18 18:50:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 07:12:48 +00:00
|
|
|
# Must use temp file to avoid pipe; pipe loses status.
|
|
|
|
xtmp=${TMPDIR-/tmp}/9l.$$.$USER.out
|
|
|
|
$ld -L"$PLAN9/lib" "$@" $libsl $extralibs $frameworks >"$xtmp" 2>&1
|
|
|
|
status=$?
|
|
|
|
quiet "$xtmp"
|
|
|
|
rm -f "$xtmp"
|
|
|
|
if [ "$status" -ne 0 ]
|
2005-01-07 20:40:44 +00:00
|
|
|
then
|
2023-12-18 07:12:48 +00:00
|
|
|
rm -f "$target"
|
2005-01-07 20:40:44 +00:00
|
|
|
fi
|
2023-12-18 07:12:48 +00:00
|
|
|
exit $status
|