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