Blob
1 #!/bin/sh3 libsl=""5 doautolib=true6 verbose=false8 if [ "x$1" = "x-l" ]9 then10 shift11 doautolib=false12 elif [ "x$1" = "x-v" ]13 then14 shift15 verbose=true16 fi18 target=a.out19 if [ "x$1" = "x-o" ]20 then21 target=$222 fi24 if $doautolib25 then26 ofiles=""27 for i28 do29 case "$i" in30 *.[ao])31 ofiles="$ofiles $i"32 ;;33 esac34 done36 # echo "ofiles $ofiles"37 autolibs=""38 if [ "x$ofiles" != "x" ]39 then40 a=`41 nm $ofiles |42 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |43 sed 's/.*__p9l_autolib_//' |44 sort -u45 `46 for i in $a47 do48 autolibs="$autolibs $i"49 eval "need$i=true"50 done51 fi53 # fetch dependencies out of libraries54 workq="$autolibs"55 while [ "x$workq" != "x" ]56 do57 w="$workq"58 workq=""59 for i in $w60 do61 # can't trust the libraries about using62 # libthread - we might not be linking with63 # those object files.64 a=`65 nm $PLAN9/lib/lib$i.a |66 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |67 sed 's/.*__p9l_autolib_//' |68 sort -u |69 grep -v thread70 `71 okayfn="true"72 for j in $a73 do74 if eval "[ x\$need$j = x ]"75 then76 autolibs="$autolibs $j"77 workq="$workq $j"78 eval "need$j=true"79 fi80 if [ $j != $i ]81 then82 okayfn="$okayfn && have$j"83 fi84 done85 # echo "can$i: $okayfn"86 eval "can$i() { $okayfn; }"87 done88 done89 if $verbose90 then91 echo "autolibs $autolibs"92 fi94 for i in $autolibs95 do96 eval "have$i() { false; }"97 done98 havethread() { false; }100 # now find correct order101 libsl=""102 while [ "x$autolibs" != x ]103 do104 stillneed=""105 didnothing=true106 for i in $autolibs107 do108 if eval "can$i"109 then110 libsl="-l$i $libsl"111 eval "have$i() { true; }"112 didnothing=false113 else114 stillneed="$stillneed $i"115 fi116 done117 # break cycle by setting the last library on the list118 # to have no dependencies119 if $didnothing120 then121 j="xxx"122 for i in $autolibs123 do124 j=$i125 done126 echo "dependency cycle: $autolibs; breaking with $j"127 eval "can$j() { true; }"128 fi129 autolibs="$stillneed"130 done131 if $verbose132 then133 echo "liborder $libsl"134 fi135 libsl="$libsl -l9"137 # cycle: lib9 expects p9main, which is defined in libthread. oops.138 if havethread139 then140 libsl="$libsl -lthread -l9"141 fi143 if [ "x$needdraw" = xtrue ]144 then145 if [ "x$X11" = "x" ]146 then147 X11=/usr/X11R6148 fi149 libsl="$libsl -L$X11/lib -lX11"150 fi151 fi153 extralibs="-lm"154 tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"155 case "$tag" in156 *OpenBSD*)157 ld=gcc158 extralibs="$extralibs -lutil -lpthread"159 ;;160 *FreeBSD*)161 ld=gcc162 extralibs="$extralibs -lutil"163 case "`uname -r`" in164 [5-9].*)165 extralibs="$extralibs -lpthread"166 ;;167 esac168 ;;169 *BSD*)170 ld=gcc171 extralibs="$extralibs -lutil"172 ;;173 *Linux*)174 ld=gcc175 extralibs="$extralibs -lutil"176 case "`uname -r`" in177 2.6.*)178 extralibs="$extralibs -lpthread"179 ;;180 esac181 ;;182 *Darwin*)183 ld=gcc184 ;;185 *SunOS*)186 ld="${CC9:-cc} -g"187 extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"188 # Record paths to shared libraries to avoid needing LD_LIBRARY_PATH189 for i in "$@"190 do191 case "$i" in192 -L*)193 s=`echo $i | sed 's/-L/-R/'`194 extralibs="$extralibs $s"195 ;;196 esac197 done198 ;;199 *)200 echo do not know how to link on "$tag" 1>&2201 exit 1202 esac204 if $verbose205 then206 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs207 fi208 if $ld -L$PLAN9/lib "$@" $libsl $extralibs209 then210 exit 0211 else212 rm -f $target213 exit 1214 fi