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