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