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 - 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 grep -v thread
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$needdraw" = xtrue ]
154 then
155 if [ "x$X11" = "x" ]
156 then
157 X11=/usr/X11R6
158 fi
159 # x86_64 seems to put its 64-bit libraries in lib64.
160 if [ "${OBJTYPE:-`uname -m`}" = "x86_64" ]
161 then
162 libsl="$libsl -L$X11/lib64"
163 fi
164 libsl="$libsl -L$X11/lib -lX11"
165 fi
166 fi
167 if $doautoframework
168 then
169 ofiles=""
170 for i
171 do
172 case "$i" in
173 *.[ao])
174 ofiles="$ofiles $i"
175 ;;
176 esac
177 done
179 # echo "ofiles $ofiles"
180 autoframeworks=""
181 if [ "x$ofiles" != "x" ]
182 then
183 a=`
184 nm $ofiles |
185 grep '__p9l_autoframework_[a-zA-Z0-9+-]*$' |
186 sed 's/.*__p9l_autoframework_//' |
187 sort -u
189 for i in $a
190 do
191 autoframeworks="$autoframeworks $i"
192 eval "need$i=true"
193 done
194 fi
196 if $verbose
197 then
198 echo "autoframeworks $autoframeworks"
199 fi
201 for i in $autoframeworks
202 do
203 eval "have$i() { false; }"
204 done
206 frameworks=""
207 for i in $autoframeworks
208 do
209 frameworks="-framework $i $frameworks"
210 done
211 fi
213 extralibs="-lm"
214 tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"
215 case "$tag" in
216 *FreeBSD*)
217 ld=gcc
218 userpath=true
219 extralibs="$extralibs -lutil"
220 case "`uname -r`" in
221 5.2.*)
222 extralibs="$extralibs -lkse"
223 ;;
224 [5-9].*)
225 extralibs="$extralibs -lpthread"
226 ;;
227 esac
228 ;;
229 *BSD*)
230 ld=gcc
231 userpath=true
232 extralibs="$extralibs -lutil"
233 ;;
234 *Linux*)
235 ld=gcc
236 userpath=true
237 extralibs="$extralibs -lutil"
238 case "${SYSVERSION:-`uname -r`}" in
239 2.6.*)
240 extralibs="$extralibs -lpthread"
241 ;;
242 esac
243 ;;
244 *Darwin*)
245 ld=gcc
246 ;;
247 *SunOS*)
248 ld="${CC9:-cc} -g"
249 extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"
250 # Record paths to shared libraries to avoid needing LD_LIBRARY_PATH
251 for i in "$libsl $@"
252 do
253 case "$i" in
254 -L*)
255 s=`echo $i | sed 's/-L/-R/'`
256 extralibs="$extralibs $s"
257 ;;
258 esac
259 done
260 case "${SYSVERSION:-`uname -r`}" in
261 5.[67])
262 echo do not know how to link right thread library on "$tag" 1>&2
263 ;;
264 5.8)
265 # Some trickery is needed to force use of
266 # alternate thread lib from /usr/lib/lwp
267 # Likely, this only works with sun cc,
268 # for other compiler/loader we would need other flags.
269 ld="$ld -i"
270 extralibs="$extralibs /usr/lib/lwp/libthread.so -R/usr/lib/lwp:/usr/lib"
271 ;;
272 esac
273 ;;
274 *)
275 echo do not know how to link on "$tag" 1>&2
276 exit 1
277 esac
279 case "$userpath" in
280 true)
281 for i in "$libsl $@"
282 do
283 case "$i" in
284 -L*)
285 s=`echo $i | sed 's/-L/-Wl,-rpath,/'`
286 extralibs="$extralibs $s"
287 ;;
288 esac
289 done
290 ;;
291 esac
293 if $verbose
294 then
295 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs $frameworks
296 fi
298 xtmp=/tmp/9l.$$.$USER.out
299 xxout() {
300 egrep -v ': In function `' $xtmp
301 rm -f $xtmp
304 if $ld -L$PLAN9/lib "$@" $libsl $extralibs $frameworks >$xtmp 2>&1
305 then
306 xxout
307 exit 0
308 else
309 xxout
310 rm -f $target
311 exit 1
312 fi