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