sets.subr revision 1.55
1#	$NetBSD: sets.subr,v 1.55 2006/12/29 15:52:20 pooka 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	MKNLS		\
52	MKPAM		\
53	MKPF		\
54	MKPIC		\
55	MKPOSTFIX	\
56	MKPROFILE	\
57	MKPUFFS		\
58	MKSENDMAIL	\
59	MKSHARE		\
60	MKSKEY		\
61	MKUUCP		\
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 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.55 2006/12/29 15:52:20 pooka 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#	nls			${MKNLS} != no
264#	pam			${MKPAM} != no
265#	pf			${MKPF} != no
266#	postfix			${MKPOSTFIX} != no
267#	profile			${MKPROFILE} != no
268#	puffs			${MKPUFFS} != no
269#	sendmail		${MKSENDMAIL} != no
270#	share			${MKSHARE} != no
271#	skey			${MKSKEY} != no
272#	yp			${MKYP} != no
273#
274#	gcc=<n>			<n> = value of ${HAVE_GCC}
275#	gdb=<n>			<n> = value of ${HAVE_GDB}
276#
277#	use_inet6		${USE_INET6} != no
278#	use_kerberos		${USE_KERBEROS} != no
279#	use_yp			${USE_YP} != no
280#
281#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
282#				  automatically append ".gz" to the filename
283#
284#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
285#				  automatically append ".gz" to the filename
286#
287list_set_files()
288{
289	for setname; do
290		list_set_lists $setname
291	done | xargs cat | \
292	${AWK} -v obsolete=${obsolete} '
293		BEGIN {
294			if (obsolete)
295				wanted["obsolete"] = 1
296		
297			split("'"${MKVARS}"'", needvars)
298			for (vi in needvars) {
299				nv = needvars[vi]
300				kw = tolower(nv)
301				sub(/^mk/, "", kw)
302				if (ENVIRON[nv] != "no")
303					wanted[kw] = 1 
304			}
305
306			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
307				wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
308				wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
309			}
310			if (("man" in wanted) && ("catpages" in wanted))
311				wanted[".cat"] = 1
312			if (("man" in wanted) && ("manpages" in wanted))
313				wanted[".man"] = 1
314		}
315
316		/^#/ {
317			next;
318		}
319
320		NF > 2 && $3 != "-" {
321			split($3, keywords, ",")
322			show = 1
323			haveobs = 0
324			for (ki in keywords) {
325				kw = keywords[ki]
326				if (("manz" in wanted) &&
327				    (kw == ".cat" || kw == ".man"))
328					$1 = $1 ".gz"
329				if (! (kw in wanted))
330					show = 0
331				if (kw == "obsolete")
332					haveobs = 1
333			}
334			if (obsolete && ! haveobs)
335				next
336			if (show)
337				print
338			next
339		}
340
341		{
342			if (! obsolete)
343				print
344		}'
345
346}
347
348#
349# list_set_lists setname
350# 
351# Print to stdout a list of files, one filename per line, which
352# concatenate to create the packing list for setname. E.g.,
353#
354# 	.../lists/base/mi
355# 	.../lists/base/rescue.mi
356# 	.../lists/base/md.i386
357#		[...]
358#
359# For a given setname $set, the following files may be selected from
360# .../list/$set:
361#	mi
362#	ad.${MACHINE_ARCH}
363# (or)	ad.${MACHINE_CPU}
364#	ad.${MACHINE_CPU}.shl
365#	md.${MACHINE}.${MACHINE_ARCH}
366# (or)	md.${MACHINE}
367#	stl.mi
368#	stl.stlib
369#	shl.mi
370#	shl.shlib
371#	lkm.mi			if ${lkm} != no
372#	rescue.shl
373#	rescue.${MACHINE}
374#	rescue.ad.${MACHINE_ARCH}
375# (or)	rescue.ad.${MACHINE_CPU}
376# 	rescue.ad.${MACHINE_CPU}.shl
377#
378# Environment:
379# 	shlib
380# 	stlib
381#
382list_set_lists()
383{
384	setname=$1
385
386	setdir=$setsdir/lists/$setname
387	echo $setdir/mi
388	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
389		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
390		# since the arch-specific one will be more specific than
391		# the cpu-specific one.
392		if [ -f $setdir/ad.${MACHINE_ARCH} ]; then
393			echo $setdir/ad.${MACHINE_ARCH}
394		elif [ -f $setdir/ad.${MACHINE_CPU} ]; then
395			echo $setdir/ad.${MACHINE_CPU}
396		fi
397		if [ "$shlib" != "no" -a \
398		     -f $setdir/ad.${MACHINE_CPU}.shl ]; then
399			echo $setdir/ad.${MACHINE_CPU}.shl
400		fi
401	fi
402	if [ -f $setdir/md.${MACHINE}.${MACHINE_ARCH} ]; then
403		echo $setdir/md.${MACHINE}.${MACHINE_ARCH}
404	elif [ -f $setdir/md.${MACHINE} ]; then
405		echo $setdir/md.${MACHINE}
406	fi
407	if [ -f $setdir/stl.mi ]; then
408		echo $setdir/stl.mi
409	fi
410	if [ -f $setdir/stl.${stlib} ]; then
411		echo $setdir/stl.${stlib}
412	fi
413	if [ "$shlib" != "no" ]; then
414		if [ -f $setdir/shl.mi ]; then
415			echo $setdir/shl.mi
416		fi
417		if [ -f $setdir/shl.${shlib} ]; then
418			echo $setdir/shl.${shlib}
419		fi
420	fi
421	if [ "$lkm" != "no" ]; then
422		if [ -f $setdir/lkm.mi ]; then
423			echo $setdir/lkm.mi
424		fi
425	fi
426
427	if [ -f $setdir/rescue.mi ]; then
428		echo $setdir/rescue.mi
429	fi
430	if [ -f $setdir/rescue.${MACHINE} ]; then
431		echo $setdir/rescue.${MACHINE}
432	fi
433	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
434		# Prefer a rescue.ad.${MACHINE_ARCH} over a
435		# rescue.ad.${MACHINE_CPU}, since the arch-
436		# specific one will be more specific than the
437		# cpu-specific one.
438		if [ -f $setdir/rescue.ad.${MACHINE_ARCH} ]; then
439			echo $setdir/rescue.ad.${MACHINE_ARCH}
440		elif [ -f $setdir/rescue.ad.${MACHINE_CPU} ]; then
441			echo $setdir/rescue.ad.${MACHINE_CPU}
442		fi
443		if [ "$shlib" != "no" -a \
444		     -f $setdir/rescue.ad.${MACHINE_CPU}.shl ]; then
445			echo $setdir/rescue.ad.${MACHINE_CPU}.shl
446		fi
447	fi
448}
449
450# arch_to_cpu mach
451#
452# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
453# as determined by <bsd.own.mk>.
454#
455arch_to_cpu()
456{
457	MACHINE_ARCH=${1} ${MAKE} -f- all <<EOMAKE
458.include <bsd.own.mk>
459all:
460	@echo \${MACHINE_CPU}
461EOMAKE
462}
463
464# arch_to_endian mach
465#
466# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
467# as determined by <bsd.endian.mk>.
468#
469arch_to_endian()
470{
471	MACHINE_ARCH=${1} ${MAKE} -f- all <<EOMAKE
472.include <bsd.endian.mk>
473all:
474	@echo \${TARGET_ENDIANNESS}
475EOMAKE
476}
477