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