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 extralibs="$extralibs -lpthread"
67 ;;
68 *)
69 echo do not know how to link on "$tag" 1>&2
70 exit 1
71 esac
73 if [ "x$1" = "x-l" ]
74 then
75 shift
76 doautolib=false
77 doautoframework=false
78 elif [ "x$1" = "x-v" ]
79 then
80 shift
81 verbose=true
82 fi
84 target=a.out
85 if [ "x$1" = "x-o" ]
86 then
87 target=$2
88 fi
90 if $doautolib
91 then
92 ofiles=""
93 lpaths="$PLAN9/lib"
94 for i
95 do
96 case "$i" in
97 *.[ao])
98 ofiles="$ofiles $i"
99 ;;
100 -L*)
101 l=`echo $i | sed 's/-L//'`
102 lpaths="$lpaths $l"
103 esac
104 done
106 if $verbose
107 then
108 echo "ofiles $ofiles"
109 echo "lpaths $lpaths"
110 fi
112 autolibs=""
113 if [ "x$ofiles" != "x" ]
114 then
115 a=`
116 nm $nmflags $ofiles |
117 grep '__p9l_autolib_[a-zA-Z0-9+-]*' |
118 sed 's/.*__p9l_autolib_//; s/:.*//' |
119 sort -u
121 for i in $a
122 do
123 autolibs="$autolibs $i"
124 eval "need$i=true"
125 done
126 fi
127 if $verbose
128 then
129 echo "autolibs1 $autolibs"
130 fi
132 # fetch dependencies out of libraries
133 workq="$autolibs"
134 while [ "x$workq" != "x" ]
135 do
136 w="$workq"
137 workq=""
138 for i in $w
139 do
140 # can't trust the libraries about using
141 # libthread or libdraw - we might not be linking with
142 # those object files.
143 a=""
144 for lpath in $lpaths
145 do
146 b=`
147 nm $lpath/lib$i.a 2>/dev/null |
148 grep '__p9l_autolib_[a-zA-Z0-9+-]*' |
149 sed 's/.*__p9l_autolib_//; s/:.*//' |
150 sort -u |
151 egrep -v '^(thread|draw)$'
153 a="$a $b"
154 done
155 # fix up libraries that really need draw
156 if [ "x$i" = "xmemdraw" -o "x$i" = "xmemlayer" -o "x$i" = "xframe" ]
157 then
158 a="$a draw"
159 fi
160 okayfn="true"
161 for j in $a
162 do
163 if eval "[ x\$need$j = x ]"
164 then
165 autolibs="$autolibs $j"
166 workq="$workq $j"
167 eval "need$j=true"
168 fi
169 if [ $j != $i ]
170 then
171 okayfn="$okayfn && have$j"
172 fi
173 done
174 if $verbose
175 then
176 echo "can$i: $okayfn"
177 fi
178 eval "can$i() { $okayfn; }"
179 done
180 done
181 if $verbose
182 then
183 echo "autolibs $autolibs"
184 fi
186 for i in $autolibs
187 do
188 eval "have$i() { false; }"
189 done
190 havethread() { false; }
191 havesec() { false; }
192 canmemlayer() { havedraw; }
194 # now find correct order
195 libsl=""
196 while [ "x$autolibs" != x ]
197 do
198 stillneed=""
199 didnothing=true
200 for i in $autolibs
201 do
202 if eval "can$i"
203 then
204 libsl="-l$i $libsl"
205 eval "have$i() { true; }"
206 didnothing=false
207 else
208 stillneed="$stillneed $i"
209 fi
210 done
211 # break cycle by setting the last library on the list
212 # to have no dependencies
213 if $didnothing
214 then
215 j="xxx"
216 for i in $autolibs
217 do
218 j=$i
219 done
220 echo "dependency cycle: $autolibs; breaking with $j"
221 eval "can$j() { true; }"
222 fi
223 autolibs="$stillneed"
224 done
225 if $verbose
226 then
227 echo "liborder $libsl"
228 fi
229 libsl="$libsl -l9"
231 # cycle: lib9 expects p9main, which is defined in libthread. oops.
232 if havethread
233 then
234 libsl="$libsl -lthread -l9"
235 fi
237 # cycle: lib9 netcrypt uses libsec
238 if havesec
239 then
240 libsl="$libsl -lsec -l9"
241 fi
243 if [ "x$needndb" = xtrue -a '(' -f /usr/lib/libresolv.a -o -f /usr/lib/libresolv.dylib ')' ]
244 then
245 libsl="$libsl -lresolv"
246 fi
248 if [ "x$needX11" = xtrue -a "x$WSYSTYPE" != xnowsys ]
249 then
250 if [ "x$X11" = "x" ]
251 then
252 X11=/usr/X11R6
253 fi
254 # Don't say -L with a non-existent directory: Xcode complains.
255 # x86_64 seems to put its 64-bit libraries in lib64.
256 if [ "${OBJTYPE:-`uname -m`}" = "x86_64" -a -d "$X11/lib64" ]
257 then
258 libsl="$libsl -L$X11/lib64"
259 fi
260 if [ -d "$X11/lib" ]
261 then
262 libsl="$libsl -L$X11/lib"
263 fi
264 libsl="$libsl -lX11"
265 fi
266 fi
267 if $doautoframework
268 then
269 ofiles=""
270 for i
271 do
272 case "$i" in
273 *.[ao])
274 ofiles="$ofiles $i"
275 ;;
276 esac
277 done
279 # echo "ofiles $ofiles"
280 autoframeworks=""
281 if [ "x$ofiles" != "x" ]
282 then
283 a=`
284 nm $ofiles |
285 grep '__p9l_autoframework_[a-zA-Z0-9+-]*$' |
286 sed 's/.*__p9l_autoframework_//' |
287 sort -u
289 for i in $a
290 do
291 autoframeworks="$autoframeworks $i"
292 eval "need$i=true"
293 done
294 fi
296 if $verbose
297 then
298 echo "autoframeworks $autoframeworks"
299 fi
301 for i in $autoframeworks
302 do
303 eval "have$i() { false; }"
304 done
306 frameworks=""
307 for i in $autoframeworks
308 do
309 frameworks="-framework $i $frameworks"
310 done
311 fi
313 case "$userpath" in
314 true)
315 for i in "$libsl $@"
316 do
317 case "$i" in
318 -L*)
319 s=`echo $i | sed 's/-L/-Wl,-rpath,/'`
320 extralibs="$extralibs $s"
321 ;;
322 esac
323 done
324 ;;
325 esac
327 if $verbose
328 then
329 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs $frameworks
330 fi
332 xtmp="${TMPDIR-/tmp}/9l.$$.$USER.out"
333 xxout() {
334 sed 's/.*: In function `[^:]*: *//' $xtmp | egrep . |
335 egrep -v 'is (often|almost always) misused|is dangerous, better use|text-based stub'
336 rm -f $xtmp
339 if $ld -L$PLAN9/lib "$@" $libsl $extralibs $frameworks >$xtmp 2>&1
340 then
341 xxout
342 exit 0
343 else
344 xxout
345 rm -f $target
346 exit 1
347 fi