aboutsummaryrefslogtreecommitdiff
path: root/bin/9l
blob: 8af271543c127b7ee082f20e2056f03d75b8ab6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/sh

test -f $PLAN9/config && . $PLAN9/config
libsl=""
frameworks=""
doautolib=true
doautoframework=true
verbose=false

nmflags=""
extralibs="-lm"
tag="${SYSNAME:-`uname`}-${OBJTYPE:-`uname -m`}"
case "$tag" in
*DragonFly*|*BSD*)
	ld=${CC9:-gcc}
	userpath=true
	extralibs="$extralibs -lutil"
	;;
*OSF1*)
	ld=${CC9:-cc}
	userpath=true
	extralibs="$extralibs -lutil"
	nmflags="-B"
	;;
*Linux*)
	ld=${CC9:-gcc}
	userpath=true
	extralibs="$extralibs -lutil -lresolv"
	case "${SYSVERSION:-`uname -r`}" in
	2.6.* | [3-9].* | [1-9][0-9].*)
		extralibs="$extralibs -lpthread"
		;;
	esac
	;;
*Darwin*x86_64*)
	ld="${CC9:-gcc} -m64"
	;;
*Darwin*)
	ld="${CC9:-gcc} -m32"
	;;
*SunOS*)
	ld="${CC9:-cc} -g"
	extralibs="$extralibs -lrt -lpthread -lsocket -lnsl"
	# Record paths to shared libraries to avoid needing LD_LIBRARY_PATH 
	for i in "$libsl $@"
	do
		case "$i" in
		-L*)
			s=`echo $i | sed 's/-L/-R/'`
			extralibs="$extralibs $s"
			;;
		esac
	done
	case "${SYSVERSION:-`uname -r`}" in
	5.[67])
		echo do not know how to link right thread library on "$tag" 1>&2
		;;
	5.8)
		# Some trickery is needed to force use of
		# alternate thread lib from /usr/lib/lwp
		# Likely, this only works with sun cc,
		# for other compiler/loader we would need other flags.
		ld="$ld -i"
		extralibs="$extralibs /usr/lib/lwp/libthread.so -R/usr/lib/lwp:/usr/lib"
		;;
	esac
	;;
*AIX*)
	ld=${CC9:-gcc}
	nmflags="-B"
	;;
*)
	echo do not know how to link on "$tag" 1>&2
	exit 1
esac

if [ "x$1" = "x-l" ]
then
	shift
	doautolib=false
	doautoframework=false
elif [ "x$1" = "x-v" ]
then
	shift
	verbose=true
fi

target=a.out
if [ "x$1" = "x-o" ]
then
	target=$2
fi

