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