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 *.o)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 a=`62 nm $PLAN9/lib/lib$i.a |63 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |64 sed 's/.*__p9l_autolib_//' |65 sort -u66 `67 okayfn="true"68 for j in $a69 do70 if eval "[ x\$need$j = x ]"71 then72 autolibs="$autolibs $j"73 workq="$workq $j"74 eval "need$j=true"75 fi76 if [ $j != $i ]77 then78 okayfn="$okayfn && have$j"79 fi80 done81 # echo "can$i: $okayfn"82 eval "can$i() { $okayfn; }"83 done84 done85 if $verbose86 then87 echo "autolibs $autolibs"88 fi90 for i in $autolibs91 do92 eval "have$i() { false; }"93 done94 havethread() { false; }96 # now find correct order97 libsl=""98 while [ "x$autolibs" != x ]99 do100 stillneed=""101 didnothing=true102 for i in $autolibs103 do104 if eval "can$i"105 then106 libsl="-l$i $libsl"107 eval "have$i() { true; }"108 didnothing=false109 else110 stillneed="$stillneed $i"111 fi112 done113 # break cycle by setting the last library on the list114 # to have no dependencies115 if $didnothing116 then117 j="xxx"118 for i in $autolibs119 do120 j=$i121 done122 echo "dependency cycle: $autolibs; breaking with $j"123 eval "can$j() { true; }"124 fi125 autolibs="$stillneed"126 done127 if $verbose128 then129 echo "liborder $libsl"130 fi131 libsl="$libsl -l9"133 # cycle: lib9 expects p9main, which is defined in libthread. oops.134 if $havethread135 then136 libsl="$libsl -lthread"137 fi139 if [ "x$needdraw" = xtrue ]140 then141 if [ "x$X11" = "x" ]142 then143 X11=/usr/X11R6144 fi145 libsl="$libsl -L$X11/lib -lX11"146 fi147 fi149 extralibs="-lm"150 tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"151 case "$tag" in152 *OpenBSD*)153 ld=gcc154 extralibs="$extralibs -lutil -lpthread"155 ;;156 *FreeBSD*)157 ld=gcc158 extralibs="$extralibs -lutil"159 case "`uname -r`" in160 [5-9].*)161 extralibs="$extralibs -lpthread"162 ;;163 esac164 ;;165 *BSD*)166 ld=gcc167 extralibs="$extralibs -lutil"168 ;;169 *Linux*)170 ld=gcc171 extralibs="$extralibs -lutil"172 case "`uname -r`" in173 2.6.*)174 extralibs="$extralibs -lpthread"175 ;;176 esac177 ;;178 *Darwin*)179 ld=gcc180 ;;181 *SunOS*)182 ld="${CC9:-cc} -g"183 extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"184 # Record paths to shared libraries to avoid needing LD_LIBRARY_PATH185 for i in "$@"186 do187 case "$i" in188 -L*)189 s=`echo $i | sed 's/-L/-R/'`190 extralibs="$extralibs $s"191 ;;192 esac193 done194 ;;195 *)196 echo do not know how to link on "$tag" 1>&2197 exit 1198 esac200 if $verbose201 then202 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs203 fi204 if $ld -L$PLAN9/lib "$@" $libsl $extralibs205 then206 exit 0207 else208 rm -f $target209 exit 1210 fi