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