sets.subr revision 1.114
1#	$NetBSD: sets.subr,v 1.114 2009/12/11 15:22:33 he 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#	TOOLCHAIN_MISSING
23#	OBJECT_FMT
24# as well as:
25#
26
27#
28# The following variables refer to tools that are used when building sets:
29#
30: ${AWK:=awk}
31: ${CKSUM:=cksum}
32: ${COMM:=comm}
33: ${DATE:=date}
34: ${DB:=db}
35: ${EGREP:=egrep}
36: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
37: ${FGREP:=fgrep}
38: ${FIND:=find}
39: ${GREP:=grep}
40: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
41: ${HOSTNAME_CMD:=hostname}	# ${HOSTNAME} is special to bash(1)
42: ${HOST_SH:=sh}
43: ${IDENT:=ident}
44: ${JOIN:=join}
45: ${LS:=ls}
46: ${MAKE:=make}
47: ${MKTEMP:=mktemp}
48: ${MTREE:=mtree}
49: ${PASTE:=paste}
50: ${PAX:=pax}
51: ${PRINTF:=printf}
52: ${SED:=sed}
53: ${SORT:=sort}
54: ${STAT:=stat}
55: ${TSORT:=tsort}
56: ${UNAME:=uname}
57: ${WC:=wc}
58
59#
60# If printf is a shell builtin command, then we can
61# implement cheaper versions of basename and dirname
62# that do not involve any fork/exec overhead.
63# If printf is not builtin, approximate it using echo,
64# and hope there are no weird file names that cause
65# some versions of echo to do the wrong thing.
66# (Converting to this version of dirname speeded up the
67# syspkgdeps script by an order of magnitude, from 68
68# seconds to 6.3 seconds on one particular host.)
69#
70# Note that naive approximations for dirname
71# using ${foo%/*} do not do the right thing in cases
72# where the result should be "/" or ".".
73#
74case "$(type printf)" in
75*builtin*)
76	basename ()
77	{
78		local bn
79		bn="${1##*/}"
80		bn="${bn%$2}"
81		printf "%s\n" "$bn"
82	}
83	dirname ()
84	{
85		local dn
86		case "$1" in
87		?*/*)	dn="${1%/*}" ;;
88		/*)	dn=/ ;;
89		*)	dn=. ;;
90		esac
91		printf "%s\n" "$dn"
92	}
93	;;
94*)
95	basename ()
96	{
97		local bn
98		bn="${1##*/}"
99		bn="${bn%$2}"
100		echo "$bn"
101	}
102	dirname ()
103	{
104		local dn
105		case "$1" in
106		?*/*)	dn="${1%/*}" ;;
107		/*)	dn=/ ;;
108		*)	dn=. ;;
109		esac
110		echo "$dn"
111	}
112	;;
113esac
114
115#####
116
117oIFS=$IFS
118IFS="
119"
120
121for x in $( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars ); do
122	eval export $x
123done
124
125IFS=$oIFS
126
127MKVARS="$( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars | sed -e 's,=.*,,' | xargs )"
128
129if [ "$SETS_SUBR_DEBUG" = "dumpmkvars" ]; then
130	for v in $MKVARS; do
131		eval echo $v=\$$v
132	done
133	exit 0
134fi
135
136#####
137
138setsdir=${0%/*}
139obsolete=0
140module=yes
141if [ "${MKKMOD}" = "no" ]; then
142	module=no			# MODULEs are off.
143fi
144# Determine lib type. Do this first so stlib also gets set.
145if [ "${OBJECT_FMT}" = "ELF" ]; then
146	shlib=elf
147else
148	shlib=aout
149fi
150stlib=$shlib
151# Now check for MKPIC or specials and turn off shlib if need be.
152if [ "${MKPIC}" = "no" ]; then
153	shlib=no
154fi
155if [ "${MACHINE_ARCH}" = "m68000" ]; then
156	shlib=no			# Turn off shlibs for some ports.
157fi
158if [ "$module" != "no" ]; then
159	nlists="base comp etc games man misc modules tests text"
160else
161	nlists="base comp etc games man misc tests text"
162fi
163xlists="xbase xcomp xetc xfont xserver"
164extlists="extbase extcomp extetc"
165
166OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
167MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
168SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
169SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
170SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
171
172#
173# list_set_files setfile [...]
174# 
175# Produce a packing list for setfile(s).
176# In each file, a record consists of a path and a System Package name,
177# separated by whitespace. E.g.,
178#
179# 	# $NetBSD: sets.subr,v 1.114 2009/12/11 15:22:33 he Exp $
180# 	.			base-sys-root	[keyword[,...]]
181# 	./altroot		base-sys-root
182# 	./bin			base-sys-root
183# 	./bin/[			base-util-root
184# 	./bin/cat		base-util-root
185#		[...]
186#
187# A # in the first column marks a comment.
188#
189# If ${obsolete} != 0, only entries with an "obsolete" keyword will
190# be printed.  All other keywords must be present.
191#
192# The third field is an optional comma separated list of keywords to
193# control if a record is printed; every keyword listed must be enabled
194# for the record to be printed.  The following keywords are available:
195#	dummy			dummy entry (ignored)
196#	obsolete		file is obsolete, and only printed if 
197#				${obsolete} != 0
198#
199#	bfd			obsolete, use binutils.
200#	binutils		${MKBINUTILS} != no
201#	catpages		${MKCATPAGES} != no
202#	compat			${MKCOMPAT} != no
203#	crypto			${MKCRYPTO} != no
204#	crypto_idea		${MKCRYPTO_IDEA} != no
205#	crypto_mdc2		${MKCRYPTO_MDC2} != no
206#	crypto_rc5		${MKCRYPTO_RC5} != no
207#	cvs			${MKCVS} != no
208#	debug			${MKDEBUG} != no
209#	debuglib		${MKDEBUGLIB} != no
210#	doc			${MKDOC} != no
211#	dynamicroot		${MKDYNAMICROOT} != no
212#	extsrc			${MKEXTSRC} != no
213#	gcc			${MKGCC} != no
214#	gcccmds			${MKGCCCMDS} != no
215#	gdb			${MKGDB} != no
216#	hesiod			${MKHESIOD} != no
217#	html			${MKHTML} != no
218#	inet6			${MKINET6} != no
219#	info			${MKINFO} != no
220#	ipfilter		${MKIPFILTER} != no
221#	iscsi			${MKISCSI} != no
222#	kerberos		${MKKERBEROS} != no
223#	kmod			${MKKMOD} != no
224#	ldap			${MKLDAP} != no
225#	lint			${MKLINT} != no
226#	lvm			${MKLVM} != no
227#	man			${MKMAN} != no
228#	manpages		${MKMANPAGES} != no
229#	manz			${MKMANZ} != no
230#	mdns			${MKMDNS} != no
231#	nls			${MKNLS} != no
232#	nvi			${MKNVI} != no
233#	pam			${MKPAM} != no
234#	pf			${MKPF} != no
235#	pic			${MKPIC} != no
236#	postfix			${MKPOSTFIX} != no
237#	profile			${MKPROFILE} != no
238#	share			${MKSHARE} != no
239#	skey			${MKSKEY} != no
240#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
241#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
242#	yp			${MKYP} != no
243#	zfs			${MKZFS} != no
244#
245#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
246#	gcc=<n>			<n> = value of ${HAVE_GCC}
247#	gdb=<n>			<n> = value of ${HAVE_GDB}
248#
249#	use_inet6		${USE_INET6} != no
250#	use_kerberos		${USE_KERBEROS} != no
251#	use_yp			${USE_YP} != no
252#
253#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
254#				  automatically append ".gz" to the filename
255#
256#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
257#				  automatically append ".gz" to the filename
258#
259list_set_files()
260{
261	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
262		verbose=false
263	else
264		verbose=true
265	fi
266	for setname; do
267		list=`list_set_lists $setname`
268		for l in $list; do
269			echo $l
270			if $verbose; then
271				echo >&2 "DEBUG: list_set_files: $l"
272			fi
273		done
274	done | xargs cat | ${SED} ${SUBST} | \
275	${AWK} -v obsolete=${obsolete} '
276		BEGIN {
277			if (obsolete)
278				wanted["obsolete"] = 1
279		
280			split("'"${MKVARS}"'", needvars)
281			for (vi in needvars) {
282				nv = needvars[vi]
283				kw = tolower(nv)
284				sub(/^mk/, "", kw)
285				if (ENVIRON[nv] != "no")
286					wanted[kw] = 1 
287			}
288
289			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
290				if ("binutils" in wanted)
291					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
292				if ("gcc" in wanted)
293					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
294				if ("gdb" in wanted)
295					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
296			}
297			if (("man" in wanted) && ("catpages" in wanted))
298				wanted[".cat"] = 1
299			if (("man" in wanted) && ("manpages" in wanted))
300				wanted[".man"] = 1
301		}
302
303		/^#/ {
304			next;
305		}
306
307		NF > 2 && $3 != "-" {
308			split($3, keywords, ",")
309			show = 1
310			haveobs = 0
311			for (ki in keywords) {
312				kw = keywords[ki]
313				if (("manz" in wanted) &&
314				    (kw == ".cat" || kw == ".man"))
315					$1 = $1 ".gz"
316				negated = match(kw, "! *")
317				if (negated > 0) {
318					kw = substr(kw, RSTART + RLENGTH)
319					if (kw in wanted)
320						show = 0
321				} else {
322					if (! (kw in wanted))
323						show = 0
324				}
325				if (kw == "obsolete")
326					haveobs = 1
327			}
328			if (obsolete && ! haveobs)
329				next
330			if (show)
331				print
332			next
333		}
334
335		{
336			if (! obsolete)
337				print
338		}'
339
340}
341
342#
343# list_set_lists setname
344# 
345# Print to stdout a list of files, one filename per line, which
346# concatenate to create the packing list for setname. E.g.,
347#
348# 	.../lists/base/mi
349# 	.../lists/base/rescue.mi
350# 	.../lists/base/md.i386
351#		[...]
352#
353# For a given setname $set, the following files may be selected from
354# .../list/$set:
355#	mi
356#	mi.ext.*
357#	ad.${MACHINE_ARCH}
358# (or)	ad.${MACHINE_CPU}
359#	ad.${MACHINE_CPU}.shl
360#	md.${MACHINE}.${MACHINE_ARCH}
361# (or)	md.${MACHINE}
362#	stl.mi
363#	stl.${stlib}
364#	shl.mi
365#	shl.mi.ext.*
366#	shl.${shlib}
367#	shl.${shlib}.ext.*
368#	module.mi			if ${module} != no
369#	module.${MACHINE}		if ${module} != no
370#	module.ad.${MACHINE_ARCH}	if ${module} != no
371# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
372#	rescue.shl
373#	rescue.${MACHINE}
374#	rescue.ad.${MACHINE_ARCH}
375# (or)	rescue.ad.${MACHINE_CPU}
376# 	rescue.ad.${MACHINE_CPU}.shl
377#
378# Environment:
379# 	shlib
380# 	stlib
381#
382list_set_lists()
383{
384	setname=$1
385
386	list_set_lists_mi $setname
387	list_set_lists_ad $setname
388	list_set_lists_md $setname
389	list_set_lists_stl $setname
390	list_set_lists_shl $setname
391	list_set_lists_module $setname
392	list_set_lists_rescue $setname
393}
394
395list_set_lists_mi()
396{
397	setdir=$setsdir/lists/$1
398	# always exist!
399	echo $setdir/mi
400}
401
402list_set_lists_ad()
403{
404	setdir=$setsdir/lists/$1
405	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
406	list_set_lists_common_ad $1
407}
408
409list_set_lists_md()
410{
411	setdir=$setsdir/lists/$1
412	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
413	echo_if_exist $setdir/md.${MACHINE}
414}
415
416list_set_lists_stl()
417{
418	setdir=$setsdir/lists/$1
419	echo_if_exist $setdir/stl.mi
420	echo_if_exist $setdir/stl.${stlib}
421}
422
423list_set_lists_shl()
424{
425	setdir=$setsdir/lists/$1
426	[ "$shlib" != "no" ] || return
427	echo_if_exist $setdir/shl.mi
428	echo_if_exist $setdir/shl.${shlib}
429}
430
431list_set_lists_module()
432{
433	setdir=$setsdir/lists/$1
434	[ "$module" != "no" ] || return
435	echo_if_exist $setdir/module.mi
436	echo_if_exist $setdir/module.${MACHINE}
437	# XXX module never has .shl
438	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
439	list_set_lists_common_ad $1 module
440}
441
442list_set_lists_rescue()
443{
444	setdir=$setsdir/lists/$1
445	echo_if_exist $setdir/rescue.mi
446	echo_if_exist $setdir/rescue.${MACHINE}
447	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
448	list_set_lists_common_ad $1 rescue
449}
450
451list_set_lists_common_ad()
452{
453	setdir=$setsdir/lists/$1; _prefix=$2
454
455	[ -n "$_prefix" ] && prefix="$_prefix".
456
457	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
458	# <prefix>.ad.${MACHINE_CPU}, since the arch-
459	# specific one will be more specific than the
460	# cpu-specific one.
461	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
462	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
463	[ "$shlib" != "no" ] && \
464	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
465}
466
467echo_if_exist()
468{
469	[ -f $1 ] && echo $1
470	return $?
471}
472
473echo_if_exist_foreach()
474{
475	local _list=$1; shift
476	for _suffix in $@; do
477		echo_if_exist ${_list}.${_suffix}
478	done
479}
480
481# arch_to_cpu mach
482#
483# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
484# as determined by <bsd.own.mk>.
485#
486arch_to_cpu()
487{
488	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
489.include <bsd.own.mk>
490all:
491	@echo \${MACHINE_CPU}
492EOMAKE
493}
494
495# arch_to_endian mach
496#
497# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
498# as determined by <bsd.endian.mk>.
499#
500arch_to_endian()
501{
502	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
503.include <bsd.endian.mk>
504all:
505	@echo \${TARGET_ENDIANNESS}
506EOMAKE
507}
508