sets.subr revision 1.166
1#	$NetBSD: sets.subr,v 1.166 2015/06/22 07:07:10 matt 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.166 2015/06/22 07:07:10 matt 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#	compattests		${MKCOMPATTESTS} != no
210#	crypto			${MKCRYPTO} != no
211#	crypto_rc5		${MKCRYPTO_RC5} != no
212#	cvs			${MKCVS} != no
213#	debug			${MKDEBUG} != no
214#	debuglib		${MKDEBUGLIB} != no
215#	doc			${MKDOC} != no
216#	dtrace			${MKDTRACE} != no
217#	dynamicroot		${MKDYNAMICROOT} != no
218#	extsrc			${MKEXTSRC} != no
219#	gcc			${MKGCC} != no
220#	gcccmds			${MKGCCCMDS} != no
221#	gdb			${MKGDB} != no
222#	hesiod			${MKHESIOD} != no
223#	html			${MKHTML} != no
224#	inet6			${MKINET6} != no
225#	info			${MKINFO} != no
226#	ipfilter		${MKIPFILTER} != no
227#	iscsi			${MKISCSI} != no
228#	kerberos		${MKKERBEROS} != no
229#	kmod			${MKKMOD} != no
230#	kyua			${MKKYUA} != no
231#	ldap			${MKLDAP} != no
232#	lint			${MKLINT} != no
233#	libcxx			${MKLIBCXX} != no
234#	libgcc_eh		${HAVE_LIBGCC_EH} != no
235#	libstdcxx		${MKLIBSTDCXX} != no
236#	lld			${MKLLD} != no
237#	lldb			${MKLLDB} != no
238#	llvm			${MKLLVM} != no
239#	lvm			${MKLVM} != no
240#	makemandb		${MKMAKEMANDB} != no
241#	man			${MKMAN} != no
242#	manpages		${MKMANPAGES} != no
243#	manz			${MKMANZ} != no
244#	mclinker		${MKMCLINKER} != no
245#	mdns			${MKMDNS} != no
246#	nls			${MKNLS} != no
247#	nvi			${MKNVI} != no
248#	pam			${MKPAM} != no
249#	pcc			${MKPCC} != no
250#	pf			${MKPF} != no
251#	pic			${MKPIC} != no
252#	picinstall		${MKPICINSTALL} != no
253#	postfix			${MKPOSTFIX} != no
254#	profile			${MKPROFILE} != no
255#	perfuse			${MKPERFUSE} != no
256#	rump			${MKRUMP} != no
257#	share			${MKSHARE} != no
258#	skey			${MKSKEY} != no
259#	sljit			${MKSLJIT} != no
260#	softfloat		${MKSOFTFLOAT} != no
261#	solaris			${MKDTRACE} != no or ${MKZFS} != no
262#	ssp			${HAVE_SSP} != no
263#	tpm			${MKTPM} != no
264#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
265#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
266#	xorg_server		${MKXORG_SERVER} != no
267#	yp			${MKYP} != no
268#	zfs			${MKZFS} != no
269#
270#	endian=<n>		<n> = value of ${TARGET_ENDIANNESS}
271#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
272#	gcc=<n>			<n> = value of ${HAVE_GCC}
273#	gdb=<n>			<n> = value of ${HAVE_GDB}
274#
275#	use_inet6		${USE_INET6} != no
276#	use_kerberos		${USE_KERBEROS} != no
277#	use_yp			${USE_YP} != no
278#
279#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
280#				  automatically append ".gz" to the filename
281#
282#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
283#				  automatically append ".gz" to the filename
284#
285list_set_files()
286{
287	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
288		verbose=false
289	else
290		verbose=true
291	fi
292	print_set_lists "$@" | \
293	${AWK} -v obsolete=${obsolete} '
294		BEGIN {
295			x="tmp.out"
296			if (obsolete)
297				wanted["obsolete"] = 1
298		
299			split("'"${MKVARS}"'", needvars)
300			doingcompat = 0
301			doingcompattests = 0
302			ignoredkeywords["compatdir"] = 1
303			ignoredkeywords["compatfile"] = 1
304			ignoredkeywords["compattestdir"] = 1
305			ignoredkeywords["compattestfile"] = 1
306			for (vi in needvars) {
307				nv = needvars[vi]
308				kw = tolower(nv)
309				sub(/^mk/, "", kw)
310				sub(/^have_/, "", kw)
311				sub(/^target_endianness/, "endian", kw)
312				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATDIRS" && nv != "MODULEARCHDIRS")
313					wanted[kw] = 1 
314			}
315
316			if (wanted["compat"]) {
317				doingcompat = 1;
318				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
319				compatdirkeywords["compatdir"] = 1
320				compatfilekeywords["compatfile"] = 1
321
322				if (wanted["compattests"]) {
323					doingcompattests = 1;
324					compatdirkeywords["compattestdir"] = 1
325					compatfilekeywords["compattestfile"] = 1
326				}
327			}
328
329			if (wanted["kmod"] && wanted["compatmodules"]) {
330				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
331				kmodpat = "./stand/" ENVIRON["MACHINE"]
332				l_kmodpat = length(kmodpat)
333			}
334
335			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
336				if ("binutils" in wanted)
337					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
338				if ("gcc" in wanted)
339					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
340				if ("gdb" in wanted)
341					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
342			}
343			if (("man" in wanted) && ("catpages" in wanted))
344				wanted[".cat"] = 1
345			if (("man" in wanted) && ("manpages" in wanted))
346				wanted[".man"] = 1
347			if ("endian" in wanted)
348				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
349			if ("machine" in wanted)
350				wanted["machine=" "'"${MACHINE}"'"] = 1
351			if ("machine_arch" in wanted)
352				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
353			if ("machine_cpu" in wanted)
354				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
355		}
356
357		/^#/ {
358			next;
359		}
360
361		/^-/ {
362			notwanted[substr($1, 2)] = 1;
363			delete list [substr($1, 2)];
364			next;
365		}
366				
367
368		NF > 2 && $3 != "-" {
369			if (notwanted[$1] != "")
370				next;
371			split($3, keywords, ",")
372			show = 1
373			haveobs = 0
374			havecompat = 0
375			havekmod = 0
376			iscompatdir = 0
377			for (ki in keywords) {
378				kw = keywords[ki]
379				if (("manz" in wanted) &&
380				    (kw == ".cat" || kw == ".man"))
381					$1 = $1 ".gz"
382				if (substr(kw, 1, 1) == "!") {
383					kw = substr(kw, 2)
384					if (kw in wanted)
385						show = 0
386				} else if (kw in compatdirkeywords) {
387					iscompatdir = 1
388				} else if (kw in compatfilekeywords) {
389					havecompat = 1
390				} else if (kw in ignoredkeywords) {
391					# ignore
392				} else {
393					if (! (kw in wanted))
394						show = 0
395					else if (kw == "kmod")
396						havekmod = 1
397				}
398				if (kw == "obsolete")
399					haveobs = 1
400			}
401			if (obsolete && ! haveobs)
402				next
403			if (!show)
404				next
405
406			list[$1] = $0
407			if (havekmod && substr($1,1,l_kmodpat) == kmodpat) {
408				for (d in kmodarchdirs) {
409					xd = "./stand/" kmodarchdirs[d]
410					xfile = xd substr($1, l_kmodpat+1)
411					tmp = xd substr($0, l_kmodpat+1)
412					list[xfile] = tmp;
413				}
414				next
415			}
416
417			if (!doingcompat || !(havecompat || iscompatdir))
418				next
419
420			if (havecompat) {
421				emitcompat[$1] = 1;
422				next;
423			}
424			cpaths[ncpaths++] = $1 "/"
425			for (d in compatarchdirs) {
426				tmp = $0
427				xfile = $1 "/" compatarchdirs[d]
428				tmp = xfile substr(tmp, length($1) + 1)
429				if (xfile in notwanted)
430					continue;
431				sub("compatdir","compat",tmp);
432				sub("compattestdir","compat",tmp);
433				list[xfile] = tmp
434			}
435			next
436		}
437
438		{
439			if ($1 in notwanted)
440				next;
441			if (! obsolete)
442				list[$1] = $0
443		}
444
445		END {
446			for (i in list) {
447				print list[i]
448				if (! (i in emitcompat))
449					continue;
450				l_i = length(i)
451				l = 0
452				for (j in cpaths) {
453					lx = length(cpaths[j])
454					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
455						continue;
456					}
457					if (lx > l) {
458						l = lx;
459						cpath = cpaths[j];
460					}
461				}
462				for (d in compatarchdirs) {
463					tmp = list[i]
464					extrapath = compatarchdirs[d] "/"
465					xfile = cpath extrapath substr(i, l + 1)
466					if (xfile in notwanted)
467						continue;
468					sub("compatfile","compat",tmp);
469					sub("compattestfile","compat",tmp);
470					tmp = xfile substr(tmp, l_i + 1)
471					print tmp;
472				}
473			}
474		}'
475
476}
477
478#
479# list_set_lists setname
480# 
481# Print to stdout a list of files, one filename per line, which
482# concatenate to create the packing list for setname. E.g.,
483#
484# 	.../lists/base/mi
485# 	.../lists/base/rescue.mi
486# 	.../lists/base/md.i386
487#		[...]
488#
489# For a given setname $set, the following files may be selected from
490# .../list/$set:
491#	mi
492#	mi.ext.*
493#	ad.${MACHINE_ARCH}
494# (or)	ad.${MACHINE_CPU}
495#	ad.${MACHINE_CPU}.shl
496#	md.${MACHINE}.${MACHINE_ARCH}
497# (or)	md.${MACHINE}
498#	stl.mi
499#	stl.${stlib}
500#	shl.mi
501#	shl.mi.ext.*
502#	shl.${shlib}
503#	shl.${shlib}.ext.*
504#	module.mi			if ${module} != no
505#	module.${MACHINE}		if ${module} != no
506#	module.ad.${MACHINE_ARCH}	if ${module} != no
507# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
508#	rescue.shl
509#	rescue.${MACHINE}
510#	rescue.ad.${MACHINE_ARCH}
511# (or)	rescue.ad.${MACHINE_CPU}
512# 	rescue.ad.${MACHINE_CPU}.shl
513#
514# Environment:
515# 	shlib
516# 	stlib
517#
518list_set_lists()
519{
520	setname=$1
521
522	list_set_lists_mi $setname
523	list_set_lists_ad $setname
524	list_set_lists_md $setname
525	list_set_lists_stl $setname
526	list_set_lists_shl $setname
527	list_set_lists_module $setname
528	list_set_lists_rescue $setname
529	return 0
530}
531
532list_set_lists_mi()
533{
534	setdir=$setsdir/lists/$1
535	# always exist!
536	echo $setdir/mi
537}
538
539list_set_lists_ad()
540{
541	setdir=$setsdir/lists/$1
542	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
543	list_set_lists_common_ad $1
544}
545
546list_set_lists_md()
547{
548	setdir=$setsdir/lists/$1
549	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
550	echo_if_exist $setdir/md.${MACHINE}
551}
552
553list_set_lists_stl()
554{
555	setdir=$setsdir/lists/$1
556	echo_if_exist $setdir/stl.mi
557	echo_if_exist $setdir/stl.${stlib}
558}
559
560list_set_lists_shl()
561{
562	setdir=$setsdir/lists/$1
563	[ "$shlib" != "no" ] || return
564	echo_if_exist $setdir/shl.mi
565	echo_if_exist $setdir/shl.${shlib}
566}
567
568list_set_lists_module()
569{
570	setdir=$setsdir/lists/$1
571	[ "$module" != "no" ] || return
572	echo_if_exist $setdir/module.mi
573	echo_if_exist $setdir/module.${MACHINE}
574	# XXX module never has .shl
575	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
576	list_set_lists_common_ad $1 module
577}
578
579list_set_lists_rescue()
580{
581	setdir=$setsdir/lists/$1
582	echo_if_exist $setdir/rescue.mi
583	echo_if_exist $setdir/rescue.${MACHINE}
584	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
585	list_set_lists_common_ad $1 rescue
586}
587
588list_set_lists_common_ad()
589{
590	setdir=$setsdir/lists/$1; _prefix=$2
591
592	[ -n "$_prefix" ] && prefix="$_prefix".
593
594	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
595	# <prefix>.ad.${MACHINE_CPU}, since the arch-
596	# specific one will be more specific than the
597	# cpu-specific one.
598	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
599	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
600	[ "$shlib" != "no" ] && \
601	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
602}
603
604echo_if_exist()
605{
606	[ -f $1 ] && echo $1
607	return $?
608}
609
610echo_if_exist_foreach()
611{
612	local _list=$1; shift
613	for _suffix in $@; do
614		echo_if_exist ${_list}.${_suffix}
615	done
616}
617
618print_set_lists()
619{
620	for setname; do
621		list=$(list_set_lists $setname)
622		for l in $list; do
623			echo $l
624			if $verbose; then
625				echo >&2 "DEBUG: list_set_files: $l"
626			fi
627		done
628	done | ${XARGS} ${SED} ${SUBST}
629}
630
631# arch_to_cpu mach
632#
633# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
634# as determined by <bsd.own.mk>.
635#
636arch_to_cpu()
637{
638	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
639.include <bsd.own.mk>
640all:
641	@echo \${MACHINE_CPU}
642EOMAKE
643}
644
645# arch_to_endian mach
646#
647# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
648# as determined by <bsd.endian.mk>.
649#
650arch_to_endian()
651{
652	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
653.include <bsd.endian.mk>
654all:
655	@echo \${TARGET_ENDIANNESS}
656EOMAKE
657}
658
659#####
660
661# print_mkvars
662print_mkvars()
663{
664	for v in $MKVARS; do
665		eval echo $v=\$$v
666	done
667}
668
669# print_set_lists_{base,x,ext}
670# list_set_lists_{base,x,ext}
671# list_set_files_{base,x,ext}
672for func in print_set_lists list_set_lists list_set_files; do
673	for x in base x ext; do
674		if [ $x = base ]; then
675			list=nlists
676		else
677			list=${x}lists
678		fi
679		eval ${func}_${x} \(\) \{ $func \$$list \; \}
680	done
681done
682