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