sets.subr revision 1.167
1#	$NetBSD: sets.subr,v 1.167 2015/06/23 00:58:16 matt 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" ]; 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.167 2015/06/23 00:58:16 matt 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#	cvs			${MKCVS} != no
213#	debug			${MKDEBUG} != no
214#	debuglib		${MKDEBUGLIB} != no
215#	doc			${MKDOC} != no
216#	dtrace			${MKDTRACE} != no
217#	dynamicroot		${MKDYNAMICROOT} != no
218#	extsrc			${MKEXTSRC} != no
219#	gcc			${MKGCC} != no
220#	gcccmds			${MKGCCCMDS} != no
221#	gdb			${MKGDB} != no
222#	hesiod			${MKHESIOD} != no
223#	html			${MKHTML} != no
224#	inet6			${MKINET6} != no
225#	info			${MKINFO} != no
226#	ipfilter		${MKIPFILTER} != no
227#	iscsi			${MKISCSI} != no
228#	kerberos		${MKKERBEROS} != no
229#	kmod			${MKKMOD} != no
230#	kyua			${MKKYUA} != no
231#	ldap			${MKLDAP} != no
232#	lint			${MKLINT} != no
233#	libcxx			${MKLIBCXX} != no
234#	libgcc_eh		${HAVE_LIBGCC_EH} != no
235#	libstdcxx		${MKLIBSTDCXX} != no
236#	lld			${MKLLD} != no
237#	lldb			${MKLLDB} != no
238#	llvm			${MKLLVM} != no
239#	lvm			${MKLVM} != no
240#	makemandb		${MKMAKEMANDB} != no
241#	man			${MKMAN} != no
242#	manpages		${MKMANPAGES} != no
243#	manz			${MKMANZ} != no
244#	mclinker		${MKMCLINKER} != no
245#	mdns			${MKMDNS} != no
246#	nls			${MKNLS} != no
247#	nvi			${MKNVI} != no
248#	pam			${MKPAM} != no
249#	pcc			${MKPCC} != no
250#	pf			${MKPF} != no
251#	pic			${MKPIC} != no
252#	picinstall		${MKPICINSTALL} != no
253#	postfix			${MKPOSTFIX} != no
254#	profile			${MKPROFILE} != no
255#	perfuse			${MKPERFUSE} != no
256#	rump			${MKRUMP} != no
257#	share			${MKSHARE} != no
258#	skey			${MKSKEY} != no
259#	sljit			${MKSLJIT} != no
260#	softfloat		${MKSOFTFLOAT} != no
261#	solaris			${MKDTRACE} != no or ${MKZFS} != no
262#	ssp			${HAVE_SSP} != no
263#	tpm			${MKTPM} != no
264#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
265#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
266#	xorg_server		${MKXORG_SERVER} != no
267#	yp			${MKYP} != no
268#	zfs			${MKZFS} != no
269#
270#	endian=<n>		<n> = value of ${TARGET_ENDIANNESS}
271#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
272#	gcc=<n>			<n> = value of ${HAVE_GCC}
273#	gdb=<n>			<n> = value of ${HAVE_GDB}
274#
275#	use_inet6		${USE_INET6} != no
276#	use_kerberos		${USE_KERBEROS} != no
277#	use_yp			${USE_YP} != no
278#
279#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
280#				  automatically append ".gz" to the filename
281#
282#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
283#				  automatically append ".gz" to the filename
284#
285list_set_files()
286{
287	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
288		verbose=false
289	else
290		verbose=true
291	fi
292	print_set_lists "$@" | \
293	${AWK} -v obsolete=${obsolete} '
294		BEGIN {
295			if (obsolete)
296				wanted["obsolete"] = 1
297		
298			split("'"${MKVARS}"'", needvars)
299			doingcompat = 0
300			doingcompattests = 0
301			ignoredkeywords["compatdir"] = 1
302			ignoredkeywords["compatfile"] = 1
303			ignoredkeywords["compattestdir"] = 1
304			ignoredkeywords["compattestfile"] = 1
305			for (vi in needvars) {
306				nv = needvars[vi]
307				kw = tolower(nv)
308				sub(/^mk/, "", kw)
309				sub(/^have_/, "", kw)
310				sub(/^target_endianness/, "endian", kw)
311				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
312					wanted[kw] = 1 
313				}
314			}
315
316			if ("compat" in wanted) {
317				doingcompat = 1;
318				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
319				compatdirkeywords["compatdir"] = 1
320				compatfilekeywords["compatfile"] = 1
321
322				if (wanted["compattests"]) {
323					doingcompattests = 1;
324					compatdirkeywords["compattestdir"] = 1
325					compatfilekeywords["compattestfile"] = 1
326				}
327			}
328
329			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
330				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
331				kmodpat = "./stand/" ENVIRON["MACHINE"]
332				l_kmodpat = length(kmodpat)
333			}
334
335			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
336				if ("binutils" in wanted)
337					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
338				if ("gcc" in wanted)
339					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
340				if ("gdb" in wanted)
341					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
342			}
343			if (("man" in wanted) && ("catpages" in wanted))
344				wanted[".cat"] = 1
345			if (("man" in wanted) && ("manpages" in wanted))
346				wanted[".man"] = 1
347			if ("endian" in wanted)
348				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
349			if ("machine" in wanted)
350				wanted["machine=" "'"${MACHINE}"'"] = 1
351			if ("machine_arch" in wanted)
352				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
353			if ("machine_cpu" in wanted)
354				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
355		}
356
357		/^#/ {
358			next;
359		}
360
361		/^-/ {
362			notwanted[substr($1, 2)] = 1;
363			delete list [substr($1, 2)];
364			next;
365		}
366				
367
368		NF > 2 && $3 != "-" {
369			if (notwanted[$1] != "")
370				next;
371			split($3, keywords, ",")
372			show = 1
373			haveobs = 0
374			iscompatfile = 0
375			havekmod = 0
376			iscompatdir = 0
377			for (ki in keywords) {
378				kw = keywords[ki]
379				if (("manz" in wanted) &&
380				    (kw == ".cat" || kw == ".man"))
381					$1 = $1 ".gz"
382				if (substr(kw, 1, 1) == "!") {
383					kw = substr(kw, 2)
384					if (kw in wanted)
385						show = 0
386				} else if (kw in compatdirkeywords) {
387					iscompatdir = 1
388				} else if (kw in compatfilekeywords) {
389					iscompatfile = 1
390				} else if (kw in ignoredkeywords) {
391					# ignore
392				} else if (! (kw in wanted)) {
393					show = 0
394				} else if (kw == "kmod") {
395					havekmod = 1
396				}
397				if (kw == "obsolete")
398					haveobs = 1
399			}
400			if (obsolete && ! haveobs)
401				next
402			if (!show)
403				next
404
405			list[$1] = $0
406			if (havekmod && substr($1,1,l_kmodpat) == kmodpat) {
407				for (d in kmodarchdirs) {
408					xd = "./stand/" kmodarchdirs[d]
409					xfile = xd substr($1, l_kmodpat+1)
410					tmp = xd substr($0, l_kmodpat+1)
411					list[xfile] = tmp;
412				}
413				next
414			}
415
416			if (!doingcompat || !(iscompatfile || iscompatdir))
417				next
418
419			if (iscompatfile) {
420				emitcompat[$1] = 1;
421				next;
422			}
423			cpaths[ncpaths++] = $1 "/"
424			for (d in compatarchdirs) {
425				tmp = $0
426				xfile = $1 "/" compatarchdirs[d]
427				tmp = xfile substr(tmp, length($1) + 1)
428				if (xfile in notwanted)
429					continue;
430				sub("compatdir","compat",tmp);
431				sub("compattestdir","compat",tmp);
432				list[xfile] = tmp
433			}
434			next
435		}
436
437		{
438			if ($1 in notwanted)
439				next;
440			if (! obsolete)
441				list[$1] = $0
442		}
443
444		END {
445			for (i in list) {
446				print list[i]
447				if (! (i in emitcompat))
448					continue;
449				l_i = length(i)
450				l = 0
451				for (j in cpaths) {
452					lx = length(cpaths[j])
453					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
454						continue;
455					}
456					if (lx > l) {
457						l = lx;
458						cpath = cpaths[j];
459					}
460				}
461				for (d in compatarchdirs) {
462					tmp = list[i]
463					extrapath = compatarchdirs[d] "/"
464					xfile = cpath extrapath substr(i, l + 1)
465					if (xfile in notwanted)
466						continue;
467					sub("compatfile","compat",tmp);
468					sub("compattestfile","compat",tmp);
469					tmp = xfile substr(tmp, l_i + 1)
470					print tmp;
471				}
472			}
473		}'
474
475}
476
477#
478# list_set_lists setname
479# 
480# Print to stdout a list of files, one filename per line, which
481# concatenate to create the packing list for setname. E.g.,
482#
483# 	.../lists/base/mi
484# 	.../lists/base/rescue.mi
485# 	.../lists/base/md.i386
486#		[...]
487#
488# For a given setname $set, the following files may be selected from
489# .../list/$set:
490#	mi
491#	mi.ext.*
492#	ad.${MACHINE_ARCH}
493# (or)	ad.${MACHINE_CPU}
494#	ad.${MACHINE_CPU}.shl
495#	md.${MACHINE}.${MACHINE_ARCH}
496# (or)	md.${MACHINE}
497#	stl.mi
498#	stl.${stlib}
499#	shl.mi
500#	shl.mi.ext.*
501#	shl.${shlib}
502#	shl.${shlib}.ext.*
503#	module.mi			if ${module} != no
504#	module.${MACHINE}		if ${module} != no
505#	module.ad.${MACHINE_ARCH}	if ${module} != no
506# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
507#	rescue.shl
508#	rescue.${MACHINE}
509#	rescue.ad.${MACHINE_ARCH}
510# (or)	rescue.ad.${MACHINE_CPU}
511# 	rescue.ad.${MACHINE_CPU}.shl
512#
513# Environment:
514# 	shlib
515# 	stlib
516#
517list_set_lists()
518{
519	setname=$1
520
521	list_set_lists_mi $setname
522	list_set_lists_ad $setname
523	list_set_lists_md $setname
524	list_set_lists_stl $setname
525	list_set_lists_shl $setname
526	list_set_lists_module $setname
527	list_set_lists_rescue $setname
528	return 0
529}
530
531list_set_lists_mi()
532{
533	setdir=$setsdir/lists/$1
534	# always exist!
535	echo $setdir/mi
536}
537
538list_set_lists_ad()
539{
540	setdir=$setsdir/lists/$1
541	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
542	list_set_lists_common_ad $1
543}
544
545list_set_lists_md()
546{
547	setdir=$setsdir/lists/$1
548	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
549	echo_if_exist $setdir/md.${MACHINE}
550}
551
552list_set_lists_stl()
553{
554	setdir=$setsdir/lists/$1
555	echo_if_exist $setdir/stl.mi
556	echo_if_exist $setdir/stl.${stlib}
557}
558
559list_set_lists_shl()
560{
561	setdir=$setsdir/lists/$1
562	[ "$shlib" != "no" ] || return
563	echo_if_exist $setdir/shl.mi
564	echo_if_exist $setdir/shl.${shlib}
565}
566
567list_set_lists_module()
568{
569	setdir=$setsdir/lists/$1
570	[ "$module" != "no" ] || return
571	echo_if_exist $setdir/module.mi
572	echo_if_exist $setdir/module.${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} ${MAKE} -B -f- all <<EOMAKE
638.include <bsd.own.mk>
639all:
640	@echo \${MACHINE_CPU}
641EOMAKE
642}
643
644# arch_to_endian mach
645#
646# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
647# as determined by <bsd.endian.mk>.
648#
649arch_to_endian()
650{
651	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
652.include <bsd.endian.mk>
653all:
654	@echo \${TARGET_ENDIANNESS}
655EOMAKE
656}
657
658#####
659
660# print_mkvars
661print_mkvars()
662{
663	for v in $MKVARS; do
664		eval echo $v=\$$v
665	done
666}
667
668# print_set_lists_{base,x,ext}
669# list_set_lists_{base,x,ext}
670# list_set_files_{base,x,ext}
671for func in print_set_lists list_set_lists list_set_files; do
672	for x in base x ext; do
673		if [ $x = base ]; then
674			list=nlists
675		else
676			list=${x}lists
677		fi
678		eval ${func}_${x} \(\) \{ $func \$$list \; \}
679	done
680done
681