sets.subr revision 1.171
1#	$NetBSD: sets.subr,v 1.171 2015/06/28 23:49:33 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" -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.171 2015/06/28 23:49:33 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#	pigzgzip		${MKPIGZGZIP} != no
254#	postfix			${MKPOSTFIX} != no
255#	profile			${MKPROFILE} != no
256#	perfuse			${MKPERFUSE} != no
257#	rump			${MKRUMP} != no
258#	share			${MKSHARE} != no
259#	skey			${MKSKEY} != no
260#	sljit			${MKSLJIT} != no
261#	softfloat		${MKSOFTFLOAT} != no
262#	solaris			${MKDTRACE} != no or ${MKZFS} != no
263#	ssp			${HAVE_SSP} != no
264#	tpm			${MKTPM} != no
265#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
266#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
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 in ignoredkeywords) {
399					# ignore
400				} else if (! (kw in wanted)) {
401					show = 0
402				} else if (kw == "kmod") {
403					havekmod = 1
404				}
405				if (kw == "obsolete")
406					haveobs = 1
407			}
408			if (obsolete && ! haveobs)
409				next
410			if (!show)
411				next
412
413			list[$1] = $0
414			if (havekmod && substr($1,1,l_kmodpat) == kmodpat) {
415				for (d in kmodarchdirs) {
416					xd = "./stand/" kmodarchdirs[d]
417					xfile = xd substr($1, l_kmodpat+1)
418					tmp = xd substr($0, l_kmodpat+1)
419					list[xfile] = tmp;
420				}
421				next
422			}
423
424			if (!doingcompat || !(iscompatfile || iscompatdir))
425				next
426
427			if (iscompatfile) {
428				emitcompat[$1] = 1;
429				next
430			}
431			for (d in cpaths) {
432				if (cpaths[d] == $1 "/")
433					next
434			}
435			cpaths[ncpaths++] = $1 "/"
436			for (d in compatarchdirs) {
437				tmp = $0
438				xfile = $1 "/" compatarchdirs[d]
439				tmp = xfile substr(tmp, length($1) + 1)
440				if (xfile in notwanted)
441					continue;
442				sub("compatdir","compat",tmp);
443				sub("compattestdir","compat",tmp);
444				list[xfile] = tmp
445			}
446			next
447		}
448
449		{
450			if ($1 in notwanted)
451				next;
452			if (! obsolete)
453				list[$1] = $0
454		}
455
456		END {
457			for (i in list) {
458				print list[i]
459				if (! (i in emitcompat))
460					continue;
461				l_i = length(i)
462				l = 0
463				for (j in cpaths) {
464					lx = length(cpaths[j])
465					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
466						continue;
467					}
468					if (lx > l) {
469						l = lx;
470						cpath = cpaths[j];
471					}
472				}
473				for (d in compatarchdirs) {
474					tmp = list[i]
475					extrapath = compatarchdirs[d] "/"
476					xfile = cpath extrapath substr(i, l + 1)
477					if (xfile in notwanted)
478						continue;
479					sub("compatfile","compat",tmp);
480					sub("compattestfile","compat",tmp);
481					tmp = xfile substr(tmp, l_i + 1)
482					print tmp;
483				}
484			}
485		}'
486
487}
488
489#
490# list_set_lists setname
491# 
492# Print to stdout a list of files, one filename per line, which
493# concatenate to create the packing list for setname. E.g.,
494#
495# 	.../lists/base/mi
496# 	.../lists/base/rescue.mi
497# 	.../lists/base/md.i386
498#		[...]
499#
500# For a given setname $set, the following files may be selected from
501# .../list/$set:
502#	mi
503#	mi.ext.*
504#	ad.${MACHINE_ARCH}
505# (or)	ad.${MACHINE_CPU}
506#	ad.${MACHINE_CPU}.shl
507#	md.${MACHINE}.${MACHINE_ARCH}
508# (or)	md.${MACHINE}
509#	stl.mi
510#	stl.${stlib}
511#	shl.mi
512#	shl.mi.ext.*
513#	shl.${shlib}
514#	shl.${shlib}.ext.*
515#	module.mi			if ${module} != no
516#	module.${MACHINE}		if ${module} != no
517#	module.ad.${MACHINE_ARCH}	if ${module} != no
518# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
519#	rescue.shl
520#	rescue.${MACHINE}
521#	rescue.ad.${MACHINE_ARCH}
522# (or)	rescue.ad.${MACHINE_CPU}
523# 	rescue.ad.${MACHINE_CPU}.shl
524#
525# Environment:
526# 	shlib
527# 	stlib
528#
529list_set_lists()
530{
531	setname=$1
532
533	list_set_lists_mi $setname
534	list_set_lists_ad $setname
535	list_set_lists_md $setname
536	list_set_lists_stl $setname
537	list_set_lists_shl $setname
538	list_set_lists_module $setname
539	list_set_lists_rescue $setname
540	return 0
541}
542
543list_set_lists_mi()
544{
545	setdir=$setsdir/lists/$1
546	# always exist!
547	echo $setdir/mi
548}
549
550list_set_lists_ad()
551{
552	setdir=$setsdir/lists/$1
553	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
554	list_set_lists_common_ad $1
555}
556
557list_set_lists_md()
558{
559	setdir=$setsdir/lists/$1
560	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
561	echo_if_exist $setdir/md.${MACHINE}
562}
563
564list_set_lists_stl()
565{
566	setdir=$setsdir/lists/$1
567	echo_if_exist $setdir/stl.mi
568	echo_if_exist $setdir/stl.${stlib}
569}
570
571list_set_lists_shl()
572{
573	setdir=$setsdir/lists/$1
574	[ "$shlib" != "no" ] || return
575	echo_if_exist $setdir/shl.mi
576	echo_if_exist $setdir/shl.${shlib}
577}
578
579list_set_lists_module()
580{
581	setdir=$setsdir/lists/$1
582	[ "$module" != "no" ] || return
583	echo_if_exist $setdir/module.mi
584	echo_if_exist $setdir/module.${MACHINE}
585	# XXX module never has .shl
586	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
587	list_set_lists_common_ad $1 module
588}
589
590list_set_lists_rescue()
591{
592	setdir=$setsdir/lists/$1
593	echo_if_exist $setdir/rescue.mi
594	echo_if_exist $setdir/rescue.${MACHINE}
595	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
596	list_set_lists_common_ad $1 rescue
597}
598
599list_set_lists_common_ad()
600{
601	setdir=$setsdir/lists/$1; _prefix=$2
602
603	[ -n "$_prefix" ] && prefix="$_prefix".
604
605	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
606	# <prefix>.ad.${MACHINE_CPU}, since the arch-
607	# specific one will be more specific than the
608	# cpu-specific one.
609	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
610	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
611	[ "$shlib" != "no" ] && \
612	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
613}
614
615echo_if_exist()
616{
617	[ -f $1 ] && echo $1
618	return $?
619}
620
621echo_if_exist_foreach()
622{
623	local _list=$1; shift
624	for _suffix in $@; do
625		echo_if_exist ${_list}.${_suffix}
626	done
627}
628
629print_set_lists()
630{
631	for setname; do
632		list=$(list_set_lists $setname)
633		for l in $list; do
634			echo $l
635			if $verbose; then
636				echo >&2 "DEBUG: list_set_files: $l"
637			fi
638		done
639	done | ${XARGS} ${SED} ${SUBST}
640}
641
642# arch_to_cpu mach
643#
644# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
645# as determined by <bsd.own.mk>.
646#
647arch_to_cpu()
648{
649	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
650.include <bsd.own.mk>
651all:
652	@echo \${MACHINE_CPU}
653EOMAKE
654}
655
656# arch_to_endian mach
657#
658# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
659# as determined by <bsd.endian.mk>.
660#
661arch_to_endian()
662{
663	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
664.include <bsd.endian.mk>
665all:
666	@echo \${TARGET_ENDIANNESS}
667EOMAKE
668}
669
670#####
671
672# print_mkvars
673print_mkvars()
674{
675	for v in $MKVARS; do
676		eval echo $v=\$$v
677	done
678}
679
680# print_set_lists_{base,x,ext}
681# list_set_lists_{base,x,ext}
682# list_set_files_{base,x,ext}
683for func in print_set_lists list_set_lists list_set_files; do
684	for x in base x ext; do
685		if [ $x = base ]; then
686			list=nlists
687		else
688			list=${x}lists
689		fi
690		eval ${func}_${x} \(\) \{ $func \$$list \; \}
691	done
692done
693