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