sets.subr revision 1.68
1#	$NetBSD: sets.subr,v 1.68 2008/08/28 07:17:13 lukem 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#	x11_version		version of XFree86 / X.org. one of:
14#				   ""	cross built from src/x11 or
15#					src/external/mit/xorg
16#				   3	XFree86 3.x from xsrc/xc
17#				   4	XFree86 4.x from xsrc/xfree/xc
18#
19# The following <bsd.own.mk> variables are exported to the environment:
20#	MACHINE	
21#	MACHINE_ARCH
22#	MACHINE_CPU
23#	HAVE_GCC
24#	HAVE_GDB
25#	TOOLCHAIN_MISSING
26#	OBJECT_FMT
27# as well as:
28#
29MKVARS="\
30	MKBFD		\
31	MKCATPAGES	\
32	MKCRYPTO	\
33	MKCRYPTO_IDEA	\
34	MKCRYPTO_MDC2	\
35	MKCRYPTO_RC5	\
36	MKCVS		\
37	MKDEBUG		\
38	MKDOC		\
39	MKDYNAMICROOT	\
40	MKGCC		\
41	MKGCCCMDS	\
42	MKGDB		\
43	MKHESIOD	\
44	MKHTML		\
45	MKINET6		\
46	MKINFO		\
47	MKIPFILTER	\
48	MKISCSI		\
49	MKKERBEROS	\
50	MKLDAP		\
51	MKLINT		\
52	MKMAN		\
53	MKMANPAGES	\
54	MKMANZ		\
55	MKMODULAR	\
56	MKNLS		\
57	MKNVI		\
58	MKPAM		\
59	MKPF		\
60	MKPIC		\
61	MKPOSTFIX	\
62	MKPROFILE	\
63	MKPUFFS		\
64	MKSENDMAIL	\
65	MKSHARE		\
66	MKSKEY		\
67	MKX11		\
68	MKXORG		\
69	MKYP		\
70	USE_INET6	\
71	USE_KERBEROS	\
72	USE_LDAP	\
73	USE_YP		\
74	NETBSDSRCDIR	\
75	MAKEVERBOSE	\
76"
77#
78# The following variables refer to tools that are used when building sets:
79#
80: ${AWK:=awk}
81: ${CKSUM:=cksum}
82: ${COMM:=comm}
83: ${DATE:=date}
84: ${DB:=db}
85: ${EGREP:=egrep}
86: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
87: ${FGREP:=fgrep}
88: ${FIND:=find}
89: ${GREP:=grep}
90: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
91: ${HOSTNAME:=hostname}
92: ${HOST_SH:=sh}
93: ${IDENT:=ident}
94: ${JOIN:=join}
95: ${LS:=ls}
96: ${MAKE:=make}
97: ${MKTEMP:=mktemp}
98: ${MTREE:=mtree}
99: ${PASTE:=paste}
100: ${PAX:=pax}
101: ${PKG_CREATE:=pkg_create}
102: ${PRINTF:=printf}
103: ${SED:=sed}
104: ${SORT:=sort}
105: ${STAT:=stat}
106: ${TSORT:=tsort}
107: ${UNAME:=uname}
108: ${WC:=wc}
109
110#
111# If printf is a shell builtin command, then we can
112# implement cheaper versions of basename and dirname
113# that do not involve any fork/exec overhead.
114# If printf is not builtin, approximate it using echo,
115# and hope there are no weird file names that cause
116# some versions of echo to do the wrong thing.
117# (Converting to this version of dirname speeded up the
118# syspkgdeps script by an order of magnitude, from 68
119# seconds to 6.3 seconds on one particular host.)
120#
121# Note that naive approximations for dirname
122# using ${foo%/*} do not do the right thing in cases
123# where the result should be "/" or ".".
124#
125case "$(type printf)" in
126*builtin*)
127	basename ()
128	{
129		local bn
130		bn="${1##*/}"
131		bn="${bn%$2}"
132		printf "%s\n" "$bn"
133	}
134	dirname ()
135	{
136		local dn
137		case "$1" in
138		?*/*)	dn="${1%/*}" ;;
139		/*)	dn=/ ;;
140		*)	dn=. ;;
141		esac
142		printf "%s\n" "$dn"
143	}
144	;;
145*)
146	basename ()
147	{
148		local bn
149		bn="${1##*/}"
150		bn="${bn%$2}"
151		echo "$bn"
152	}
153	dirname ()
154	{
155		local dn
156		case "$1" in
157		?*/*)	dn="${1%/*}" ;;
158		/*)	dn=/ ;;
159		*)	dn=. ;;
160		esac
161		echo "$dn"
162	}
163	;;
164esac
165
166oIFS=$IFS
167IFS="
168"
169for x in $(
170${MAKE} -B -f- all <<EOMAKE
171.include <bsd.own.mk>
172.if (\${MKMAN} == "no" || empty(MANINSTALL:Mmaninstall))
173MKMANPAGES=no
174.else
175MKMANPAGES=yes
176.endif
177all:
178.for i in MACHINE MACHINE_ARCH MACHINE_CPU \
179		HAVE_GCC HAVE_GDB OBJECT_FMT TOOLCHAIN_MISSING \
180		${MKVARS}
181	@echo "export \$i=\${\$i}"
182.endfor
183.if (\${MKX11:Uno} != "no" || \${MKXORG:Uno} != "no")
184	@echo x11_version=""
185.else
186	@echo x11_version=4
187.endif
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"
198case $x11_version in
1993)	xlists="xbase3 xcomp3 xcontrib3 xfont3 xmisc3 xserver3" ;;
2004)	xlists="xbase4 xcomp4 xcontrib4 xfont4 xmisc4 xserver4" ;;
201"")	xlists="xbase xcomp xetc xfont xserver" ;;
202*)	xlists="" ;;	# unknown!
203esac
204obsolete=0
205lkm=yes
206if [ "${MACHINE}" = "evbppc" ]; then
207	lkm=no				# Turn off LKMs for some ports.
208fi
209# Determine lib type. Do this first so stlib also gets set.
210if [ "${OBJECT_FMT}" = "ELF" ]; then
211	shlib=elf
212else
213	shlib=aout
214fi
215stlib=$shlib
216# Now check for MKPIC or specials and turn off shlib if need be.
217if [ "${MKPIC}" = "no" ]; then
218	shlib=no
219fi
220if [ "${MACHINE_ARCH}" = "m68000" ]; then
221	shlib=no			# Turn off shlibs for some ports.
222fi
223
224OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
225MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
226SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
227SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
228SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
229
230#
231# list_set_files setfile [...]
232# 
233# Produce a packing list for setfile(s).
234# In each file, a record consists of a path and a System Package name,
235# separated by whitespace. E.g.,
236#
237# 	# $NetBSD: sets.subr,v 1.68 2008/08/28 07:17:13 lukem Exp $
238# 	.			base-sys-root	[keyword[,...]]
239# 	./altroot		base-sys-root
240# 	./bin			base-sys-root
241# 	./bin/[			base-util-root
242# 	./bin/cat		base-util-root
243#		[...]
244#
245# A # in the first column marks a comment.
246#
247# If ${obsolete} != 0, only entries with an "obsolete" keyword will
248# be printed.  All other keywords must be present.
249#
250# The third field is an optional comma separated list of keywords to
251# control if a record is printed; every keyword listed must be enabled
252# for the record to be printed.  The following keywords are available:
253#	dummy			dummy entry (ignored)
254#	obsolete		file is obsolete, and only printed if 
255#				${obsolete} != 0
256#
257#	bfd			${MKBFD} != no
258#	catpages		${MKCATPAGES} != 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#	doc			${MKDOC} != no
266#	dynamicroot		${MKDYNAMICROOT} != no
267#	gcc			${MKGCC} != no
268#	gcccmds			${MKGCCCMDS} != no
269#	gdb			${MKGDB} != no
270#	hesiod			${MKHESIOD} != no
271#	html			${MKHTML} != no
272#	inet6			${MKINET6} != no
273#	info			${MKINFO} != no
274#	ipfilter		${MKIPFILTER} != no
275#	iscsi			${MKISCSI} != no
276#	kerberos		${MKKERBEROS} != no
277#	ldap			${MKLDAP} != no
278#	lint			${MKLINT} != no
279#	man			${MKMAN} != no
280#	manpages		${MKMANPAGES} != no
281#	manz			${MKMANZ} != no
282#	modular			${MKMODULAR} != no
283#	nls			${MKNLS} != no
284#	nvi			${MKNVI} != no
285#	pam			${MKPAM} != no
286#	pf			${MKPF} != no
287#	pic			${MKPIC} != no
288#	postfix			${MKPOSTFIX} != no
289#	profile			${MKPROFILE} != no
290#	puffs			${MKPUFFS} != no
291#	sendmail		${MKSENDMAIL} != no
292#	share			${MKSHARE} != no
293#	skey			${MKSKEY} != no
294#	x11			${MKX11} != no
295#	xorg			${MKXORG} != no
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 2 ]; 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 $l >&2
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#	lkm.mi			if ${lkm} != no
414#	rescue.shl
415#	rescue.${MACHINE}
416#	rescue.ad.${MACHINE_ARCH}
417# (or)	rescue.ad.${MACHINE_CPU}
418# 	rescue.ad.${MACHINE_CPU}.shl
419#
420# Environment:
421# 	shlib
422# 	stlib
423#
424list_set_lists()
425{
426	setname=$1
427
428	setdir=$setsdir/lists/$setname
429	echo $setdir/mi
430	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
431		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
432		# since the arch-specific one will be more specific than
433		# the cpu-specific one.
434		if [ -f $setdir/ad.${MACHINE_ARCH} ]; then
435			echo $setdir/ad.${MACHINE_ARCH}
436		elif [ -f $setdir/ad.${MACHINE_CPU} ]; then
437			echo $setdir/ad.${MACHINE_CPU}
438		fi
439		if [ "$shlib" != "no" -a \
440		     -f $setdir/ad.${MACHINE_CPU}.shl ]; then
441			echo $setdir/ad.${MACHINE_CPU}.shl
442		fi
443	fi
444	if [ -f $setdir/md.${MACHINE}.${MACHINE_ARCH} ]; then
445		echo $setdir/md.${MACHINE}.${MACHINE_ARCH}
446	elif [ -f $setdir/md.${MACHINE} ]; then
447		echo $setdir/md.${MACHINE}
448	fi
449	if [ -f $setdir/stl.mi ]; then
450		echo $setdir/stl.mi
451	fi
452	if [ -f $setdir/stl.${stlib} ]; then
453		echo $setdir/stl.${stlib}
454	fi
455	if [ "$shlib" != "no" ]; then
456		if [ -f $setdir/shl.mi ]; then
457			echo $setdir/shl.mi
458		fi
459		if [ -f $setdir/shl.${shlib} ]; then
460			echo $setdir/shl.${shlib}
461		fi
462	fi
463	if [ "$lkm" != "no" ]; then
464		if [ -f $setdir/lkm.mi ]; then
465			echo $setdir/lkm.mi
466		fi
467	fi
468
469	if [ -f $setdir/rescue.mi ]; then
470		echo $setdir/rescue.mi
471	fi
472	if [ -f $setdir/rescue.${MACHINE} ]; then
473		echo $setdir/rescue.${MACHINE}
474	fi
475	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
476		# Prefer a rescue.ad.${MACHINE_ARCH} over a
477		# rescue.ad.${MACHINE_CPU}, since the arch-
478		# specific one will be more specific than the
479		# cpu-specific one.
480		if [ -f $setdir/rescue.ad.${MACHINE_ARCH} ]; then
481			echo $setdir/rescue.ad.${MACHINE_ARCH}
482		elif [ -f $setdir/rescue.ad.${MACHINE_CPU} ]; then
483			echo $setdir/rescue.ad.${MACHINE_CPU}
484		fi
485		if [ "$shlib" != "no" -a \
486		     -f $setdir/rescue.ad.${MACHINE_CPU}.shl ]; then
487			echo $setdir/rescue.ad.${MACHINE_CPU}.shl
488		fi
489	fi
490}
491
492# arch_to_cpu mach
493#
494# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
495# as determined by <bsd.own.mk>.
496#
497arch_to_cpu()
498{
499	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
500.include <bsd.own.mk>
501all:
502	@echo \${MACHINE_CPU}
503EOMAKE
504}
505
506# arch_to_endian mach
507#
508# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
509# as determined by <bsd.endian.mk>.
510#
511arch_to_endian()
512{
513	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
514.include <bsd.endian.mk>
515all:
516	@echo \${TARGET_ENDIANNESS}
517EOMAKE
518}
519