sets.subr revision 1.197
1#	$NetBSD: sets.subr,v 1.197 2021/09/25 21:26:03 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 [ "${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.197 2021/09/25 21:26:03 maya 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	print_set_lists "$@" | \
267	${AWK} -v obsolete=${obsolete} '
268		function addkmod(line, fname, prefix, pat, patlen) {
269			if (substr(line, 1, patlen) != pat) {
270				return;
271			}
272			for (d in kmodarchdirs) {
273				xd = prefix kmodarchdirs[d]
274				xfile = xd substr(line, patlen + 1)
275				tmp = xd substr(fname, patlen + 1)
276				list[xfile] = tmp;
277			}
278		}
279		BEGIN {
280			if (obsolete)
281				wanted["obsolete"] = 1
282		
283			split("'"${MKVARS}"'", needvars)
284			doingcompat = 0
285			doingcompattests = 0
286			ignoredkeywords["compatdir"] = 1
287			ignoredkeywords["compatfile"] = 1
288			ignoredkeywords["compattestdir"] = 1
289			ignoredkeywords["compattestfile"] = 1
290			ignoredkeywords["compatx11dir"] = 1
291			ignoredkeywords["compatx11file"] = 1
292			for (vi in needvars) {
293				nv = needvars[vi]
294				kw = tolower(nv)
295				sub(/^mk/, "", kw)
296				sub(/^have_/, "", kw)
297				sub(/^target_endianness/, "endian", kw)
298				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
299					wanted[kw] = 1 
300				}
301			}
302
303			if ("compat" in wanted) {
304				doingcompat = 1;
305				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
306				compatdirkeywords["compatdir"] = 1
307				compatfilekeywords["compatfile"] = 1
308
309				if (wanted["compattests"]) {
310					doingcompattests = 1;
311					compatdirkeywords["compattestdir"] = 1
312					compatfilekeywords["compattestfile"] = 1
313				}
314				if (wanted["compatx11"]) {
315					doingcompatx11 = 1;
316					compatdirkeywords["compatx11dir"] = 1
317					compatfilekeywords["compatx11file"] = 1
318				}
319			}
320
321			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
322				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
323				kmodprefix = "./stand/"
324				kmodpat = kmodprefix ENVIRON["MACHINE"]
325				l_kmodpat = length(kmodpat)
326				kmoddbprefix = "./usr/libdata/debug/stand/" 
327				kmoddbpat = kmoddbprefix ENVIRON["MACHINE"]
328				l_kmoddbpat = length(kmoddbpat)
329			}
330
331			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
332				if ("binutils" in wanted)
333					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
334				if ("gcc" in wanted)
335					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
336				if ("gdb" in wanted)
337					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
338			}
339			if ("acpi" in wanted) {
340				wanted["acpi=" "'"${HAVE_ACPI}"'"] = 1
341			}
342			if ("mesa_ver" in wanted) {
343				wanted["mesa_ver=" "'"${HAVE_MESA_VER}"'"] = 1
344			}
345			if ("nvmm" in wanted) {
346				wanted["nvmm=" "'"${HAVE_NVMM}"'"] = 1
347			}
348			if ("openssl" in wanted) {
349				wanted["openssl=" "'"${HAVE_OPENSSL}"'"] = 1
350			}
351			if ("xorg_server_ver" in wanted) {
352				wanted["xorg_server_ver=" "'"${HAVE_XORG_SERVER_VER}"'"] = 1
353			}
354			if ("uefi" in wanted) {
355				wanted["uefi=" "'"${HAVE_UEFI}"'"] = 1
356			}
357			if (("man" in wanted) && ("catpages" in wanted))
358				wanted[".cat"] = 1
359			if (("man" in wanted) && ("manpages" in wanted))
360				wanted[".man"] = 1
361			if ("endian" in wanted)
362				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
363			if ("machine" in wanted)
364				wanted["machine=" "'"${MACHINE}"'"] = 1
365			if ("machine_arch" in wanted)
366				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
367			if ("machine_cpu" in wanted)
368				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
369		}
370
371		/^#/ {
372			next;
373		}
374
375		/^-/ {
376			notwanted[substr($1, 2)] = 1;
377			delete list [substr($1, 2)];
378			next;
379		}
380				
381
382		NF > 2 && $3 != "-" {
383			if (notwanted[$1] != "")
384				next;
385			split($3, keywords, ",")
386			show = 1
387			haveobs = 0
388			iscompatfile = 0
389			havekmod = 0
390			iscompatdir = 0
391			for (ki in keywords) {
392				kw = keywords[ki]
393				if (("manz" in wanted) &&
394				    (kw == ".cat" || kw == ".man"))
395					$1 = $1 ".gz"
396				if (substr(kw, 1, 1) == "!") {
397					kw = substr(kw, 2)
398					if (kw in wanted)
399						show = 0
400				} else if (kw in compatdirkeywords) {
401					iscompatdir = 1
402				} else if (kw in compatfilekeywords) {
403					iscompatfile = 1
404				} else if (kw == "nocompatmodules") {
405					havekmod = -1
406				} else if (kw in ignoredkeywords) {
407					# ignore
408				} else if (! (kw in wanted)) {
409					show = 0
410				} else if (kw == "kmod" && havekmod == 0) {
411					havekmod = 1
412				}
413				if (kw == "obsolete")
414					haveobs = 1
415			}
416			if (iscompatdir) {
417				for (d in cpaths) {
418					if (cpaths[d] == $1 "/")
419						next
420				}
421				cpaths[ncpaths++] = $1 "/"
422			}
423			if (obsolete && ! haveobs)
424				next
425			if (!show)
426				next
427
428			list[$1] = $0
429			if (havekmod > 0) {
430				addkmod($0, $1, kmodprefix, kmodpat, l_kmodpat)
431				addkmod($0, $1, kmoddbprefix, kmoddbpat, l_kmoddbpat)
432				next
433			}
434
435			if (!doingcompat || !(iscompatfile || iscompatdir))
436				next
437
438			if (iscompatfile) {
439				emitcompat[$1] = 1;
440				next
441			}
442			for (d in compatarchdirs) {
443				tmp = $0
444				xfile = $1 "/" compatarchdirs[d]
445				tmp = xfile substr(tmp, length($1) + 1)
446				if (xfile in notwanted)
447					continue;
448				sub("compatdir","compat",tmp);
449				sub("compattestdir","compat",tmp);
450				list[xfile] = tmp
451			}
452			next
453		}
454
455		{
456			if ($1 in notwanted)
457				next;
458			if (! obsolete)
459				list[$1] = $0
460		}
461
462		END {
463			for (i in list) {
464				print list[i]
465				if (! (i in emitcompat))
466					continue;
467				l_i = length(i)
468				l = 0
469				for (j in cpaths) {
470					lx = length(cpaths[j])
471					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
472						continue;
473					}
474					if (lx > l) {
475						l = lx;
476						cpath = cpaths[j];
477					}
478				}
479				for (d in compatarchdirs) {
480					tmp = list[i]
481					extrapath = compatarchdirs[d] "/"
482					xfile = cpath extrapath substr(i, l + 1)
483					if (xfile in notwanted)
484						continue;
485					sub("compatfile","compat",tmp);
486					sub("compattestfile","compat",tmp);
487					tmp = xfile substr(tmp, l_i + 1)
488					print tmp;
489				}
490			}
491		}'
492
493}
494
495#
496# list_set_lists setname
497# 
498# Print to stdout a list of files, one filename per line, which
499# concatenate to create the packing list for setname. E.g.,
500#
501# 	.../lists/base/mi
502# 	.../lists/base/rescue.mi
503# 	.../lists/base/md.i386
504#		[...]
505#
506# For a given setname $set, the following files may be selected from
507# .../list/$set:
508#	mi
509#	mi.ext.*
510#	ad.${MACHINE_ARCH}
511# (or)	ad.${MACHINE_CPU}
512#	ad.${MACHINE_CPU}.shl
513#	md.${MACHINE}.${MACHINE_ARCH}
514# (or)	md.${MACHINE}
515#	stl.mi
516#	stl.${stlib}
517#	shl.mi
518#	shl.mi.ext.*
519#	shl.${shlib}
520#	shl.${shlib}.ext.*
521#	module.mi			if ${module} != no
522#	module.${MACHINE}		if ${module} != no
523#	module.ad.${MACHINE_ARCH}	if ${module} != no
524# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
525#	rescue.shl
526#	rescue.${MACHINE}
527#	rescue.ad.${MACHINE_ARCH}
528# (or)	rescue.ad.${MACHINE_CPU}
529# 	rescue.ad.${MACHINE_CPU}.shl
530#
531# Environment:
532# 	shlib
533# 	stlib
534#
535list_set_lists()
536{
537	setname=$1
538
539	list_set_lists_mi $setname
540	list_set_lists_ad $setname
541	list_set_lists_md $setname
542	list_set_lists_stl $setname
543	list_set_lists_shl $setname
544	list_set_lists_module $setname
545	list_set_lists_rescue $setname
546	return 0
547}
548
549list_set_lists_mi()
550{
551	setdir=$setsdir/lists/$1
552	# always exist!
553	echo $setdir/mi
554}
555
556list_set_lists_ad()
557{
558	setdir=$setsdir/lists/$1
559	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
560	list_set_lists_common_ad $1
561}
562
563list_set_lists_md()
564{
565	setdir=$setsdir/lists/$1
566	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
567	echo_if_exist $setdir/md.${MACHINE}
568}
569
570list_set_lists_stl()
571{
572	setdir=$setsdir/lists/$1
573	echo_if_exist $setdir/stl.mi
574	echo_if_exist $setdir/stl.${stlib}
575}
576
577list_set_lists_shl()
578{
579	setdir=$setsdir/lists/$1
580	[ "$shlib" != "no" ] || return
581	echo_if_exist $setdir/shl.mi
582	echo_if_exist $setdir/shl.${shlib}
583}
584
585list_set_lists_module()
586{
587	setdir=$setsdir/lists/$1
588	[ "$module" != "no" ] || return
589	echo_if_exist $setdir/module.mi
590	echo_if_exist $setdir/module.${MACHINE}
591	echo_if_exist $setdir/module.ad.${MACHINE}
592	echo_if_exist $setdir/module.md.${MACHINE}
593	# XXX module never has .shl
594	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
595	list_set_lists_common_ad $1 module
596}
597
598list_set_lists_rescue()
599{
600	setdir=$setsdir/lists/$1
601	echo_if_exist $setdir/rescue.mi
602	echo_if_exist $setdir/rescue.${MACHINE}
603	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
604	list_set_lists_common_ad $1 rescue
605}
606
607list_set_lists_common_ad()
608{
609	setdir=$setsdir/lists/$1; _prefix=$2
610
611	[ -n "$_prefix" ] && prefix="$_prefix".
612
613	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
614	# <prefix>.ad.${MACHINE_CPU}, since the arch-
615	# specific one will be more specific than the
616	# cpu-specific one.
617	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
618	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
619	[ "$shlib" != "no" ] && \
620	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
621}
622
623echo_if_exist()
624{
625	[ -f $1 ] && echo $1
626	return $?
627}
628
629echo_if_exist_foreach()
630{
631	local _list=$1; shift
632	for _suffix in $@; do
633		echo_if_exist ${_list}.${_suffix}
634	done
635}
636
637print_set_lists()
638{
639	for setname; do
640		list=$(list_set_lists $setname)
641		for l in $list; do
642			echo $l
643			if $verbose; then
644				echo >&2 "DEBUG: list_set_files: $l"
645			fi
646		done
647	done | ${XARGS} ${SED} ${SUBST}
648}
649
650# arch_to_cpu mach
651#
652# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
653# as determined by <bsd.own.mk>.
654#
655arch_to_cpu()
656{
657	MACHINE_ARCH=${1} MAKEFLAGS= \
658	${MAKE} -m ${NETBSDSRCDIR}/share/mk \
659		-f ${NETBSDSRCDIR}/share/mk/bsd.own.mk \
660		-V '${MACHINE_CPU}'
661}
662
663# arch_to_endian mach
664#
665# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
666# as determined by <bsd.endian.mk>.
667#
668arch_to_endian()
669{
670	MACHINE_ARCH=${1} MAKEFLAGS= \
671	${MAKE} -m ${NETBSDSRCDIR}/share/mk \
672		-f ${NETBSDSRCDIR}/share/mk/bsd.endian.mk \
673		-V '${TARGET_ENDIANNESS}'
674}
675
676#####
677
678# print_mkvars
679print_mkvars()
680{
681	for v in $MKVARS; do
682		eval echo $v=\$$v
683	done
684}
685
686# print_set_lists_{base,x,ext}
687# list_set_lists_{base,x,ext}
688# list_set_files_{base,x,ext}
689for func in print_set_lists list_set_lists list_set_files; do
690	for x in base x ext; do
691		if [ $x = base ]; then
692			list=nlists
693		else
694			list=${x}lists
695		fi
696		eval ${func}_${x} \(\) \{ $func \$$list \; \}
697	done
698done
699