sets.subr revision 1.110
1#	$NetBSD: sets.subr,v 1.110 2009/12/11 12:57:39 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
129if [ "$SETS_SUBR_DEBUG" = "dumpmkvars" ]; then
130	for v in $MKVARS; do
131		eval echo $v=\$$v
132	done
133	exit 0
134fi
135
136#####
137
138setsdir=${0%/*}
139obsolete=0
140module=yes
141if [ "${MKKMOD}" = "no" ]; then
142	module=no			# MODULEs are off.
143elif [ "${MACHINE}" = "evbppc" ]; then
144	module=no			# Turn off MODULEs for some ports.
145fi
146# Determine lib type. Do this first so stlib also gets set.
147if [ "${OBJECT_FMT}" = "ELF" ]; then
148	shlib=elf
149else
150	shlib=aout
151fi
152stlib=$shlib
153# Now check for MKPIC or specials and turn off shlib if need be.
154if [ "${MKPIC}" = "no" ]; then
155	shlib=no
156fi
157if [ "${MACHINE_ARCH}" = "m68000" ]; then
158	shlib=no			# Turn off shlibs for some ports.
159fi
160if [ "$module" != "no" ]; then
161	nlists="base comp etc games man misc modules tests text"
162else
163	nlists="base comp etc games man misc tests text"
164fi
165xlists="xbase xcomp xetc xfont xserver"
166extlists="extbase extcomp extetc"
167
168OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
169MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
170SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
171SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
172SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
173
174#
175# list_set_files setfile [...]
176# 
177# Produce a packing list for setfile(s).
178# In each file, a record consists of a path and a System Package name,
179# separated by whitespace. E.g.,
180#
181# 	# $NetBSD: sets.subr,v 1.110 2009/12/11 12:57:39 uebayasi Exp $
182# 	.			base-sys-root	[keyword[,...]]
183# 	./altroot		base-sys-root
184# 	./bin			base-sys-root
185# 	./bin/[			base-util-root
186# 	./bin/cat		base-util-root
187#		[...]
188#
189# A # in the first column marks a comment.
190#
191# If ${obsolete} != 0, only entries with an "obsolete" keyword will
192# be printed.  All other keywords must be present.
193#
194# The third field is an optional comma separated list of keywords to
195# control if a record is printed; every keyword listed must be enabled
196# for the record to be printed.  The following keywords are available:
197#	dummy			dummy entry (ignored)
198#	obsolete		file is obsolete, and only printed if 
199#				${obsolete} != 0
200#
201#	bfd			obsolete, use binutils.
202#	binutils		${MKBINUTILS} != no
203#	catpages		${MKCATPAGES} != no
204#	compat			${MKCOMPAT} != no
205#	crypto			${MKCRYPTO} != no
206#	crypto_idea		${MKCRYPTO_IDEA} != no
207#	crypto_mdc2		${MKCRYPTO_MDC2} != no
208#	crypto_rc5		${MKCRYPTO_RC5} != no
209#	cvs			${MKCVS} != no
210#	debug			${MKDEBUG} != no
211#	debuglib		${MKDEBUGLIB} != no
212#	doc			${MKDOC} != no
213#	dynamicroot		${MKDYNAMICROOT} != no
214#	extsrc			${MKEXTSRC} != no
215#	gcc			${MKGCC} != no
216#	gcccmds			${MKGCCCMDS} != no
217#	gdb			${MKGDB} != no
218#	hesiod			${MKHESIOD} != no
219#	html			${MKHTML} != no
220#	inet6			${MKINET6} != no
221#	info			${MKINFO} != no
222#	ipfilter		${MKIPFILTER} != no
223#	iscsi			${MKISCSI} != no
224#	kerberos		${MKKERBEROS} != no
225#	kmod			${MKKMOD} != no
226#	ldap			${MKLDAP} != no
227#	lint			${MKLINT} != no
228#	lvm			${MKLVM} != no
229#	man			${MKMAN} != no
230#	manpages		${MKMANPAGES} != no
231#	manz			${MKMANZ} != no
232#	mdns			${MKMDNS} != no
233#	nls			${MKNLS} != no
234#	nvi			${MKNVI} != no
235#	pam			${MKPAM} != no
236#	pf			${MKPF} != no
237#	pic			${MKPIC} != no
238#	postfix			${MKPOSTFIX} != no
239#	profile			${MKPROFILE} != no
240#	share			${MKSHARE} != no
241#	skey			${MKSKEY} != no
242#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
243#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
244#	yp			${MKYP} != no
245#	zfs			${MKZFS} != no
246#
247#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
248#	gcc=<n>			<n> = value of ${HAVE_GCC}
249#	gdb=<n>			<n> = value of ${HAVE_GDB}
250#
251#	use_inet6		${USE_INET6} != no
252#	use_kerberos		${USE_KERBEROS} != no
253#	use_yp			${USE_YP} != no
254#
255#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
256#				  automatically append ".gz" to the filename
257#
258#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
259#				  automatically append ".gz" to the filename
260#
261list_set_files()
262{
263	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
264		verbose=false
265	else
266		verbose=true
267	fi
268	for setname; do
269		list=`list_set_lists $setname`
270		for l in $list; do
271			echo $l
272			if $verbose; then
273				echo >&2 "DEBUG: list_set_files: $l"
274			fi
275		done
276	done | xargs cat | ${SED} ${SUBST} | \
277	${AWK} -v obsolete=${obsolete} '
278		BEGIN {
279			if (obsolete)
280				wanted["obsolete"] = 1
281		
282			split("'"${MKVARS}"'", needvars)
283			for (vi in needvars) {
284				nv = needvars[vi]
285				kw = tolower(nv)
286				sub(/^mk/, "", kw)
287				if (ENVIRON[nv] != "no")
288					wanted[kw] = 1 
289			}
290
291			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
292				if ("binutils" in wanted)
293					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
294				if ("gcc" in wanted)
295					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
296				if ("gdb" in wanted)
297					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
298			}
299			if (("man" in wanted) && ("catpages" in wanted))
300				wanted[".cat"] = 1
301			if (("man" in wanted) && ("manpages" in wanted))
302				wanted[".man"] = 1
303		}
304
305		/^#/ {
306			next;
307		}
308
309		NF > 2 && $3 != "-" {
310			split($3, keywords, ",")
311			show = 1
312			haveobs = 0
313			for (ki in keywords) {
314				kw = keywords[ki]
315				if (("manz" in wanted) &&
316				    (kw == ".cat" || kw == ".man"))
317					$1 = $1 ".gz"
318				negated = match(kw, "! *")
319				if (negated > 0) {
320					kw = substr(kw, RSTART + RLENGTH)
321					if (kw in wanted)
322						show = 0
323				} else {
324					if (! (kw in wanted))
325						show = 0
326				}
327				if (kw == "obsolete")
328					haveobs = 1
329			}
330			if (obsolete && ! haveobs)
331				next
332			if (show)
333				print
334			next
335		}
336
337		{
338			if (! obsolete)
339				print
340		}'
341
342}
343
344#
345# list_set_lists setname
346# 
347# Print to stdout a list of files, one filename per line, which
348# concatenate to create the packing list for setname. E.g.,
349#
350# 	.../lists/base/mi
351# 	.../lists/base/rescue.mi
352# 	.../lists/base/md.i386
353#		[...]
354#
355# For a given setname $set, the following files may be selected from
356# .../list/$set:
357#	mi
358#	mi.ext.*
359#	ad.${MACHINE_ARCH}
360# (or)	ad.${MACHINE_CPU}
361#	ad.${MACHINE_CPU}.shl
362#	md.${MACHINE}.${MACHINE_ARCH}
363# (or)	md.${MACHINE}
364#	stl.mi
365#	stl.${stlib}
366#	shl.mi
367#	shl.mi.ext.*
368#	shl.${shlib}
369#	shl.${shlib}.ext.*
370#	module.mi			if ${module} != no
371#	module.${MACHINE}		if ${module} != no
372#	module.ad.${MACHINE_ARCH}	if ${module} != no
373# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
374#	rescue.shl
375#	rescue.${MACHINE}
376#	rescue.ad.${MACHINE_ARCH}
377# (or)	rescue.ad.${MACHINE_CPU}
378# 	rescue.ad.${MACHINE_CPU}.shl
379#
380# Environment:
381# 	shlib
382# 	stlib
383#
384list_set_lists()
385{
386	setname=$1
387
388	setdir=$setsdir/lists/$setname
389
390	echo $setdir/mi
391
392	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
393		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
394		# since the arch-specific one will be more specific than
395		# the cpu-specific one.
396		echo_if_exist $setdir/ad.${MACHINE_ARCH} || \
397		echo_if_exist $setdir/ad.${MACHINE_CPU}
398		if [ "$shlib" != "no" ]; then
399			echo_if_exist $setdir/ad.${MACHINE_CPU}.shl
400		fi
401	fi
402
403	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
404	echo_if_exist $setdir/md.${MACHINE}
405
406	echo_if_exist $setdir/stl.mi
407	echo_if_exist $setdir/stl.${stlib}
408
409	if [ "$shlib" != "no" ]; then
410		echo_if_exist $setdir/shl.mi
411		echo_if_exist $setdir/shl.${shlib}
412	fi
413
414	if [ "$module" != "no" ]; then
415		echo_if_exist $setdir/module.mi
416		echo_if_exist $setdir/module.${MACHINE}
417		if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
418			# Prefer a module.ad.${MACHINE_ARCH} over a
419			# module.ad.${MACHINE_CPU}, since the arch-
420			# specific one will be more specific than the
421			# cpu-specific one.
422			echo_if_exist $setdir/module.ad.${MACHINE_ARCH} || \
423			echo_if_exist $setdir/module.ad.${MACHINE_CPU}
424		fi
425	fi
426
427	echo_if_exist $setdir/rescue.mi
428	echo_if_exist $setdir/rescue.${MACHINE}
429	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
430		# Prefer a rescue.ad.${MACHINE_ARCH} over a
431		# rescue.ad.${MACHINE_CPU}, since the arch-
432		# specific one will be more specific than the
433		# cpu-specific one.
434		echo_if_exist $setdir/rescue.ad.${MACHINE_ARCH} || \
435		echo_if_exist $setdir/rescue.ad.${MACHINE_CPU}
436		if [ "$shlib" != "no" ]; then
437			echo_if_exist $setdir/rescue.ad.${MACHINE_CPU}.shl
438		fi
439	fi
440}
441
442echo_if_exist()
443{
444	[ -f $1 ] && echo $1
445	return $?
446}
447
448echo_if_exist_foreach()
449{
450	local _list=$1; shift
451	for _suffix in $@; do
452		echo_if_exist ${_list}.${_suffix}
453	done
454}
455
456# arch_to_cpu mach
457#
458# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
459# as determined by <bsd.own.mk>.
460#
461arch_to_cpu()
462{
463	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
464.include <bsd.own.mk>
465all:
466	@echo \${MACHINE_CPU}
467EOMAKE
468}
469
470# arch_to_endian mach
471#
472# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
473# as determined by <bsd.endian.mk>.
474#
475arch_to_endian()
476{
477	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
478.include <bsd.endian.mk>
479all:
480	@echo \${TARGET_ENDIANNESS}
481EOMAKE
482}
483