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