sets.subr revision 1.67
1#	$NetBSD: sets.subr,v 1.67 2008/08/25 07:27:27 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. one of:
14#				   ""	cross built from src/x11
15#				   3	XFree86 3.x from xsrc/xc
16#				   4	XFree86 4.x from xsrc/xfree/xc
17#
18# The following <bsd.own.mk> variables are exported to the environment:
19#	MACHINE	
20#	MACHINE_ARCH
21#	MACHINE_CPU
22#	HAVE_GCC
23#	HAVE_GDB
24#	TOOLCHAIN_MISSING
25#	OBJECT_FMT
26# as well as:
27#
28MKVARS="\
29	MKBFD		\
30	MKCATPAGES	\
31	MKCRYPTO	\
32	MKCRYPTO_IDEA	\
33	MKCRYPTO_MDC2	\
34	MKCRYPTO_RC5	\
35	MKCVS		\
36	MKDEBUG		\
37	MKDOC		\
38	MKDYNAMICROOT	\
39	MKGCC		\
40	MKGCCCMDS	\
41	MKGDB		\
42	MKHESIOD	\
43	MKINET6		\
44	MKINFO		\
45	MKIPFILTER	\
46	MKISCSI		\
47	MKKERBEROS	\
48	MKLDAP		\
49	MKLINT		\
50	MKMAN		\
51	MKMANPAGES	\
52	MKMANZ		\
53	MKMODULAR	\
54	MKNLS		\
55	MKNVI		\
56	MKPAM		\
57	MKPF		\
58	MKPIC		\
59	MKPOSTFIX	\
60	MKPROFILE	\
61	MKPUFFS		\
62	MKSENDMAIL	\
63	MKSHARE		\
64	MKSKEY		\
65	MKX11		\
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
174all:
175.for i in MACHINE MACHINE_ARCH MACHINE_CPU \
176		HAVE_GCC HAVE_GDB OBJECT_FMT TOOLCHAIN_MISSING \
177		${MKVARS}
178	@echo "export \$i=\${\$i}"
179.endfor
180.if (\${MKX11:Uno} != "no")
181	@echo x11_version=""
182.else
183	@echo x11_version=4
184.endif
185
186EOMAKE
187); do
188#	echo 1>&2 "DEBUG: read $x"
189	eval $x
190done
191IFS=$oIFS
192
193setsdir=${0%/*}
194nlists="base comp etc games man misc tests text"
195case $x11_version in
1963)	xlists="xbase3 xcomp3 xcontrib3 xfont3 xmisc3 xserver3" ;;
1974)	xlists="xbase4 xcomp4 xcontrib4 xfont4 xmisc4 xserver4" ;;
198"")	xlists="xbase xcomp xetc xfont xserver" ;;
199*)	xlists="" ;;	# unknown!
200esac
201obsolete=0
202lkm=yes
203if [ "${MACHINE}" = "evbppc" ]; then
204	lkm=no				# Turn off LKMs 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
220
221OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
222MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
223SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
224SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
225SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
226
227#
228# list_set_files setfile [...]
229# 
230# Produce a packing list for setfile(s).
231# In each file, a record consists of a path and a System Package name,
232# separated by whitespace. E.g.,
233#
234# 	# $NetBSD: sets.subr,v 1.67 2008/08/25 07:27:27 lukem Exp $
235# 	.			base-sys-root	[keyword[,...]]
236# 	./altroot		base-sys-root
237# 	./bin			base-sys-root
238# 	./bin/[			base-util-root
239# 	./bin/cat		base-util-root
240#		[...]
241#
242# A # in the first column marks a comment.
243#
244# If ${obsolete} != 0, only entries with an "obsolete" keyword will
245# be printed.  All other keywords must be present.
246#
247# The third field is an optional comma separated list of keywords to
248# control if a record is printed; every keyword listed must be enabled
249# for the record to be printed.  The following keywords are available:
250#	dummy			dummy entry (ignored)
251#	obsolete		file is obsolete, and only printed if 
252#				${obsolete} != 0
253#
254#	bfd			${MKBFD} != no
255#	catpages		${MKCATPAGES} != no
256#	crypto			${MKCRYPTO} != no
257#	crypto_idea		${MKCRYPTO_IDEA} != no
258#	crypto_mdc2		${MKCRYPTO_MDC2} != no
259#	crypto_rc5		${MKCRYPTO_RC5} != no
260#	cvs			${MKCVS} != no
261#	debug			${MKDEBUG} != 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#	inet6			${MKINET6} != no
269#	info			${MKINFO} != no
270#	ipfilter		${MKIPFILTER} != no
271#	iscsi			${MKISCSI} != no
272#	kerberos		${MKKERBEROS} != no
273#	ldap			${MKLDAP} != no
274#	lint			${MKLINT} != no
275#	man			${MKMAN} != no
276#	manpages		${MKMANPAGES} != no
277#	manz			${MKMANZ} != no
278#	modular			${MKMODULAR} != no
279#	nls			${MKNLS} != no
280#	nvi			${MKNVI} != no
281#	pam			${MKPAM} != no
282#	pf			${MKPF} != no
283#	pic			${MKPIC} != no
284#	postfix			${MKPOSTFIX} != no
285#	profile			${MKPROFILE} != no
286#	puffs			${MKPUFFS} != no
287#	sendmail		${MKSENDMAIL} != no
288#	share			${MKSHARE} != no
289#	skey			${MKSKEY} != no
290#	x11			${MKX11} != no
291#	yp			${MKYP} != no
292#
293#	gcc=<n>			<n> = value of ${HAVE_GCC}
294#	gdb=<n>			<n> = value of ${HAVE_GDB}
295#
296#	use_inet6		${USE_INET6} != no
297#	use_kerberos		${USE_KERBEROS} != no
298#	use_yp			${USE_YP} != no
299#
300#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
301#				  automatically append ".gz" to the filename
302#
303#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
304#				  automatically append ".gz" to the filename
305#
306list_set_files()
307{
308	if [ ${MAKEVERBOSE:-2} -lt 2 ]; then
309		verbose=false
310	else
311		verbose=true
312	fi
313	for setname; do
314		list=`list_set_lists $setname`
315		for l in $list; do
316			echo $l
317			if $verbose; then
318				echo $l >&2
319			fi
320		done
321	done | xargs cat | ${SED} ${SUBST} | \
322	${AWK} -v obsolete=${obsolete} '
323		BEGIN {
324			if (obsolete)
325				wanted["obsolete"] = 1
326		
327			split("'"${MKVARS}"'", needvars)
328			for (vi in needvars) {
329				nv = needvars[vi]
330				kw = tolower(nv)
331				sub(/^mk/, "", kw)
332				if (ENVIRON[nv] != "no")
333					wanted[kw] = 1 
334			}
335
336			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
337				wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
338				wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
339			}
340			if (("man" in wanted) && ("catpages" in wanted))
341				wanted[".cat"] = 1
342			if (("man" in wanted) && ("manpages" in wanted))
343				wanted[".man"] = 1
344		}
345
346		/^#/ {
347			next;
348		}
349
350		NF > 2 && $3 != "-" {
351			split($3, keywords, ",")
352			show = 1
353			haveobs = 0
354			for (ki in keywords) {
355				kw = keywords[ki]
356				if (("manz" in wanted) &&
357				    (kw == ".cat" || kw == ".man"))
358					$1 = $1 ".gz"
359				negated = match(kw, "! *")
360				if (negated > 0) {
361					kw = substr(kw, RSTART + RLENGTH)
362					if (kw in wanted)
363						show = 0
364				} else {
365					if (! (kw in wanted))
366						show = 0
367				}
368				if (kw == "obsolete")
369					haveobs = 1
370			}
371			if (obsolete && ! haveobs)
372				next
373			if (show)
374				print
375			next
376		}
377
378		{
379			if (! obsolete)
380				print
381		}'
382
383}
384
385#
386# list_set_lists setname
387# 
388# Print to stdout a list of files, one filename per line, which
389# concatenate to create the packing list for setname. E.g.,
390#
391# 	.../lists/base/mi
392# 	.../lists/base/rescue.mi
393# 	.../lists/base/md.i386
394#		[...]
395#
396# For a given setname $set, the following files may be selected from
397# .../list/$set:
398#	mi
399#	ad.${MACHINE_ARCH}
400# (or)	ad.${MACHINE_CPU}
401#	ad.${MACHINE_CPU}.shl
402#	md.${MACHINE}.${MACHINE_ARCH}
403# (or)	md.${MACHINE}
404#	stl.mi
405#	stl.stlib
406#	shl.mi
407#	shl.shlib
408#	lkm.mi			if ${lkm} != no
409#	rescue.shl
410#	rescue.${MACHINE}
411#	rescue.ad.${MACHINE_ARCH}
412# (or)	rescue.ad.${MACHINE_CPU}
413# 	rescue.ad.${MACHINE_CPU}.shl
414#
415# Environment:
416# 	shlib
417# 	stlib
418#
419list_set_lists()
420{
421	setname=$1
422
423	setdir=$setsdir/lists/$setname
424	echo $setdir/mi
425	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
426		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
427		# since the arch-specific one will be more specific than
428		# the cpu-specific one.
429		if [ -f $setdir/ad.${MACHINE_ARCH} ]; then
430			echo $setdir/ad.${MACHINE_ARCH}
431		elif [ -f $setdir/ad.${MACHINE_CPU} ]; then
432			echo $setdir/ad.${MACHINE_CPU}
433		fi
434		if [ "$shlib" != "no" -a \
435		     -f $setdir/ad.${MACHINE_CPU}.shl ]; then
436			echo $setdir/ad.${MACHINE_CPU}.shl
437		fi
438	fi
439	if [ -f $setdir/md.${MACHINE}.${MACHINE_ARCH} ]; then
440		echo $setdir/md.${MACHINE}.${MACHINE_ARCH}
441	elif [ -f $setdir/md.${MACHINE} ]; then
442		echo $setdir/md.${MACHINE}
443	fi
444	if [ -f $setdir/stl.mi ]; then
445		echo $setdir/stl.mi
446	fi
447	if [ -f $setdir/stl.${stlib} ]; then
448		echo $setdir/stl.${stlib}
449	fi
450	if [ "$shlib" != "no" ]; then
451		if [ -f $setdir/shl.mi ]; then
452			echo $setdir/shl.mi
453		fi
454		if [ -f $setdir/shl.${shlib} ]; then
455			echo $setdir/shl.${shlib}
456		fi
457	fi
458	if [ "$lkm" != "no" ]; then
459		if [ -f $setdir/lkm.mi ]; then
460			echo $setdir/lkm.mi
461		fi
462	fi
463
464	if [ -f $setdir/rescue.mi ]; then
465		echo $setdir/rescue.mi
466	fi
467	if [ -f $setdir/rescue.${MACHINE} ]; then
468		echo $setdir/rescue.${MACHINE}
469	fi
470	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
471		# Prefer a rescue.ad.${MACHINE_ARCH} over a
472		# rescue.ad.${MACHINE_CPU}, since the arch-
473		# specific one will be more specific than the
474		# cpu-specific one.
475		if [ -f $setdir/rescue.ad.${MACHINE_ARCH} ]; then
476			echo $setdir/rescue.ad.${MACHINE_ARCH}
477		elif [ -f $setdir/rescue.ad.${MACHINE_CPU} ]; then
478			echo $setdir/rescue.ad.${MACHINE_CPU}
479		fi
480		if [ "$shlib" != "no" -a \
481		     -f $setdir/rescue.ad.${MACHINE_CPU}.shl ]; then
482			echo $setdir/rescue.ad.${MACHINE_CPU}.shl
483		fi
484	fi
485}
486
487# arch_to_cpu mach
488#
489# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
490# as determined by <bsd.own.mk>.
491#
492arch_to_cpu()
493{
494	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
495.include <bsd.own.mk>
496all:
497	@echo \${MACHINE_CPU}
498EOMAKE
499}
500
501# arch_to_endian mach
502#
503# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
504# as determined by <bsd.endian.mk>.
505#
506arch_to_endian()
507{
508	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
509.include <bsd.endian.mk>
510all:
511	@echo \${TARGET_ENDIANNESS}
512EOMAKE
513}
514