sets.subr revision 1.125
1#	$NetBSD: sets.subr,v 1.125 2010/12/08 23:56:01 njoly 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
134if [ "${MKKMOD}" = "no" ]; then
135	module=no			# MODULEs are off.
136	modset=""
137else
138	module=yes
139	modset="modules"
140fi
141if [ "${MKATF}" = "no" ]; then
142	testset=""
143else
144	testset="tests"
145fi
146# Determine lib type. Do this first so stlib also gets set.
147if [ "${OBJECT_FMT}" = "ELF" ]; then
148	shlib=elf
149else
150	shlib=aout
151fi
152stlib=$shlib
153# Now check for MKPIC or specials and turn off shlib if need be.
154if [ "${MKPIC}" = "no" ]; then
155	shlib=no
156fi
157nlists="base comp etc games man misc $modset $testset text"
158xlists="xbase xcomp xetc xfont xserver"
159extlists="extbase extcomp extetc"
160
161OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
162MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
163SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
164SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
165SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
166
167#
168# list_set_files setfile [...]
169# 
170# Produce a packing list for setfile(s).
171# In each file, a record consists of a path and a System Package name,
172# separated by whitespace. E.g.,
173#
174# 	# $NetBSD: sets.subr,v 1.125 2010/12/08 23:56:01 njoly Exp $
175# 	.			base-sys-root	[keyword[,...]]
176# 	./altroot		base-sys-root
177# 	./bin			base-sys-root
178# 	./bin/[			base-util-root
179# 	./bin/cat		base-util-root
180#		[...]
181#
182# A # in the first column marks a comment.
183#
184# If ${obsolete} != 0, only entries with an "obsolete" keyword will
185# be printed.  All other keywords must be present.
186#
187# The third field is an optional comma separated list of keywords to
188# control if a record is printed; every keyword listed must be enabled
189# for the record to be printed.  The following keywords are available:
190#	dummy			dummy entry (ignored)
191#	obsolete		file is obsolete, and only printed if 
192#				${obsolete} != 0
193#
194#	atf			${MKATF} != no
195#	bfd			obsolete, use binutils.
196#	binutils		${MKBINUTILS} != no
197#	catpages		${MKCATPAGES} != no
198#	compat			${MKCOMPAT} != no
199#	crypto			${MKCRYPTO} != no
200#	crypto_idea		${MKCRYPTO_IDEA} != no
201#	crypto_mdc2		${MKCRYPTO_MDC2} != no
202#	crypto_rc5		${MKCRYPTO_RC5} != no
203#	cvs			${MKCVS} != no
204#	debug			${MKDEBUG} != no
205#	debuglib		${MKDEBUGLIB} != no
206#	doc			${MKDOC} != no
207#	dtrace			${MKDTRACE} != no
208#	dynamicroot		${MKDYNAMICROOT} != no
209#	extsrc			${MKEXTSRC} != no
210#	gcc			${MKGCC} != no
211#	gcccmds			${MKGCCCMDS} != no
212#	gdb			${MKGDB} != no
213#	hesiod			${MKHESIOD} != no
214#	html			${MKHTML} != no
215#	inet6			${MKINET6} != no
216#	info			${MKINFO} != no
217#	ipfilter		${MKIPFILTER} != no
218#	iscsi			${MKISCSI} != no
219#	kerberos		${MKKERBEROS} != no
220#	kmod			${MKKMOD} != no
221#	ldap			${MKLDAP} != no
222#	lint			${MKLINT} != no
223#	lvm			${MKLVM} != no
224#	man			${MKMAN} != no
225#	manpages		${MKMANPAGES} != no
226#	manz			${MKMANZ} != no
227#	mdns			${MKMDNS} != no
228#	nls			${MKNLS} != no
229#	nvi			${MKNVI} != no
230#	pam			${MKPAM} != no
231#	pcc			${MKPCC} != no
232#	pcccmds			${MKPCCCMDS} != no
233#	pf			${MKPF} != no
234#	pic			${MKPIC} != no
235#	postfix			${MKPOSTFIX} != no
236#	profile			${MKPROFILE} != no
237#	share			${MKSHARE} != no
238#	skey			${MKSKEY} != no
239#	solaris			${MKDTRACE} != no or ${MKZFS} != no
240#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
241#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
242#	yp			${MKYP} != no
243#	zfs			${MKZFS} != no
244#
245#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
246#	gcc=<n>			<n> = value of ${HAVE_GCC}
247#	gdb=<n>			<n> = value of ${HAVE_GDB}
248#
249#	use_inet6		${USE_INET6} != no
250#	use_kerberos		${USE_KERBEROS} != no
251#	use_yp			${USE_YP} != no
252#
253#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
254#				  automatically append ".gz" to the filename
255#
256#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
257#				  automatically append ".gz" to the filename
258#
259list_set_files()
260{
261	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
262		verbose=false
263	else
264		verbose=true
265	fi
266	print_set_lists "$@" | \
267	${AWK} -v obsolete=${obsolete} '
268		BEGIN {
269			if (obsolete)
270				wanted["obsolete"] = 1
271		
272			split("'"${MKVARS}"'", needvars)
273			for (vi in needvars) {
274				nv = needvars[vi]
275				kw = tolower(nv)
276				sub(/^mk/, "", kw)
277				if (ENVIRON[nv] != "no")
278					wanted[kw] = 1 
279			}
280
281			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
282				if ("binutils" in wanted)
283					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
284				if ("gcc" in wanted)
285					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
286				if ("gdb" in wanted)
287					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
288			}
289			if (("man" in wanted) && ("catpages" in wanted))
290				wanted[".cat"] = 1
291			if (("man" in wanted) && ("manpages" in wanted))
292				wanted[".man"] = 1
293		}
294
295		/^#/ {
296			next;
297		}
298
299		NF > 2 && $3 != "-" {
300			split($3, keywords, ",")
301			show = 1
302			haveobs = 0
303			for (ki in keywords) {
304				kw = keywords[ki]
305				if (("manz" in wanted) &&
306				    (kw == ".cat" || kw == ".man"))
307					$1 = $1 ".gz"
308				negated = match(kw, "! *")
309				if (negated > 0) {
310					kw = substr(kw, RSTART + RLENGTH)
311					if (kw in wanted)
312						show = 0
313				} else {
314					if (! (kw in wanted))
315						show = 0
316				}
317				if (kw == "obsolete")
318					haveobs = 1
319			}
320			if (obsolete && ! haveobs)
321				next
322			if (show)
323				print
324			next
325		}
326
327		{
328			if (! obsolete)
329				print
330		}'
331
332}
333
334#
335# list_set_lists setname
336# 
337# Print to stdout a list of files, one filename per line, which
338# concatenate to create the packing list for setname. E.g.,
339#
340# 	.../lists/base/mi
341# 	.../lists/base/rescue.mi
342# 	.../lists/base/md.i386
343#		[...]
344#
345# For a given setname $set, the following files may be selected from
346# .../list/$set:
347#	mi
348#	mi.ext.*
349#	ad.${MACHINE_ARCH}
350# (or)	ad.${MACHINE_CPU}
351#	ad.${MACHINE_CPU}.shl
352#	md.${MACHINE}.${MACHINE_ARCH}
353# (or)	md.${MACHINE}
354#	stl.mi
355#	stl.${stlib}
356#	shl.mi
357#	shl.mi.ext.*
358#	shl.${shlib}
359#	shl.${shlib}.ext.*
360#	module.mi			if ${module} != no
361#	module.${MACHINE}		if ${module} != no
362#	module.ad.${MACHINE_ARCH}	if ${module} != no
363# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
364#	rescue.shl
365#	rescue.${MACHINE}
366#	rescue.ad.${MACHINE_ARCH}
367# (or)	rescue.ad.${MACHINE_CPU}
368# 	rescue.ad.${MACHINE_CPU}.shl
369#
370# Environment:
371# 	shlib
372# 	stlib
373#
374list_set_lists()
375{
376	setname=$1
377
378	list_set_lists_mi $setname
379	list_set_lists_ad $setname
380	list_set_lists_md $setname
381	list_set_lists_stl $setname
382	list_set_lists_shl $setname
383	list_set_lists_module $setname
384	list_set_lists_rescue $setname
385	return 0
386}
387
388list_set_lists_mi()
389{
390	setdir=$setsdir/lists/$1
391	# always exist!
392	echo $setdir/mi
393}
394
395list_set_lists_ad()
396{
397	setdir=$setsdir/lists/$1
398	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
399	list_set_lists_common_ad $1
400}
401
402list_set_lists_md()
403{
404	setdir=$setsdir/lists/$1
405	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
406	echo_if_exist $setdir/md.${MACHINE}
407}
408
409list_set_lists_stl()
410{
411	setdir=$setsdir/lists/$1
412	echo_if_exist $setdir/stl.mi
413	echo_if_exist $setdir/stl.${stlib}
414}
415
416list_set_lists_shl()
417{
418	setdir=$setsdir/lists/$1
419	[ "$shlib" != "no" ] || return
420	echo_if_exist $setdir/shl.mi
421	echo_if_exist $setdir/shl.${shlib}
422}
423
424list_set_lists_module()
425{
426	setdir=$setsdir/lists/$1
427	[ "$module" != "no" ] || return
428	echo_if_exist $setdir/module.mi
429	echo_if_exist $setdir/module.${MACHINE}
430	# XXX module never has .shl
431	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
432	list_set_lists_common_ad $1 module
433}
434
435list_set_lists_rescue()
436{
437	setdir=$setsdir/lists/$1
438	echo_if_exist $setdir/rescue.mi
439	echo_if_exist $setdir/rescue.${MACHINE}
440	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
441	list_set_lists_common_ad $1 rescue
442}
443
444list_set_lists_common_ad()
445{
446	setdir=$setsdir/lists/$1; _prefix=$2
447
448	[ -n "$_prefix" ] && prefix="$_prefix".
449
450	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
451	# <prefix>.ad.${MACHINE_CPU}, since the arch-
452	# specific one will be more specific than the
453	# cpu-specific one.
454	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
455	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
456	[ "$shlib" != "no" ] && \
457	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
458}
459
460echo_if_exist()
461{
462	[ -f $1 ] && echo $1
463	return $?
464}
465
466echo_if_exist_foreach()
467{
468	local _list=$1; shift
469	for _suffix in $@; do
470		echo_if_exist ${_list}.${_suffix}
471	done
472}
473
474print_set_lists()
475{
476	for setname; do
477		list=`list_set_lists $setname`
478		for l in $list; do
479			echo $l
480			if $verbose; then
481				echo >&2 "DEBUG: list_set_files: $l"
482			fi
483		done
484	done | ${XARGS} ${SED} ${SUBST}
485}
486
487# arch_to_cpu mach
488#
489# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
490# as determined by <bsd.own.mk>.
491#
492arch_to_cpu()
493{
494	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
495.include <bsd.own.mk>
496all:
497	@echo \${MACHINE_CPU}
498EOMAKE
499}
500
501# arch_to_endian mach
502#
503# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
504# as determined by <bsd.endian.mk>.
505#
506arch_to_endian()
507{
508	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
509.include <bsd.endian.mk>
510all:
511	@echo \${TARGET_ENDIANNESS}
512EOMAKE
513}
514
515#####
516
517# print_mkvars
518print_mkvars()
519{
520	for v in $MKVARS; do
521		eval echo $v=\$$v
522	done
523}
524
525# print_set_lists_{base,x,ext}
526# list_set_lists_{base,x,ext}
527# list_set_files_{base,x,ext}
528for func in print_set_lists list_set_lists list_set_files; do
529	for x in base x ext; do
530		if [ $x = base ]; then
531			list=nlists
532		else
533			list=${x}lists
534		fi
535		eval ${func}_${x} \(\) \{ $func \$$list \; \}
536	done
537done
538