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 |
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; }
101 # now find correct order
102 libsl=""
103 while [ "x$autolibs" != x ]
104 do
105 stillneed=""
106 didnothing=true
107 for i in $autolibs
108 do
109 if eval "can$i"
110 then
111 libsl="-l$i $libsl"
112 eval "have$i() { true; }"
113 didnothing=false
114 else
115 stillneed="$stillneed $i"
116 fi
117 done
118 # break cycle by setting the last library on the list
119 # to have no dependencies
120 if $didnothing
121 then
122 j="xxx"
123 for i in $autolibs
124 do
125 j=$i
126 done
127 echo "dependency cycle: $autolibs; breaking with $j"
128 eval "can$j() { true; }"
129 fi
130 autolibs="$stillneed"
131 done
132 if $verbose
133 then
134 echo "liborder $libsl"
135 fi
136 libsl="$libsl -l9"
138 # cycle: lib9 expects p9main, which is defined in libthread. oops.
139 if havethread
140 then
141 libsl="$libsl -lthread -l9"
142 fi
144 if [ "x$needdraw" = xtrue ]
145 then
146 if [ "x$X11" = "x" ]
147 then
148 X11=/usr/X11R6
149 fi
150 # x86_64 seems to put its 64-bit libraries in lib64.
151 if [ "${OBJTYPE:-`uname -m`}" = "x86_64" ]
152 then
153 libsl="$libsl -L$X11/lib64"
154 fi
155 libsl="$libsl -L$X11/lib -lX11"
156 fi
157 fi
159 extralibs="-lm"
160 tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"
161 case "$tag" in
162 *OpenBSD*)
163 ld=gcc
164 userpath=true
165 extralibs="$extralibs -lutil -lpthread"
166 ;;
167 *FreeBSD*)
168 ld=gcc
169 userpath=true
170 extralibs="$extralibs -lutil"
171 case "`uname -r`" in
172 5.2.*)
173 extralibs="$extralibs -lkse"
174 ;;
175 [5-9].*)
176 extralibs="$extralibs -lpthread"
177 ;;
178 esac
179 ;;
180 *BSD*)
181 ld=gcc
182 userpath=true
183 extralibs="$extralibs -lutil"
184 ;;
185 *Linux*)
186 ld=gcc
187 userpath=true
188 extralibs="$extralibs -lutil"
189 case "${SYSVERSION:-`uname -r`}" in
190 2.6.*)
191 extralibs="$extralibs -lpthread"
192 ;;
193 esac
194 ;;
195 *Darwin*)
196 ld=gcc
197 ;;
198 *SunOS*)
199 ld="${CC9:-cc} -g"
200 extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"
201 # Record paths to shared libraries to avoid needing LD_LIBRARY_PATH
202 for i in "$libsl $@"
203 do
204 case "$i" in
205 -L*)
206 s=`echo $i | sed 's/-L/-R/'`
207 extralibs="$extralibs $s"
208 ;;
209 esac
210 done
211 case "${SYSVERSION:-`uname -r`}" in
212 5.[67])
213 echo do not know how to link right thread library on "$tag" 1>&2
214 ;;
215 5.8)
216 # Some trickery is needed to force use of
217 # alternate thread lib from /usr/lib/lwp
218 # Likely, this only works with sun cc,
219 # for other compiler/loader we would need other flags.
220 ld="$ld -i"
221 extralibs="$extralibs /usr/lib/lwp/libthread.so -R/usr/lib/lwp:/usr/lib"
222 ;;
223 esac
224 ;;
225 *)
226 echo do not know how to link on "$tag" 1>&2
227 exit 1
228 esac
230 case "$userpath" in
231 true)
232 for i in "$libsl $@"
233 do
234 case "$i" in
235 -L*)
236 s=`echo $i | sed 's/-L/-Wl,-rpath,/'`
237 extralibs="$extralibs $s"
238 ;;
239 esac
240 done
241 ;;
242 esac
244 if $verbose
245 then
246 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs
247 fi
248 if $ld -L$PLAN9/lib "$@" $libsl $extralibs
249 then
250 exit 0
251 else
252 rm -f $target
253 exit 1
254 fi