sets.subr revision 1.137
1#	$NetBSD: sets.subr,v 1.137 2012/07/14 16:04:04 spz 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 -k)
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.137 2012/07/14 16:04:04 spz 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_rc5		${MKCRYPTO_RC5} != no
203#	cvs			${MKCVS} != no
204#	debug			${MKDEBUG} != no
205#	debuglib		${MKDEBUGLIB} != no
206#	doc			${MKDOC} != no
207#	dtrace			${MKDTRACE} != no
208#	dynamicroot		${MKDYNAMICROOT} != no
209#	extsrc			${MKEXTSRC} != no
210#	gcc			${MKGCC} != no
211#	gcccmds			${MKGCCCMDS} != no
212#	gdb			${MKGDB} != no
213#	hesiod			${MKHESIOD} != no
214#	html			${MKHTML} != no
215#	inet6			${MKINET6} != no
216#	info			${MKINFO} != no
217#	ipfilter		${MKIPFILTER} != no
218#	iscsi			${MKISCSI} != no
219#	kerberos		${MKKERBEROS} != no
220#	kmod			${MKKMOD} != no
221#	ldap			${MKLDAP} != no
222#	lint			${MKLINT} != no
223#	llvm			${MKLLVM} != no
224#	lvm			${MKLVM} != no
225#	makemandb		${MKMAKEMANDB} != 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#	pf			${MKPF} != no
235#	pic			${MKPIC} != no
236#	postfix			${MKPOSTFIX} != no
237#	profile			${MKPROFILE} != no
238#	perfuse			${MKPERFUSE} != 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				if (substr(kw, 1, 1) == "!") {
322					kw = substr(kw, 2)
323					if (kw in wanted)
324						show = 0
325				} else {
326					if (! (kw in wanted))
327						show = 0
328				}
329				if (kw == "obsolete")
330					haveobs = 1
331			}
332			if (obsolete && ! haveobs)
333				next
334			if (show)
335				list[$1] = $0
336			next
337		}
338
339		{
340			if (notwanted[$1] != "")
341				next;
342			if (! obsolete)
343				list[$1] = $0
344		}
345
346		END {
347			for (i in list) {
348				print list[i]
349			}
350		}'
351
352}
353
354#
355# list_set_lists setname
356# 
357# Print to stdout a list of files, one filename per line, which
358# concatenate to create the packing list for setname. E.g.,
359#
360# 	.../lists/base/mi
361# 	.../lists/base/rescue.mi
362# 	.../lists/base/md.i386
363#		[...]
364#
365# For a given setname $set, the following files may be selected from
366# .../list/$set:
367#	mi
368#	mi.ext.*
369#	ad.${MACHINE_ARCH}
370# (or)	ad.${MACHINE_CPU}
371#	ad.${MACHINE_CPU}.shl
372#	md.${MACHINE}.${MACHINE_ARCH}
373# (or)	md.${MACHINE}
374#	stl.mi
375#	stl.${stlib}
376#	shl.mi
377#	shl.mi.ext.*
378#	shl.${shlib}
379#	shl.${shlib}.ext.*
380#	module.mi			if ${module} != no
381#	module.${MACHINE}		if ${module} != no
382#	module.ad.${MACHINE_ARCH}	if ${module} != no
383# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
384#	rescue.shl
385#	rescue.${MACHINE}
386#	rescue.ad.${MACHINE_ARCH}
387# (or)	rescue.ad.${MACHINE_CPU}
388# 	rescue.ad.${MACHINE_CPU}.shl
389#
390# Environment:
391# 	shlib
392# 	stlib
393#
394list_set_lists()
395{
396	setname=$1
397
398	list_set_lists_mi $setname
399	list_set_lists_ad $setname
400	list_set_lists_md $setname
401	list_set_lists_stl $setname
402	list_set_lists_shl $setname
403	list_set_lists_module $setname
404	list_set_lists_rescue $setname
405	return 0
406}
407
408list_set_lists_mi()
409{
410	setdir=$setsdir/lists/$1
411	# always exist!
412	echo $setdir/mi
413}
414
415list_set_lists_ad()
416{
417	setdir=$setsdir/lists/$1
418	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
419	list_set_lists_common_ad $1
420}
421
422list_set_lists_md()
423{
424	setdir=$setsdir/lists/$1
425	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
426	echo_if_exist $setdir/md.${MACHINE}
427}
428
429list_set_lists_stl()
430{
431	setdir=$setsdir/lists/$1
432	echo_if_exist $setdir/stl.mi
433	echo_if_exist $setdir/stl.${stlib}
434}
435
436list_set_lists_shl()
437{
438	setdir=$setsdir/lists/$1
439	[ "$shlib" != "no" ] || return
440	echo_if_exist $setdir/shl.mi
441	echo_if_exist $setdir/shl.${shlib}
442}
443
444list_set_lists_module()
445{
446	setdir=$setsdir/lists/$1
447	[ "$module" != "no" ] || return
448	echo_if_exist $setdir/module.mi
449	echo_if_exist $setdir/module.${MACHINE}
450	# XXX module never has .shl
451	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
452	list_set_lists_common_ad $1 module
453}
454
455list_set_lists_rescue()
456{
457	setdir=$setsdir/lists/$1
458	echo_if_exist $setdir/rescue.mi
459	echo_if_exist $setdir/rescue.${MACHINE}
460	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
461	list_set_lists_common_ad $1 rescue
462}
463
464list_set_lists_common_ad()
465{
466	setdir=$setsdir/lists/$1; _prefix=$2
467
468	[ -n "$_prefix" ] && prefix="$_prefix".
469
470	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
471	# <prefix>.ad.${MACHINE_CPU}, since the arch-
472	# specific one will be more specific than the
473	# cpu-specific one.
474	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
475	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
476	[ "$shlib" != "no" ] && \
477	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
478}
479
480echo_if_exist()
481{
482	[ -f $1 ] && echo $1
483	return $?
484}
485
486echo_if_exist_foreach()
487{
488	local _list=$1; shift
489	for _suffix in $@; do
490		echo_if_exist ${_list}.${_suffix}
491	done
492}
493
494print_set_lists()
495{
496	for setname; do
497		list=$(list_set_lists $setname)
498		for l in $list; do
499			echo $l
500			if $verbose; then
501				echo >&2 "DEBUG: list_set_files: $l"
502			fi
503		done
504	done | ${XARGS} ${SED} ${SUBST}
505}
506
507# arch_to_cpu mach
508#
509# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
510# as determined by <bsd.own.mk>.
511#
512arch_to_cpu()
513{
514	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
515.include <bsd.own.mk>
516all:
517	@echo \${MACHINE_CPU}
518EOMAKE
519}
520
521# arch_to_endian mach
522#
523# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
524# as determined by <bsd.endian.mk>.
525#
526arch_to_endian()
527{
528	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
529.include <bsd.endian.mk>
530all:
531	@echo \${TARGET_ENDIANNESS}
532EOMAKE
533}
534
535#####
536
537# print_mkvars
538print_mkvars()
539{
540	for v in $MKVARS; do
541		eval echo $v=\$$v
542	done
543}
544
545# print_set_lists_{base,x,ext}
546# list_set_lists_{base,x,ext}
547# list_set_files_{base,x,ext}
548for func in print_set_lists list_set_lists list_set_files; do
549	for x in base x ext; do
550		if [ $x = base ]; then
551			list=nlists
552		else
553			list=${x}lists
554		fi
555		eval ${func}_${x} \(\) \{ $func \$$list \; \}
556	done
557done
558