sets.subr revision 1.61
1#	$NetBSD: sets.subr,v 1.61 2008/05/20 15:18:29 ad 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
215OSRELEASE=`${HOST_SH} ../../sys/conf/osrelease.sh`
216MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
217
218#
219# list_set_files setfile [...]
220# 
221# Produce a packing list for setfile(s).
222# In each file, a record consists of a path and a System Package name,
223# separated by whitespace. E.g.,
224#
225# 	# $NetBSD: sets.subr,v 1.61 2008/05/20 15:18:29 ad Exp $
226# 	.			base-sys-root	[keyword[,...]]
227# 	./altroot		base-sys-root
228# 	./bin			base-sys-root
229# 	./bin/[			base-util-root
230# 	./bin/cat		base-util-root
231#		[...]
232#
233# A # in the first column marks a comment.
234#
235# If ${obsolete} != 0, only entries with an "obsolete" keyword will
236# be printed.  All other keywords must be present.
237#
238# The third field is an optional comma separated list of keywords to
239# control if a record is printed; every keyword listed must be enabled
240# for the record to be printed.  The following keywords are available:
241#	dummy			dummy entry (ignored)
242#	obsolete		file is obsolete, and only printed if 
243#				${obsolete} != 0
244#
245#	bfd			${MKBFD} != no
246#	catpages		${MKCATPAGES} != no
247#	crypto			${MKCRYPTO} != no
248#	crypto_idea		${MKCRYPTO_IDEA} != no
249#	crypto_mdc2		${MKCRYPTO_MDC2} != no
250#	crypto_rc5		${MKCRYPTO_RC5} != no
251#	cvs			${MKCVS} != no
252#	debug			${MKDEBUG} != no
253#	doc			${MKDOC} != no
254#	gcc			${MKGCC} != no
255#	gcccmds			${MKGCCCMDS} != no
256#	gdb			${MKGDB} != no
257#	hesiod			${MKHESIOD} != no
258#	info			${MKINFO} != no
259#	ipfilter		${MKIPFILTER} != no
260#	inet6			${MKINET6} != no
261#	iscsi			${MKISCSI} != no
262#	kerberos		${MKKERBEROS} != no
263#	lint			${MKLINT} != no
264#	man			${MKMAN} != no
265#	manz			${MKMANZ} != no
266#	modular			${MKMODULAR} != no
267#	nls			${MKNLS} != no
268#	pam			${MKPAM} != no
269#	pf			${MKPF} != no
270#	postfix			${MKPOSTFIX} != no
271#	profile			${MKPROFILE} != no
272#	puffs			${MKPUFFS} != no
273#	sendmail		${MKSENDMAIL} != no
274#	share			${MKSHARE} != no
275#	skey			${MKSKEY} != no
276#	yp			${MKYP} != no
277#
278#	gcc=<n>			<n> = value of ${HAVE_GCC}
279#	gdb=<n>			<n> = value of ${HAVE_GDB}
280#
281#	use_inet6		${USE_INET6} != no
282#	use_kerberos		${USE_KERBEROS} != no
283#	use_yp			${USE_YP} != no
284#
285#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
286#				  automatically append ".gz" to the filename
287#
288#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
289#				  automatically append ".gz" to the filename
290#
291list_set_files()
292{
293	for setname; do
294		list_set_lists $setname
295	done | xargs cat | ${SED} "s#@MODULEDIR@#${MODULEDIR}#g" | \
296	${AWK} -v obsolete=${obsolete} '
297		BEGIN {
298			if (obsolete)
299				wanted["obsolete"] = 1
300		
301			split("'"${MKVARS}"'", needvars)
302			for (vi in needvars) {
303				nv = needvars[vi]
304				kw = tolower(nv)
305				sub(/^mk/, "", kw)
306				if (ENVIRON[nv] != "no")
307					wanted[kw] = 1 
308			}
309
310			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
311				wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
312				wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
313			}
314			if (("man" in wanted) && ("catpages" in wanted))
315				wanted[".cat"] = 1
316			if (("man" in wanted) && ("manpages" in wanted))
317				wanted[".man"] = 1
318		}
319
320		/^#/ {
321			next;
322		}
323
324		NF > 2 && $3 != "-" {
325			split($3, keywords, ",")
326			show = 1
327			haveobs = 0
328			for (ki in keywords) {
329				kw = keywords[ki]
330				if (("manz" in wanted) &&
331				    (kw == ".cat" || kw == ".man"))
332					$1 = $1 ".gz"
333				negated = match(kw, "! *")
334				if (negated > 0) {
335					kw = substr(kw, RSTART + RLENGTH)
336					if (kw in wanted)
337						show = 0
338				} else {
339					if (! (kw in wanted))
340						show = 0
341				}
342				if (kw == "obsolete")
343					haveobs = 1
344			}
345			if (obsolete && ! haveobs)
346				next
347			if (show)
348				print
349			next
350		}
351
352		{
353			if (! obsolete)
354				print
355		}'
356
357}
358
359#
360# list_set_lists setname
361# 
362# Print to stdout a list of files, one filename per line, which
363# concatenate to create the packing list for setname. E.g.,
364#
365# 	.../lists/base/mi
366# 	.../lists/base/rescue.mi
367# 	.../lists/base/md.i386
368#		[...]
369#
370# For a given setname $set, the following files may be selected from
371# .../list/$set:
372#	mi
373#	ad.${MACHINE_ARCH}
374# (or)	ad.${MACHINE_CPU}
375#	ad.${MACHINE_CPU}.shl
376#	md.${MACHINE}.${MACHINE_ARCH}
377# (or)	md.${MACHINE}
378#	stl.mi
379#	stl.stlib
380#	shl.mi
381#	shl.shlib
382#	lkm.mi			if ${lkm} != no
383#	rescue.shl
384#	rescue.${MACHINE}
385#	rescue.ad.${MACHINE_ARCH}
386# (or)	rescue.ad.${MACHINE_CPU}
387# 	rescue.ad.${MACHINE_CPU}.shl
388#
389# Environment:
390# 	shlib
391# 	stlib
392#
393list_set_lists()
394{
395	setname=$1
396
397	setdir=$setsdir/lists/$setname
398	echo $setdir/mi
399	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
400		# Prefer an ad.${MACHINE_ARCH} over an ad.${MACHINE_CPU},
401		# since the arch-specific one will be more specific than
402		# the cpu-specific one.
403		if [ -f $setdir/ad.${MACHINE_ARCH} ]; then
404			echo $setdir/ad.${MACHINE_ARCH}
405		elif [ -f $setdir/ad.${MACHINE_CPU} ]; then
406			echo $setdir/ad.${MACHINE_CPU}
407		fi
408		if [ "$shlib" != "no" -a \
409		     -f $setdir/ad.${MACHINE_CPU}.shl ]; then
410			echo $setdir/ad.${MACHINE_CPU}.shl
411		fi
412	fi
413	if [ -f $setdir/md.${MACHINE}.${MACHINE_ARCH} ]; then
414		echo $setdir/md.${MACHINE}.${MACHINE_ARCH}
415	elif [ -f $setdir/md.${MACHINE} ]; then
416		echo $setdir/md.${MACHINE}
417	fi
418	if [ -f $setdir/stl.mi ]; then
419		echo $setdir/stl.mi
420	fi
421	if [ -f $setdir/stl.${stlib} ]; then
422		echo $setdir/stl.${stlib}
423	fi
424	if [ "$shlib" != "no" ]; then
425		if [ -f $setdir/shl.mi ]; then
426			echo $setdir/shl.mi
427		fi
428		if [ -f $setdir/shl.${shlib} ]; then
429			echo $setdir/shl.${shlib}
430		fi
431	fi
432	if [ "$lkm" != "no" ]; then
433		if [ -f $setdir/lkm.mi ]; then
434			echo $setdir/lkm.mi
435		fi
436	fi
437
438	if [ -f $setdir/rescue.mi ]; then
439		echo $setdir/rescue.mi
440	fi
441	if [ -f $setdir/rescue.${MACHINE} ]; then
442		echo $setdir/rescue.${MACHINE}
443	fi
444	if [ "${MACHINE}" != "${MACHINE_ARCH}" ]; then
445		# Prefer a rescue.ad.${MACHINE_ARCH} over a
446		# rescue.ad.${MACHINE_CPU}, since the arch-
447		# specific one will be more specific than the
448		# cpu-specific one.
449		if [ -f $setdir/rescue.ad.${MACHINE_ARCH} ]; then
450			echo $setdir/rescue.ad.${MACHINE_ARCH}
451		elif [ -f $setdir/rescue.ad.${MACHINE_CPU} ]; then
452			echo $setdir/rescue.ad.${MACHINE_CPU}
453		fi
454		if [ "$shlib" != "no" -a \
455		     -f $setdir/rescue.ad.${MACHINE_CPU}.shl ]; then
456			echo $setdir/rescue.ad.${MACHINE_CPU}.shl
457		fi
458	fi
459}
460
461# arch_to_cpu mach
462#
463# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
464# as determined by <bsd.own.mk>.
465#
466arch_to_cpu()
467{
468	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
469.include <bsd.own.mk>
470all:
471	@echo \${MACHINE_CPU}
472EOMAKE
473}
474
475# arch_to_endian mach
476#
477# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
478# as determined by <bsd.endian.mk>.
479#
480arch_to_endian()
481{
482	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
483.include <bsd.endian.mk>
484all:
485	@echo \${TARGET_ENDIANNESS}
486EOMAKE
487}
488