Blob


1 #!/bin/sh
3 libsl=""
5 doautolib=true
6 verbose=false
8 if [ "x$1" = "x-l" ]
9 then
10 shift
11 doautolib=false
12 elif [ "x$1" = "x-v" ]
13 then
14 shift
15 verbose=true
16 fi
18 target=a.out
19 if [ "x$1" = "x-o" ]
20 then
21 target=$2
22 fi
24 if $doautolib
25 then
26 ofiles=""
27 for i
28 do
29 case "$i" in
30 *.o)
31 ofiles="$ofiles $i"
32 ;;
33 esac
34 done
36 # echo "ofiles $ofiles"
37 autolibs=""
38 if [ "x$ofiles" != "x" ]
39 then
40 a=`
41 nm $ofiles |
42 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
43 sed 's/.*__p9l_autolib_//' |
44 sort -u
45 `
46 for i in $a
47 do
48 autolibs="$autolibs $i"
49 eval "need$i=true"
50 done
51 fi
53 # fetch dependencies out of libraries
54 workq="$autolibs"
55 while [ "x$workq" != "x" ]
56 do
57 w="$workq"
58 workq=""
59 for i in $w
60 do
61 a=`
62 nm $PLAN9/lib/lib$i.a |
63 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
64 sed 's/.*__p9l_autolib_//' |
65 sort -u
66 `
67 okayfn="true"
68 for j in $a
69 do
70 if eval "[ x\$need$j = x ]"
71 then
72 autolibs="$autolibs $j"
73 workq="$workq $j"
74 eval "need$j=true"
75 fi
76 if [ $j != $i ]
77 then
78 okayfn="$okayfn && have$j"
79 fi
80 done
81 # echo "can$i: $okayfn"
82 eval "can$i() { $okayfn; }"
83 done
84 done
85 if $verbose
86 then
87 echo "autolibs $autolibs"
88 fi
90 for i in $autolibs
91 do
92 eval "have$i() { false; }"
93 done
95 # now find correct order
96 libsl=""
97 while [ "x$autolibs" != x ]
98 do
99 stillneed=""
100 didnothing=true
101 for i in $autolibs
102 do
103 if eval "can$i"
104 then
105 libsl="-l$i $libsl"
106 eval "have$i() { true; }"
107 didnothing=false
108 else
109 stillneed="$stillneed $i"
110 fi
111 done
112 # break cycle by setting the last library on the list
113 # to have no dependencies
114 if $didnothing
115 then
116 j="xxx"
117 for i in $autolibs
118 do
119 j=$i
120 done
121 echo "dependency cycle: $autolibs; breaking with $j"
122 eval "can$j() { true; }"
123 fi
124 autolibs="$stillneed"
125 done
126 if $verbose
127 then
128 echo "liborder $libsl"
129 fi
130 libsl="$libsl -l9"
132 if [ "x$needdraw" = xtrue ]
133 then
134 if [ "x$X11" = "x" ]
135 then
136 X11=/usr/X11R6
137 fi
138 libsl="$libsl -L$X11/lib -lX11"
139 fi
140 fi
142 extralibs="-lm"
143 tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"
144 case "$tag" in
145 *OpenBSD*)
146 ld=gcc
147 extralibs="$extralibs -lutil -lpthread"
148 ;;
149 *FreeBSD*)
150 ld=gcc
151 extralibs="$extralibs -lutil"
152 case "`uname -r`" in
153 [5-9].*)
154 extralibs="$extralibs -lpthread"
155 ;;
156 esac
157 ;;
158 *BSD*)
159 ld=gcc
160 extralibs="$extralibs -lutil"
161 ;;
162 *Linux*)
163 ld=gcc
164 extralibs="$extralibs -lutil"
165 case "`uname -r`" in
166 2.6.*)
167 extralibs="$extralibs -lpthread"
168 ;;
169 esac
170 ;;
171 *Darwin*)
172 ld=gcc
173 ;;
174 *SunOS*)
175 ld="${CC9:-cc} -g"
176 extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"
177 # Record paths to shared libraries to avoid needing LD_LIBRARY_PATH
178 for i in "$@"
179 do
180 case "$i" in
181 -L*)
182 s=`echo $i | sed 's/-L/-R/'`
183 extralibs="$extralibs $s"
184 ;;
185 esac
186 done
187 ;;
188 *)
189 echo do not know how to link on "$tag" 1>&2
190 exit 1
191 esac
193 if $verbose
194 then
195 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs
196 fi
197 if ! $ld -L$PLAN9/lib "$@" $libsl $extralibs
198 then
199 rm -f $target
200 exit 1
201 fi
202 exit 0