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