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