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