sets.subr revision 1.123
1#	$NetBSD: sets.subr,v 1.123 2010/06/18 04:21:21 mrg 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#	extlists		list of extsrc sets
10#	obsolete		controls if obsolete files are selected instead
11#	module			if != "no", enable MODULE sets
12#	shlib			shared library format (a.out, elf, or "")
13#	stlib			static library format (a.out, elf)
14#
15# The following <bsd.own.mk> variables are exported to the environment:
16#	MACHINE	
17#	MACHINE_ARCH
18#	MACHINE_CPU
19#	HAVE_BINUTILS
20#	HAVE_GCC
21#	HAVE_GDB
22#	TOOLCHAIN_MISSING
23#	OBJECT_FMT
24# as well as:
25#
26
27#
28# The following variables refer to tools that are used when building sets:
29#
30: ${AWK:=awk}
31: ${CKSUM:=cksum}
32: ${COMM:=comm}
33: ${DATE:=date}
34: ${DB:=db}
35: ${EGREP:=egrep}
36: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
37: ${FGREP:=fgrep}
38: ${FIND:=find}
39: ${GREP:=grep}
40: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
41: ${HOSTNAME_CMD:=hostname}	# ${HOSTNAME} is special to bash(1)
42: ${HOST_SH:=sh}
43: ${IDENT:=ident}
44: ${JOIN:=join}
45: ${LS:=ls}
46: ${MAKE:=make}
47: ${MKTEMP:=mktemp}
48: ${MTREE:=mtree}
49: ${PASTE:=paste}
50: ${PAX:=pax}
51: ${PRINTF:=printf}
52: ${SED:=sed}
53: ${SORT:=sort}
54: ${STAT:=stat}
55: ${TSORT:=tsort}
56: ${UNAME:=uname}
57: ${WC:=wc}
58: ${XARGS:=xargs}
59
60#
61# If printf is a shell builtin command, then we can
62# implement cheaper versions of basename and dirname
63# that do not involve any fork/exec overhead.
64# If printf is not builtin, approximate it using echo,
65# and hope there are no weird file names that cause
66# some versions of echo to do the wrong thing.
67# (Converting to this version of dirname speeded up the
68# syspkgdeps script by an order of magnitude, from 68
69# seconds to 6.3 seconds on one particular host.)
70#
71# Note that naive approximations for dirname
72# using ${foo%/*} do not do the right thing in cases
73# where the result should be "/" or ".".
74#
75case "$(type printf)" in
76*builtin*)
77	basename ()
78	{
79		local bn
80		bn="${1##*/}"
81		bn="${bn%$2}"
82		printf "%s\n" "$bn"
83	}
84	dirname ()
85	{
86		local dn
87		case "$1" in
88		?*/*)	dn="${1%/*}" ;;
89		/*)	dn=/ ;;
90		*)	dn=. ;;
91		esac
92		printf "%s\n" "$dn"
93	}
94	;;
95*)
96	basename ()
97	{
98		local bn
99		bn="${1##*/}"
100		bn="${bn%$2}"
101		echo "$bn"
102	}
103	dirname ()
104	{
105		local dn
106		case "$1" in
107		?*/*)	dn="${1%/*}" ;;
108		/*)	dn=/ ;;
109		*)	dn=. ;;
110		esac
111		echo "$dn"
112	}
113	;;
114esac
115
116#####
117
118oIFS=$IFS
119IFS="
120"
121
122for x in $( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars ); do
123	eval export $x
124done
125
126IFS=$oIFS
127
128MKVARS="$( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars | ${SED} -e 's,=.*,,' | ${XARGS} )"
129
130#####
131
132setsdir=${rundir}
133obsolete=0
134module=yes
135if [ "${MKKMOD}" = "no" ]; then
136	module=no			# MODULEs are off.
137	kmod=no
138fi
139# Determine lib type. Do this first so stlib also gets set.
140if [ "${OBJECT_FMT}" = "ELF" ]; then
141	shlib=elf
142else
143	shlib=aout
144fi
145stlib=$shlib
146# Now check for MKPIC or specials and turn off shlib if need be.
147if [ "${MKPIC}" = "no" ]; then
148	shlib=no
149fi
150if [ "$module" != "no" ]; then
151	nlists="base comp etc games man misc modules tests text"
152else
153	nlists="base comp etc games man misc tests text"
154fi
155xlists="xbase xcomp xetc xfont xserver"
156extlists="extbase extcomp extetc"
157
158OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
159MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
160SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
161SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
162SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
163
164#
165# list_set_files setfile [...]
166# 
167# Produce a packing list for setfile(s).
168# In each file, a record consists of a path and a System Package name,
169# separated by whitespace. E.g.,
170#
171# 	# $NetBSD: sets.subr,v 1.123 2010/06/18 04:21:21 mrg Exp $
172# 	.			base-sys-root	[keyword[,...]]
173# 	./altroot		base-sys-root
174# 	./bin			base-sys-root
175# 	./bin/[			base-util-root
176# 	./bin/cat		base-util-root
177#		[...]
178#
179# A # in the first column marks a comment.
180#
181# If ${obsolete} != 0, only entries with an "obsolete" keyword will
182# be printed.  All other keywords must be present.
183#
184# The third field is an optional comma separated list of keywords to
185# control if a record is printed; every keyword listed must be enabled
186# for the record to be printed.  The following keywords are available:
187#	dummy			dummy entry (ignored)
188#	obsolete		file is obsolete, and only printed if 
189#				${obsolete} != 0
190#
191#	bfd			obsolete, use binutils.
192#	binutils		${MKBINUTILS} != no
193#	catpages		${MKCATPAGES} != no
194#	compat			${MKCOMPAT} != no
195#	crypto			${MKCRYPTO} != no
196#	crypto_idea		${MKCRYPTO_IDEA} != no
197#	crypto_mdc2		${MKCRYPTO_MDC2} != no
198#	crypto_rc5		${MKCRYPTO_RC5} != no
199#	cvs			${MKCVS} != no
200#	debug			${MKDEBUG} != no
201#	debuglib		${MKDEBUGLIB} != no
202#	doc			${MKDOC} != no
203#	dtrace			${MKDTRACE} != no
204#	dynamicroot		${MKDYNAMICROOT} != no
205#	extsrc			${MKEXTSRC} != no
206#	gcc			${MKGCC} != no
207#	gcccmds			${MKGCCCMDS} != no
208#	gdb			${MKGDB} != no
209#	hesiod			${MKHESIOD} != no
210#	html			${MKHTML} != no
211#	inet6			${MKINET6} != no
212#	info			${MKINFO} != no
213#	ipfilter		${MKIPFILTER} != no
214#	iscsi			${MKISCSI} != no
215#	kerberos		${MKKERBEROS} != no
216#	kmod			${MKKMOD} != no
217#	ldap			${MKLDAP} != no
218#	lint			${MKLINT} != no
219#	lvm			${MKLVM} != no
220#	man			${MKMAN} != no
221#	manpages		${MKMANPAGES} != no
222#	manz			${MKMANZ} != no
223#	mdns			${MKMDNS} != no
224#	nls			${MKNLS} != no
225#	nvi			${MKNVI} != no
226#	pam			${MKPAM} != no
227#	pcc			${MKPCC} != no
228#	pcccmds			${MKPCCCMDS} != no
229#	pf			${MKPF} != no
230#	pic			${MKPIC} != no
231#	pigz			${MKPIGZ} != no
232#	postfix			${MKPOSTFIX} != no
233#	profile			${MKPROFILE} != no
234#	share			${MKSHARE} != no
235#	skey			${MKSKEY} != no
236#	solaris			${MKDTRACE} != no or ${MKZFS} != no
237#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
238#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
239#	yp			${MKYP} != no
240#	zfs			${MKZFS} != no
241#
242#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
243#	gcc=<n>			<n> = value of ${HAVE_GCC}
244#	gdb=<n>			<n> = value of ${HAVE_GDB}
245#
246#	use_inet6		${USE_INET6} != no
247#	use_kerberos		${USE_KERBEROS} != no
248#	use_yp			${USE_YP} != no
249#
250#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
251#				  automatically append ".gz" to the filename
252#
253#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
254#				  automatically append ".gz" to the filename
255#
256list_set_files()
257{
258	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
259		verbose=false
260	else
261		verbose=true
262	fi
263	print_set_lists "$@" | \
264	${AWK} -v obsolete=${obsolete} '
265		BEGIN {
266			if (obsolete)
267				wanted["obsolete"] = 1
268		
269			split("'"${MKVARS}"'", needvars)
270			for (vi in needvars) {
271				nv = needvars[vi]
272				kw = tolower(nv)
273				sub(/^mk/, "", kw)
274				if (ENVIRON[nv] != "no")
275					wanted[kw] = 1 
276			}
277
278			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
279				if ("binutils" in wanted)
280					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
281				if ("gcc" in wanted)
282					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
283				if ("gdb" in wanted)
284					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
285			}
286			if (("man" in wanted) && ("catpages" in wanted))
287				wanted[".cat"] = 1
288			if (("man" in wanted) && ("manpages" in wanted))
289				wanted[".man"] = 1
290		}
291
292		/^#/ {
293			next;
294		}
295
296		NF > 2 && $3 != "-" {
297			split($3, keywords, ",")
298			show = 1
299			haveobs = 0
300			for (ki in keywords) {
301				kw = keywords[ki]
302				if (("manz" in wanted) &&
303				    (kw == ".cat" || kw == ".man"))
304					$1 = $1 ".gz"
305				negated = match(kw, "! *")
306				if (negated > 0) {
307					kw = substr(kw, RSTART + RLENGTH)
308					if (kw in wanted)
309						show = 0
310				} else {
311					if (! (kw in wanted))
312						show = 0
313				}
314				if (kw == "obsolete")
315					haveobs = 1
316			}
317			if (obsolete && ! haveobs)
318				next
319			if (show)
320				print
321			next
322		}
323
324		{
325			if (! obsolete)
326				print
327		}'
328
329}
330
331#
332# list_set_lists setname
333# 
334# Print to stdout a list of files, one filename per line, which
335# concatenate to create the packing list for setname. E.g.,
336#
337# 	.../lists/base/mi
338# 	.../lists/base/rescue.mi
339# 	.../lists/base/md.i386
340#		[...]
341#
342# For a given setname $set, the following files may be selected from
343# .../list/$set:
344#	mi
345#	mi.ext.*
346#	ad.${MACHINE_ARCH}
347# (or)	ad.${MACHINE_CPU}
348#	ad.${MACHINE_CPU}.shl
349#	md.${MACHINE}.${MACHINE_ARCH}
350# (or)	md.${MACHINE}
351#	stl.mi
352#	stl.${stlib}
353#	shl.mi
354#	shl.mi.ext.*
355#	shl.${shlib}
356#	shl.${shlib}.ext.*
357#	module.mi			if ${module} != no
358#	module.${MACHINE}		if ${module} != no
359#	module.ad.${MACHINE_ARCH}	if ${module} != no
360# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
361#	rescue.shl
362#	rescue.${MACHINE}
363#	rescue.ad.${MACHINE_ARCH}
364# (or)	rescue.ad.${MACHINE_CPU}
365# 	rescue.ad.${MACHINE_CPU}.shl
366#
367# Environment:
368# 	shlib
369# 	stlib
370#
371list_set_lists()
372{
373	setname=$1
374
375	list_set_lists_mi $setname
376	list_set_lists_ad $setname
377	list_set_lists_md $setname
378	list_set_lists_stl $setname
379	list_set_lists_shl $setname
380	list_set_lists_module $setname
381	list_set_lists_rescue $setname
382	return 0
383}
384
385list_set_lists_mi()
386{
387	setdir=$setsdir/lists/$1
388	# always exist!
389	echo $setdir/mi
390}
391
392list_set_lists_ad()
393{
394	setdir=$setsdir/lists/$1
395	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
396	list_set_lists_common_ad $1
397}
398
399list_set_lists_md()
400{
401	setdir=$setsdir/lists/$1
402	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
403	echo_if_exist $setdir/md.${MACHINE}
404}
405
406list_set_lists_stl()
407{
408	setdir=$setsdir/lists/$1
409	echo_if_exist $setdir/stl.mi
410	echo_if_exist $setdir/stl.${stlib}
411}
412
413list_set_lists_shl()
414{
415	setdir=$setsdir/lists/$1
416	[ "$shlib" != "no" ] || return
417	echo_if_exist $setdir/shl.mi
418	echo_if_exist $setdir/shl.${shlib}
419}
420
421list_set_lists_module()
422{
423	setdir=$setsdir/lists/$1
424	[ "$module" != "no" ] || return
425	echo_if_exist $setdir/module.mi
426	echo_if_exist $setdir/module.${MACHINE}
427	# XXX module never has .shl
428	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
429	list_set_lists_common_ad $1 module
430}
431
432list_set_lists_rescue()
433{
434	setdir=$setsdir/lists/$1
435	echo_if_exist $setdir/rescue.mi
436	echo_if_exist $setdir/rescue.${MACHINE}
437	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
438	list_set_lists_common_ad $1 rescue
439}
440
441list_set_lists_common_ad()
442{
443	setdir=$setsdir/lists/$1; _prefix=$2
444
445	[ -n "$_prefix" ] && prefix="$_prefix".
446
447	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
448	# <prefix>.ad.${MACHINE_CPU}, since the arch-
449	# specific one will be more specific than the
450	# cpu-specific one.
451	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
452	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
453	[ "$shlib" != "no" ] && \
454	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
455}
456
457echo_if_exist()
458{
459	[ -f $1 ] && echo $1
460	return $?
461}
462
463echo_if_exist_foreach()
464{
465	local _list=$1; shift
466	for _suffix in $@; do
467		echo_if_exist ${_list}.${_suffix}
468	done
469}
470
471print_set_lists()
472{
473	for setname; do
474		list=`list_set_lists $setname`
475		for l in $list; do
476			echo $l
477			if $verbose; then
478				echo >&2 "DEBUG: list_set_files: $l"
479			fi
480		done
481	done | ${XARGS} ${SED} ${SUBST}
482}
483
484# arch_to_cpu mach
485#
486# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
487# as determined by <bsd.own.mk>.
488#
489arch_to_cpu()
490{
491	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
492.include <bsd.own.mk>
493all:
494	@echo \${MACHINE_CPU}
495EOMAKE
496}
497
498# arch_to_endian mach
499#
500# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
501# as determined by <bsd.endian.mk>.
502#
503arch_to_endian()
504{
505	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
506.include <bsd.endian.mk>
507all:
508	@echo \${TARGET_ENDIANNESS}
509EOMAKE
510}
511
512#####
513
514# print_mkvars
515print_mkvars()
516{
517	for v in $MKVARS; do
518		eval echo $v=\$$v
519	done
520}
521
522# print_set_lists_{base,x,ext}
523# list_set_lists_{base,x,ext}
524# list_set_files_{base,x,ext}
525for func in print_set_lists list_set_lists list_set_files; do
526	for x in base x ext; do
527		if [ $x = base ]; then
528			list=nlists
529		else
530			list=${x}lists
531		fi
532		eval ${func}_${x} \(\) \{ $func \$$list \; \}
533	done
534done
535