sets.subr revision 1.90
1#	$NetBSD: sets.subr,v 1.90 2009/11/03 00:38:56 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 [ "${MACHINE}" = "evbppc" ]; then
204	module=no			# Turn off MODULEs for some ports.
205fi
206# Determine lib type. Do this first so stlib also gets set.
207if [ "${OBJECT_FMT}" = "ELF" ]; then
208	shlib=elf
209else
210	shlib=aout
211fi
212stlib=$shlib
213# Now check for MKPIC or specials and turn off shlib if need be.
214if [ "${MKPIC}" = "no" ]; then
215	shlib=no
216fi
217if [ "${MACHINE_ARCH}" = "m68000" ]; then
218	shlib=no			# Turn off shlibs for some ports.
219fi
220if [ "$module" != "no" ]; then
221	nlists="base comp etc games man misc modules tests text"
222else
223	nlists="base comp etc games man misc tests text"
224fi
225xlists="xbase xcomp xetc xfont xserver"
226
227OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
228MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
229SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
230SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
231SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
232
233#
234# list_set_files setfile [...]
235# 
236# Produce a packing list for setfile(s).
237# In each file, a record consists of a path and a System Package name,
238# separated by whitespace. E.g.,
239#
240# 	# $NetBSD: sets.subr,v 1.90 2009/11/03 00:38:56 dyoung Exp $
241# 	.			base-sys-root	[keyword[,...]]
242# 	./altroot		base-sys-root
243# 	./bin			base-sys-root
244# 	./bin/[			base-util-root
245# 	./bin/cat		base-util-root
246#		[...]
247#
248# A # in the first column marks a comment.
249#
250# If ${obsolete} != 0, only entries with an "obsolete" keyword will
251# be printed.  All other keywords must be present.
252#
253# The third field is an optional comma separated list of keywords to
254# control if a record is printed; every keyword listed must be enabled
255# for the record to be printed.  The following keywords are available:
256#	dummy			dummy entry (ignored)
257#	obsolete		file is obsolete, and only printed if 
258#				${obsolete} != 0
259#
260#	bfd			obsolete, use binutils.
261#	binutils		${MKBINUTILS} != no
262#	catpages		${MKCATPAGES} != no
263#	compat			${MKCOMPAT} != no
264#	crypto			${MKCRYPTO} != no
265#	crypto_idea		${MKCRYPTO_IDEA} != no
266#	crypto_mdc2		${MKCRYPTO_MDC2} != no
267#	crypto_rc5		${MKCRYPTO_RC5} != no
268#	cvs			${MKCVS} != no
269#	debug			${MKDEBUG} != no
270#	debuglib		${MKDEBUGLIB} != no
271#	doc			${MKDOC} != no
272#	dynamicroot		${MKDYNAMICROOT} != no
273#	gcc			${MKGCC} != no
274#	gcccmds			${MKGCCCMDS} != no
275#	gdb			${MKGDB} != no
276#	hesiod			${MKHESIOD} != no
277#	html			${MKHTML} != no
278#	inet6			${MKINET6} != no
279#	info			${MKINFO} != no
280#	ipfilter		${MKIPFILTER} != no
281#	iscsi			${MKISCSI} != no
282#	kerberos		${MKKERBEROS} != no
283#	kmod			${MKKMOD} != no
284#	ldap			${MKLDAP} != no
285#	lint			${MKLINT} != no
286#	lvm			${MKLVM} != no
287#	man			${MKMAN} != no
288#	manpages		${MKMANPAGES} != no
289#	manz			${MKMANZ} != no
290#	mdns			${MKMDNS} != no
291#	nls			${MKNLS} != no
292#	nvi			${MKNVI} != no
293#	pam			${MKPAM} != no
294#	pf			${MKPF} != no
295#	pic			${MKPIC} != no
296#	postfix			${MKPOSTFIX} != no
297#	profile			${MKPROFILE} != no
298#	share			${MKSHARE} != no
299#	skey			${MKSKEY} != no
300#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
301#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
302#	yp			${MKYP} != no
303#	zfs			${MKZFS} != no
304#
305#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
306#	gcc=<n>			<n> = value of ${HAVE_GCC}
307#	gdb=<n>			<n> = value of ${HAVE_GDB}
308#
309#	use_inet6		${USE_INET6} != no
310#	use_kerberos		${USE_KERBEROS} != no
311#	use_yp			${USE_YP} != no
312#
313#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
314#				  automatically append ".gz" to the filename
315#
316#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
317#				  automatically append ".gz" to the filename
318#
319list_set_files()
320{
321	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
322		verbose=false
323	else
324		verbose=true
325	fi
326	for setname; do
327		list=`list_set_lists $setname`
328		for l in $list; do
329			echo $l
330			if $verbose; then
331				echo >&2 "DEBUG: list_set_files: $l"
332			fi
333		done
334	done | xargs cat | ${SED} ${SUBST} | \
335	${AWK} -v obsolete=${obsolete} '
336		BEGIN {
337			if (obsolete)
338				wanted["obsolete"] = 1
339		
340			split("'"${MKVARS}"'", needvars)
341			for (vi in needvars) {
342				nv = needvars[vi]
343				kw = tolower(nv)
344				sub(/^mk/, "", kw)
345				if (ENVIRON[nv] != "no")
346					wanted[kw] = 1 
347			}
348
349			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
350				if ("binutils" in wanted)
351					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
352				if ("gcc" in wanted)
353					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
354				if ("gdb" in wanted)
355					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
356			}
357			if (("man" in wanted) && ("catpages" in wanted))
358				wanted[".cat"] = 1
359			if (("man" in wanted) && ("manpages" in wanted))
360				wanted[".man"] = 1
361		}
362
363		/^#/ {
364			next;
365		}
366
367		NF > 2 && $3 != "-" {
368			split($3, keywords, ",")
369			show = 1
370			haveobs = 0
371			for (ki in keywords) {
372				kw = keywords[ki]
373				if (("manz" in wanted) &&
374				    (kw == ".cat" || kw == ".man"))
375					$1 = $1 ".gz"
376				negated = match(kw, "! *")
377				if (negated > 0) {
378					kw = substr(kw, RSTART + RLENGTH)
379					if (kw in wanted)
380						show = 0
381				} else {
382					if (! (kw in wanted))
383						show = 0
384				}
385				if (kw == "obsolete")
386					haveobs = 1
387			}
388			if (obsolete && ! haveobs)
389				next
390			if (show)
391				print
392			next
393		}
394
395		{
396			if (! obsolete)
397				print
398		}'
399
400}
401
402#
403# list_set_lists setname
404# 
405# Print to stdout a list of files, one filename per line, which
406# concatenate to create the packing list for setname. E.g.,
407#
408# 	.../lists/base/mi
409# 	.../lists/base/rescue.mi
410# 	.../lists/base/md.i386
411#		[...]
412#
413# For a given setname $set, the following files may be selected from
414# .../list/$set:
415#	mi
416#	ad.${MACHINE_ARCH}
417# (or)	ad.${MACHINE_CPU}
418#	ad.${MACHINE_CPU}.shl
419#	md.${MACHINE}.${MACHINE_ARCH}
420# (or)	md.${MACHINE}
421#	stl.mi
422#	stl.stlib
423#	shl.mi
424#	shl.shlib
425#	module.mi			if ${module} != no
426#	module.${MACHINE}		if ${module} != no
427#	module.ad.${MACHINE_ARCH}	if ${module} != no
428# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
429#	rescue.shl
430#	rescue.${MACHINE}
431#	rescue.ad.${MACHINE_ARCH}
432# (or)	rescue.ad.${MACHINE_CPU}
433# 	rescue.ad.${MACHINE_CPU}.shl
434#
435# Environment:
436# 	shlib
437# 	stlib
438#
439list_set_lists()
440{
441	setname=$1
442
443	setdir=$setsdir/lists/$setname
444	echo $setdir/mi
445	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
446		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
447		# since the arch-specific one will be more specific than
448		# the cpu-specific one.
449		if [ -f $setdir/ad.${MACHINE_ARCH} ]; then
450			echo $setdir/ad.${MACHINE_ARCH}
451		elif [ -f $setdir/ad.${MACHINE_CPU} ]; then
452			echo $setdir/ad.${MACHINE_CPU}
453		fi
454		if [ "$shlib" != "no" -a \
455		     -f $setdir/ad.${MACHINE_CPU}.shl ]; then
456			echo $setdir/ad.${MACHINE_CPU}.shl
457		fi
458	fi
459	if [ -f $setdir/md.${MACHINE}.${MACHINE_ARCH} ]; then
460		echo $setdir/md.${MACHINE}.${MACHINE_ARCH}
461	elif [ -f $setdir/md.${MACHINE} ]; then
462		echo $setdir/md.${MACHINE}
463	fi
464	if [ -f $setdir/stl.mi ]; then
465		echo $setdir/stl.mi
466	fi
467	if [ -f $setdir/stl.${stlib} ]; then
468		echo $setdir/stl.${stlib}
469	fi
470	if [ "$shlib" != "no" ]; then
471		if [ -f $setdir/shl.mi ]; then
472			echo $setdir/shl.mi
473		fi
474		if [ -f $setdir/shl.${shlib} ]; then
475			echo $setdir/shl.${shlib}
476		fi
477	fi
478	if [ "$module" != "no" ]; then
479		if [ -f $setdir/module.mi ]; then
480			echo $setdir/module.mi
481		fi
482		if [ -f $setdir/module.${MACHINE} ]; then
483			echo $setdir/module.${MACHINE}
484		fi
485		if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
486			# Prefer a module.ad.${MACHINE_ARCH} over a
487			# module.ad.${MACHINE_CPU}, since the arch-
488			# specific one will be more specific than the
489			# cpu-specific one.
490			if [ -f $setdir/module.ad.${MACHINE_ARCH} ]; then
491				echo $setdir/module.ad.${MACHINE_ARCH}
492			elif [ -f $setdir/module.ad.${MACHINE_CPU} ]; then
493				echo $setdir/module.ad.${MACHINE_CPU}
494			fi
495		fi
496	fi
497
498	if [ -f $setdir/rescue.mi ]; then
499		echo $setdir/rescue.mi
500	fi
501	if [ -f $setdir/rescue.${MACHINE} ]; then
502		echo $setdir/rescue.${MACHINE}
503	fi
504	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
505		# Prefer a rescue.ad.${MACHINE_ARCH} over a
506		# rescue.ad.${MACHINE_CPU}, since the arch-
507		# specific one will be more specific than the
508		# cpu-specific one.
509		if [ -f $setdir/rescue.ad.${MACHINE_ARCH} ]; then
510			echo $setdir/rescue.ad.${MACHINE_ARCH}
511		elif [ -f $setdir/rescue.ad.${MACHINE_CPU} ]; then
512			echo $setdir/rescue.ad.${MACHINE_CPU}
513		fi
514		if [ "$shlib" != "no" -a \
515		     -f $setdir/rescue.ad.${MACHINE_CPU}.shl ]; then
516			echo $setdir/rescue.ad.${MACHINE_CPU}.shl
517		fi
518	fi
519}
520
521# arch_to_cpu mach
522#
523# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
524# as determined by <bsd.own.mk>.
525#
526arch_to_cpu()
527{
528	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
529.include <bsd.own.mk>
530all:
531	@echo \${MACHINE_CPU}
532EOMAKE
533}
534
535# arch_to_endian mach
536#
537# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
538# as determined by <bsd.endian.mk>.
539#
540arch_to_endian()
541{
542	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
543.include <bsd.endian.mk>
544all:
545	@echo \${TARGET_ENDIANNESS}
546EOMAKE
547}
548