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