sets.subr revision 1.176
1#	$NetBSD: sets.subr,v 1.176 2016/05/25 00:14:47 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#	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.176 2016/05/25 00:14:47 christos 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#
276#	use_inet6		${USE_INET6} != no
277#	use_kerberos		${USE_KERBEROS} != no
278#	use_yp			${USE_YP} != no
279#
280#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
281#				  automatically append ".gz" to the filename
282#
283#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
284#				  automatically append ".gz" to the filename
285#
286list_set_files()
287{
288	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
289		verbose=false
290	else
291		verbose=true
292	fi
293	print_set_lists "$@" | \
294	${AWK} -v obsolete=${obsolete} '
295		BEGIN {
296			if (obsolete)
297				wanted["obsolete"] = 1
298		
299			split("'"${MKVARS}"'", needvars)
300			doingcompat = 0
301			doingcompattests = 0
302			ignoredkeywords["compatdir"] = 1
303			ignoredkeywords["compatfile"] = 1
304			ignoredkeywords["compattestdir"] = 1
305			ignoredkeywords["compattestfile"] = 1
306			ignoredkeywords["compatx11dir"] = 1
307			ignoredkeywords["compatx11file"] = 1
308			for (vi in needvars) {
309				nv = needvars[vi]
310				kw = tolower(nv)
311				sub(/^mk/, "", kw)
312				sub(/^have_/, "", kw)
313				sub(/^target_endianness/, "endian", kw)
314				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
315					wanted[kw] = 1 
316				}
317			}
318
319			if ("compat" in wanted) {
320				doingcompat = 1;
321				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
322				compatdirkeywords["compatdir"] = 1
323				compatfilekeywords["compatfile"] = 1
324
325				if (wanted["compattests"]) {
326					doingcompattests = 1;
327					compatdirkeywords["compattestdir"] = 1
328					compatfilekeywords["compattestfile"] = 1
329				}
330				if (wanted["compatx11"]) {
331					doingcompatx11 = 1;
332					compatdirkeywords["compatx11dir"] = 1
333					compatfilekeywords["compatx11file"] = 1
334				}
335			}
336
337			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
338				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
339				kmodpat = "./stand/" ENVIRON["MACHINE"]
340				l_kmodpat = length(kmodpat)
341			}
342
343			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
344				if ("binutils" in wanted)
345					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
346				if ("gcc" in wanted)
347					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
348				if ("gdb" in wanted)
349					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
350			}
351			if (("man" in wanted) && ("catpages" in wanted))
352				wanted[".cat"] = 1
353			if (("man" in wanted) && ("manpages" in wanted))
354				wanted[".man"] = 1
355			if ("endian" in wanted)
356				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
357			if ("machine" in wanted)
358				wanted["machine=" "'"${MACHINE}"'"] = 1
359			if ("machine_arch" in wanted)
360				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
361			if ("machine_cpu" in wanted)
362				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
363		}
364
365		/^#/ {
366			next;
367		}
368
369		/^-/ {
370			notwanted[substr($1, 2)] = 1;
371			delete list [substr($1, 2)];
372			next;
373		}
374				
375
376		NF > 2 && $3 != "-" {
377			if (notwanted[$1] != "")
378				next;
379			split($3, keywords, ",")
380			show = 1
381			haveobs = 0
382			iscompatfile = 0
383			havekmod = 0
384			iscompatdir = 0
385			for (ki in keywords) {
386				kw = keywords[ki]
387				if (("manz" in wanted) &&
388				    (kw == ".cat" || kw == ".man"))
389					$1 = $1 ".gz"
390				if (substr(kw, 1, 1) == "!") {
391					kw = substr(kw, 2)
392					if (kw in wanted)
393						show = 0
394				} else if (kw in compatdirkeywords) {
395					iscompatdir = 1
396				} else if (kw in compatfilekeywords) {
397					iscompatfile = 1
398				} else if (kw == "nocompatmodules") {
399					havekmod = -1
400				} else if (kw in ignoredkeywords) {
401					# ignore
402				} else if (! (kw in wanted)) {
403					show = 0
404				} else if (kw == "kmod" && havekmod == 0) {
405					havekmod = 1
406				}
407				if (kw == "obsolete")
408					haveobs = 1
409			}
410			if (obsolete && ! haveobs)
411				next
412			if (!show)
413				next
414
415			list[$1] = $0
416			if (havekmod > 0 && substr($1,1,l_kmodpat) == kmodpat) {
417				for (d in kmodarchdirs) {
418					xd = "./stand/" kmodarchdirs[d]
419					xfile = xd substr($1, l_kmodpat+1)
420					tmp = xd substr($0, l_kmodpat+1)
421					list[xfile] = tmp;
422				}
423				next
424			}
425
426			if (!doingcompat || !(iscompatfile || iscompatdir))
427				next
428
429			if (iscompatfile) {
430				emitcompat[$1] = 1;
431				next
432			}
433			for (d in cpaths) {
434				if (cpaths[d] == $1 "/")
435					next
436			}
437			cpaths[ncpaths++] = $1 "/"
438			for (d in compatarchdirs) {
439				tmp = $0
440				xfile = $1 "/" compatarchdirs[d]
441				tmp = xfile substr(tmp, length($1) + 1)
442				if (xfile in notwanted)
443					continue;
444				sub("compatdir","compat",tmp);
445				sub("compattestdir","compat",tmp);
446				list[xfile] = tmp
447			}
448			next
449		}
450
451		{
452			if ($1 in notwanted)
453				next;
454			if (! obsolete)
455				list[$1] = $0
456		}
457
458		END {
459			for (i in list) {
460				print list[i]
461				if (! (i in emitcompat))
462					continue;
463				l_i = length(i)
464				l = 0
465				for (j in cpaths) {
466					lx = length(cpaths[j])
467					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
468						continue;
469					}
470					if (lx > l) {
471						l = lx;
472						cpath = cpaths[j];
473					}
474				}
475				for (d in compatarchdirs) {
476					tmp = list[i]
477					extrapath = compatarchdirs[d] "/"
478					xfile = cpath extrapath substr(i, l + 1)
479					if (xfile in notwanted)
480						continue;
481					sub("compatfile","compat",tmp);
482					sub("compattestfile","compat",tmp);
483					tmp = xfile substr(tmp, l_i + 1)
484					print tmp;
485				}
486			}
487		}'
488
489}
490
491#
492# list_set_lists setname
493# 
494# Print to stdout a list of files, one filename per line, which
495# concatenate to create the packing list for setname. E.g.,
496#
497# 	.../lists/base/mi
498# 	.../lists/base/rescue.mi
499# 	.../lists/base/md.i386
500#		[...]
501#
502# For a given setname $set, the following files may be selected from
503# .../list/$set:
504#	mi
505#	mi.ext.*
506#	ad.${MACHINE_ARCH}
507# (or)	ad.${MACHINE_CPU}
508#	ad.${MACHINE_CPU}.shl
509#	md.${MACHINE}.${MACHINE_ARCH}
510# (or)	md.${MACHINE}
511#	stl.mi
512#	stl.${stlib}
513#	shl.mi
514#	shl.mi.ext.*
515#	shl.${shlib}
516#	shl.${shlib}.ext.*
517#	module.mi			if ${module} != no
518#	module.${MACHINE}		if ${module} != no
519#	module.ad.${MACHINE_ARCH}	if ${module} != no
520# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
521#	rescue.shl
522#	rescue.${MACHINE}
523#	rescue.ad.${MACHINE_ARCH}
524# (or)	rescue.ad.${MACHINE_CPU}
525# 	rescue.ad.${MACHINE_CPU}.shl
526#
527# Environment:
528# 	shlib
529# 	stlib
530#
531list_set_lists()
532{
533	setname=$1
534
535	list_set_lists_mi $setname
536	list_set_lists_ad $setname
537	list_set_lists_md $setname
538	list_set_lists_stl $setname
539	list_set_lists_shl $setname
540	list_set_lists_module $setname
541	list_set_lists_rescue $setname
542	return 0
543}
544
545list_set_lists_mi()
546{
547	setdir=$setsdir/lists/$1
548	# always exist!
549	echo $setdir/mi
550}
551
552list_set_lists_ad()
553{
554	setdir=$setsdir/lists/$1
555	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
556	list_set_lists_common_ad $1
557}
558
559list_set_lists_md()
560{
561	setdir=$setsdir/lists/$1
562	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
563	echo_if_exist $setdir/md.${MACHINE}
564}
565
566list_set_lists_stl()
567{
568	setdir=$setsdir/lists/$1
569	echo_if_exist $setdir/stl.mi
570	echo_if_exist $setdir/stl.${stlib}
571}
572
573list_set_lists_shl()
574{
575	setdir=$setsdir/lists/$1
576	[ "$shlib" != "no" ] || return
577	echo_if_exist $setdir/shl.mi
578	echo_if_exist $setdir/shl.${shlib}
579}
580
581list_set_lists_module()
582{
583	setdir=$setsdir/lists/$1
584	[ "$module" != "no" ] || return
585	echo_if_exist $setdir/module.mi
586	echo_if_exist $setdir/module.${MACHINE}
587	# XXX module never has .shl
588	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
589	list_set_lists_common_ad $1 module
590}
591
592list_set_lists_rescue()
593{
594	setdir=$setsdir/lists/$1
595	echo_if_exist $setdir/rescue.mi
596	echo_if_exist $setdir/rescue.${MACHINE}
597	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
598	list_set_lists_common_ad $1 rescue
599}
600
601list_set_lists_common_ad()
602{
603	setdir=$setsdir/lists/$1; _prefix=$2
604
605	[ -n "$_prefix" ] && prefix="$_prefix".
606
607	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
608	# <prefix>.ad.${MACHINE_CPU}, since the arch-
609	# specific one will be more specific than the
610	# cpu-specific one.
611	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
612	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
613	[ "$shlib" != "no" ] && \
614	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
615}
616
617echo_if_exist()
618{
619	[ -f $1 ] && echo $1
620	return $?
621}
622
623echo_if_exist_foreach()
624{
625	local _list=$1; shift
626	for _suffix in $@; do
627		echo_if_exist ${_list}.${_suffix}
628	done
629}
630
631print_set_lists()
632{
633	for setname; do
634		list=$(list_set_lists $setname)
635		for l in $list; do
636			echo $l
637			if $verbose; then
638				echo >&2 "DEBUG: list_set_files: $l"
639			fi
640		done
641	done | ${XARGS} ${SED} ${SUBST}
642}
643
644# arch_to_cpu mach
645#
646# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
647# as determined by <bsd.own.mk>.
648#
649arch_to_cpu()
650{
651	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
652.include <bsd.own.mk>
653all:
654	@echo \${MACHINE_CPU}
655EOMAKE
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} ${MAKE} -B -f- all <<EOMAKE
666.include <bsd.endian.mk>
667all:
668	@echo \${TARGET_ENDIANNESS}
669EOMAKE
670}
671
672#####
673
674# print_mkvars
675print_mkvars()
676{
677	for v in $MKVARS; do
678		eval echo $v=\$$v
679	done
680}
681
682# print_set_lists_{base,x,ext}
683# list_set_lists_{base,x,ext}
684# list_set_files_{base,x,ext}
685for func in print_set_lists list_set_lists list_set_files; do
686	for x in base x ext; do
687		if [ $x = base ]; then
688			list=nlists
689		else
690			list=${x}lists
691		fi
692		eval ${func}_${x} \(\) \{ $func \$$list \; \}
693	done
694done
695