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