xref: /netbsd-src/distrib/sets/sets.subr (revision 9aa0541bdf64142d9a27c2cf274394d60182818f)
1#	$NetBSD: sets.subr,v 1.132 2011/05/20 16:02:22 plunky 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#	extlists		list of extsrc sets
10#	obsolete		controls if obsolete files are selected instead
11#	module			if != "no", enable MODULE sets
12#	shlib			shared library format (a.out, elf, or "")
13#	stlib			static library format (a.out, elf)
14#
15# The following <bsd.own.mk> variables are exported to the environment:
16#	MACHINE
17#	MACHINE_ARCH
18#	MACHINE_CPU
19#	HAVE_BINUTILS
20#	HAVE_GCC
21#	HAVE_GDB
22#	HAS_SSP
23#	TOOLCHAIN_MISSING
24#	OBJECT_FMT
25# as well as:
26#
27
28#
29# The following variables refer to tools that are used when building sets:
30#
31: ${AWK:=awk}
32: ${CKSUM:=cksum}
33: ${COMM:=comm}
34: ${DATE:=date}
35: ${DB:=db}
36: ${EGREP:=egrep}
37: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
38: ${FGREP:=fgrep}
39: ${FIND:=find}
40: ${GREP:=grep}
41: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
42: ${HOSTNAME_CMD:=hostname}	# ${HOSTNAME} is special to bash(1)
43: ${HOST_SH:=sh}
44: ${IDENT:=ident}
45: ${JOIN:=join}
46: ${LS:=ls}
47: ${MAKE:=make}
48: ${MKTEMP:=mktemp}
49: ${MTREE:=mtree}
50: ${PASTE:=paste}
51: ${PAX:=pax}
52: ${PRINTF:=printf}
53: ${SED:=sed}
54: ${SORT:=sort}
55: ${STAT:=stat}
56: ${TSORT:=tsort}
57: ${UNAME:=uname}
58: ${WC:=wc}
59: ${XARGS:=xargs}
60
61#
62# If printf is a shell builtin command, then we can
63# implement cheaper versions of basename and dirname
64# that do not involve any fork/exec overhead.
65# If printf is not builtin, approximate it using echo,
66# and hope there are no weird file names that cause
67# some versions of echo to do the wrong thing.
68# (Converting to this version of dirname speeded up the
69# syspkgdeps script by an order of magnitude, from 68
70# seconds to 6.3 seconds on one particular host.)
71#
72# Note that naive approximations for dirname
73# using ${foo%/*} do not do the right thing in cases
74# where the result should be "/" or ".".
75#
76case "$(type printf)" in
77*builtin*)
78	basename ()
79	{
80		local bn
81		bn="${1##*/}"
82		bn="${bn%$2}"
83		printf "%s\n" "$bn"
84	}
85	dirname ()
86	{
87		local dn
88		case "$1" in
89		?*/*)	dn="${1%/*}" ;;
90		/*)	dn=/ ;;
91		*)	dn=. ;;
92		esac
93		printf "%s\n" "$dn"
94	}
95	;;
96*)
97	basename ()
98	{
99		local bn
100		bn="${1##*/}"
101		bn="${bn%$2}"
102		echo "$bn"
103	}
104	dirname ()
105	{
106		local dn
107		case "$1" in
108		?*/*)	dn="${1%/*}" ;;
109		/*)	dn=/ ;;
110		*)	dn=. ;;
111		esac
112		echo "$dn"
113	}
114	;;
115esac
116
117#####
118
119oIFS=$IFS
120IFS="
121"
122
123for x in $( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars ); do
124	eval export $x
125done
126
127IFS=$oIFS
128
129MKVARS="$( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars | ${SED} -e 's,=.*,,' | ${XARGS} )"
130
131#####
132
133setsdir=${rundir}
134obsolete=0
135if [ "${MKKMOD}" = "no" ]; then
136	module=no			# MODULEs are off.
137	modset=""
138else
139	module=yes
140	modset="modules"
141fi
142if [ "${MKATF}" = "no" ]; then
143	testset=""
144else
145	testset="tests"
146fi
147# Determine lib type. Do this first so stlib also gets set.
148if [ "${OBJECT_FMT}" = "ELF" ]; then
149	shlib=elf
150else
151	shlib=aout
152fi
153stlib=$shlib
154# Now check for MKPIC or specials and turn off shlib if need be.
155if [ "${MKPIC}" = "no" ]; then
156	shlib=no
157fi
158nlists="base comp etc games man misc $modset $testset text"
159xlists="xbase xcomp xetc xfont xserver"
160extlists="extbase extcomp extetc"
161
162OSRELEASE=`${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh`
163MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
164SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
165SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
166SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
167
168#
169# list_set_files setfile [...]
170#
171# Produce a packing list for setfile(s).
172# In each file, a record consists of a path and a System Package name,
173# separated by whitespace. E.g.,
174#
175# 	# $NetBSD: sets.subr,v 1.132 2011/05/20 16:02:22 plunky Exp $
176# 	.			base-sys-root	[keyword[,...]]
177# 	./altroot		base-sys-root
178# 	./bin			base-sys-root
179# 	./bin/[			base-util-root
180# 	./bin/cat		base-util-root
181#		[...]
182#
183# A # in the first column marks a comment.
184#
185# If ${obsolete} != 0, only entries with an "obsolete" keyword will
186# be printed.  All other keywords must be present.
187#
188# The third field is an optional comma separated list of keywords to
189# control if a record is printed; every keyword listed must be enabled
190# for the record to be printed.  The following keywords are available:
191#	dummy			dummy entry (ignored)
192#	obsolete		file is obsolete, and only printed if
193#				${obsolete} != 0
194#
195#	atf			${MKATF} != no
196#	bfd			obsolete, use binutils.
197#	binutils		${MKBINUTILS} != no
198#	bsdgrep			${MKBSDGREP} != no
199#	catpages		${MKCATPAGES} != no
200#	compat			${MKCOMPAT} != no
201#	crypto			${MKCRYPTO} != no
202#	crypto_idea		${MKCRYPTO_IDEA} != no
203#	crypto_mdc2		${MKCRYPTO_MDC2} != no
204#	crypto_rc5		${MKCRYPTO_RC5} != no
205#	cvs			${MKCVS} != no
206#	debug			${MKDEBUG} != no
207#	debuglib		${MKDEBUGLIB} != no
208#	doc			${MKDOC} != no
209#	dtrace			${MKDTRACE} != no
210#	dynamicroot		${MKDYNAMICROOT} != no
211#	extsrc			${MKEXTSRC} != no
212#	gcc			${MKGCC} != no
213#	gcccmds			${MKGCCCMDS} != no
214#	gdb			${MKGDB} != no
215#	hesiod			${MKHESIOD} != no
216#	html			${MKHTML} != no
217#	inet6			${MKINET6} != no
218#	info			${MKINFO} != no
219#	ipfilter		${MKIPFILTER} != no
220#	iscsi			${MKISCSI} != no
221#	kerberos		${MKKERBEROS} != no
222#	kmod			${MKKMOD} != no
223#	ldap			${MKLDAP} != no
224#	lint			${MKLINT} != no
225#	llvm			${MKLLVM} != no
226#	lvm			${MKLVM} != no
227#	man			${MKMAN} != no
228#	manpages		${MKMANPAGES} != no
229#	manz			${MKMANZ} != no
230#	mdns			${MKMDNS} != no
231#	nls			${MKNLS} != no
232#	nvi			${MKNVI} != no
233#	pam			${MKPAM} != no
234#	pcc			${MKPCC} != no
235#	pf			${MKPF} != no
236#	pic			${MKPIC} != no
237#	postfix			${MKPOSTFIX} != no
238#	profile			${MKPROFILE} != no
239#	perfuse			${MKPERFUSE} != no
240#	share			${MKSHARE} != no
241#	skey			${MKSKEY} != no
242#	solaris			${MKDTRACE} != no or ${MKZFS} != no
243#	ssp			${HAS_SSP} != no
244#	x11			${MKX11} != no && ${X11FLAVOUR} != "Xorg"
245#	xorg			${MKX11} != no && ${X11FLAVOUR} == "Xorg"
246#	yp			${MKYP} != no
247#	zfs			${MKZFS} != no
248#
249#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
250#	gcc=<n>			<n> = value of ${HAVE_GCC}
251#	gdb=<n>			<n> = value of ${HAVE_GDB}
252#
253#	use_inet6		${USE_INET6} != no
254#	use_kerberos		${USE_KERBEROS} != no
255#	use_yp			${USE_YP} != no
256#
257#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
258#				  automatically append ".gz" to the filename
259#
260#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
261#				  automatically append ".gz" to the filename
262#
263list_set_files()
264{
265	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
266		verbose=false
267	else
268		verbose=true
269	fi
270	print_set_lists "$@" | \
271	${AWK} -v obsolete=${obsolete} '
272		BEGIN {
273			if (obsolete)
274				wanted["obsolete"] = 1
275
276			split("'"${MKVARS}"'", needvars)
277			for (vi in needvars) {
278				nv = needvars[vi]
279				kw = tolower(nv)
280				sub(/^mk/, "", kw)
281				sub(/^has_/, "", kw)
282				if (ENVIRON[nv] != "no")
283					wanted[kw] = 1
284			}
285
286			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
287				if ("binutils" in wanted)
288					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
289				if ("gcc" in wanted)
290					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
291				if ("gdb" in wanted)
292					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
293			}
294			if (("man" in wanted) && ("catpages" in wanted))
295				wanted[".cat"] = 1
296			if (("man" in wanted) && ("manpages" in wanted))
297				wanted[".man"] = 1
298		}
299
300		/^#/ {
301			next;
302		}
303
304		/^-/ {
305			notwanted[substr($1, 2)] = 1;
306			delete list [substr($1, 2)];
307			next;
308		}
309
310
311		NF > 2 && $3 != "-" {
312			if (notwanted[$1] != "")
313				next;
314			split($3, keywords, ",")
315			show = 1
316			haveobs = 0
317			for (ki in keywords) {
318				kw = keywords[ki]
319				if (("manz" in wanted) &&
320				    (kw == ".cat" || kw == ".man"))
321					$1 = $1 ".gz"
322				negated = match(kw, "! *")
323				if (negated > 0) {
324					kw = substr(kw, RSTART + RLENGTH)
325					if (kw in wanted)
326						show = 0
327				} else {
328					if (! (kw in wanted))
329						show = 0
330				}
331				if (kw == "obsolete")
332					haveobs = 1
333			}
334			if (obsolete && ! haveobs)
335				next
336			if (show)
337				list[$1] = $0
338			next
339		}
340
341		{
342			if (notwanted[$1] != "")
343				next;
344			if (! obsolete)
345				list[$1] = $0
346		}
347
348		END {
349			for (i in list) {
350				print list[i]
351			}
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#	mi.ext.*
371#	ad.${MACHINE_ARCH}
372# (or)	ad.${MACHINE_CPU}
373#	ad.${MACHINE_CPU}.shl
374#	md.${MACHINE}.${MACHINE_ARCH}
375# (or)	md.${MACHINE}
376#	stl.mi
377#	stl.${stlib}
378#	shl.mi
379#	shl.mi.ext.*
380#	shl.${shlib}
381#	shl.${shlib}.ext.*
382#	module.mi			if ${module} != no
383#	module.${MACHINE}		if ${module} != no
384#	module.ad.${MACHINE_ARCH}	if ${module} != no
385# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
386#	rescue.shl
387#	rescue.${MACHINE}
388#	rescue.ad.${MACHINE_ARCH}
389# (or)	rescue.ad.${MACHINE_CPU}
390# 	rescue.ad.${MACHINE_CPU}.shl
391#
392# Environment:
393# 	shlib
394# 	stlib
395#
396list_set_lists()
397{
398	setname=$1
399
400	list_set_lists_mi $setname
401	list_set_lists_ad $setname
402	list_set_lists_md $setname
403	list_set_lists_stl $setname
404	list_set_lists_shl $setname
405	list_set_lists_module $setname
406	list_set_lists_rescue $setname
407	return 0
408}
409
410list_set_lists_mi()
411{
412	setdir=$setsdir/lists/$1
413	# always exist!
414	echo $setdir/mi
415}
416
417list_set_lists_ad()
418{
419	setdir=$setsdir/lists/$1
420	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
421	list_set_lists_common_ad $1
422}
423
424list_set_lists_md()
425{
426	setdir=$setsdir/lists/$1
427	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
428	echo_if_exist $setdir/md.${MACHINE}
429}
430
431list_set_lists_stl()
432{
433	setdir=$setsdir/lists/$1
434	echo_if_exist $setdir/stl.mi
435	echo_if_exist $setdir/stl.${stlib}
436}
437
438list_set_lists_shl()
439{
440	setdir=$setsdir/lists/$1
441	[ "$shlib" != "no" ] || return
442	echo_if_exist $setdir/shl.mi
443	echo_if_exist $setdir/shl.${shlib}
444}
445
446list_set_lists_module()
447{
448	setdir=$setsdir/lists/$1
449	[ "$module" != "no" ] || return
450	echo_if_exist $setdir/module.mi
451	echo_if_exist $setdir/module.${MACHINE}
452	# XXX module never has .shl
453	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
454	list_set_lists_common_ad $1 module
455}
456
457list_set_lists_rescue()
458{
459	setdir=$setsdir/lists/$1
460	echo_if_exist $setdir/rescue.mi
461	echo_if_exist $setdir/rescue.${MACHINE}
462	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
463	list_set_lists_common_ad $1 rescue
464}
465
466list_set_lists_common_ad()
467{
468	setdir=$setsdir/lists/$1; _prefix=$2
469
470	[ -n "$_prefix" ] && prefix="$_prefix".
471
472	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
473	# <prefix>.ad.${MACHINE_CPU}, since the arch-
474	# specific one will be more specific than the
475	# cpu-specific one.
476	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
477	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
478	[ "$shlib" != "no" ] && \
479	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
480}
481
482echo_if_exist()
483{
484	[ -f $1 ] && echo $1
485	return $?
486}
487
488echo_if_exist_foreach()
489{
490	local _list=$1; shift
491	for _suffix in $@; do
492		echo_if_exist ${_list}.${_suffix}
493	done
494}
495
496print_set_lists()
497{
498	for setname; do
499		list=`list_set_lists $setname`
500		for l in $list; do
501			echo $l
502			if $verbose; then
503				echo >&2 "DEBUG: list_set_files: $l"
504			fi
505		done
506	done | ${XARGS} ${SED} ${SUBST}
507}
508
509# arch_to_cpu mach
510#
511# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
512# as determined by <bsd.own.mk>.
513#
514arch_to_cpu()
515{
516	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
517.include <bsd.own.mk>
518all:
519	@echo \${MACHINE_CPU}
520EOMAKE
521}
522
523# arch_to_endian mach
524#
525# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
526# as determined by <bsd.endian.mk>.
527#
528arch_to_endian()
529{
530	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
531.include <bsd.endian.mk>
532all:
533	@echo \${TARGET_ENDIANNESS}
534EOMAKE
535}
536
537#####
538
539# print_mkvars
540print_mkvars()
541{
542	for v in $MKVARS; do
543		eval echo $v=\$$v
544	done
545}
546
547# print_set_lists_{base,x,ext}
548# list_set_lists_{base,x,ext}
549# list_set_files_{base,x,ext}
550for func in print_set_lists list_set_lists list_set_files; do
551	for x in base x ext; do
552		if [ $x = base ]; then
553			list=nlists
554		else
555			list=${x}lists
556		fi
557		eval ${func}_${x} \(\) \{ $func \$$list \; \}
558	done
559done
560