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