Blob


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