sets.subr revision 1.80
1#	$NetBSD: sets.subr,v 1.80 2009/01/24 00:29:12 rmind 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	MKLDAP		\
48	MKLINT		\
49	MKLVM		\
50	MKMAN		\
51	MKMANPAGES	\
52	MKMANZ		\
53	MKNLS		\
54	MKNVI		\
55	MKPAM		\
56	MKPF		\
57	MKPIC		\
58	MKPOSTFIX	\
59	MKPROFILE	\
60	MKSHARE		\
61	MKSKEY		\
62	MKX11		\
63	MKXORG		\
64	MKYP		\
65	USE_INET6	\
66	USE_KERBEROS	\
67	USE_LDAP	\
68	USE_YP		\
69	NETBSDSRCDIR	\
70	MAKEVERBOSE	\
71"
72#
73# The following variables refer to tools that are used when building sets:
74#
75: ${AWK:=awk}
76: ${CKSUM:=cksum}
77: ${COMM:=comm}
78: ${DATE:=date}
79: ${DB:=db}
80: ${EGREP:=egrep}
81: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
82: ${FGREP:=fgrep}
83: ${FIND:=find}
84: ${GREP:=grep}
85: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
86: ${HOSTNAME:=hostname}
87: ${HOST_SH:=sh}
88: ${IDENT:=ident}
89: ${JOIN:=join}
90: ${LS:=ls}
91: ${MAKE:=make}
92: ${MKTEMP:=mktemp}
93: ${MTREE:=mtree}
94: ${PASTE:=paste}
95: ${PAX:=pax}
96: ${PKG_CREATE:=pkg_create}
97: ${PRINTF:=printf}
98: ${SED:=sed}
99: ${SORT:=sort}
100: ${STAT:=stat}
101: ${TSORT:=tsort}
102: ${UNAME:=uname}
103: ${WC:=wc}
104
105#
106# If printf is a shell builtin command, then we can
107# implement cheaper versions of basename and dirname
108# that do not involve any fork/exec overhead.
109# If printf is not builtin, approximate it using echo,
110# and hope there are no weird file names that cause
111# some versions of echo to do the wrong thing.
112# (Converting to this version of dirname speeded up the
113# syspkgdeps script by an order of magnitude, from 68
114# seconds to 6.3 seconds on one particular host.)
115#
116# Note that naive approximations for dirname
117# using ${foo%/*} do not do the right thing in cases
118# where the result should be "/" or ".".
119#
120case "$(type printf)" in
121*builtin*)
122	basename ()
123	{
124		local bn
125		bn="${1##*/}"
126		bn="${bn%$2}"
127		printf "%s\n" "$bn"
128	}
129	dirname ()
130	{
131		local dn
132		case "$1" in
133		?*/*)	dn="${1%/*}" ;;
134		/*)	dn=/ ;;
135		*)	dn=. ;;
136		esac
137		printf "%s\n" "$dn"
138	}
139	;;
140*)
141	basename ()
142	{
143		local bn
144		bn="${1##*/}"
145		bn="${bn%$2}"
146		echo "$bn"
147	}
148	dirname ()
149	{
150		local dn
151		case "$1" in
152		?*/*)	dn="${1%/*}" ;;
153		/*)	dn=/ ;;
154		*)	dn=. ;;
155		esac
156		echo "$dn"
157	}
158	;;
159esac
160
161oIFS=$IFS
162IFS="
163"
164for x in $(
165${MAKE} -B -f- all <<EOMAKE
166.include <bsd.own.mk>
167.if (\${MKMAN} == "no" || empty(MANINSTALL:Mmaninstall))
168MKMANPAGES=no
169.else
170MKMANPAGES=yes
171.endif
172.if \${MKX11} != "no"
173. if \${X11FLAVOUR} == "Xorg"
174MKXORG:=yes
175MKX11:=no
176. else
177MKXORG:=no
178. endif
179.endif
180all:
181.for i in MACHINE MACHINE_ARCH MACHINE_CPU \
182		HAVE_GCC HAVE_GDB OBJECT_FMT TOOLCHAIN_MISSING \
183		${MKVARS}
184	@echo "export \$i=\${\$i}"
185.endfor
186
187EOMAKE
188); do
189#	echo 1>&2 "DEBUG: read $x"
190	eval $x
191done
192IFS=$oIFS
193
194setsdir=${0%/*}
195nlists="base comp etc games man misc tests text"
196xlists="xbase xcomp xetc xfont xserver"
197obsolete=0
198module=yes
199if [ "${MACHINE}" = "evbppc" ]; then
200	module=no			# Turn off MODULEs for some ports.
201fi
202# Determine lib type. Do this first so stlib also gets set.
203if [ "${OBJECT_FMT}" = "ELF" ]; then
204	shlib=elf
205else
206	shlib=aout
207fi
208stlib=$shlib
209# Now check for MKPIC or specials and turn off shlib if need be.
210if [ "${MKPIC}" = "no" ]; then
211	shlib=no
212fi
213if [ "${MACHINE_ARCH}" = "m68000" ]; then
214	shlib=no			# Turn off shlibs for some ports.
215fi
216
217OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
218MODULEDIR="kernel/${MACHINE}/${OSRELEASE}/modules"
219SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
220SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
221SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
222
223#
224# list_set_files setfile [...]
225# 
226# Produce a packing list for setfile(s).
227# In each file, a record consists of a path and a System Package name,
228# separated by whitespace. E.g.,
229#
230# 	# $NetBSD: sets.subr,v 1.80 2009/01/24 00:29:12 rmind Exp $
231# 	.			base-sys-root	[keyword[,...]]
232# 	./altroot		base-sys-root
233# 	./bin			base-sys-root
234# 	./bin/[			base-util-root
235# 	./bin/cat		base-util-root
236#		[...]
237#
238# A # in the first column marks a comment.
239#
240# If ${obsolete} != 0, only entries with an "obsolete" keyword will
241# be printed.  All other keywords must be present.
242#
243# The third field is an optional comma separated list of keywords to
244# control if a record is printed; every keyword listed must be enabled
245# for the record to be printed.  The following keywords are available:
246#	dummy			dummy entry (ignored)
247#	obsolete		file is obsolete, and only printed if 
248#				${obsolete} != 0
249#
250#	bfd			${MKBFD} != no
251#	catpages		${MKCATPAGES} != no
252#	compat			${MKCOMPAT} != no
253#	crypto			${MKCRYPTO} != no
254#	crypto_idea		${MKCRYPTO_IDEA} != no
255#	crypto_mdc2		${MKCRYPTO_MDC2} != no
256#	crypto_rc5		${MKCRYPTO_RC5} != no
257#	cvs			${MKCVS} != no
258#	debug			${MKDEBUG} != no
259#	debuglib		${MKDEBUGLIB} != no
260#	doc			${MKDOC} != no
261#	dynamicroot		${MKDYNAMICROOT} != no
262#	gcc			${MKGCC} != no
263#	gcccmds			${MKGCCCMDS} != no
264#	gdb			${MKGDB} != no
265#	hesiod			${MKHESIOD} != no
266#	html			${MKHTML} != no
267#	inet6			${MKINET6} != no
268#	info			${MKINFO} != no
269#	ipfilter		${MKIPFILTER} != no
270#	iscsi			${MKISCSI} != no
271#	kerberos		${MKKERBEROS} != no
272#	ldap			${MKLDAP} != no
273#	lint			${MKLINT} != no
274#	lvm			${MKLVM} != no
275#	man			${MKMAN} != no
276#	manpages		${MKMANPAGES} != no
277#	manz			${MKMANZ} != no
278#	nls			${MKNLS} != no
279#	nvi			${MKNVI} != no
280#	pam			${MKPAM} != no
281#	pf			${MKPF} != no
282#	pic			${MKPIC} != no
283#	postfix			${MKPOSTFIX} != no
284#	profile			${MKPROFILE} != no
285#	share			${MKSHARE} != no
286#	skey			${MKSKEY} != no
287#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
288#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
289#	yp			${MKYP} != no
290#
291#	gcc=<n>			<n> = value of ${HAVE_GCC}
292#	gdb=<n>			<n> = value of ${HAVE_GDB}
293#
294#	use_inet6		${USE_INET6} != no
295#	use_kerberos		${USE_KERBEROS} != no
296#	use_yp			${USE_YP} != no
297#
298#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
299#				  automatically append ".gz" to the filename
300#
301#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
302#				  automatically append ".gz" to the filename
303#
304list_set_files()
305{
306	if [ ${MAKEVERBOSE:-2} -lt 2 ]; then
307		verbose=false
308	else
309		verbose=true
310	fi
311	for setname; do
312		list=`list_set_lists $setname`
313		for l in $list; do
314			echo $l
315			if $verbose; then
316				echo $l >&2
317			fi
318		done
319	done | xargs cat | ${SED} ${SUBST} | \
320	${AWK} -v obsolete=${obsolete} '
321		BEGIN {
322			if (obsolete)
323				wanted["obsolete"] = 1
324		
325			split("'"${MKVARS}"'", needvars)
326			for (vi in needvars) {
327				nv = needvars[vi]
328				kw = tolower(nv)
329				sub(/^mk/, "", kw)
330				if (ENVIRON[nv] != "no")
331					wanted[kw] = 1 
332			}
333
334			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
335				wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
336				wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
337			}
338			if (("man" in wanted) && ("catpages" in wanted))
339				wanted[".cat"] = 1
340			if (("man" in wanted) && ("manpages" in wanted))
341				wanted[".man"] = 1
342		}
343
344		/^#/ {
345			next;
346		}
347
348		NF > 2 && $3 != "-" {
349			split($3, keywords, ",")
350			show = 1
351			haveobs = 0
352			for (ki in keywords) {
353				kw = keywords[ki]
354				if (("manz" in wanted) &&
355				    (kw == ".cat" || kw == ".man"))
356					$1 = $1 ".gz"
357				negated = match(kw, "! *")
358				if (negated > 0) {
359					kw = substr(kw, RSTART + RLENGTH)
360					if (kw in wanted)
361						show = 0
362				} else {
363					if (! (kw in wanted))
364						show = 0
365				}
366				if (kw == "obsolete")
367					haveobs = 1
368			}
369			if (obsolete && ! haveobs)
370				next
371			if (show)
372				print
373			next
374		}
375
376		{
377			if (! obsolete)
378				print
379		}'
380
381}
382
383#
384# list_set_lists setname
385# 
386# Print to stdout a list of files, one filename per line, which
387# concatenate to create the packing list for setname. E.g.,
388#
389# 	.../lists/base/mi
390# 	.../lists/base/rescue.mi
391# 	.../lists/base/md.i386
392#		[...]
393#
394# For a given setname $set, the following files may be selected from
395# .../list/$set:
396#	mi
397#	ad.${MACHINE_ARCH}
398# (or)	ad.${MACHINE_CPU}
399#	ad.${MACHINE_CPU}.shl
400#	md.${MACHINE}.${MACHINE_ARCH}
401# (or)	md.${MACHINE}
402#	stl.mi
403#	stl.stlib
404#	shl.mi
405#	shl.shlib
406#	module.mi			if ${module} != no
407#	module.${MACHINE}		if ${module} != no
408#	module.ad.${MACHINE_ARCH}	if ${module} != no
409# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
410#	rescue.shl
411#	rescue.${MACHINE}
412#	rescue.ad.${MACHINE_ARCH}
413# (or)	rescue.ad.${MACHINE_CPU}
414# 	rescue.ad.${MACHINE_CPU}.shl
415#
416# Environment:
417# 	shlib
418# 	stlib
419#
420list_set_lists()
421{
422	setname=$1
423
424	setdir=$setsdir/lists/$setname
425	echo $setdir/mi
426	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
427		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
428		# since the arch-specific one will be more specific than
429		# the cpu-specific one.
430		if [ -f $setdir/ad.${MACHINE_ARCH} ]; then
431			echo $setdir/ad.${MACHINE_ARCH}
432		elif [ -f $setdir/ad.${MACHINE_CPU} ]; then
433			echo $setdir/ad.${MACHINE_CPU}
434		fi
435		if [ "$shlib" != "no" -a \
436		     -f $setdir/ad.${MACHINE_CPU}.shl ]; then
437			echo $setdir/ad.${MACHINE_CPU}.shl
438		fi
439	fi
440	if [ -f $setdir/md.${MACHINE}.${MACHINE_ARCH} ]; then
441		echo $setdir/md.${MACHINE}.${MACHINE_ARCH}
442	elif [ -f $setdir/md.${MACHINE} ]; then
443		echo $setdir/md.${MACHINE}
444	fi
445	if [ -f $setdir/stl.mi ]; then
446		echo $setdir/stl.mi
447	fi
448	if [ -f $setdir/stl.${stlib} ]; then
449		echo $setdir/stl.${stlib}
450	fi
451	if [ "$shlib" != "no" ]; then
452		if [ -f $setdir/shl.mi ]; then
453			echo $setdir/shl.mi
454		fi
455		if [ -f $setdir/shl.${shlib} ]; then
456			echo $setdir/shl.${shlib}
457		fi
458	fi
459	if [ "$module" != "no" ]; then
460		if [ -f $setdir/module.mi ]; then
461			echo $setdir/module.mi
462		fi
463		if [ -f $setdir/module.${MACHINE} ]; then
464			echo $setdir/module.${MACHINE}
465		fi
466		if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
467			# Prefer a module.ad.${MACHINE_ARCH} over a
468			# module.ad.${MACHINE_CPU}, since the arch-
469			# specific one will be more specific than the
470			# cpu-specific one.
471			if [ -f $setdir/module.ad.${MACHINE_ARCH} ]; then
472				echo $setdir/module.ad.${MACHINE_ARCH}
473			elif [ -f $setdir/module.ad.${MACHINE_CPU} ]; then
474				echo $setdir/module.ad.${MACHINE_CPU}
475			fi
476		fi
477	fi
478
479	if [ -f $setdir/rescue.mi ]; then
480		echo $setdir/rescue.mi
481	fi
482	if [ -f $setdir/rescue.${MACHINE} ]; then
483		echo $setdir/rescue.${MACHINE}
484	fi
485	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
486		# Prefer a rescue.ad.${MACHINE_ARCH} over a
487		# rescue.ad.${MACHINE_CPU}, since the arch-
488		# specific one will be more specific than the
489		# cpu-specific one.
490		if [ -f $setdir/rescue.ad.${MACHINE_ARCH} ]; then
491			echo $setdir/rescue.ad.${MACHINE_ARCH}
492		elif [ -f $setdir/rescue.ad.${MACHINE_CPU} ]; then
493			echo $setdir/rescue.ad.${MACHINE_CPU}
494		fi
495		if [ "$shlib" != "no" -a \
496		     -f $setdir/rescue.ad.${MACHINE_CPU}.shl ]; then
497			echo $setdir/rescue.ad.${MACHINE_CPU}.shl
498		fi
499	fi
500}
501
502# arch_to_cpu mach
503#
504# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
505# as determined by <bsd.own.mk>.
506#
507arch_to_cpu()
508{
509	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
510.include <bsd.own.mk>
511all:
512	@echo \${MACHINE_CPU}
513EOMAKE
514}
515
516# arch_to_endian mach
517#
518# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
519# as determined by <bsd.endian.mk>.
520#
521arch_to_endian()
522{
523	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
524.include <bsd.endian.mk>
525all:
526	@echo \${TARGET_ENDIANNESS}
527EOMAKE
528}
529