sets.subr revision 1.85
1#	$NetBSD: sets.subr,v 1.85 2009/08/30 01:49:41 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_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%/*}
197nlists="base comp etc games man misc tests text"
198xlists="xbase xcomp xetc xfont xserver"
199obsolete=0
200module=yes
201if [ "${MACHINE}" = "evbppc" ]; then
202	module=no			# Turn off MODULEs 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.85 2009/08/30 01:49:41 dyoung 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#	kmod			${MKKMOD} != no
275#	ldap			${MKLDAP} != no
276#	lint			${MKLINT} != no
277#	lvm			${MKLVM} != no
278#	man			${MKMAN} != no
279#	manpages		${MKMANPAGES} != no
280#	manz			${MKMANZ} != no
281#	nls			${MKNLS} != no
282#	nvi			${MKNVI} != no
283#	pam			${MKPAM} != no
284#	pf			${MKPF} != no
285#	pic			${MKPIC} != no
286#	postfix			${MKPOSTFIX} != no
287#	profile			${MKPROFILE} != no
288#	share			${MKSHARE} != no
289#	skey			${MKSKEY} != no
290#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
291#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
292#	yp			${MKYP} != no
293#
294#	gcc=<n>			<n> = value of ${HAVE_GCC}
295#	gdb=<n>			<n> = value of ${HAVE_GDB}
296#
297#	use_inet6		${USE_INET6} != no
298#	use_kerberos		${USE_KERBEROS} != no
299#	use_yp			${USE_YP} != no
300#
301#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
302#				  automatically append ".gz" to the filename
303#
304#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
305#				  automatically append ".gz" to the filename
306#
307list_set_files()
308{
309	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
310		verbose=false
311	else
312		verbose=true
313	fi
314	for setname; do
315		list=`list_set_lists $setname`
316		for l in $list; do
317			echo $l
318			if $verbose; then
319				echo >&2 "DEBUG: list_set_files: $l"
320			fi
321		done
322	done | xargs cat | ${SED} ${SUBST} | \
323	${AWK} -v obsolete=${obsolete} '
324		BEGIN {
325			if (obsolete)
326				wanted["obsolete"] = 1
327		
328			split("'"${MKVARS}"'", needvars)
329			for (vi in needvars) {
330				nv = needvars[vi]
331				kw = tolower(nv)
332				sub(/^mk/, "", kw)
333				if (ENVIRON[nv] != "no")
334					wanted[kw] = 1 
335			}
336
337			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
338				wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
339				wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
340			}
341			if (("man" in wanted) && ("catpages" in wanted))
342				wanted[".cat"] = 1
343			if (("man" in wanted) && ("manpages" in wanted))
344				wanted[".man"] = 1
345		}
346
347		/^#/ {
348			next;
349		}
350
351		NF > 2 && $3 != "-" {
352			split($3, keywords, ",")
353			show = 1
354			haveobs = 0
355			for (ki in keywords) {
356				kw = keywords[ki]
357				if (("manz" in wanted) &&
358				    (kw == ".cat" || kw == ".man"))
359					$1 = $1 ".gz"
360				negated = match(kw, "! *")
361				if (negated > 0) {
362					kw = substr(kw, RSTART + RLENGTH)
363					if (kw in wanted)
364						show = 0
365				} else {
366					if (! (kw in wanted))
367						show = 0
368				}
369				if (kw == "obsolete")
370					haveobs = 1
371			}
372			if (obsolete && ! haveobs)
373				next
374			if (show)
375				print
376			next
377		}
378
379		{
380			if (! obsolete)
381				print
382		}'
383
384}
385
386#
387# list_set_lists setname
388# 
389# Print to stdout a list of files, one filename per line, which
390# concatenate to create the packing list for setname. E.g.,
391#
392# 	.../lists/base/mi
393# 	.../lists/base/rescue.mi
394# 	.../lists/base/md.i386
395#		[...]
396#
397# For a given setname $set, the following files may be selected from
398# .../list/$set:
399#	mi
400#	ad.${MACHINE_ARCH}
401# (or)	ad.${MACHINE_CPU}
402#	ad.${MACHINE_CPU}.shl
403#	md.${MACHINE}.${MACHINE_ARCH}
404# (or)	md.${MACHINE}
405#	stl.mi
406#	stl.stlib
407#	shl.mi
408#	shl.shlib
409#	module.mi			if ${module} != no
410#	module.${MACHINE}		if ${module} != no
411#	module.ad.${MACHINE_ARCH}	if ${module} != no
412# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
413#	rescue.shl
414#	rescue.${MACHINE}
415#	rescue.ad.${MACHINE_ARCH}
416# (or)	rescue.ad.${MACHINE_CPU}
417# 	rescue.ad.${MACHINE_CPU}.shl
418#
419# Environment:
420# 	shlib
421# 	stlib
422#
423list_set_lists()
424{
425	setname=$1
426
427	setdir=$setsdir/lists/$setname
428	echo $setdir/mi
429	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
430		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
431		# since the arch-specific one will be more specific than
432		# the cpu-specific one.
433		if [ -f $setdir/ad.${MACHINE_ARCH} ]; then
434			echo $setdir/ad.${MACHINE_ARCH}
435		elif [ -f $setdir/ad.${MACHINE_CPU} ]; then
436			echo $setdir/ad.${MACHINE_CPU}
437		fi
438		if [ "$shlib" != "no" -a \
439		     -f $setdir/ad.${MACHINE_CPU}.shl ]; then
440			echo $setdir/ad.${MACHINE_CPU}.shl
441		fi
442	fi
443	if [ -f $setdir/md.${MACHINE}.${MACHINE_ARCH} ]; then
444		echo $setdir/md.${MACHINE}.${MACHINE_ARCH}
445	elif [ -f $setdir/md.${MACHINE} ]; then
446		echo $setdir/md.${MACHINE}
447	fi
448	if [ -f $setdir/stl.mi ]; then
449		echo $setdir/stl.mi
450	fi
451	if [ -f $setdir/stl.${stlib} ]; then
452		echo $setdir/stl.${stlib}
453	fi
454	if [ "$shlib" != "no" ]; then
455		if [ -f $setdir/shl.mi ]; then
456			echo $setdir/shl.mi
457		fi
458		if [ -f $setdir/shl.${shlib} ]; then
459			echo $setdir/shl.${shlib}
460		fi
461	fi
462	if [ "$module" != "no" ]; then
463		if [ -f $setdir/module.mi ]; then
464			echo $setdir/module.mi
465		fi
466		if [ -f $setdir/module.${MACHINE} ]; then
467			echo $setdir/module.${MACHINE}
468		fi
469		if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
470			# Prefer a module.ad.${MACHINE_ARCH} over a
471			# module.ad.${MACHINE_CPU}, since the arch-
472			# specific one will be more specific than the
473			# cpu-specific one.
474			if [ -f $setdir/module.ad.${MACHINE_ARCH} ]; then
475				echo $setdir/module.ad.${MACHINE_ARCH}
476			elif [ -f $setdir/module.ad.${MACHINE_CPU} ]; then
477				echo $setdir/module.ad.${MACHINE_CPU}
478			fi
479		fi
480	fi
481
482	if [ -f $setdir/rescue.mi ]; then
483		echo $setdir/rescue.mi
484	fi
485	if [ -f $setdir/rescue.${MACHINE} ]; then
486		echo $setdir/rescue.${MACHINE}
487	fi
488	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
489		# Prefer a rescue.ad.${MACHINE_ARCH} over a
490		# rescue.ad.${MACHINE_CPU}, since the arch-
491		# specific one will be more specific than the
492		# cpu-specific one.
493		if [ -f $setdir/rescue.ad.${MACHINE_ARCH} ]; then
494			echo $setdir/rescue.ad.${MACHINE_ARCH}
495		elif [ -f $setdir/rescue.ad.${MACHINE_CPU} ]; then
496			echo $setdir/rescue.ad.${MACHINE_CPU}
497		fi
498		if [ "$shlib" != "no" -a \
499		     -f $setdir/rescue.ad.${MACHINE_CPU}.shl ]; then
500			echo $setdir/rescue.ad.${MACHINE_CPU}.shl
501		fi
502	fi
503}
504
505# arch_to_cpu mach
506#
507# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
508# as determined by <bsd.own.mk>.
509#
510arch_to_cpu()
511{
512	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
513.include <bsd.own.mk>
514all:
515	@echo \${MACHINE_CPU}
516EOMAKE
517}
518
519# arch_to_endian mach
520#
521# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
522# as determined by <bsd.endian.mk>.
523#
524arch_to_endian()
525{
526	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
527.include <bsd.endian.mk>
528all:
529	@echo \${TARGET_ENDIANNESS}
530EOMAKE
531}
532