xref: /netbsd-src/distrib/sets/sets.subr (revision 782713e6c126f1866c6d9cfdee4ceb49483b5828)
1#	$NetBSD: sets.subr,v 1.204 2022/09/10 12:31:42 rillig 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 [ "${MKDTB}" = "no" ]; then
158	dtbset=""
159else
160	dtbset="dtb"
161fi
162# Determine lib type. Do this first so stlib also gets set.
163if [ "${OBJECT_FMT}" = "ELF" ]; then
164	shlib=elf
165else
166	shlib=aout
167fi
168stlib=$shlib
169# Now check for MKPIC or specials and turn off shlib if need be.
170if [ "${MKPIC}" = "no" ]; then
171	shlib=no
172fi
173nlists="base comp $debugset $dtbset etc games gpufw man misc $modset rescue $testset text"
174xlists="xbase xcomp $xdebugset xetc xfont xserver"
175
176OSRELEASE=$(${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh -k)
177if [ "${KERNEL_DIR}" = "yes" ]; then
178	MODULEDIR="netbsd/modules"
179else
180	MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
181fi
182SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
183SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
184SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
185
186#
187# list_set_files setfile [...]
188#
189# Produce a packing list for setfile(s).
190# In each file, a record consists of a path and a System Package name,
191# separated by whitespace. E.g.,
192#
193# 	# $NetBSD: sets.subr,v 1.204 2022/09/10 12:31:42 rillig Exp $
194# 	.			base-sys-root	[keyword[,...]]
195# 	./altroot		base-sys-root
196# 	./bin			base-sys-root
197# 	./bin/[			base-util-root
198# 	./bin/cat		base-util-root
199#		[...]
200#
201# A # in the first column marks a comment.
202#
203# If ${obsolete} != 0, only entries with an "obsolete" keyword will
204# be printed.  All other keywords must be present.
205#
206# The third field is an optional comma separated list of keywords to
207# control if a record is printed; every keyword listed must be enabled
208# for the record to be printed. The list of all available make variables
209# that can be turned on or off can be found by running in this directory:
210#
211#	make -f mkvars.mk mkvarsyesno
212#
213# These MK<NAME> variables can be used as selectors in the sets as <name>.
214#
215# The following extra keywords are also available, listed by:
216#
217#	make -f mkvars.mk mkextravars
218#
219# These are:
220#    1. The HAVE_<name>:
221#	ssp			${HAVE_SSP} != no
222#	libgcc_eh		${HAVE_LIBGCC_EH} != no
223#	acpi			${HAVE_ACPI} != no
224#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
225#	gcc=<n>			<n> = value of ${HAVE_GCC}
226#	gdb=<n>			<n> = value of ${HAVE_GDB}
227#	mesa_ver=<n>		<n> = value of ${HAVE_MESA_VER}
228#	nvmm			${HAVE_NVMM} != no
229#	openssl=<n>		<n> = value of ${HAVE_OPENSSL}
230#	uefi			${HAVE_UEFI} != no
231#	xorg_server_ver=<n>	<n> = value of ${HAVE_XORG_SERVER_VER}
232#	xorg_glamor		${HAVE_XORG_GLAMOR} != no
233#
234#    2. The USE_<name>:
235#	use_inet6		${USE_INET6} != no
236#	use_kerberos		${USE_KERBEROS} != no
237#	use_ldap		${USE_LDAP} != no
238#	use_yp			${USE_YP} != no
239#
240#    3. Finally:
241#	dummy			dummy entry (ignored)
242#	obsolete		file is obsolete, and only printed if
243#				${obsolete} != 0
244#
245#	solaris			${MKDTRACE} != no or ${MKZFS} != no or ${MKCTF} != no
246#
247#
248#	endian=<n>		<n> = value of ${TARGET_ENDIANNESS}
249#
250#
251#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
252#				  automatically append ".gz" to the filename
253#
254#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
255#				  automatically append ".gz" to the filename
256#
257list_set_files()
258{
259	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
260		verbose=false
261	else
262		verbose=true
263	fi
264	local CONFIGS="$( list_kernel_configs )"
265	print_set_lists "$@" | \
266	${AWK} -v obsolete=${obsolete} '
267		function addkmod(line, fname, prefix, pat, patlen) {
268			if (substr(line, 1, patlen) != pat) {
269				return
270			}
271			for (d in kmodarchdirs) {
272				xd = prefix kmodarchdirs[d]
273				xline = xd substr(line, patlen + 1)
274				xfname = xd substr(fname, patlen + 1)
275				list[xline] = xfname
276			}
277		}
278		function adddebugkernel(line, fname, pat, patlen) {
279			if (pat == "" || substr(line, 1, patlen) != pat) {
280				return 0
281			}
282			split("'"${CONFIGS}"'", configs)
283			for (d in configs) {
284				xfname = fname
285				sub("@CONFIG@", configs[d], xfname)
286				xline = line;
287				sub("@CONFIG@", configs[d], xline)
288				list[xline] = xfname
289			}
290			return 1
291		}
292		BEGIN {
293			if (obsolete)
294				wanted["obsolete"] = 1
295
296			split("'"${MKVARS}"'", needvars)
297			doingcompat = 0
298			doingcompattests = 0
299			ignoredkeywords["compatdir"] = 1
300			ignoredkeywords["compatfile"] = 1
301			ignoredkeywords["compattestdir"] = 1
302			ignoredkeywords["compattestfile"] = 1
303			ignoredkeywords["compatx11dir"] = 1
304			ignoredkeywords["compatx11file"] = 1
305			for (vi in needvars) {
306				nv = needvars[vi]
307				kw = tolower(nv)
308				sub(/^mk/, "", kw)
309				sub(/^have_/, "", kw)
310				sub(/^target_endianness/, "endian", kw)
311				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
312					wanted[kw] = 1
313				}
314			}
315
316			if ("compat" in wanted) {
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				if (wanted["compatx11"]) {
328					doingcompatx11 = 1;
329					compatdirkeywords["compatx11dir"] = 1
330					compatfilekeywords["compatx11file"] = 1
331				}
332			}
333
334			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
335				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
336				kmodprefix = "./stand/"
337				kmodpat = kmodprefix ENVIRON["MACHINE"]
338				l_kmodpat = length(kmodpat)
339				kmoddbprefix = "./usr/libdata/debug/stand/"
340				kmoddbpat = kmoddbprefix ENVIRON["MACHINE"]
341				l_kmoddbpat = length(kmoddbpat)
342			}
343			if ("debug" in wanted) {
344				debugkernelname = "./usr/libdata/debug/netbsd-@CONFIG@.debug"
345				l_debugkernelname = length(debugkernelname);
346			}
347
348			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
349				if ("binutils" in wanted)
350					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
351				if ("gcc" in wanted)
352					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
353				if ("gdb" in wanted)
354					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
355			}
356			if ("acpi" in wanted) {
357				wanted["acpi=" "'"${HAVE_ACPI}"'"] = 1
358			}
359			if ("mesa_ver" in wanted) {
360				wanted["mesa_ver=" "'"${HAVE_MESA_VER}"'"] = 1
361			}
362			if ("nvmm" in wanted) {
363				wanted["nvmm=" "'"${HAVE_NVMM}"'"] = 1
364			}
365			if ("openssl" in wanted) {
366				wanted["openssl=" "'"${HAVE_OPENSSL}"'"] = 1
367			}
368			if ("xorg_server_ver" in wanted) {
369				wanted["xorg_server_ver=" "'"${HAVE_XORG_SERVER_VER}"'"] = 1
370			}
371			if ("uefi" in wanted) {
372				wanted["uefi=" "'"${HAVE_UEFI}"'"] = 1
373			}
374			if (("man" in wanted) && ("catpages" in wanted))
375				wanted[".cat"] = 1
376			if (("man" in wanted) && ("manpages" in wanted))
377				wanted[".man"] = 1
378			if ("endian" in wanted)
379				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
380			if ("machine" in wanted)
381				wanted["machine=" "'"${MACHINE}"'"] = 1
382			if ("machine_arch" in wanted)
383				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
384			if ("machine_cpu" in wanted)
385				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
386		}
387
388		/^#/ {
389			next;
390		}
391
392		/^-/ {
393			notwanted[substr($1, 2)] = 1;
394			delete list [substr($1, 2)];
395			next;
396		}
397
398
399		NF > 2 && $3 != "-" {
400			if (notwanted[$1] != "")
401				next;
402			split($3, keywords, ",")
403			show = 1
404			haveobs = 0
405			iscompatfile = 0
406			havekmod = 0
407			iscompatdir = 0
408			for (ki in keywords) {
409				kw = keywords[ki]
410				if (("manz" in wanted) &&
411				    (kw == ".cat" || kw == ".man"))
412					$1 = $1 ".gz"
413				if (substr(kw, 1, 1) == "!") {
414					kw = substr(kw, 2)
415					if (kw in wanted)
416						show = 0
417				} else if (kw in compatdirkeywords) {
418					iscompatdir = 1
419				} else if (kw in compatfilekeywords) {
420					iscompatfile = 1
421				} else if (kw == "nocompatmodules") {
422					havekmod = -1
423				} else if (kw in ignoredkeywords) {
424					# ignore
425				} else if (! (kw in wanted)) {
426					show = 0
427				} else if (kw == "kmod" && havekmod == 0) {
428					havekmod = 1
429				}
430				if (kw == "obsolete")
431					haveobs = 1
432			}
433			if (iscompatdir) {
434				for (d in cpaths) {
435					if (cpaths[d] == $1 "/")
436						next
437				}
438				cpaths[ncpaths++] = $1 "/"
439			}
440			if (obsolete && ! haveobs)
441				next
442			if (!show)
443				next
444			if (adddebugkernel($0, $1, debugkernelname, l_debugkernelname))
445				next
446
447			list[$1] = $0
448			if (havekmod > 0) {
449				addkmod($0, $1, kmodprefix, kmodpat, l_kmodpat)
450				addkmod($0, $1, kmoddbprefix, kmoddbpat, l_kmoddbpat)
451				next
452			}
453
454			if (!doingcompat || !(iscompatfile || iscompatdir))
455				next
456
457			if (iscompatfile) {
458				emitcompat[$1] = 1;
459				next
460			}
461			for (d in compatarchdirs) {
462				tmp = $0
463				xfile = $1 "/" compatarchdirs[d]
464				tmp = xfile substr(tmp, length($1) + 1)
465				if (xfile in notwanted)
466					continue;
467				sub("compatdir","compat",tmp);
468				sub("compattestdir","compat",tmp);
469				list[xfile] = tmp
470			}
471			next
472		}
473
474		{
475			if ($1 in notwanted)
476				next;
477			if (! obsolete)
478				list[$1] = $0
479		}
480
481		END {
482			for (i in list) {
483				print list[i]
484				if (! (i in emitcompat))
485					continue;
486				l_i = length(i)
487				l = 0
488				for (j in cpaths) {
489					lx = length(cpaths[j])
490					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
491						continue;
492					}
493					if (lx > l) {
494						l = lx;
495						cpath = cpaths[j];
496					}
497				}
498				for (d in compatarchdirs) {
499					tmp = list[i]
500					extrapath = compatarchdirs[d] "/"
501					xfile = cpath extrapath substr(i, l + 1)
502					if (xfile in notwanted)
503						continue;
504					sub("compatfile","compat",tmp);
505					sub("compattestfile","compat",tmp);
506					tmp = xfile substr(tmp, l_i + 1)
507					print tmp;
508				}
509			}
510		}'
511
512}
513
514#
515# list_set_lists setname
516#
517# Print to stdout a list of files, one filename per line, which
518# concatenate to create the packing list for setname. E.g.,
519#
520# 	.../lists/base/mi
521# 	.../lists/base/rescue.mi
522# 	.../lists/base/md.i386
523#		[...]
524#
525# For a given setname $set, the following files may be selected from
526# .../list/$set:
527#	mi
528#	mi.ext.*
529#	ad.${MACHINE_ARCH}
530# (or)	ad.${MACHINE_CPU}
531#	ad.${MACHINE_CPU}.shl
532#	md.${MACHINE}.${MACHINE_ARCH}
533# (or)	md.${MACHINE}
534#	stl.mi
535#	stl.${stlib}
536#	shl.mi
537#	shl.mi.ext.*
538#	shl.${shlib}
539#	shl.${shlib}.ext.*
540#	module.mi			if ${module} != no
541#	module.${MACHINE}		if ${module} != no
542#	module.ad.${MACHINE_ARCH}	if ${module} != no
543# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
544#	rescue.shl
545#	rescue.${MACHINE}
546#	rescue.ad.${MACHINE_ARCH}
547# (or)	rescue.ad.${MACHINE_CPU}
548# 	rescue.ad.${MACHINE_CPU}.shl
549#
550# Environment:
551# 	shlib
552# 	stlib
553#
554list_set_lists()
555{
556	setname=$1
557
558	list_set_lists_mi $setname
559	list_set_lists_ad $setname
560	list_set_lists_md $setname
561	list_set_lists_stl $setname
562	list_set_lists_shl $setname
563	list_set_lists_module $setname
564	list_set_lists_rescue $setname
565	return 0
566}
567
568list_set_lists_mi()
569{
570	setdir=$setsdir/lists/$1
571	# always exist!
572	echo $setdir/mi
573}
574
575list_set_lists_ad()
576{
577	setdir=$setsdir/lists/$1
578	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
579	list_set_lists_common_ad $1
580}
581
582list_set_lists_md()
583{
584	setdir=$setsdir/lists/$1
585	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
586	echo_if_exist $setdir/md.${MACHINE}
587}
588
589list_set_lists_stl()
590{
591	setdir=$setsdir/lists/$1
592	echo_if_exist $setdir/stl.mi
593	echo_if_exist $setdir/stl.${stlib}
594}
595
596list_set_lists_shl()
597{
598	setdir=$setsdir/lists/$1
599	[ "$shlib" != "no" ] || return
600	echo_if_exist $setdir/shl.mi
601	echo_if_exist $setdir/shl.${shlib}
602}
603
604list_set_lists_module()
605{
606	setdir=$setsdir/lists/$1
607	[ "$module" != "no" ] || return
608	echo_if_exist $setdir/module.mi
609	echo_if_exist $setdir/module.${MACHINE}
610	echo_if_exist $setdir/module.ad.${MACHINE}
611	echo_if_exist $setdir/module.md.${MACHINE}
612	# XXX module never has .shl
613	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
614	list_set_lists_common_ad $1 module
615}
616
617list_set_lists_rescue()
618{
619	setdir=$setsdir/lists/$1
620	echo_if_exist $setdir/rescue.mi
621	echo_if_exist $setdir/rescue.${MACHINE}
622	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
623	list_set_lists_common_ad $1 rescue
624}
625
626list_set_lists_common_ad()
627{
628	setdir=$setsdir/lists/$1; _prefix=$2
629
630	[ -n "$_prefix" ] && prefix="$_prefix".
631
632	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
633	# <prefix>.ad.${MACHINE_CPU}, since the arch-
634	# specific one will be more specific than the
635	# cpu-specific one.
636	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
637	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
638	[ "$shlib" != "no" ] && \
639	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
640}
641
642echo_if_exist()
643{
644	[ -f $1 ] && echo $1
645	return $?
646}
647
648echo_if_exist_foreach()
649{
650	local _list=$1; shift
651	for _suffix in $@; do
652		echo_if_exist ${_list}.${_suffix}
653	done
654}
655
656print_set_lists()
657{
658	for setname; do
659		list=$(list_set_lists $setname)
660		for l in $list; do
661			echo $l
662			if $verbose; then
663				echo >&2 "DEBUG: list_set_files: $l"
664			fi
665		done
666	done | ${XARGS} ${SED} ${SUBST}
667}
668
669
670list_kernel_configs()
671{
672	(cd ${NETBSDSRCDIR}/etc
673	MAKEFLAGS= \
674	${MAKE} -m ${NETBSDSRCDIR}/share/mk -V '${ALL_KERNELS}')
675}
676
677# arch_to_cpu mach
678#
679# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
680# as determined by <bsd.own.mk>.
681#
682arch_to_cpu()
683{
684	MACHINE_ARCH=${1} MAKEFLAGS= \
685	${MAKE} -m ${NETBSDSRCDIR}/share/mk \
686		-f ${NETBSDSRCDIR}/share/mk/bsd.own.mk \
687		-V '${MACHINE_CPU}'
688}
689
690# arch_to_endian mach
691#
692# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
693# as determined by <bsd.endian.mk>.
694#
695arch_to_endian()
696{
697	MACHINE_ARCH=${1} MAKEFLAGS= \
698	${MAKE} -m ${NETBSDSRCDIR}/share/mk \
699		-f ${NETBSDSRCDIR}/share/mk/bsd.endian.mk \
700		-V '${TARGET_ENDIANNESS}'
701}
702
703#####
704
705# print_mkvars
706print_mkvars()
707{
708	for v in $MKVARS; do
709		eval echo $v=\$$v
710	done
711}
712
713# print_set_lists_{base,x,ext}
714# list_set_lists_{base,x,ext}
715# list_set_files_{base,x,ext}
716for func in print_set_lists list_set_lists list_set_files; do
717	for x in base x ext; do
718		if [ $x = base ]; then
719			list=nlists
720		else
721			list=${x}lists
722		fi
723		eval ${func}_${x} \(\) \{ $func \$$list \; \}
724	done
725done
726