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