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