sets.subr revision 1.52
1#	$NetBSD: sets.subr,v 1.52 2006/07/12 13:39:04 lukem Exp $
2#
3
4#
5# The following variables contain defaults for sets.subr functions and callers:
6#	setsdir			path to src/distrib/sets
7#	nlists			list of base sets
8#	xlists			list of x11 sets
9#	obsolete		controls if obsolete files are selected instead
10#	lkm			if != "no", enable LKM sets
11#	shlib			shared library format (a.out, elf, or "")
12#	stlib			static library format (a.out, elf)
13#	x11_version		version of XFree86. one of:
14#				   ""	cross built from src/x11
15#				   3	XFree86 3.x from xsrc/xc
16#				   4	XFree86 4.x from xsrc/xfree/xc
17#
18# The following <bsd.own.mk> variables are exported to the environment:
19#	MACHINE	
20#	MACHINE_ARCH
21#	MACHINE_CPU
22#	HAVE_GCC
23#	HAVE_GDB
24#	TOOLCHAIN_MISSING
25#	OBJECT_FMT
26# as well as:
27#
28MKVARS="\
29	MKBFD		\
30	MKCATPAGES	\
31	MKCRYPTO	\
32	MKCRYPTO_IDEA	\
33	MKCRYPTO_MDC2	\
34	MKCRYPTO_RC5	\
35	MKCVS		\
36	MKGCC		\
37	MKGCCCMDS	\
38	MKDOC		\
39	MKGDB		\
40	MKHESIOD	\
41	MKINFO		\
42	MKIPFILTER	\
43	MKINET6		\
44	MKISCSI		\
45	MKKERBEROS	\
46	MKLINT		\
47	MKMAN		\
48	MKMANPAGES	\
49	MKMANZ		\
50	MKNLS		\
51	MKPAM		\
52	MKPF		\
53	MKPIC		\
54	MKPOSTFIX	\
55	MKPROFILE	\
56	MKSENDMAIL	\
57	MKSHARE		\
58	MKSKEY		\
59	MKUUCP		\
60	MKX11		\
61	MKYP		\
62	USE_INET6	\
63	USE_KERBEROS	\
64	USE_YP		\
65"
66#
67# The following variables refer to tools that are used when building sets:
68#
69: ${AWK:=awk}
70: ${CKSUM:=cksum}
71: ${COMM:=comm}
72: ${DATE:=date}
73: ${DB:=db}
74: ${EGREP:=egrep}
75: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
76: ${FGREP:=fgrep}
77: ${FIND:=find}
78: ${GREP:=grep}
79: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
80: ${HOSTNAME:=hostname}
81: ${HOST_SH:=sh}
82: ${IDENT:=ident}
83: ${JOIN:=join}
84: ${LS:=ls}
85: ${MAKE:=make}
86: ${MKTEMP:=mktemp}
87: ${MTREE:=mtree}
88: ${PASTE:=paste}
89: ${PAX:=pax}
90: ${PKG_CREATE:=pkg_create}
91: ${PRINTF:=printf}
92: ${SED:=sed}
93: ${SORT:=sort}
94: ${STAT:=stat}
95: ${TSORT:=tsort}
96: ${UNAME:=uname}
97: ${WC:=wc}
98
99#
100# If printf is a shell builtin command, then we can
101# implement cheaper versions of basename and dirname
102# that do not involve any fork/exec overhead.
103# If printf is not builtin, approximate it using echo,
104# and hope there are no weird file names that cause
105# some versions of echo to do the wrong thing.
106# (Converting to this version of dirname speeded up the
107# syspkgdeps script by an order of magnitude, from 68
108# seconds to 6.3 seconds on one particular host.)
109#
110# Note that naive approximations for dirname
111# using ${foo%/*} do not do the right thing in cases
112# where the result should be "/" or ".".
113#
114case "$(type printf)" in
115*builtin*)
116	basename ()
117	{
118		local bn
119		bn="${1##*/}"
120		bn="${bn%$2}"
121		printf "%s\n" "$bn"
122	}
123	dirname ()
124	{
125		local dn
126		case "$1" in
127		?*/*)	dn="${1%/*}" ;;
128		/*)	dn=/ ;;
129		*)	dn=. ;;
130		esac
131		printf "%s\n" "$dn"
132	}
133	;;
134*)
135	basename ()
136	{
137		local bn
138		bn="${1##*/}"
139		bn="${bn%$2}"
140		echo "$bn"
141	}
142	dirname ()
143	{
144		local dn
145		case "$1" in
146		?*/*)	dn="${1%/*}" ;;
147		/*)	dn=/ ;;
148		*)	dn=. ;;
149		esac
150		echo "$dn"
151	}
152	;;
153esac
154
155oIFS=$IFS
156IFS="
157"
158for x in $(
159${MAKE} -B -f- all <<EOMAKE
160.include <bsd.own.mk>
161.if (\${MKMAN} == "no" || empty(MANINSTALL:Mmaninstall))
162MKMANPAGES=no
163.else
164MKMANPAGES=yes
165.endif
166all:
167.for i in MACHINE MACHINE_ARCH MACHINE_CPU \
168		HAVE_GCC HAVE_GDB OBJECT_FMT TOOLCHAIN_MISSING \
169		${MKVARS}
170	@echo "export \$i=\${\$i}"
171.endfor
172.if (\${MKX11:Uno} != "no")
173	@echo x11_version=""
174.else
175	@echo x11_version=4
176.endif
177
178EOMAKE
179); do
180#	echo 1>&2 "DEBUG: read $x"
181	eval $x
182done
183IFS=$oIFS
184
185setsdir=${0%/*}
186nlists="base comp etc games man misc text"
187case $x11_version in
1883)	xlists="xbase3 xcomp3 xcontrib3 xfont3 xmisc3 xserver3" ;;
1894)	xlists="xbase4 xcomp4 xcontrib4 xfont4 xmisc4 xserver4" ;;
190"")	xlists="xbase xcomp xetc xfont xserver" ;;
191*)	xlists="" ;;	# unknown!
192esac
193obsolete=0
194lkm=yes
195if [ "${MACHINE}" = "evbppc" ]; then
196	lkm=no				# Turn off LKMs for some ports.
197fi
198# Determine lib type. Do this first so stlib also gets set.
199if [ "${OBJECT_FMT}" = "ELF" ]; then
200	shlib=elf
201else
202	shlib=aout
203fi
204stlib=$shlib
205# Now check for MKPIC or specials and turn off shlib if need be.
206if [ "${MKPIC}" = "no" ]; then
207	shlib=no
208fi
209if [ "${MACHINE_ARCH}" = "m68000" ]; then
210	shlib=no			# Turn off shlibs for some ports.
211fi
212
213#
214# list_set_files setfile [...]
215# 
216# Produce a packing list for setfile(s).
217# In each file, a record consists of a path and a System Package name,
218# separated by whitespace. E.g.,
219#
220# 	# $NetBSD: sets.subr,v 1.52 2006/07/12 13:39:04 lukem Exp $
221# 	.			base-sys-root	[keyword[,...]]
222# 	./altroot		base-sys-root
223# 	./bin			base-sys-root
224# 	./bin/[			base-util-root
225# 	./bin/cat		base-util-root
226#		[...]
227#
228# A # in the first column marks a comment.
229#
230# If ${obsolete} != 0, only entries with an "obsolete" keyword will
231# be printed.  All other keywords must be present.
232#
233# The third field is an optional comma separated list of keywords to
234# control if a record is printed; every keyword listed must be enabled
235# for the record to be printed.  The following keywords are available:
236#	dummy			dummy entry (ignored)
237#	obsolete		file is obsolete, and only printed if 
238#				${obsolete} != 0
239#
240#	bfd			${MKBFD} != no
241#	catpages		${MKCATPAGES} != no
242#	crypto			${MKCRYPTO} != no
243#	crypto_idea		${MKCRYPTO_IDEA} != no
244#	crypto_mdc2		${MKCRYPTO_MDC2} != no
245#	crypto_rc5		${MKCRYPTO_RC5} != no
246#	cvs			${MKCVS} != no
247#	doc			${MKDOC} != no
248#	gcc			${MKGCC} != no
249#	gcccmds			${MKGCCCMDS} != no
250#	gdb			${MKGDB} != no
251#	hesiod			${MKHESIOD} != no
252#	info			${MKINFO} != no
253#	ipfilter		${MKIPFILTER} != no
254#	inet6			${MKINET6} != no
255#	iscsi			${MKISCSI} != no
256#	kerberos		${MKKERBEROS} != no
257#	lint			${MKLINT} != no
258#	man			${MKMAN} != no
259#	manz			${MKMANZ} != no
260#	nls			${MKNLS} != no
261#	pam			${MKPAM} != no
262#	pf			${MKPF} != no
263#	postfix			${MKPOSTFIX} != no
264#	profile			${MKPROFILE} != no
265#	sendmail		${MKSENDMAIL} != no
266#	share			${MKSHARE} != no
267#	skey			${MKSKEY} != no
268#	uucp			${MKUUCP} != no
269#	yp			${MKYP} != no
270#
271#	gcc=<n>			<n> = value of ${HAVE_GCC}
272#	gdb=<n>			<n> = value of ${HAVE_GDB}
273#
274#	use_inet6		${USE_INET6} != no
275#	use_kerberos		${USE_KERBEROS} != no
276#	use_yp			${USE_YP} != no
277#
278#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
279#				  automatically append ".gz" to the filename
280#
281#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
282#				  automatically append ".gz" to the filename
283#
284list_set_files()
285{
286	for setname; do
287		list_set_lists $setname
288	done | xargs cat | \
289	${AWK} -v obsolete=${obsolete} '
290		BEGIN {
291			if (obsolete)
292				wanted["obsolete"] = 1
293		
294			split("'"${MKVARS}"'", needvars)
295			for (vi in needvars) {
296				nv = needvars[vi]
297				kw = tolower(nv)
298				sub(/^mk/, "", kw)
299				if (ENVIRON[nv] != "no")
300					wanted[kw] = 1 
301			}
302
303			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
304				wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
305				wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
306			}
307			if (("man" in wanted) && ("catpages" in wanted))
308				wanted[".cat"] = 1
309			if (("man" in wanted) && ("manpages" in wanted))
310				wanted[".man"] = 1
311		}
312
313		/^#/ {
314			next;
315		}
316
317		NF > 2 && $3 != "-" {
318			split($3, keywords, ",")
319			show = 1
320			haveobs = 0
321			for (ki in keywords) {
322				kw = keywords[ki]
323				if (("manz" in wanted) &&
324				    (kw == ".cat" || kw == ".man"))
325					$1 = $1 ".gz"
326				if (! (kw in wanted))
327					show = 0
328				if (kw == "obsolete")
329					haveobs = 1
330			}
331			if (obsolete && ! haveobs)
332				next
333			if (show)
334				print
335			next
336		}
337
338		{
339			if (! obsolete)
340				print
341		}'
342
343}
344
345#
346# list_set_lists setname
347# 
348# Print to stdout a list of files, one filename per line, which
349# concatenate to create the packing list for setname. E.g.,
350#
351# 	.../lists/base/mi
352# 	.../lists/base/rescue.mi
353# 	.../lists/base/md.i386
354#		[...]
355#
356# For a given setname $set, the following files may be selected from
357# .../list/$set:
358#	mi
359#	ad.${MACHINE_ARCH}
360# (or)	ad.${MACHINE_CPU}
361#	ad.${MACHINE_CPU}.shl
362#	md.${MACHINE}.${MACHINE_ARCH}
363# (or)	md.${MACHINE}
364#	stl.mi
365#	stl.stlib
366#	shl.mi
367#	shl.shlib
368#	lkm.mi			if ${lkm} != no
369#	rescue.shl
370#	rescue.${MACHINE}
371#	rescue.ad.${MACHINE_ARCH}
372# (or)	rescue.ad.${MACHINE_CPU}
373# 	rescue.ad.${MACHINE_CPU}.shl
374#
375# Environment:
376# 	shlib
377# 	stlib
378#
379list_set_lists()
380{
381	setname=$1
382
383	setdir=$setsdir/lists/$setname
384	echo $setdir/mi
385	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
386		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
387		# since the arch-specific one will be more specific than
388		# the cpu-specific one.
389		if [ -f $setdir/ad.${MACHINE_ARCH} ]; then
390			echo $setdir/ad.${MACHINE_ARCH}
391		elif [ -f $setdir/ad.${MACHINE_CPU} ]; then
392			echo $setdir/ad.${MACHINE_CPU}
393		fi
394		if [ "$shlib" != "no" -a \
395		     -f $setdir/ad.${MACHINE_CPU}.shl ]; then
396			echo $setdir/ad.${MACHINE_CPU}.shl
397		fi
398	fi
399	if [ -f $setdir/md.${MACHINE}.${MACHINE_ARCH} ]; then
400		echo $setdir/md.${MACHINE}.${MACHINE_ARCH}
401	elif [ -f $setdir/md.${MACHINE} ]; then
402		echo $setdir/md.${MACHINE}
403	fi
404	if [ -f $setdir/stl.mi ]; then
405		echo $setdir/stl.mi
406	fi
407	if [ -f $setdir/stl.${stlib} ]; then
408		echo $setdir/stl.${stlib}
409	fi
410	if [ "$shlib" != "no" ]; then
411		if [ -f $setdir/shl.mi ]; then
412			echo $setdir/shl.mi
413		fi
414		if [ -f $setdir/shl.${shlib} ]; then
415			echo $setdir/shl.${shlib}
416		fi
417	fi
418	if [ "$lkm" != "no" ]; then
419		if [ -f $setdir/lkm.mi ]; then
420			echo $setdir/lkm.mi
421		fi
422	fi
423
424	if [ -f $setdir/rescue.mi ]; then
425		echo $setdir/rescue.mi
426	fi
427	if [ -f $setdir/rescue.${MACHINE} ]; then
428		echo $setdir/rescue.${MACHINE}
429	fi
430	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
431		# Prefer a rescue.ad.${MACHINE_ARCH} over a
432		# rescue.ad.${MACHINE_CPU}, since the arch-
433		# specific one will be more specific than the
434		# cpu-specific one.
435		if [ -f $setdir/rescue.ad.${MACHINE_ARCH} ]; then
436			echo $setdir/rescue.ad.${MACHINE_ARCH}
437		elif [ -f $setdir/rescue.ad.${MACHINE_CPU} ]; then
438			echo $setdir/rescue.ad.${MACHINE_CPU}
439		fi
440		if [ "$shlib" != "no" -a \
441		     -f $setdir/rescue.ad.${MACHINE_CPU}.shl ]; then
442			echo $setdir/rescue.ad.${MACHINE_CPU}.shl
443		fi
444	fi
445}
446
447# arch_to_cpu mach
448#
449# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
450# as determined by <bsd.own.mk>.
451#
452arch_to_cpu()
453{
454	MACHINE_ARCH=${1} ${MAKE} -f- all <<EOMAKE
455.include <bsd.own.mk>
456all:
457	@echo \${MACHINE_CPU}
458EOMAKE
459}
460
461# arch_to_endian mach
462#
463# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
464# as determined by <bsd.endian.mk>.
465#
466arch_to_endian()
467{
468	MACHINE_ARCH=${1} ${MAKE} -f- all <<EOMAKE
469.include <bsd.endian.mk>
470all:
471	@echo \${TARGET_ENDIANNESS}
472EOMAKE
473}
474