Blob


1 #!/bin/sh
3 test -f $PLAN9/config && . $PLAN9/config
4 libsl=""
5 frameworks=""
6 doautolib=true
7 doautoframework=true
8 verbose=false
10 if [ "x$1" = "x-l" ]
11 then
12 shift
13 doautolib=false
14 doautoframework=false
15 elif [ "x$1" = "x-v" ]
16 then
17 shift
18 verbose=true
19 fi
21 target=a.out
22 if [ "x$1" = "x-o" ]
23 then
24 target=$2
25 fi
27 if $doautolib
28 then
29 ofiles=""
30 lpaths="$PLAN9/lib"
31 for i
32 do
33 case "$i" in
34 *.[ao])
35 ofiles="$ofiles $i"
36 ;;
37 -L*)
38 l=`echo $i | sed 's/-L//'`
39 lpaths="$lpaths $l"
40 esac
41 done
43 if $verbose
44 then
45 echo "ofiles $ofiles"
46 echo "lpaths $lpaths"
47 fi
49 autolibs=""
50 if [ "x$ofiles" != "x" ]
51 then
52 a=`
53 nm $ofiles |
54 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
55 sed 's/.*__p9l_autolib_//' |
56 sort -u
57 `
58 for i in $a
59 do
60 autolibs="$autolibs $i"
61 eval "need$i=true"
62 done
63 fi
64 if $verbose
65 then
66 echo "autolibs1 $autolibs"
67 fi
69 # fetch dependencies out of libraries
70 workq="$autolibs"
71 while [ "x$workq" != "x" ]
72 do
73 w="$workq"
74 workq=""
75 for i in $w
76 do
77 # can't trust the libraries about using
78 # libthread or libdraw - we might not be linking with
79 # those object files.
80 a=""
81 for lpath in $lpaths
82 do
83 b=`
84 nm $lpath/lib$i.a 2>/dev/null |
85 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
86 sed 's/.*__p9l_autolib_//' |
87 sort -u |
88 egrep -v '^(thread|draw)$'
89 `
90 a="$a $b"
91 done
92 # fix up libraries that really need draw
93 if [ "x$i" = "xmemdraw" -o "x$i" = "xmemlayer" -o "x$i" = "xframe" ]
94 then
95 a="$a draw"
96 fi
97 okayfn="true"
98 for j in $a
99 do
100 if eval "[ x\$need$j = x ]"
101 then
102 autolibs="$autolibs $j"
103 workq="$workq $j"
104 eval "need$j=true"
105 fi
106 if [ $j != $i ]
107 then
108 okayfn="$okayfn && have$j"
109 fi
110 done
111 if $verbose
112 then
113 echo "can$i: $okayfn"
114 fi
115 eval "can$i() { $okayfn; }"
116 done
117 done
118 if $verbose
119 then
120 echo "autolibs $autolibs"
121 fi
123 for i in $autolibs
124 do
125 eval "have$i() { false; }"
126 done
127 havethread() { false; }
128 havesec() { false; }
129 canmemlayer() { havedraw; }
131 # now find correct order
132 libsl=""
133 while [ "x$autolibs" != x ]
134 do
135 stillneed=""
136 didnothing=true
137 for i in $autolibs
138 do
139 if eval "can$i"
140 then
141 libsl="-l$i $libsl"
142 eval "have$i() { true; }"
143 didnothing=false
144 else
145 stillneed="$stillneed $i"
146 fi
147 done
148 # break cycle by setting the last library on the list
149 # to have no dependencies
150 if $didnothing
151 then
152 j="xxx"
153 for i in $autolibs
154 do
155 j=$i
156 done
157 echo "dependency cycle: $autolibs; breaking with $j"
158 eval "can$j() { true; }"
159 fi
160 autolibs="$stillneed"
161 done
162 if $verbose
163 then
164 echo "liborder $libsl"
165 fi
166 libsl="$libsl -l9"
168 # cycle: lib9 expects p9main, which is defined in libthread. oops.
169 if havethread
170 then
171 libsl="$libsl -lthread -l9"
172 fi
174 # cycle: lib9 netcrypt uses libsec
175 if havesec
176 then
177 libsl="$libsl -lsec -l9"
178 fi
180 if [ "x$needndb" = xtrue -a '(' -f /usr/lib/libresolv.a -o -f /usr/lib/libresolv.dylib ')' ]
181 then
182 libsl="$libsl -lresolv"
183 fi
185 if [ "x$needX11" = xtrue -a "x$WSYSTYPE" != xnowsys ]
186 then
187 if [ "x$X11" = "x" ]
188 then
189 X11=/usr/X11R6
190 fi
191 # x86_64 seems to put its 64-bit libraries in lib64.
192 if [ "${OBJTYPE:-`uname -m`}" = "x86_64" ]
193 then
194 libsl="$libsl -L$X11/lib64"
195 fi
196 libsl="$libsl -L$X11/lib -lX11"
197 fi
198 fi
199 if $doautoframework
200 then
201 ofiles=""
202 for i
203 do
204 case "$i" in
205 *.[ao])
206 ofiles="$ofiles $i"
207 ;;
208 esac
209 done
211 # echo "ofiles $ofiles"
212 autoframeworks=""
213 if [ "x$ofiles" != "x" ]
214 then
215 a=`
216 nm $ofiles |
217 grep '__p9l_autoframework_[a-zA-Z0-9+-]*$' |
218 sed 's/.*__p9l_autoframework_//' |
219 sort -u
221 for i in $a
222 do
223 autoframeworks="$autoframeworks $i"
224 eval "need$i=true"
225 done
226 fi
228 if $verbose
229 then
230 echo "autoframeworks $autoframeworks"
231 fi
233 for i in $autoframeworks
234 do
235 eval "have$i() { false; }"
236 done
238 frameworks=""
239 for i in $autoframeworks
240 do
241 frameworks="-framework $i $frameworks"
242 done
243 fi
245 extralibs="-lm"
246 tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"
247 case "$tag" in
248 *FreeBSD*)
249 ld=gcc
250 userpath=true
251 extralibs="$extralibs -lutil"
252 case "`uname -r`" in
253 5.2.*)
254 extralibs="$extralibs -lkse"
255 ;;
256 [5-9].*)
257 extralibs="$extralibs -lpthread"
258 ;;
259 esac
260 ;;
261 *BSD*)
262 ld=gcc
263 userpath=true
264 extralibs="$extralibs -lutil"
265 ;;
266 *Linux*)
267 ld=gcc
268 userpath=true
269 extralibs="$extralibs -lutil"
270 case "${SYSVERSION:-`uname -r`}" in
271 2.6.*)
272 extralibs="$extralibs -lpthread"
273 ;;
274 esac
275 ;;
276 *Darwin*)
277 ld=gcc
278 ;;
279 *SunOS*)
280 ld="${CC9:-cc} -g"
281 extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"
282 # Record paths to shared libraries to avoid needing LD_LIBRARY_PATH
283 for i in "$libsl $@"
284 do
285 case "$i" in
286 -L*)
287 s=`echo $i | sed 's/-L/-R/'`
288 extralibs="$extralibs $s"
289 ;;
290 esac
291 done
292 case "${SYSVERSION:-`uname -r`}" in
293 5.[67])
294 echo do not know how to link right thread library on "$tag" 1>&2
295 ;;
296 5.8)
297 # Some trickery is needed to force use of
298 # alternate thread lib from /usr/lib/lwp
299 # Likely, this only works with sun cc,
300 # for other compiler/loader we would need other flags.
301 ld="$ld -i"
302 extralibs="$extralibs /usr/lib/lwp/libthread.so -R/usr/lib/lwp:/usr/lib"
303 ;;
304 esac
305 ;;
306 *)
307 echo do not know how to link on "$tag" 1>&2
308 exit 1
309 esac
311 case "$userpath" in
312 true)
313 for i in "$libsl $@"
314 do
315 case "$i" in
316 -L*)
317 s=`echo $i | sed 's/-L/-Wl,-rpath,/'`
318 extralibs="$extralibs $s"
319 ;;
320 esac
321 done
322 ;;
323 esac
325 if $verbose
326 then
327 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs $frameworks
328 fi
330 xtmp=/tmp/9l.$$.$USER.out
331 xxout() {
332 sed 's/.*: In function `[^:]*: *//' $xtmp | egrep . |
333 egrep -v 'is (often|almost always) misused|is dangerous, better use'
334 rm -f $xtmp
337 if $ld -L$PLAN9/lib "$@" $libsl $extralibs $frameworks >$xtmp 2>&1
338 then
339 xxout
340 exit 0
341 else
342 xxout
343 rm -f $target
344 exit 1
345 fi