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