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