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