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