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