sets.subr revision 1.177
1#	$NetBSD: sets.subr,v 1.177 2016/08/16 01:05:19 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.177 2016/08/16 01:05:19 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 ("'"${MKXORG_SERVER}"'" != "no") {
353				if ("xorg_server_ver" in wanted) {
354					wanted["xorg_server_ver=" "'"${HAVE_XORG_SERVER_VER}"'"] = 1
355				}
356			}
357			if (("man" in wanted) && ("catpages" in wanted))
358				wanted[".cat"] = 1
359			if (("man" in wanted) && ("manpages" in wanted))
360				wanted[".man"] = 1
361			if ("endian" in wanted)
362				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
363			if ("machine" in wanted)
364				wanted["machine=" "'"${MACHINE}"'"] = 1
365			if ("machine_arch" in wanted)
366				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
367			if ("machine_cpu" in wanted)
368				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
369		}
370
371		/^#/ {
372			next;
373		}
374
375		/^-/ {
376			notwanted[substr($1, 2)] = 1;
377			delete list [substr($1, 2)];
378			next;
379		}
380				
381
382		NF > 2 && $3 != "-" {
383			if (notwanted[$1] != "")
384				next;
385			split($3, keywords, ",")
386			show = 1
387			haveobs = 0
388			iscompatfile = 0
389			havekmod = 0
390			iscompatdir = 0
391			for (ki in keywords) {
392				kw = keywords[ki]
393				if (("manz" in wanted) &&
394				    (kw == ".cat" || kw == ".man"))
395					$1 = $1 ".gz"
396				if (substr(kw, 1, 1) == "!") {
397					kw = substr(kw, 2)
398					if (kw in wanted)
399						show = 0
400				} else if (kw in compatdirkeywords) {
401					iscompatdir = 1
402				} else if (kw in compatfilekeywords) {
403					iscompatfile = 1
404				} else if (kw == "nocompatmodules") {
405					havekmod = -1
406				} else if (kw in ignoredkeywords) {
407					# ignore
408				} else if (! (kw in wanted)) {
409					show = 0
410				} else if (kw == "kmod" && havekmod == 0) {
411					havekmod = 1
412				}
413				if (kw == "obsolete")
414					haveobs = 1
415			}
416			if (obsolete && ! haveobs)
417				next
418			if (!show)
419				next
420
421			list[$1] = $0
422			if (havekmod > 0 && substr($1,1,l_kmodpat) == kmodpat) {
423				for (d in kmodarchdirs) {
424					xd = "./stand/" kmodarchdirs[d]
425					xfile = xd substr($1, l_kmodpat+1)
426					tmp = xd substr($0, l_kmodpat+1)
427					list[xfile] = tmp;
428				}
429				next
430			}
431
432			if (!doingcompat || !(iscompatfile || iscompatdir))
433				next
434
435			if (iscompatfile) {
436				emitcompat[$1] = 1;
437				next
438			}
439			for (d in cpaths) {
440				if (cpaths[d] == $1 "/")
441					next
442			}
443			cpaths[ncpaths++] = $1 "/"
444			for (d in compatarchdirs) {
445				tmp = $0
446				xfile = $1 "/" compatarchdirs[d]
447				tmp = xfile substr(tmp, length($1) + 1)
448				if (xfile in notwanted)
449					continue;
450				sub("compatdir","compat",tmp);
451				sub("compattestdir","compat",tmp);
452				list[xfile] = tmp
453			}
454			next
455		}
456
457		{
458			if ($1 in notwanted)
459				next;
460			if (! obsolete)
461				list[$1] = $0
462		}
463
464		END {
465			for (i in list) {
466				print list[i]
467				if (! (i in emitcompat))
468					continue;
469				l_i = length(i)
470				l = 0
471				for (j in cpaths) {
472					lx = length(cpaths[j])
473					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
474						continue;
475					}
476					if (lx > l) {
477						l = lx;
478						cpath = cpaths[j];
479					}
480				}
481				for (d in compatarchdirs) {
482					tmp = list[i]
483					extrapath = compatarchdirs[d] "/"
484					xfile = cpath extrapath substr(i, l + 1)
485					if (xfile in notwanted)
486						continue;
487					sub("compatfile","compat",tmp);
488					sub("compattestfile","compat",tmp);
489					tmp = xfile substr(tmp, l_i + 1)
490					print tmp;
491				}
492			}
493		}'
494
495}
496
497#
498# list_set_lists setname
499# 
500# Print to stdout a list of files, one filename per line, which
501# concatenate to create the packing list for setname. E.g.,
502#
503# 	.../lists/base/mi
504# 	.../lists/base/rescue.mi
505# 	.../lists/base/md.i386
506#		[...]
507#
508# For a given setname $set, the following files may be selected from
509# .../list/$set:
510#	mi
511#	mi.ext.*
512#	ad.${MACHINE_ARCH}
513# (or)	ad.${MACHINE_CPU}
514#	ad.${MACHINE_CPU}.shl
515#	md.${MACHINE}.${MACHINE_ARCH}
516# (or)	md.${MACHINE}
517#	stl.mi
518#	stl.${stlib}
519#	shl.mi
520#	shl.mi.ext.*
521#	shl.${shlib}
522#	shl.${shlib}.ext.*
523#	module.mi			if ${module} != no
524#	module.${MACHINE}		if ${module} != no
525#	module.ad.${MACHINE_ARCH}	if ${module} != no
526# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
527#	rescue.shl
528#	rescue.${MACHINE}
529#	rescue.ad.${MACHINE_ARCH}
530# (or)	rescue.ad.${MACHINE_CPU}
531# 	rescue.ad.${MACHINE_CPU}.shl
532#
533# Environment:
534# 	shlib
535# 	stlib
536#
537list_set_lists()
538{
539	setname=$1
540
541	list_set_lists_mi $setname
542	list_set_lists_ad $setname
543	list_set_lists_md $setname
544	list_set_lists_stl $setname
545	list_set_lists_shl $setname
546	list_set_lists_module $setname
547	list_set_lists_rescue $setname
548	return 0
549}
550
551list_set_lists_mi()
552{
553	setdir=$setsdir/lists/$1
554	# always exist!
555	echo $setdir/mi
556}
557
558list_set_lists_ad()
559{
560	setdir=$setsdir/lists/$1
561	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
562	list_set_lists_common_ad $1
563}
564
565list_set_lists_md()
566{
567	setdir=$setsdir/lists/$1
568	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
569	echo_if_exist $setdir/md.${MACHINE}
570}
571
572list_set_lists_stl()
573{
574	setdir=$setsdir/lists/$1
575	echo_if_exist $setdir/stl.mi
576	echo_if_exist $setdir/stl.${stlib}
577}
578
579list_set_lists_shl()
580{
581	setdir=$setsdir/lists/$1
582	[ "$shlib" != "no" ] || return
583	echo_if_exist $setdir/shl.mi
584	echo_if_exist $setdir/shl.${shlib}
585}
586
587list_set_lists_module()
588{
589	setdir=$setsdir/lists/$1
590	[ "$module" != "no" ] || return
591	echo_if_exist $setdir/module.mi
592	echo_if_exist $setdir/module.${MACHINE}
593	# XXX module never has .shl
594	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
595	list_set_lists_common_ad $1 module
596}
597
598list_set_lists_rescue()
599{
600	setdir=$setsdir/lists/$1
601	echo_if_exist $setdir/rescue.mi
602	echo_if_exist $setdir/rescue.${MACHINE}
603	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
604	list_set_lists_common_ad $1 rescue
605}
606
607list_set_lists_common_ad()
608{
609	setdir=$setsdir/lists/$1; _prefix=$2
610
611	[ -n "$_prefix" ] && prefix="$_prefix".
612
613	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
614	# <prefix>.ad.${MACHINE_CPU}, since the arch-
615	# specific one will be more specific than the
616	# cpu-specific one.
617	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
618	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
619	[ "$shlib" != "no" ] && \
620	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
621}
622
623echo_if_exist()
624{
625	[ -f $1 ] && echo $1
626	return $?
627}
628
629echo_if_exist_foreach()
630{
631	local _list=$1; shift
632	for _suffix in $@; do
633		echo_if_exist ${_list}.${_suffix}
634	done
635}
636
637print_set_lists()
638{
639	for setname; do
640		list=$(list_set_lists $setname)
641		for l in $list; do
642			echo $l
643			if $verbose; then
644				echo >&2 "DEBUG: list_set_files: $l"
645			fi
646		done
647	done | ${XARGS} ${SED} ${SUBST}
648}
649
650# arch_to_cpu mach
651#
652# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
653# as determined by <bsd.own.mk>.
654#
655arch_to_cpu()
656{
657	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
658.include <bsd.own.mk>
659all:
660	@echo \${MACHINE_CPU}
661EOMAKE
662}
663
664# arch_to_endian mach
665#
666# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
667# as determined by <bsd.endian.mk>.
668#
669arch_to_endian()
670{
671	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
672.include <bsd.endian.mk>
673all:
674	@echo \${TARGET_ENDIANNESS}
675EOMAKE
676}
677
678#####
679
680# print_mkvars
681print_mkvars()
682{
683	for v in $MKVARS; do
684		eval echo $v=\$$v
685	done
686}
687
688# print_set_lists_{base,x,ext}
689# list_set_lists_{base,x,ext}
690# list_set_files_{base,x,ext}
691for func in print_set_lists list_set_lists list_set_files; do
692	for x in base x ext; do
693		if [ $x = base ]; then
694			list=nlists
695		else
696			list=${x}lists
697		fi
698		eval ${func}_${x} \(\) \{ $func \$$list \; \}
699	done
700done
701