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