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