xref: /netbsd-src/distrib/sets/sets.subr (revision 9616dacfef448e70e3fbbd865bddf60d54b656c5)
1#	$NetBSD: sets.subr,v 1.178 2016/08/19 10:23:26 mrg 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#	TOOLCHAIN_MISSING
24#	OBJECT_FMT
25# as well as:
26#
27
28#
29# The following variables refer to tools that are used when building sets:
30#
31: ${AWK:=awk}
32: ${CKSUM:=cksum}
33: ${COMM:=comm}
34: ${DATE:=date}
35: ${DB:=db}
36: ${EGREP:=egrep}
37: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
38: ${FGREP:=fgrep}
39: ${FIND:=find}
40: ${GREP:=grep}
41: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
42: ${HOSTNAME_CMD:=hostname}	# ${HOSTNAME} is special to bash(1)
43: ${HOST_SH:=sh}
44: ${IDENT:=ident}
45: ${JOIN:=join}
46: ${LS:=ls}
47: ${MAKE:=make}
48: ${MKTEMP:=mktemp}
49: ${MTREE:=mtree}
50: ${PASTE:=paste}
51: ${PAX:=pax}
52: ${PRINTF:=printf}
53: ${SED:=sed}
54: ${SORT:=sort}
55: ${STAT:=stat}
56: ${TSORT:=tsort}
57: ${UNAME:=uname}
58: ${WC:=wc}
59: ${XARGS:=xargs}
60
61#
62# If printf is a shell builtin command, then we can
63# implement cheaper versions of basename and dirname
64# that do not involve any fork/exec overhead.
65# If printf is not builtin, approximate it using echo,
66# and hope there are no weird file names that cause
67# some versions of echo to do the wrong thing.
68# (Converting to this version of dirname speeded up the
69# syspkgdeps script by an order of magnitude, from 68
70# seconds to 6.3 seconds on one particular host.)
71#
72# Note that naive approximations for dirname
73# using ${foo%/*} do not do the right thing in cases
74# where the result should be "/" or ".".
75#
76case "$(type printf)" in
77*builtin*)
78	basename ()
79	{
80		local bn
81		bn="${1##*/}"
82		bn="${bn%$2}"
83		printf "%s\n" "$bn"
84	}
85	dirname ()
86	{
87		local dn
88		case "$1" in
89		?*/*)	dn="${1%/*}" ;;
90		/*)	dn=/ ;;
91		*)	dn=. ;;
92		esac
93		printf "%s\n" "$dn"
94	}
95	;;
96*)
97	basename ()
98	{
99		local bn
100		bn="${1##*/}"
101		bn="${bn%$2}"
102		echo "$bn"
103	}
104	dirname ()
105	{
106		local dn
107		case "$1" in
108		?*/*)	dn="${1%/*}" ;;
109		/*)	dn=/ ;;
110		*)	dn=. ;;
111		esac
112		echo "$dn"
113	}
114	;;
115esac
116
117#####
118
119oIFS=$IFS
120IFS="
121"
122
123for x in $( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars ); do
124	eval export $x
125done
126
127IFS=$oIFS
128
129MKVARS="$( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars | ${SED} -e 's,=.*,,' | ${XARGS} )"
130
131#####
132
133setsdir=${rundir}
134obsolete=0
135if [ "${MKKMOD}" = "no" ]; then
136	module=no			# MODULEs are off.
137	modset=""
138else
139	module=yes
140	modset="modules"
141fi
142if [ "${MKATF}" = "no" ]; then
143	testset=""
144else
145	testset="tests"
146fi
147if [ "${MKDEBUG}" = "no" -a "${MKDEBUGLIB}" = "no" ]; then
148	debugset=""
149	xdebugset=""
150else
151	debugset="debug"
152	xdebugset="xdebug"
153fi
154# Determine lib type. Do this first so stlib also gets set.
155if [ "${OBJECT_FMT}" = "ELF" ]; then
156	shlib=elf
157else
158	shlib=aout
159fi
160stlib=$shlib
161# Now check for MKPIC or specials and turn off shlib if need be.
162if [ "${MKPIC}" = "no" ]; then
163	shlib=no
164fi
165nlists="base comp $debugset etc games man misc $modset $testset text"
166xlists="xbase xcomp $xdebugset xetc xfont xserver"
167extlists="extbase extcomp extetc"
168
169OSRELEASE=$(${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh -k)
170MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
171SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
172SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
173SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
174
175#
176# list_set_files setfile [...]
177#
178# Produce a packing list for setfile(s).
179# In each file, a record consists of a path and a System Package name,
180# separated by whitespace. E.g.,
181#
182# 	# $NetBSD: sets.subr,v 1.178 2016/08/19 10:23:26 mrg Exp $
183# 	.			base-sys-root	[keyword[,...]]
184# 	./altroot		base-sys-root
185# 	./bin			base-sys-root
186# 	./bin/[			base-util-root
187# 	./bin/cat		base-util-root
188#		[...]
189#
190# A # in the first column marks a comment.
191#
192# If ${obsolete} != 0, only entries with an "obsolete" keyword will
193# be printed.  All other keywords must be present.
194#
195# The third field is an optional comma separated list of keywords to
196# control if a record is printed; every keyword listed must be enabled
197# for the record to be printed.  The following keywords are available:
198#	dummy			dummy entry (ignored)
199#	obsolete		file is obsolete, and only printed if
200#				${obsolete} != 0
201#
202#	atf			${MKATF} != no
203#	bfd			obsolete, use binutils.
204#	binutils		${MKBINUTILS} != no
205#	bsdgrep			${MKBSDGREP} != no
206#	catpages		${MKCATPAGES} != no
207#	compat			${MKCOMPAT} != no
208#	compatmodules		${MKCOMPATMODULES} != no
209#	compattests		${MKCOMPATTESTS} != no
210#	crypto			${MKCRYPTO} != no
211#	crypto_rc5		${MKCRYPTO_RC5} != no
212#	ctf			${MKCTF} != no
213#	cvs			${MKCVS} != no
214#	debug			${MKDEBUG} != no
215#	debuglib		${MKDEBUGLIB} != no
216#	doc			${MKDOC} != no
217#	dtrace			${MKDTRACE} != no
218#	dynamicroot		${MKDYNAMICROOT} != no
219#	extsrc			${MKEXTSRC} != no
220#	gcc			${MKGCC} != no
221#	gcccmds			${MKGCCCMDS} != no
222#	gdb			${MKGDB} != no
223#	hesiod			${MKHESIOD} != no
224#	html			${MKHTML} != no
225#	inet6			${MKINET6} != no
226#	info			${MKINFO} != no
227#	ipfilter		${MKIPFILTER} != no
228#	iscsi			${MKISCSI} != no
229#	kerberos		${MKKERBEROS} != no
230#	kmod			${MKKMOD} != no
231#	kyua			${MKKYUA} != no
232#	ldap			${MKLDAP} != no
233#	lint			${MKLINT} != no
234#	libcxx			${MKLIBCXX} != no
235#	libgcc_eh		${HAVE_LIBGCC_EH} != no
236#	libstdcxx		${MKLIBSTDCXX} != no
237#	lld			${MKLLD} != no
238#	lldb			${MKLLDB} != no
239#	llvm			${MKLLVM} != no
240#	lvm			${MKLVM} != no
241#	makemandb		${MKMAKEMANDB} != no
242#	man			${MKMAN} != no
243#	manpages		${MKMANPAGES} != no
244#	manz			${MKMANZ} != no
245#	mclinker		${MKMCLINKER} != no
246#	mdns			${MKMDNS} != no
247#	nls			${MKNLS} != no
248#	nvi			${MKNVI} != no
249#	pam			${MKPAM} != no
250#	pcc			${MKPCC} != no
251#	pf			${MKPF} != no
252#	pic			${MKPIC} != no
253#	picinstall		${MKPICINSTALL} != no
254#	pigzgzip		${MKPIGZGZIP} != no
255#	postfix			${MKPOSTFIX} != no
256#	profile			${MKPROFILE} != no
257#	perfuse			${MKPERFUSE} != no
258#	rump			${MKRUMP} != no
259#	share			${MKSHARE} != no
260#	skey			${MKSKEY} != no
261#	sljit			${MKSLJIT} != no
262#	softfloat		${MKSOFTFLOAT} != no
263#	solaris			${MKDTRACE} != no or ${MKZFS} != no or ${MKCTF} != no
264#	ssp			${HAVE_SSP} != no
265#	tpm			${MKTPM} != no
266#	xorg			${MKX11} != no
267#	xorg_server		${MKXORG_SERVER} != no
268#	yp			${MKYP} != no
269#	zfs			${MKZFS} != no
270#
271#	endian=<n>		<n> = value of ${TARGET_ENDIANNESS}
272#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
273#	gcc=<n>			<n> = value of ${HAVE_GCC}
274#	gdb=<n>			<n> = value of ${HAVE_GDB}
275#	xorg_server_ver=<n>	<n> = value of ${HAVE_XORG_SERVER_VER}
276#
277#	use_inet6		${USE_INET6} != no
278#	use_kerberos		${USE_KERBEROS} != no
279#	use_yp			${USE_YP} != no
280#
281#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
282#				  automatically append ".gz" to the filename
283#
284#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
285#				  automatically append ".gz" to the filename
286#
287list_set_files()
288{
289	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
290		verbose=false
291	else
292		verbose=true
293	fi
294	print_set_lists "$@" | \
295	${AWK} -v obsolete=${obsolete} '
296		BEGIN {
297			if (obsolete)
298				wanted["obsolete"] = 1
299
300			split("'"${MKVARS}"'", needvars)
301			doingcompat = 0
302			doingcompattests = 0
303			ignoredkeywords["compatdir"] = 1
304			ignoredkeywords["compatfile"] = 1
305			ignoredkeywords["compattestdir"] = 1
306			ignoredkeywords["compattestfile"] = 1
307			ignoredkeywords["compatx11dir"] = 1
308			ignoredkeywords["compatx11file"] = 1
309			for (vi in needvars) {
310				nv = needvars[vi]
311				kw = tolower(nv)
312				sub(/^mk/, "", kw)
313				sub(/^have_/, "", kw)
314				sub(/^target_endianness/, "endian", kw)
315				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
316					wanted[kw] = 1
317				}
318			}
319
320			if ("compat" in wanted) {
321				doingcompat = 1;
322				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
323				compatdirkeywords["compatdir"] = 1
324				compatfilekeywords["compatfile"] = 1
325
326				if (wanted["compattests"]) {
327					doingcompattests = 1;
328					compatdirkeywords["compattestdir"] = 1
329					compatfilekeywords["compattestfile"] = 1
330				}
331				if (wanted["compatx11"]) {
332					doingcompatx11 = 1;
333					compatdirkeywords["compatx11dir"] = 1
334					compatfilekeywords["compatx11file"] = 1
335				}
336			}
337
338			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
339				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
340				kmodpat = "./stand/" ENVIRON["MACHINE"]
341				l_kmodpat = length(kmodpat)
342			}
343
344			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
345				if ("binutils" in wanted)
346					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
347				if ("gcc" in wanted)
348					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
349				if ("gdb" in wanted)
350					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
351			}
352			if ("xorg_server_ver" in wanted) {
353				wanted["xorg_server_ver=" "'"${HAVE_XORG_SERVER_VER}"'"] = 1
354			}
355			if (("man" in wanted) && ("catpages" in wanted))
356				wanted[".cat"] = 1
357			if (("man" in wanted) && ("manpages" in wanted))
358				wanted[".man"] = 1
359			if ("endian" in wanted)
360				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
361			if ("machine" in wanted)
362				wanted["machine=" "'"${MACHINE}"'"] = 1
363			if ("machine_arch" in wanted)
364				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
365			if ("machine_cpu" in wanted)
366				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
367		}
368
369		/^#/ {
370			next;
371		}
372
373		/^-/ {
374			notwanted[substr($1, 2)] = 1;
375			delete list [substr($1, 2)];
376			next;
377		}
378
379
380		NF > 2 && $3 != "-" {
381			if (notwanted[$1] != "")
382				next;
383			split($3, keywords, ",")
384			show = 1
385			haveobs = 0
386			iscompatfile = 0
387			havekmod = 0
388			iscompatdir = 0
389			for (ki in keywords) {
390				kw = keywords[ki]
391				if (("manz" in wanted) &&
392				    (kw == ".cat" || kw == ".man"))
393					$1 = $1 ".gz"
394				if (substr(kw, 1, 1) == "!") {
395					kw = substr(kw, 2)
396					if (kw in wanted)
397						show = 0
398				} else if (kw in compatdirkeywords) {
399					iscompatdir = 1
400				} else if (kw in compatfilekeywords) {
401					iscompatfile = 1
402				} else if (kw == "nocompatmodules") {
403					havekmod = -1
404				} else if (kw in ignoredkeywords) {
405					# ignore
406				} else if (! (kw in wanted)) {
407					show = 0
408				} else if (kw == "kmod" && havekmod == 0) {
409					havekmod = 1
410				}
411				if (kw == "obsolete")
412					haveobs = 1
413			}
414			if (obsolete && ! haveobs)
415				next
416			if (!show)
417				next
418
419			list[$1] = $0
420			if (havekmod > 0 && substr($1,1,l_kmodpat) == kmodpat) {
421				for (d in kmodarchdirs) {
422					xd = "./stand/" kmodarchdirs[d]
423					xfile = xd substr($1, l_kmodpat+1)
424					tmp = xd substr($0, l_kmodpat+1)
425					list[xfile] = tmp;
426				}
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 cpaths) {
438				if (cpaths[d] == $1 "/")
439					next
440			}
441			cpaths[ncpaths++] = $1 "/"
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	# XXX module never has .shl
592	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
593	list_set_lists_common_ad $1 module
594}
595
596list_set_lists_rescue()
597{
598	setdir=$setsdir/lists/$1
599	echo_if_exist $setdir/rescue.mi
600	echo_if_exist $setdir/rescue.${MACHINE}
601	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
602	list_set_lists_common_ad $1 rescue
603}
604
605list_set_lists_common_ad()
606{
607	setdir=$setsdir/lists/$1; _prefix=$2
608
609	[ -n "$_prefix" ] && prefix="$_prefix".
610
611	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
612	# <prefix>.ad.${MACHINE_CPU}, since the arch-
613	# specific one will be more specific than the
614	# cpu-specific one.
615	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
616	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
617	[ "$shlib" != "no" ] && \
618	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
619}
620
621echo_if_exist()
622{
623	[ -f $1 ] && echo $1
624	return $?
625}
626
627echo_if_exist_foreach()
628{
629	local _list=$1; shift
630	for _suffix in $@; do
631		echo_if_exist ${_list}.${_suffix}
632	done
633}
634
635print_set_lists()
636{
637	for setname; do
638		list=$(list_set_lists $setname)
639		for l in $list; do
640			echo $l
641			if $verbose; then
642				echo >&2 "DEBUG: list_set_files: $l"
643			fi
644		done
645	done | ${XARGS} ${SED} ${SUBST}
646}
647
648# arch_to_cpu mach
649#
650# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
651# as determined by <bsd.own.mk>.
652#
653arch_to_cpu()
654{
655	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
656.include <bsd.own.mk>
657all:
658	@echo \${MACHINE_CPU}
659EOMAKE
660}
661
662# arch_to_endian mach
663#
664# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
665# as determined by <bsd.endian.mk>.
666#
667arch_to_endian()
668{
669	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
670.include <bsd.endian.mk>
671all:
672	@echo \${TARGET_ENDIANNESS}
673EOMAKE
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