Blob


1 #!/bin/sh
3 libsl=""
5 doautolib=true
6 verbose=false
8 if [ "x$1" = "x-l" ]
9 then
10 shift
11 doautolib=false
12 elif [ "x$1" = "x-v" ]
13 then
14 shift
15 verbose=true
16 fi
18 target=a.out
19 if [ "x$1" = "x-o" ]
20 then
21 target=$2
22 fi
24 if $doautolib
25 then
26 ofiles=""
27 for i
28 do
29 case "$i" in
30 *.[ao])
31 ofiles="$ofiles $i"
32 ;;
33 esac
34 done
36 # echo "ofiles $ofiles"
37 autolibs=""
38 if [ "x$ofiles" != "x" ]
39 then
40 a=`
41 nm $ofiles |
42 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
43 sed 's/.*__p9l_autolib_//' |
44 sort -u
45 `
46 for i in $a
47 do
48 autolibs="$autolibs $i"
49 eval "need$i=true"
50 done
51 fi
53 # fetch dependencies out of libraries
54 workq="$autolibs"
55 while [ "x$workq" != "x" ]
56 do
57 w="$workq"
58 workq=""
59 for i in $w
60 do
61 # can't trust the libraries about using
62 # libthread - we might not be linking with
63 # those object files.
64 a=`
65 nm $PLAN9/lib/lib$i.a |
66 grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
67 sed 's/.*__p9l_autolib_//' |
68 sort -u |
69 grep -v thread
70 `
71 okayfn="true"
72 for j in $a
73 do
74 if eval "[ x\$need$j = x ]"
75 then
76 autolibs="$autolibs $j"
77 workq="$workq $j"
78 eval "need$j=true"
79 fi
80 if [ $j != $i ]
81 then
82 okayfn="$okayfn && have$j"
83 fi
84 done
85 # echo "can$i: $okayfn"
86 eval "can$i() { $okayfn; }"
87 done
88 done
89 if $verbose
90 then
91 echo "autolibs $autolibs"
92 fi
94 for i in $autolibs
95 do
96 eval "have$i() { false; }"
97 done
98 havethread() { false; }
100 # now find correct order
101 libsl=""
102 while [ "x$autolibs" != x ]
103 do
104 stillneed=""
105 didnothing=true
106 for i in $autolibs
107 do
108 if eval "can$i"
109 then
110 libsl="-l$i $libsl"
111 eval "have$i() { true; }"
112 didnothing=false
113 else
114 stillneed="$stillneed $i"
115 fi
116 done
117 # break cycle by setting the last library on the list
118 # to have no dependencies
119 if $didnothing
120 then
121 j="xxx"
122 for i in $autolibs
123 do
124 j=$i
125 done
126 echo "dependency cycle: $autolibs; breaking with $j"
127 eval "can$j() { true; }"
128 fi
129 autolibs="$stillneed"
130 done
131 if $verbose
132 then
133 echo "liborder $libsl"
134 fi
135 libsl="$libsl -l9"
137 # cycle: lib9 expects p9main, which is defined in libthread. oops.
138 if havethread
139 then
140 libsl="$libsl -lthread -l9"
141 fi
143 if [ "x$needdraw" = xtrue ]
144 then
145 if [ "x$X11" = "x" ]
146 then
147 X11=/usr/X11R6
148 fi
149 libsl="$libsl -L$X11/lib -lX11"
150 fi
151 fi
153 extralibs="-lm"
154 tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"
155 case "$tag" in
156 *OpenBSD*)
157 ld=gcc
158 extralibs="$extralibs -lutil -lpthread"
159 ;;
160 *FreeBSD*)
161 ld=gcc
162 extralibs="$extralibs -lutil"
163 case "`uname -r`" in
164 [5-9].*)
165 extralibs="$extralibs -lpthread"
166 ;;
167 esac
168 ;;
169 *BSD*)
170 ld=gcc
171 extralibs="$extralibs -lutil"
172 ;;
173 *Linux*)
174 ld=gcc
175 extralibs="$extralibs -lutil"
176 case "`uname -r`" in
177 2.6.*)
178 extralibs="$extralibs -lpthread"
179 ;;
180 esac
181 ;;
182 *Darwin*)
183 ld=gcc
184 ;;
185 *SunOS*)
186 ld="${CC9:-cc} -g"
187 extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"
188 # Record paths to shared libraries to avoid needing LD_LIBRARY_PATH
189 for i in "$@"
190 do
191 case "$i" in
192 -L*)
193 s=`echo $i | sed 's/-L/-R/'`
194 extralibs="$extralibs $s"
195 ;;
196 esac
197 done
198 ;;
199 *)
200 echo do not know how to link on "$tag" 1>&2
201 exit 1
202 esac
204 if $verbose
205 then
206 echo $ld -L$PLAN9/lib "$@" $libsl $extralibs
207 fi
208 if $ld -L$PLAN9/lib "$@" $libsl $extralibs
209 then
210 exit 0
211 else
212 rm -f $target
213 exit 1
214 fi