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