sets.subr revision 1.207
1#	$NetBSD: sets.subr,v 1.207 2024/07/11 08:13:50 riastradh 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#	obsolete		controls if obsolete files are selected instead
10#	module			if != "no", enable MODULE sets
11#	shlib			shared library format (a.out, elf, or "")
12#	stlib			static library format (a.out, elf)
13#
14# The following <bsd.own.mk> variables are exported to the environment:
15#	MACHINE
16#	MACHINE_ARCH
17#	MACHINE_CPU
18#	HAVE_ACPI
19#	HAVE_BINUTILS
20#	HAVE_GCC
21#	HAVE_GDB
22#	HAVE_NVMM
23#	HAVE_OPENSSL
24#	HAVE_SSP
25#	HAVE_UEFI
26#	TOOLCHAIN_MISSING
27#	OBJECT_FMT
28# as well as:
29#
30
31#
32# The following variables refer to tools that are used when building sets:
33#
34: ${AWK:=awk}
35: ${CKSUM:=cksum}
36: ${COMM:=comm}
37: ${DATE:=date}
38: ${DB:=db}
39: ${EGREP:=egrep}
40: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
41: ${FGREP:=fgrep}
42: ${FIND:=find}
43: ${GREP:=grep}
44: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
45: ${HOSTNAME_CMD:=hostname}	# ${HOSTNAME} is special to bash(1)
46: ${HOST_SH:=sh}
47: ${IDENT:=ident}
48: ${JOIN:=join}
49: ${LS:=ls}
50: ${MAKE:=make}
51: ${MKTEMP:=mktemp}
52: ${MTREE:=mtree}
53: ${PASTE:=paste}
54: ${PAX:=pax}
55: ${PRINTF:=printf}
56: ${SED:=sed}
57: ${SORT:=sort}
58: ${STAT:=stat}
59: ${TSORT:=tsort}
60: ${UNAME:=uname}
61: ${WC:=wc}
62: ${XARGS:=xargs}
63
64#
65# If printf is a shell builtin command, then we can
66# implement cheaper versions of basename and dirname
67# that do not involve any fork/exec overhead.
68# If printf is not builtin, approximate it using echo,
69# and hope there are no weird file names that cause
70# some versions of echo to do the wrong thing.
71# (Converting to this version of dirname speeded up the
72# syspkgdeps script by an order of magnitude, from 68
73# seconds to 6.3 seconds on one particular host.)
74#
75# Note that naive approximations for dirname
76# using ${foo%/*} do not do the right thing in cases
77# where the result should be "/" or ".".
78#
79case "$(type printf)" in
80*builtin*)
81	basename ()
82	{
83		local bn
84		bn="${1##*/}"
85		bn="${bn%$2}"
86		printf "%s\n" "$bn"
87	}
88	dirname ()
89	{
90		local dn
91		case "$1" in
92		?*/*)	dn="${1%/*}" ;;
93		/*)	dn=/ ;;
94		*)	dn=. ;;
95		esac
96		printf "%s\n" "$dn"
97	}
98	;;
99*)
100	basename ()
101	{
102		local bn
103		bn="${1##*/}"
104		bn="${bn%$2}"
105		echo "$bn"
106	}
107	dirname ()
108	{
109		local dn
110		case "$1" in
111		?*/*)	dn="${1%/*}" ;;
112		/*)	dn=/ ;;
113		*)	dn=. ;;
114		esac
115		echo "$dn"
116	}
117	;;
118esac
119
120#####
121
122oIFS=$IFS
123IFS="
124"
125
126for x in $( MAKEVERBOSE= ${MAKE} -B -f ${rundir}/mkvars.mk mkvars ); do
127	eval export $x
128done
129
130IFS=$oIFS
131
132MKVARS="$( MAKEVERBOSE= ${MAKE} -B -f ${rundir}/mkvars.mk mkvars | ${SED} -e 's,=.*,,' | ${XARGS} )"
133
134#####
135
136setsdir=${rundir}
137obsolete=0
138if [ "${MKKMOD}" = "no" ]; then
139	module=no			# MODULEs are off.
140	modset=""
141else
142	module=yes
143	modset="modules"
144fi
145if [ "${MKATF}" = "no" ]; then
146	testset=""
147else
148	testset="tests"
149fi
150if [ "${MKDEBUG}" = "no" -a "${MKDEBUGLIB}" = "no" ]; then
151	debugset=""
152	xdebugset=""
153else
154	debugset="debug"
155	xdebugset="xdebug"
156fi
157if [ -z "${debugset}" -o "${MKCOMPAT}" = "no" ]; then
158	debug32set=""
159else
160	debug32set="debug32"
161fi
162if [ -z "${debug32set}" ]; then
163	debug64set=""
164else
165	if [ "${MACHINE_ARCH}" = "mips64eb" -o "${MACHINE_ARCH}" = "mips64el" ]; then
166		debug64set="debug64"
167	else
168		debug64set=""
169	fi
170fi
171if [ "${MKDTB}" = "no" ]; then
172	dtbset=""
173else
174	dtbset="dtb"
175fi
176if [ "${MKHTML}" = "no" ]; then
177	manhtmlset=""
178else
179	manhtmlset="manhtml"
180fi
181if [ "${MKCOMPAT}" = "no" ]; then
182	base32set=""
183else
184	base32set="base32"
185fi
186if [ "${MKCOMPAT}" != "no" ]; then
187	if [ "${MACHINE_ARCH}" = "mips64eb" -o "${MACHINE_ARCH}" = "mips64el" ]; then
188		base64set="base64"
189	else
190		base64set=""
191	fi
192else
193	base64set=""
194fi
195
196# XXX This should not be encoded here -- this mostly duplicates
197# information in compat/archdirs.mk, except that it also identifies
198# which compat architectures are `32-bit' and which ones are `64-bit'.
199case $MACHINE_ARCH in
200aarch64)
201	compat32arches='eabi eabihf'
202	;;
203aarch64eb)
204	compat32arches=eabi
205	;;
206mips64eb|mips64el)
207	compat32arches=o32
208	compat64arches=64
209	;;
210mipsn64eb|mipsn64el)
211	compat32arches='n32 o32'
212	;;
213powerpc64)
214	compat32arches=powerpc
215	;;
216riscv64)
217	compat32arches=riscv32
218	;;
219sparc64)
220	compat32arches=sparc
221	;;
222x86_64)	compat32arches=i386
223	;;
224esac
225: ${compat64arches:=}
226
227
228# Determine lib type. Do this first so stlib also gets set.
229if [ "${OBJECT_FMT}" = "ELF" ]; then
230	shlib=elf
231else
232	shlib=aout
233fi
234stlib=$shlib
235# Now check for MKPIC or specials and turn off shlib if need be.
236if [ "${MKPIC}" = "no" ]; then
237	shlib=no
238fi
239nlists="base $base32set $base64set comp $debugset $debug32set $debug64set $dtbset etc games gpufw man $manhtmlset misc $modset rescue $testset text"
240xlists="xbase xcomp $xdebugset xetc xfont xserver"
241
242OSRELEASE=$(${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh -k)
243if [ "${KERNEL_DIR}" = "yes" ]; then
244	MODULEDIR="netbsd/modules"
245else
246	MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
247fi
248SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
249SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
250SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
251
252#
253# list_set_files setfile [...]
254#
255# Produce a packing list for setfile(s).
256# In each file, a record consists of a path and a System Package name,
257# separated by whitespace. E.g.,
258#
259# 	# $NetBSD: sets.subr,v 1.207 2024/07/11 08:13:50 riastradh Exp $
260# 	.			base-sys-root	[keyword[,...]]
261# 	./altroot		base-sys-root
262# 	./bin			base-sys-root
263# 	./bin/[			base-util-root
264# 	./bin/cat		base-util-root
265#		[...]
266#
267# A # in the first column marks a comment.
268#
269# If ${obsolete} != 0, only entries with an "obsolete" keyword will
270# be printed.  All other keywords must be present.
271#
272# The third field is an optional comma separated list of keywords to
273# control if a record is printed; every keyword listed must be enabled
274# for the record to be printed. The list of all available make variables
275# that can be turned on or off can be found by running in this directory:
276#
277#	make -f mkvars.mk mkvarsyesno
278#
279# These MK<NAME> variables can be used as selectors in the sets as <name>.
280#
281# The following extra keywords are also available, listed by:
282#
283#	make -f mkvars.mk mkextravars
284#
285# These are:
286#    1. The HAVE_<name>:
287#	ssp			${HAVE_SSP} != no
288#	libgcc_eh		${HAVE_LIBGCC_EH} != no
289#	acpi			${HAVE_ACPI} != no
290#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
291#	gcc=<n>			<n> = value of ${HAVE_GCC}
292#	gdb=<n>			<n> = value of ${HAVE_GDB}
293#	mesa_ver=<n>		<n> = value of ${HAVE_MESA_VER}
294#	nvmm			${HAVE_NVMM} != no
295#	openssl=<n>		<n> = value of ${HAVE_OPENSSL}
296#	uefi			${HAVE_UEFI} != no
297#	xorg_server_ver=<n>	<n> = value of ${HAVE_XORG_SERVER_VER}
298#	xorg_glamor		${HAVE_XORG_GLAMOR} != no
299#
300#    2. The USE_<name>:
301#	use_inet6		${USE_INET6} != no
302#	use_kerberos		${USE_KERBEROS} != no
303#	use_ldap		${USE_LDAP} != no
304#	use_yp			${USE_YP} != no
305#
306#    3. Finally:
307#	dummy			dummy entry (ignored)
308#	obsolete		file is obsolete, and only printed if
309#				${obsolete} != 0
310#
311#	solaris			${MKDTRACE} != no or ${MKZFS} != no or ${MKCTF} != no
312#
313#
314#	endian=<n>		<n> = value of ${TARGET_ENDIANNESS}
315#
316#
317#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
318#				  automatically append ".gz" to the filename
319#
320#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
321#				  automatically append ".gz" to the filename
322#
323list_set_files()
324{
325	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
326		verbose=false
327	else
328		verbose=true
329	fi
330	local CONFIGS="$( list_kernel_configs )"
331	print_set_lists "$@" | \
332	${AWK} -v obsolete=${obsolete} '
333		function addkmod(line, fname, prefix, pat, patlen) {
334			if (substr(line, 1, patlen) != pat) {
335				return
336			}
337			for (d in kmodarchdirs) {
338				xd = prefix kmodarchdirs[d]
339				xline = xd substr(line, patlen + 1)
340				xfname = xd substr(fname, patlen + 1)
341				list[xline] = xfname
342				emit(xline)
343			}
344		}
345		function adddebugkernel(line, fname, pat, patlen) {
346			if (pat == "" || substr(line, 1, patlen) != pat) {
347				return 0
348			}
349			split("'"${CONFIGS}"'", configs)
350			for (d in configs) {
351				xfname = fname
352				sub("@CONFIG@", configs[d], xfname)
353				xline = line;
354				sub("@CONFIG@", configs[d], xline)
355				list[xline] = xfname
356				emit(xline)
357			}
358			return 1
359		}
360		function emit(fname) {
361			emitf[fname] = 1
362		}
363		BEGIN {
364			if (obsolete)
365				wanted["obsolete"] = 1
366
367			ncpaths = 0
368			split("'"${MKVARS}"'", needvars)
369			doingcompat = 0
370			doingcompattests = 0
371			ignoredkeywords["compatdir"] = 1
372			ignoredkeywords["compatfile"] = 1
373			ignoredkeywords["compattestdir"] = 1
374			ignoredkeywords["compattestfile"] = 1
375			ignoredkeywords["compatx11dir"] = 1
376			ignoredkeywords["compatx11file"] = 1
377			for (vi in needvars) {
378				nv = needvars[vi]
379				kw = tolower(nv)
380				sub(/^mk/, "", kw)
381				sub(/^have_/, "", kw)
382				sub(/^target_endianness/, "endian", kw)
383				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
384					wanted[kw] = 1
385				}
386			}
387
388			if ("compat" in wanted) {
389				doingcompat = 1;
390				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
391				compatdirkeywords["compatdir"] = 1
392				compatfilekeywords["compatfile"] = 1
393
394				if (wanted["compattests"]) {
395					doingcompattests = 1;
396					compatdirkeywords["compattestdir"] = 1
397					compatfilekeywords["compattestfile"] = 1
398				}
399				if (wanted["compatx11"]) {
400					doingcompatx11 = 1;
401					compatdirkeywords["compatx11dir"] = 1
402					compatfilekeywords["compatx11file"] = 1
403				}
404			}
405
406			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
407				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
408				kmodprefix = "./stand/"
409				kmodpat = kmodprefix ENVIRON["MACHINE"]
410				l_kmodpat = length(kmodpat)
411				kmoddbprefix = "./usr/libdata/debug/stand/"
412				kmoddbpat = kmoddbprefix ENVIRON["MACHINE"]
413				l_kmoddbpat = length(kmoddbpat)
414			}
415			if ("debug" in wanted) {
416				debugkernelname = "./usr/libdata/debug/netbsd-@CONFIG@.debug"
417				l_debugkernelname = length(debugkernelname);
418			}
419
420			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
421				if ("binutils" in wanted)
422					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
423				if ("gcc" in wanted)
424					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
425				if ("gdb" in wanted)
426					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
427			}
428			if ("acpi" in wanted) {
429				wanted["acpi=" "'"${HAVE_ACPI}"'"] = 1
430			}
431			if ("mesa_ver" in wanted) {
432				wanted["mesa_ver=" "'"${HAVE_MESA_VER}"'"] = 1
433			}
434			if ("nvmm" in wanted) {
435				wanted["nvmm=" "'"${HAVE_NVMM}"'"] = 1
436			}
437			if ("openssl" in wanted) {
438				wanted["openssl=" "'"${HAVE_OPENSSL}"'"] = 1
439			}
440			if ("xorg_server_ver" in wanted) {
441				wanted["xorg_server_ver=" "'"${HAVE_XORG_SERVER_VER}"'"] = 1
442			}
443			if ("uefi" in wanted) {
444				wanted["uefi=" "'"${HAVE_UEFI}"'"] = 1
445			}
446			if (("man" in wanted) && ("catpages" in wanted))
447				wanted[".cat"] = 1
448			if (("man" in wanted) && ("manpages" in wanted))
449				wanted[".man"] = 1
450			if ("endian" in wanted)
451				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
452			if ("machine" in wanted)
453				wanted["machine=" "'"${MACHINE}"'"] = 1
454			if ("machine_arch" in wanted)
455				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
456			if ("machine_cpu" in wanted)
457				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
458		}
459
460		/^#/ {
461			next;
462		}
463
464		/^-/ {
465			notwanted[substr($1, 2)] = 1;
466			delete list [substr($1, 2)];
467			next;
468		}
469
470		NF > 2 && $3 != "-" {
471			if (notwanted[$1] != "")
472				next;
473			split($3, keywords, ",")
474			show = 1
475			haveobs = 0
476			iscompatfile = 0
477			havekmod = 0
478			iscompatdir = 0
479			omitcompat = 0
480			takecompat[$1] = 0
481			for (ki in keywords) {
482				kw = keywords[ki]
483				if (("manz" in wanted) &&
484				    (kw == ".cat" || kw == ".man"))
485					$1 = $1 ".gz"
486				if (substr(kw, 1, 1) == "!") {
487					kw = substr(kw, 2)
488					if (kw in wanted)
489						show = 0
490				} else if (kw in compatdirkeywords) {
491					iscompatdir = 1
492				} else if (kw in compatfilekeywords) {
493					iscompatfile = 1
494				} else if (kw == "nocompatmodules") {
495					havekmod = -1
496				} else if (kw == "omitcompat") {
497					omitcompat = 1
498				} else if (kw ~ /^takecompat=/) {
499					takecompat[$1] = 1
500					takecompatarch[substr(kw,
501					    1 + length("takecompat=")), $1] = 1
502				} else if (kw in ignoredkeywords) {
503					# ignore
504				} else if (! (kw in wanted)) {
505					show = 0
506				} else if (kw == "kmod" && havekmod == 0) {
507					havekmod = 1
508				}
509				if (kw == "obsolete")
510					haveobs = 1
511			}
512
513			if (takecompat[$1] && !(iscompatdir || iscompatfile)) {
514				next
515			}
516			if (iscompatdir && !omitcompat) {
517				for (d in cpaths) {
518					if (cpaths[d] == $1 "/") {
519						break
520					}
521				}
522				cpaths[ncpaths++] = $1 "/"
523			}
524			if (obsolete && ! haveobs)
525				next
526			if (!show)
527				next
528			if (adddebugkernel($0, $1, debugkernelname, l_debugkernelname))
529				next
530
531			list[$1] = $0
532			if (havekmod > 0) {
533				addkmod($0, $1, kmodprefix, kmodpat, l_kmodpat)
534				addkmod($0, $1, kmoddbprefix, kmoddbpat, l_kmoddbpat)
535				emit($1)
536				next
537			}
538
539			if (!doingcompat || !(iscompatfile || iscompatdir)) {
540				emit($1)
541				next
542			}
543
544			if (iscompatfile) {
545				if (omitcompat) {
546					emit($1)
547				} else if (takecompat[$1]) {
548					emitcompat[$1] = 1
549				} else {
550					emit($1)
551					emitcompat[$1] = 1
552				}
553				next
554			}
555			if (iscompatdir) {
556				if (omitcompat) {
557					# /lib in base
558					emit($1)
559				} else if (takecompat[$1]) {
560					# /lib in base32
561					# nothing to do
562				} else {
563					# /usr/include in comp
564					emit($1)
565				}
566			} else {
567				emit($1)
568			}
569			if (omitcompat)
570				next
571			for (d in compatarchdirs) {
572				if (takecompat[$1] &&
573				    !takecompatarch[compatarchdirs[d], $1])
574					continue
575				tmp = $0
576				xfile = $1 "/" compatarchdirs[d]
577				tmp = xfile substr(tmp, length($1) + 1)
578				if (xfile in notwanted)
579					continue;
580				sub("compatdir","compat",tmp);
581				sub("compattestdir","compat",tmp);
582				list[xfile] = tmp
583				emit(xfile)
584			}
585			next
586		}
587
588		{
589			if ($1 in notwanted)
590				next;
591			if (! obsolete) {
592				list[$1] = $0
593				emit($1)
594			}
595		}
596
597		END {
598			for (i in list) {
599				if (i in emitf)
600					print list[i]
601				if (! (i in emitcompat))
602					continue;
603				l_i = length(i)
604				l = 0
605				for (j in cpaths) {
606					lx = length(cpaths[j])
607					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
608						continue;
609					}
610					if (lx > l) {
611						l = lx;
612						cpath = cpaths[j];
613					}
614				}
615				for (d in compatarchdirs) {
616					if (takecompat[$1] &&
617					    !takecompatarch[compatarchdirs[d],
618						i]) {
619						continue
620					}
621					tmp = list[i]
622					extrapath = compatarchdirs[d] "/"
623					xfile = cpath extrapath substr(i, l + 1)
624					if (xfile in notwanted)
625						continue;
626					sub("compatfile","compat",tmp);
627					sub("compattestfile","compat",tmp);
628					tmp = xfile substr(tmp, l_i + 1)
629					print tmp;
630				}
631			}
632		}'
633
634}
635
636#
637# list_set_lists setname
638#
639# Print to stdout a list of files, one filename per line, which
640# concatenate to create the packing list for setname. E.g.,
641#
642# 	.../lists/base/mi
643# 	.../lists/base/rescue.mi
644# 	.../lists/base/md.i386
645#		[...]
646#
647# For a given setname $set, the following files may be selected from
648# .../list/$set:
649#	mi
650#	mi.ext.*
651#	ad.${MACHINE_ARCH}
652# (or)	ad.${MACHINE_CPU}
653#	ad.${MACHINE_CPU}.shl
654#	md.${MACHINE}.${MACHINE_ARCH}
655# (or)	md.${MACHINE}
656#	stl.mi
657#	stl.${stlib}
658#	shl.mi
659#	shl.mi.ext.*
660#	shl.${shlib}
661#	shl.${shlib}.ext.*
662#	module.mi			if ${module} != no
663#	module.${MACHINE}		if ${module} != no
664#	module.ad.${MACHINE_ARCH}	if ${module} != no
665# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
666#	rescue.shl
667#	rescue.${MACHINE}
668#	rescue.ad.${MACHINE_ARCH}
669# (or)	rescue.ad.${MACHINE_CPU}
670# 	rescue.ad.${MACHINE_CPU}.shl
671#
672# Environment:
673# 	shlib
674# 	stlib
675#
676list_set_lists()
677{
678	setname=$1
679
680	list_set_lists_mi $setname
681	list_set_lists_ad $setname
682	list_set_lists_md $setname
683	list_set_lists_stl $setname
684	list_set_lists_shl $setname
685	list_set_lists_module $setname
686	list_set_lists_rescue $setname
687	return 0
688}
689
690list_set_lists_mi()
691{
692	setdir=$setsdir/lists/$1
693	# always exist!
694	echo $setdir/mi
695}
696
697list_set_lists_ad()
698{
699	setdir=$setsdir/lists/$1
700	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
701	list_set_lists_common_ad $1
702}
703
704list_set_lists_md()
705{
706	setdir=$setsdir/lists/$1
707	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
708	echo_if_exist $setdir/md.${MACHINE}
709}
710
711list_set_lists_stl()
712{
713	setdir=$setsdir/lists/$1
714	echo_if_exist $setdir/stl.mi
715	echo_if_exist $setdir/stl.${stlib}
716}
717
718list_set_lists_shl()
719{
720	setdir=$setsdir/lists/$1
721	[ "$shlib" != "no" ] || return
722	echo_if_exist $setdir/shl.mi
723	echo_if_exist $setdir/shl.${shlib}
724}
725
726list_set_lists_module()
727{
728	setdir=$setsdir/lists/$1
729	[ "$module" != "no" ] || return
730	echo_if_exist $setdir/module.mi
731	echo_if_exist $setdir/module.${MACHINE}
732	echo_if_exist $setdir/module.ad.${MACHINE}
733	echo_if_exist $setdir/module.md.${MACHINE}
734	# XXX module never has .shl
735	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
736	list_set_lists_common_ad $1 module
737}
738
739list_set_lists_rescue()
740{
741	setdir=$setsdir/lists/$1
742	echo_if_exist $setdir/rescue.mi
743	echo_if_exist $setdir/rescue.${MACHINE}
744	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
745	list_set_lists_common_ad $1 rescue
746}
747
748list_set_lists_common_ad()
749{
750	setdir=$setsdir/lists/$1; _prefix=$2
751
752	[ -n "$_prefix" ] && prefix="$_prefix".
753
754	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
755	# <prefix>.ad.${MACHINE_CPU}, since the arch-
756	# specific one will be more specific than the
757	# cpu-specific one.
758	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
759	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
760	[ "$shlib" != "no" ] && \
761	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
762}
763
764echo_if_exist()
765{
766	[ -f $1 ] && echo $1
767	return $?
768}
769
770echo_if_exist_foreach()
771{
772	local _list=$1; shift
773	for _suffix in $@; do
774		echo_if_exist ${_list}.${_suffix}
775	done
776}
777
778print_set_lists()
779{
780	for setname; do
781		list=$(list_set_lists $setname)
782		for l in $list; do
783			echo $l
784			if $verbose; then
785				echo >&2 "DEBUG: list_set_files: $l"
786			fi
787		done \
788		| ${XARGS} ${SED} ${SUBST} \
789		| case $setname in
790		base|debug)
791			awk '
792				!/^#/ && !/^$/ {
793					print $1, $2, \
794					    ($3 ? $3"," : "")"omitcompat"
795				}
796			'
797			;;
798		*)	cat
799			;;
800		esac
801
802		case $setname in
803		base32|base64)
804			ursetname=base
805			;;
806		debug32|debug64)
807			ursetname=debug
808			;;
809		*)	continue
810			;;
811		esac
812		case $setname in
813		*32)	compatarches=$compat32arches
814			;;
815		*64)	compatarches=$compat64arches
816			;;
817		esac
818		list=$(list_set_lists $ursetname)
819		for l in $list; do
820			echo $l
821			if $verbose; then
822				echo >&2 "DEBUG: list_set_files: $l"
823			fi
824		done \
825		| ${XARGS} ${SED} ${SUBST} \
826		| awk -v compatarches="$compatarches" '
827			BEGIN {
828				split(compatarches, compatarch, " ")
829				flags = ""
830				for (i in compatarch)
831					flags = (flags ? flags"," : "") \
832					    "takecompat="compatarch[i]
833			}
834			!/^#/ && !/^$/ {
835				print $1, $2, ($3 ? $3"," : "")flags
836			}
837		'
838	done
839}
840
841
842list_kernel_configs()
843{
844	(cd ${NETBSDSRCDIR}/etc
845	MAKEFLAGS= \
846	${MAKE} -m ${NETBSDSRCDIR}/share/mk -V '${ALL_KERNELS}')
847}
848
849# arch_to_cpu mach
850#
851# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
852# as determined by <bsd.own.mk>.
853#
854arch_to_cpu()
855{
856	MACHINE_ARCH=${1} MAKEFLAGS= \
857	${MAKE} -m ${NETBSDSRCDIR}/share/mk \
858		-f ${NETBSDSRCDIR}/share/mk/bsd.own.mk \
859		-V '${MACHINE_CPU}'
860}
861
862# arch_to_endian mach
863#
864# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
865# as determined by <bsd.endian.mk>.
866#
867arch_to_endian()
868{
869	MACHINE_ARCH=${1} MAKEFLAGS= \
870	${MAKE} -m ${NETBSDSRCDIR}/share/mk \
871		-f ${NETBSDSRCDIR}/share/mk/bsd.endian.mk \
872		-V '${TARGET_ENDIANNESS}'
873}
874
875#####
876
877# print_mkvars
878print_mkvars()
879{
880	for v in $MKVARS; do
881		eval echo $v=\$$v
882	done
883}
884
885# print_set_lists_{base,x,ext}
886# list_set_lists_{base,x,ext}
887# list_set_files_{base,x,ext}
888for func in print_set_lists list_set_lists list_set_files; do
889	for x in base x ext; do
890		if [ $x = base ]; then
891			list=nlists
892		else
893			list=${x}lists
894		fi
895		eval ${func}_${x} \(\) \{ $func \$$list \; \}
896	done
897done
898