sets.subr revision 1.124
1#	$NetBSD: sets.subr,v 1.124 2010/06/19 03:50:30 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.124 2010/06/19 03:50:30 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#	postfix			${MKPOSTFIX} != no
232#	profile			${MKPROFILE} != no
233#	share			${MKSHARE} != no
234#	skey			${MKSKEY} != no
235#	solaris			${MKDTRACE} != no or ${MKZFS} != no
236#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
237#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
238#	yp			${MKYP} != no
239#	zfs			${MKZFS} != no
240#
241#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
242#	gcc=<n>			<n> = value of ${HAVE_GCC}
243#	gdb=<n>			<n> = value of ${HAVE_GDB}
244#
245#	use_inet6		${USE_INET6} != no
246#	use_kerberos		${USE_KERBEROS} != no
247#	use_yp			${USE_YP} != no
248#
249#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
250#				  automatically append ".gz" to the filename
251#
252#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
253#				  automatically append ".gz" to the filename
254#
255list_set_files()
256{
257	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
258		verbose=false
259	else
260		verbose=true
261	fi
262	print_set_lists "$@" | \
263	${AWK} -v obsolete=${obsolete} '
264		BEGIN {
265			if (obsolete)
266				wanted["obsolete"] = 1
267		
268			split("'"${MKVARS}"'", needvars)
269			for (vi in needvars) {
270				nv = needvars[vi]
271				kw = tolower(nv)
272				sub(/^mk/, "", kw)
273				if (ENVIRON[nv] != "no")
274					wanted[kw] = 1 
275			}
276
277			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
278				if ("binutils" in wanted)
279					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
280				if ("gcc" in wanted)
281					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
282				if ("gdb" in wanted)
283					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
284			}
285			if (("man" in wanted) && ("catpages" in wanted))
286				wanted[".cat"] = 1
287			if (("man" in wanted) && ("manpages" in wanted))
288				wanted[".man"] = 1
289		}
290
291		/^#/ {
292			next;
293		}
294
295		NF > 2 && $3 != "-" {
296			split($3, keywords, ",")
297			show = 1
298			haveobs = 0
299			for (ki in keywords) {
300				kw = keywords[ki]
301				if (("manz" in wanted) &&
302				    (kw == ".cat" || kw == ".man"))
303					$1 = $1 ".gz"
304				negated = match(kw, "! *")
305				if (negated > 0) {
306					kw = substr(kw, RSTART + RLENGTH)
307					if (kw in wanted)
308						show = 0
309				} else {
310					if (! (kw in wanted))
311						show = 0
312				}
313				if (kw == "obsolete")
314					haveobs = 1
315			}
316			if (obsolete && ! haveobs)
317				next
318			if (show)
319				print
320			next
321		}
322
323		{
324			if (! obsolete)
325				print
326		}'
327
328}
329
330#
331# list_set_lists setname
332# 
333# Print to stdout a list of files, one filename per line, which
334# concatenate to create the packing list for setname. E.g.,
335#
336# 	.../lists/base/mi
337# 	.../lists/base/rescue.mi
338# 	.../lists/base/md.i386
339#		[...]
340#
341# For a given setname $set, the following files may be selected from
342# .../list/$set:
343#	mi
344#	mi.ext.*
345#	ad.${MACHINE_ARCH}
346# (or)	ad.${MACHINE_CPU}
347#	ad.${MACHINE_CPU}.shl
348#	md.${MACHINE}.${MACHINE_ARCH}
349# (or)	md.${MACHINE}
350#	stl.mi
351#	stl.${stlib}
352#	shl.mi
353#	shl.mi.ext.*
354#	shl.${shlib}
355#	shl.${shlib}.ext.*
356#	module.mi			if ${module} != no
357#	module.${MACHINE}		if ${module} != no
358#	module.ad.${MACHINE_ARCH}	if ${module} != no
359# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
360#	rescue.shl
361#	rescue.${MACHINE}
362#	rescue.ad.${MACHINE_ARCH}
363# (or)	rescue.ad.${MACHINE_CPU}
364# 	rescue.ad.${MACHINE_CPU}.shl
365#
366# Environment:
367# 	shlib
368# 	stlib
369#
370list_set_lists()
371{
372	setname=$1
373
374	list_set_lists_mi $setname
375	list_set_lists_ad $setname
376	list_set_lists_md $setname
377	list_set_lists_stl $setname
378	list_set_lists_shl $setname
379	list_set_lists_module $setname
380	list_set_lists_rescue $setname
381	return 0
382}
383
384list_set_lists_mi()
385{
386	setdir=$setsdir/lists/$1
387	# always exist!
388	echo $setdir/mi
389}
390
391list_set_lists_ad()
392{
393	setdir=$setsdir/lists/$1
394	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
395	list_set_lists_common_ad $1
396}
397
398list_set_lists_md()
399{
400	setdir=$setsdir/lists/$1
401	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
402	echo_if_exist $setdir/md.${MACHINE}
403}
404
405list_set_lists_stl()
406{
407	setdir=$setsdir/lists/$1
408	echo_if_exist $setdir/stl.mi
409	echo_if_exist $setdir/stl.${stlib}
410}
411
412list_set_lists_shl()
413{
414	setdir=$setsdir/lists/$1
415	[ "$shlib" != "no" ] || return
416	echo_if_exist $setdir/shl.mi
417	echo_if_exist $setdir/shl.${shlib}
418}
419
420list_set_lists_module()
421{
422	setdir=$setsdir/lists/$1
423	[ "$module" != "no" ] || return
424	echo_if_exist $setdir/module.mi
425	echo_if_exist $setdir/module.${MACHINE}
426	# XXX module never has .shl
427	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
428	list_set_lists_common_ad $1 module
429}
430
431list_set_lists_rescue()
432{
433	setdir=$setsdir/lists/$1
434	echo_if_exist $setdir/rescue.mi
435	echo_if_exist $setdir/rescue.${MACHINE}
436	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
437	list_set_lists_common_ad $1 rescue
438}
439
440list_set_lists_common_ad()
441{
442	setdir=$setsdir/lists/$1; _prefix=$2
443
444	[ -n "$_prefix" ] && prefix="$_prefix".
445
446	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
447	# <prefix>.ad.${MACHINE_CPU}, since the arch-
448	# specific one will be more specific than the
449	# cpu-specific one.
450	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
451	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
452	[ "$shlib" != "no" ] && \
453	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
454}
455
456echo_if_exist()
457{
458	[ -f $1 ] && echo $1
459	return $?
460}
461
462echo_if_exist_foreach()
463{
464	local _list=$1; shift
465	for _suffix in $@; do
466		echo_if_exist ${_list}.${_suffix}
467	done
468}
469
470print_set_lists()
471{
472	for setname; do
473		list=`list_set_lists $setname`
474		for l in $list; do
475			echo $l
476			if $verbose; then
477				echo >&2 "DEBUG: list_set_files: $l"
478			fi
479		done
480	done | ${XARGS} ${SED} ${SUBST}
481}
482
483# arch_to_cpu mach
484#
485# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
486# as determined by <bsd.own.mk>.
487#
488arch_to_cpu()
489{
490	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
491.include <bsd.own.mk>
492all:
493	@echo \${MACHINE_CPU}
494EOMAKE
495}
496
497# arch_to_endian mach
498#
499# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
500# as determined by <bsd.endian.mk>.
501#
502arch_to_endian()
503{
504	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
505.include <bsd.endian.mk>
506all:
507	@echo \${TARGET_ENDIANNESS}
508EOMAKE
509}
510
511#####
512
513# print_mkvars
514print_mkvars()
515{
516	for v in $MKVARS; do
517		eval echo $v=\$$v
518	done
519}
520
521# print_set_lists_{base,x,ext}
522# list_set_lists_{base,x,ext}
523# list_set_files_{base,x,ext}
524for func in print_set_lists list_set_lists list_set_files; do
525	for x in base x ext; do
526		if [ $x = base ]; then
527			list=nlists
528		else
529			list=${x}lists
530		fi
531		eval ${func}_${x} \(\) \{ $func \$$list \; \}
532	done
533done
534