if $doautolib
then
	ofiles=""
	lpaths="$PLAN9/lib"
	for i
	do
		case "$i" in
		*.[ao])
			ofiles="$ofiles $i"
			;;
		-L*)
			l=`echo $i | sed 's/-L//'`
			lpaths="$lpaths $l"
		esac
	done
	
	if $verbose
	then
		echo "ofiles $ofiles"
		echo "lpaths $lpaths"
	fi

	autolibs=""
	if [ "x$ofiles" != "x" ]
	then
		a=`
			nm $nmflags $ofiles |
			grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
			sed 's/.*__p9l_autolib_//' |
			sort -u
		`
		for i in $a
		do
			autolibs="$autolibs $i"
			eval "need$i=true"
		done
	fi
	if $verbose
	then
		echo "autolibs1 $autolibs"
	fi

	# fetch dependencies out of libraries
	workq="$autolibs"
	while [ "x$workq" != "x" ]
	do
		w="$workq"
		workq=""
		for i in $w
		do
			# can't trust the libraries about using
			# libthread or libdraw - we might not be linking with
			# those object files.
			a=""
			for lpath in $lpaths
			do
				b=`
					nm $lpath/lib$i.a 2>/dev/null |
					grep '__p9l_autolib_[a-zA-Z0-9+-]*$' |
					sed 's/.*__p9l_autolib_//' |
					sort -u |
					egrep -v '^(thread|draw)$'
				`
				a="$a $b"
			done
			# fix up libraries that really need draw
			if [ "x$i" = "xmemdraw" -o "x$i" = "xmemlayer" -o "x$i" = "xframe" ]
			then
				a="$a draw"
			fi
			okayfn="true"
			for j in $a
			do
				if eval "[ x\$need$j = x ]"
				then
					autolibs="$autolibs $j"
					workq="$workq $j"
					eval "need$j=true"
				fi
				if [ $j != $i ]
				then
					okayfn="$okayfn && have$j"
				fi
			done
			if $verbose
			then
				echo "can$i: $okayfn"
			fi
			eval "can$i() { $okayfn; }"
		done
	done
	if $verbose
	then
		echo "autolibs $autolibs"
	fi

	for i in $autolibs
	do
		eval "have$i() { false; }"
	done
	havethread() { false; }
	havesec() { false; }
	canmemlayer() { havedraw; }

	# now find correct order
	libsl=""
	while [ "x$autolibs" != x ]
	do
		stillneed=""
		didnothing=true
		for i in $autolibs
		do
			if eval "can$i"
			then
				libsl="-l$i $libsl"
				eval "have$i() { true; }"
				didnothing=false
			else
				stillneed="$stillneed $i"
			fi
		done
		# break cycle by setting the last library on the list
		# to have no dependencies
		if $didnothing
		then
			j="xxx"
			for i in $autolibs
			do
				j=$i
			done
			echo "dependency cycle: $autolibs; breaking with $j"
			eval "can$j() { true; }"
		fi
		autolibs="$stillneed"
	done
	if $verbose
	then
		echo "liborder $libsl"
	fi
	libsl="$libsl -l9"

	# cycle: lib9 expects p9main, which is defined in libthread.  oops.
	if havethread
	then
		libsl="$libsl -lthread -l9"
	fi

	# cycle: lib9 netcrypt uses libsec
	if havesec
	then
		libsl="$libsl -lsec -l9"
	fi

	if [ "x$needndb" = xtrue -a '(' -f /usr/lib/libresolv.a -o -f /usr/lib/libresolv.dylib ')' ]
	then
		libsl="$libsl -lresolv"
	fi

	if [ "x$needX11" = xtrue -a "x$WSYSTYPE" != xnowsys ]
	then
		if [ "x$X11" = "x" ]
		then
			X11=/usr/X11R6
		fi
		# Don't say -L with a non-existent directory: Xcode complains.
		# x86_64 seems to put its 64-bit libraries in lib64.
		if [ "${OBJTYPE:-`uname -m`}" = "x86_64" -a -d "$X11/lib64" ]
		then
			libsl="$libsl -L$X11/lib64"
		fi
		if [ -d "$X11/lib" ]
		then
			libsl="$libsl -L$X11/lib"
		fi
		libsl="$libsl -lX11"
	fi
fi
if $doautoframework
then
	ofiles=""
	for i
	do
		case "$i" in
		*.[ao])
			ofiles="$ofiles $i"
			;;
		esac
	done

	# echo "ofiles $ofiles"
	autoframeworks=""
	if [ "x$ofiles" != "x" ]
	then
		a=`
			nm $ofiles |
			grep '__p9l_autoframework_[a-zA-Z0-9+-]*$' |
			sed 's/.*__p9l_autoframework_//' |
			sort -u
		`
		for i in $a
		do
			autoframeworks="$autoframeworks $i"
			eval "need$i=true"
		done
	fi

	if $verbose
	then
		echo "autoframeworks $autoframeworks"
	fi

	for i in $autoframeworks
	do
		eval "have$i() { false; }"
	done

	frameworks=""
	for i in $autoframeworks
	do	
		frameworks="-framework $i $frameworks"
	done
fi

case "$userpath" in
true)
	for i in "$libsl $@"
	do
		case "$i" in
		-L*)
			s=`echo $i | sed 's/-L/-Wl,-rpath,/'`
			extralibs="$extralibs $s"
			;;
		esac
	done
	;;
esac

if $verbose
then
	echo $ld -L$PLAN9/lib "$@" $libsl $extralibs $frameworks
fi

xtmp="${TMPDIR-/tmp}/9l.$$.$USER.out"
xxout() {
	sed 's/.*: In function `[^:]*: *//' $xtmp | egrep . | 
	egrep -v 'is (often|almost always) misused|is dangerous, better use|text-based stub' 
	rm -f $xtmp
}

if $ld -L$PLAN9/lib "$@" $libsl $extralibs $frameworks >$xtmp 2>&1
then
	xxout
	exit 0
else
	xxout
	rm -f $target
	exit 1
fi