xref: /netbsd-src/distrib/sets/sets.subr (revision eceb233b9bd0dfebb902ed73b531ae6964fa3f9b)
1#	$NetBSD: sets.subr,v 1.192 2020/09/12 15:25:41 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_ACPI
20#	HAVE_BINUTILS
21#	HAVE_GCC
22#	HAVE_GDB
23#	HAVE_SSP
24#	HAVE_OPENSSL
25#	HAVE_UEFI
26#	TOOLCHAIN_MISSING
27#	OBJECT_FMT
28# as well as:
29#
30
31#
32# The following variables refer to tools that are used when building sets:
33#
34: ${AWK:=awk}
35: ${CKSUM:=cksum}
36: ${COMM:=comm}
37: ${DATE:=date}
38: ${DB:=db}
39: ${EGREP:=egrep}
40: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
41: ${FGREP:=fgrep}
42: ${FIND:=find}
43: ${GREP:=grep}
44: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
45: ${HOSTNAME_CMD:=hostname}	# ${HOSTNAME} is special to bash(1)
46: ${HOST_SH:=sh}
47: ${IDENT:=ident}
48: ${JOIN:=join}
49: ${LS:=ls}
50: ${MAKE:=make}
51: ${MKTEMP:=mktemp}
52: ${MTREE:=mtree}
53: ${PASTE:=paste}
54: ${PAX:=pax}
55: ${PRINTF:=printf}
56: ${SED:=sed}
57: ${SORT:=sort}
58: ${STAT:=stat}
59: ${TSORT:=tsort}
60: ${UNAME:=uname}
61: ${WC:=wc}
62: ${XARGS:=xargs}
63
64#
65# If printf is a shell builtin command, then we can
66# implement cheaper versions of basename and dirname
67# that do not involve any fork/exec overhead.
68# If printf is not builtin, approximate it using echo,
69# and hope there are no weird file names that cause
70# some versions of echo to do the wrong thing.
71# (Converting to this version of dirname speeded up the
72# syspkgdeps script by an order of magnitude, from 68
73# seconds to 6.3 seconds on one particular host.)
74#
75# Note that naive approximations for dirname
76# using ${foo%/*} do not do the right thing in cases
77# where the result should be "/" or ".".
78#
79case "$(type printf)" in
80*builtin*)
81	basename ()
82	{
83		local bn
84		bn="${1##*/}"
85		bn="${bn%$2}"
86		printf "%s\n" "$bn"
87	}
88	dirname ()
89	{
90		local dn
91		case "$1" in
92		?*/*)	dn="${1%/*}" ;;
93		/*)	dn=/ ;;
94		*)	dn=. ;;
95		esac
96		printf "%s\n" "$dn"
97	}
98	;;
99*)
100	basename ()
101	{
102		local bn
103		bn="${1##*/}"
104		bn="${bn%$2}"
105		echo "$bn"
106	}
107	dirname ()
108	{
109		local dn
110		case "$1" in
111		?*/*)	dn="${1%/*}" ;;
112		/*)	dn=/ ;;
113		*)	dn=. ;;
114		esac
115		echo "$dn"
116	}
117	;;
118esac
119
120#####
121
122oIFS=$IFS
123IFS="
124"
125
126for x in $( MAKEVERBOSE= ${MAKE} -B -f ${rundir}/mkvars.mk mkvars ); do
127	eval export $x
128done
129
130IFS=$oIFS
131
132MKVARS="$( MAKEVERBOSE= ${MAKE} -B -f ${rundir}/mkvars.mk mkvars | ${SED} -e 's,=.*,,' | ${XARGS} )"
133
134#####
135
136setsdir=${rundir}
137obsolete=0
138if [ "${MKKMOD}" = "no" ]; then
139	module=no			# MODULEs are off.
140	modset=""
141else
142	module=yes
143	modset="modules"
144fi
145if [ "${MKATF}" = "no" ]; then
146	testset=""
147else
148	testset="tests"
149fi
150if [ "${MKDEBUG}" = "no" -a "${MKDEBUGLIB}" = "no" ]; then
151	debugset=""
152	xdebugset=""
153else
154	debugset="debug"
155	xdebugset="xdebug"
156fi
157if [ "${MKDTB}" = "no" ]; then
158	dtbset=""
159else
160	dtbset="dtb"
161fi
162# Determine lib type. Do this first so stlib also gets set.
163if [ "${OBJECT_FMT}" = "ELF" ]; then
164	shlib=elf
165else
166	shlib=aout
167fi
168stlib=$shlib
169# Now check for MKPIC or specials and turn off shlib if need be.
170if [ "${MKPIC}" = "no" ]; then
171	shlib=no
172fi
173nlists="base comp $debugset $dtbset etc games man misc $modset rescue $testset text"
174xlists="xbase xcomp $xdebugset xetc xfont xserver"
175extlists="extbase extcomp extetc"
176
177OSRELEASE=$(${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh -k)
178if [ "${KERNEL_DIR}" = "yes" ]; then
179	MODULEDIR="netbsd/modules"
180else
181	MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
182fi
183SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
184SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
185SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
186
187#
188# list_set_files setfile [...]
189#
190# Produce a packing list for setfile(s).
191# In each file, a record consists of a path and a System Package name,
192# separated by whitespace. E.g.,
193#
194# 	# $NetBSD: sets.subr,v 1.192 2020/09/12 15:25:41 jmcneill Exp $
195# 	.			base-sys-root	[keyword[,...]]
196# 	./altroot		base-sys-root
197# 	./bin			base-sys-root
198# 	./bin/[			base-util-root
199# 	./bin/cat		base-util-root
200#		[...]
201#
202# A # in the first column marks a comment.
203#
204# If ${obsolete} != 0, only entries with an "obsolete" keyword will
205# be printed.  All other keywords must be present.
206#
207# The third field is an optional comma separated list of keywords to
208# control if a record is printed; every keyword listed must be enabled
209# for the record to be printed. The list of all avalaible make variables
210# that can be turned on or off can be found by running in this directory:
211#
212#	make -f mkvars.mk mkvarsyesno
213#
214# These MK<NAME> variables can be used as selectors in the sets as <name>.
215#
216# The following extra keywords are also available, listed by:
217#
218#	make -f mkvars.mk mkextravars
219#
220# These are:
221#    1. The HAVE_<name>:
222#	ssp			${HAVE_SSP} != no
223#	libgcc_eh		${HAVE_LIBGCC_EH} != no
224#	acpi			${HAVE_ACPI} != no
225#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
226#	gcc=<n>			<n> = value of ${HAVE_GCC}
227#	gdb=<n>			<n> = value of ${HAVE_GDB}
228#	mesa_ver=<n>		<n> = value of ${HAVE_MESA_VER}
229#	openssl=<n>		<n> = value of ${HAVE_OPENSSL}
230#	uefi			${HAVE_UEFI} != no
231#	xorg_server_ver=<n>	<n> = value of ${HAVE_XORG_SERVER_VER}
232#	xorg_glamor		${HAVE_XORG_GLAMOR} != no
233#
234#    2. The USE_<name>:
235#	use_inet6		${USE_INET6} != no
236#	use_kerberos		${USE_KERBEROS} != no
237#	use_ldap		${USE_LDAP} != no
238#	use_yp			${USE_YP} != no
239#
240#    3. Finally:
241#	dummy			dummy entry (ignored)
242#	obsolete		file is obsolete, and only printed if
243#				${obsolete} != 0
244#
245#	solaris			${MKDTRACE} != no or ${MKZFS} != no or ${MKCTF} != no
246#
247#
248#	endian=<n>		<n> = value of ${TARGET_ENDIANNESS}
249#
250#
251#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
252#				  automatically append ".gz" to the filename
253#
254#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
255#				  automatically append ".gz" to the filename
256#
257list_set_files()
258{
259	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
260		verbose=false
261	else
262		verbose=true
263	fi
264	print_set_lists "$@" | \
265	${AWK} -v obsolete=${obsolete} '
266		function addkmod(line, fname, prefix, pat, patlen) {
267			if (substr(line, 1, patlen) != pat) {
268				return;
269			}
270			for (d in kmodarchdirs) {
271				xd = prefix kmodarchdirs[d]
272				xfile = xd substr(line, patlen + 1)
273				tmp = xd substr(fname, patlen + 1)
274				list[xfile] = tmp;
275			}
276		}
277		BEGIN {
278			if (obsolete)
279				wanted["obsolete"] = 1
280
281			split("'"${MKVARS}"'", needvars)
282			doingcompat = 0
283			doingcompattests = 0
284			ignoredkeywords["compatdir"] = 1
285			ignoredkeywords["compatfile"] = 1
286			ignoredkeywords["compattestdir"] = 1
287			ignoredkeywords["compattestfile"] = 1
288			ignoredkeywords["compatx11dir"] = 1
289			ignoredkeywords["compatx11file"] = 1
290			for (vi in needvars) {
291				nv = needvars[vi]
292				kw = tolower(nv)
293				sub(/^mk/, "", kw)
294				sub(/^have_/, "", kw)
295				sub(/^target_endianness/, "endian", kw)
296				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
297					wanted[kw] = 1
298				}
299			}
300
301			if ("compat" in wanted) {
302				doingcompat = 1;
303				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
304				compatdirkeywords["compatdir"] = 1
305				compatfilekeywords["compatfile"] = 1
306
307				if (wanted["compattests"]) {
308					doingcompattests = 1;
309					compatdirkeywords["compattestdir"] = 1
310					compatfilekeywords["compattestfile"] = 1
311				}
312				if (wanted["compatx11"]) {
313					doingcompatx11 = 1;
314					compatdirkeywords["compatx11dir"] = 1
315					compatfilekeywords["compatx11file"] = 1
316				}
317			}
318
319			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
320				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
321				kmodprefix = "./stand/"
322				kmodpat = kmodprefix ENVIRON["MACHINE"]
323				l_kmodpat = length(kmodpat)
324				kmoddbprefix = "./usr/libdata/debug/stand/"
325				kmoddbpat = kmoddbprefix ENVIRON["MACHINE"]
326				l_kmoddbpat = length(kmoddbpat)
327			}
328
329			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
330				if ("binutils" in wanted)
331					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
332				if ("gcc" in wanted)
333					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
334				if ("gdb" in wanted)
335					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
336			}
337			if ("acpi" in wanted) {
338				wanted["acpi=" "'"${HAVE_ACPI}"'"] = 1
339			}
340			if ("mesa_ver" in wanted) {
341				wanted["mesa_ver=" "'"${HAVE_MESA_VER}"'"] = 1
342			}
343			if ("openssl" in wanted) {
344				wanted["openssl=" "'"${HAVE_OPENSSL}"'"] = 1
345			}
346			if ("xorg_server_ver" in wanted) {
347				wanted["xorg_server_ver=" "'"${HAVE_XORG_SERVER_VER}"'"] = 1
348			}
349			if ("uefi" in wanted) {
350				wanted["uefi=" "'"${HAVE_UEFI}"'"] = 1
351			}
352			if (("man" in wanted) && ("catpages" in wanted))
353				wanted[".cat"] = 1
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