Blob


1 #!/bin/sh
3 test -f $PLAN9/config && . $PLAN9/config
4 libsl=""
6 doautolib=true
7 verbose=false
9 if [ "x$1" = "x-l" ]
10 then
11 shift
12 doautolib=false
13 elif [ "x$1" = "x-v" ]
14 then
15 shift
16 verbose=true
17 fi
19 target=a.out
20 if [ "x$1" = "x-o" ]
21 then
22 target=$2
23 fi
25 if $doautolib
26 then
27 ofiles=""
28 for i
29 do
30 case "$i" in
31 *.[ao])
32 ofiles="$ofiles $i"
33 ;;
34 esac
35 done
37 # echo "ofiles $ofiles"
38 autolibs=""
39 if [ "x$ofiles" != "x" ]
40 then
41 a=`
42 nm $ofiles |
43 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
44 sed 's/.*__p9l_autolib_//' |
45 sort -u
46 `
47 for i in $a
48 do
49 autolibs="$autolibs $i"
50 eval "need$i=true"
51 done
52 fi
54 # fetch dependencies out of libraries
55 workq="$autolibs"
56 while [ "x$workq" != "x" ]
57 do
58 w="$workq"
59 workq=""
60 for i in $w
61 do
62 # can't trust the libraries about using
63 # libthread - we might not be linking with
64 # those object files.
65 a=`
66 nm $PLAN9/lib/lib$i.a 2>/dev/null |
67 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
68 sed 's/.*__p9l_autolib_//' |
69 sort -u |
70 grep -v thread
71 `
72 okayfn="true"
73 for j in $a
74 do
75 if eval "[ x\$need$j = x ]"
76 then
77 autolibs="$autolibs $j"
78 workq="$workq $j"
79 eval "need$j=true"
80 fi
81 if [ $j != $i ]
82 then
83 okayfn="$okayfn && have$j"
84 fi
85 done
86 # echo "can$i: $okayfn"
87 eval "can$i() { $okayfn; }"
88 done
89 done
90 if $verbose
91 then
92 echo "autolibs $autolibs"
93 fi
95 for i in $autolibs
96 do
97 eval "have$i() { false; }"
98 done
99 havethread() { false; }
100 havesec() { false; }
102 # now find correct order
103 libsl=""
104 while [ "x$autolibs" != x ]
105 do
106 stillneed=""
107 didnothing=true
108 for i in $autolibs
109 do
110 if eval "can$i"
111 then
112 libsl="-l$i $libsl"
113 eval "have$i() { true; }"
114 didnothing=false
115 else
116 stillneed="$stillneed $i"
117 fi
118 done
119 # break cycle by setting the last library on the list
120 # to have no dependencies
121 if $didnothing
122 then
123 j="xxx"
124 for i in $autolibs
125 do
126 j=$i
127 done
128 echo "dependency cycle: $autolibs; breaking with $j"
129 eval "can$j() { true; }"
130 fi
131 autolibs="$stillneed"
132 done
133 if $verbose
134 then
135 echo "liborder $libsl"
136 fi
137 libsl="$libsl -l9"
139 # cycle: lib9 expects p9main, which is defined in libthread. oops.
140 if havethread
141 then
142 libsl="$libsl -lthread -l9"
143 fi
145 # cycle: lib9 netcrypt uses libsec
146 if havesec
147 then
148 libsl="$libsl -lsec -l9"
149 fi
151 if [ "x$needdraw" = xtrue ]
152 then
153 if [ "x$X11" = "x" ]
154 then
155 X11=/usr/X11R6
156 fi
157 # x86_64 seems to put its 64-bit libraries in lib64.
158 if [ "${OBJTYPE:-`uname -m`}" = "x86_64" ]
159 then
160 libsl="$libsl -L$X11/lib64"
161 fi
162 libsl="$libsl -L$X11/lib -lX11"
163 fi
164 fi
166 extralibs="-lm"
167 tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"
168 case "$tag" in
169 *OpenBSD*)
170 ld=gcc
171 userpath=true
172 extralibs="$extralibs -lutil"
173 ;;
174 *FreeBSD*)
175 ld=gcc
176 userpath=true
177 extralibs="$extralibs -lutil"
178 case "`uname -r`" in
179 5.2.*)
180 extralibs="$extralibs -lkse"
181 ;;
182 [5-9].*)
183 extralibs="$extralibs -lpthread"
184 ;;
185 esac
186 ;;
187 *BSD*)
188 ld=gcc
189 userpath=true
190 extralibs="$extralibs -lutil"
191 ;;
192 *Linux*)
193 ld=gcc
194 userpath=true
195 extralibs="$extralibs -lutil"
196 case "${SYSVERSION:-`uname -r`}" in
197 2.6.*)
198 extralibs="$extralibs -lpthread"
199 ;;
200 esac
201 ;;
202 *Darwin*)
203 ld=gcc
204 ;;
205 *SunOS*)
206 ld="${CC9:-cc} -g"
207 extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"
208 # Record paths to shared libraries to avoid needing LD_LIBRARY_PATH
209 for i in "$libsl $@"
210 do
211 case "$i" in
212 -L*)
213 s=`echo $i | sed 's/-L/-R/'`
214 extralibs="$extralibs $s"
215 ;;
216 esac
217 done
218 case "${SYSVERSION:-`uname -r`}" in
219 5.[67])
220 echo do not know how to link right thread library on "$tag" 1>&2
221 ;;
222 5.8)
223 # Some trickery is needed to force use of
224 # alternate thread lib from /usr/lib/lwp
225 # Likely, this only works with sun cc,
226 # for other compiler/loader we would need other flags.
227 ld="$ld -i"
228 extralibs="$extralibs /usr/lib/lwp/libthread.so -R/usr/lib/lwp:/usr/lib"
229 ;;
230 esac
231 ;;
232 *)
233 echo do not know how to link on "$tag" 1>&2
234 exit 1
235 esac
237 case "$userpath" in
238 true)
239 for i in "$libsl $@"
240 do
241 case "$i" in
242 -L*)
243 s=`echo $i | sed 's/-L/-Wl,-rpath,/'`
244 extralibs="$extralibs $s"
245 ;;
246 esac
247 done
248 ;;
249 esac
251 if $verbose
252 then
253 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs
254 fi
256 xtmp=/tmp/9l.$$.$USER.out
257 xxout() {
258 egrep -v ': In function `' $xtmp
259 rm -f $xtmp
262 if $ld -L$PLAN9/lib "$@" $libsl $extralibs >$xtmp 2>&1
263 then
264 xxout
265 exit 0
266 else
267 xxout
268 rm -f $target
269 exit 1
270 fi