xref: /netbsd-src/distrib/sets/sets.subr (revision 7e30e94394d0994ab9534f68a8f91665045c91ce)
1#	$NetBSD: sets.subr,v 1.179 2017/02/08 18:21:23 christos 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#	HAVE_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
147if [ "${MKDEBUG}" = "no" -a "${MKDEBUGLIB}" = "no" ]; then
148	debugset=""
149	xdebugset=""
150else
151	debugset="debug"
152	xdebugset="xdebug"
153fi
154# Determine lib type. Do this first so stlib also gets set.
155if [ "${OBJECT_FMT}" = "ELF" ]; then
156	shlib=elf
157else
158	shlib=aout
159fi
160stlib=$shlib
161# Now check for MKPIC or specials and turn off shlib if need be.
162if [ "${MKPIC}" = "no" ]; then
163	shlib=no
164fi
165nlists="base comp $debugset etc games man misc $modset $testset text"
166xlists="xbase xcomp $xdebugset xetc xfont xserver"
167extlists="extbase extcomp extetc"
168
169OSRELEASE=$(${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh -k)
170MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
171SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
172SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
173SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
174
175#
176# list_set_files setfile [...]
177#
178# Produce a packing list for setfile(s).
179# In each file, a record consists of a path and a System Package name,
180# separated by whitespace. E.g.,
181#
182# 	# $NetBSD: sets.subr,v 1.179 2017/02/08 18:21:23 christos Exp $
183# 	.			base-sys-root	[keyword[,...]]
184# 	./altroot		base-sys-root
185# 	./bin			base-sys-root
186# 	./bin/[			base-util-root
187# 	./bin/cat		base-util-root
188#		[...]
189#
190# A # in the first column marks a comment.
191#
192# If ${obsolete} != 0, only entries with an "obsolete" keyword will
193# be printed.  All other keywords must be present.
194#
195# The third field is an optional comma separated list of keywords to
196# control if a record is printed; every keyword listed must be enabled
197# for the record to be printed. The list of all avalaible make variables
198# that can be turned on or off can be found by running in this directory:
199#
200#	make -f mkvars.mk mkvarsyesno
201#
202# These MK<NAME> variables can be used as selectors in the sets as <name>.
203#
204# The following extra keywords are also available, listed by:
205#
206#	make -f mkvars.mk mkextravars
207#
208# These are:
209#    1. The HAVE_<name>:
210#	ssp			${HAVE_SSP} != no
211#	libgcc_eh		${HAVE_LIBGCC_EH} != no
212#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
213#	gcc=<n>			<n> = value of ${HAVE_GCC}
214#	gdb=<n>			<n> = value of ${HAVE_GDB}
215#	xorg_server_ver=<n>	<n> = value of ${HAVE_XORG_SERVER_VER}
216#
217#    2. The USE_<name>:
218#	use_inet6		${USE_INET6} != no
219#	use_kerberos		${USE_KERBEROS} != no
220#	use_ldap		${USE_LDAP} != no
221#	use_yp			${USE_YP} != no
222#
223#    3. Finally:
224#	dummy			dummy entry (ignored)
225#	obsolete		file is obsolete, and only printed if
226#				${obsolete} != 0
227#
228#	solaris			${MKDTRACE} != no or ${MKZFS} != no or ${MKCTF} != no
229#
230#
231#	endian=<n>		<n> = value of ${TARGET_ENDIANNESS}
232#
233#
234#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
235#				  automatically append ".gz" to the filename
236#
237#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
238#				  automatically append ".gz" to the filename
239#
240list_set_files()
241{
242	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
243		verbose=false
244	else
245		verbose=true
246	fi
247	print_set_lists "$@" | \
248	${AWK} -v obsolete=${obsolete} '
249		BEGIN {
250			if (obsolete)
251				wanted["obsolete"] = 1
252
253			split("'"${MKVARS}"'", needvars)
254			doingcompat = 0
255			doingcompattests = 0
256			ignoredkeywords["compatdir"] = 1
257			ignoredkeywords["compatfile"] = 1
258			ignoredkeywords["compattestdir"] = 1
259			ignoredkeywords["compattestfile"] = 1
260			ignoredkeywords["compatx11dir"] = 1
261			ignoredkeywords["compatx11file"] = 1
262			for (vi in needvars) {
263				nv = needvars[vi]
264				kw = tolower(nv)
265				sub(/^mk/, "", kw)
266				sub(/^have_/, "", kw)
267				sub(/^target_endianness/, "endian", kw)
268				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
269					wanted[kw] = 1
270				}
271			}
272
273			if ("compat" in wanted) {
274				doingcompat = 1;
275				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
276				compatdirkeywords["compatdir"] = 1
277				compatfilekeywords["compatfile"] = 1
278
279				if (wanted["compattests"]) {
280					doingcompattests = 1;
281					compatdirkeywords["compattestdir"] = 1
282					compatfilekeywords["compattestfile"] = 1
283				}
284				if (wanted["compatx11"]) {
285					doingcompatx11 = 1;
286					compatdirkeywords["compatx11dir"] = 1
287					compatfilekeywords["compatx11file"] = 1
288				}
289			}
290
291			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
292				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
293				kmodpat = "./stand/" ENVIRON["MACHINE"]
294				l_kmodpat = length(kmodpat)
295			}
296
297			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
298				if ("binutils" in wanted)
299					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
300				if ("gcc" in wanted)
301					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
302				if ("gdb" in wanted)
303					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
304			}
305			if ("xorg_server_ver" in wanted) {
306				wanted["xorg_server_ver=" "'"${HAVE_XORG_SERVER_VER}"'"] = 1
307			}
308			if (("man" in wanted) && ("catpages" in wanted))
309				wanted[".cat"] = 1
310			if (("man" in wanted) && ("manpages" in wanted))
311				wanted[".man"] = 1
312			if ("endian" in wanted)
313				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
314			if ("machine" in wanted)
315				wanted["machine=" "'"${MACHINE}"'"] = 1
316			if ("machine_arch" in wanted)
317				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
318			if ("machine_cpu" in wanted)
319				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
320		}
321
322		/^#/ {
323			next;
324		}
325
326		/^-/ {
327			notwanted[substr($1, 2)] = 1;
328			delete list [substr($1, 2)];
329			next;
330		}
331
332
333		NF > 2 && $3 != "-" {
334			if (notwanted[$1] != "")
335				next;
336			split($3, keywords, ",")
337			show = 1
338			haveobs = 0
339			iscompatfile = 0
340			havekmod = 0
341			iscompatdir = 0
342			for (ki in keywords) {
343				kw = keywords[ki]
344				if (("manz" in wanted) &&
345				    (kw == ".cat" || kw == ".man"))
346					$1 = $1 ".gz"
347				if (substr(kw, 1, 1) == "!") {
348					kw = substr(kw, 2)
349					if (kw in wanted)
350						show = 0
351				} else if (kw in compatdirkeywords) {
352					iscompatdir = 1
353				} else if (kw in compatfilekeywords) {
354					iscompatfile = 1
355				} else if (kw == "nocompatmodules") {
356					havekmod = -1
357				} else if (kw in ignoredkeywords) {
358					# ignore
359				} else if (! (kw in wanted)) {
360					show = 0
361				} else if (kw == "kmod" && havekmod == 0) {
362					havekmod = 1
363				}
364				if (kw == "obsolete")
365					haveobs = 1
366			}
367			if (obsolete && ! haveobs)
368				next
369			if (!show)
370				next
371
372			list[$1] = $0
373			if (havekmod > 0 && substr($1,1,l_kmodpat) == kmodpat) {
374				for (d in kmodarchdirs) {
375					xd = "./stand/" kmodarchdirs[d]
376					xfile = xd substr($1, l_kmodpat+1)
377					tmp = xd substr($0, l_kmodpat+1)
378					list[xfile] = tmp;
379				}
380				next
381			}
382
383			if (!doingcompat || !(iscompatfile || iscompatdir))
384				next
385
386			if (iscompatfile) {
387				emitcompat[$1] = 1;
388				next
389			}
390			for (d in cpaths) {
391				if (cpaths[d] == $1 "/")
392					next
393			}
394			cpaths[ncpaths++] = $1 "/"
395			for (d in compatarchdirs) {
396				tmp = $0
397				xfile = $1 "/" compatarchdirs[d]
398				tmp = xfile substr(tmp, length($1) + 1)
399				if (xfile in notwanted)
400					continue;
401				sub("compatdir","compat",tmp);
402				sub("compattestdir","compat",tmp);
403				list[xfile] = tmp
404			}
405			next
406		}
407
408		{
409			if ($1 in notwanted)
410				next;
411			if (! obsolete)
412				list[$1] = $0
413		}
414
415		END {
416			for (i in list) {
417				print list[i]
418				if (! (i in emitcompat))
419					continue;
420				l_i = length(i)
421				l = 0
422				for (j in cpaths) {
423					lx = length(cpaths[j])
424					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
425						continue;
426					}
427					if (lx > l) {
428						l = lx;
429						cpath = cpaths[j];
430					}
431				}
432				for (d in compatarchdirs) {
433					tmp = list[i]
434					extrapath = compatarchdirs[d] "/"
435					xfile = cpath extrapath substr(i, l + 1)
436					if (xfile in notwanted)
437						continue;
438					sub("compatfile","compat",tmp);
439					sub("compattestfile","compat",tmp);
440					tmp = xfile substr(tmp, l_i + 1)
441					print tmp;
442				}
443			}
444		}'
445
446}
447
448#
449# list_set_lists setname
450#
451# Print to stdout a list of files, one filename per line, which
452# concatenate to create the packing list for setname. E.g.,
453#
454# 	.../lists/base/mi
455# 	.../lists/base/rescue.mi
456# 	.../lists/base/md.i386
457#		[...]
458#
459# For a given setname $set, the following files may be selected from
460# .../list/$set:
461#	mi
462#	mi.ext.*
463#	ad.${MACHINE_ARCH}
464# (or)	ad.${MACHINE_CPU}
465#	ad.${MACHINE_CPU}.shl
466#	md.${MACHINE}.${MACHINE_ARCH}
467# (or)	md.${MACHINE}
468#	stl.mi
469#	stl.${stlib}
470#	shl.mi
471#	shl.mi.ext.*
472#	shl.${shlib}
473#	shl.${shlib}.ext.*
474#	module.mi			if ${module} != no
475#	module.${MACHINE}		if ${module} != no
476#	module.ad.${MACHINE_ARCH}	if ${module} != no
477# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
478#	rescue.shl
479#	rescue.${MACHINE}
480#	rescue.ad.${MACHINE_ARCH}
481# (or)	rescue.ad.${MACHINE_CPU}
482# 	rescue.ad.${MACHINE_CPU}.shl
483#
484# Environment:
485# 	shlib
486# 	stlib
487#
488list_set_lists()
489{
490	setname=$1
491
492	list_set_lists_mi $setname
493	list_set_lists_ad $setname
494	list_set_lists_md $setname
495	list_set_lists_stl $setname
496	list_set_lists_shl $setname
497	list_set_lists_module $setname
498	list_set_lists_rescue $setname
499	return 0
500}
501
502list_set_lists_mi()
503{
504	setdir=$setsdir/lists/$1
505	# always exist!
506	echo $setdir/mi
507}
508
509list_set_lists_ad()
510{
511	setdir=$setsdir/lists/$1
512	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
513	list_set_lists_common_ad $1
514}
515
516list_set_lists_md()
517{
518	setdir=$setsdir/lists/$1
519	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
520	echo_if_exist $setdir/md.${MACHINE}
521}
522
523list_set_lists_stl()
524{
525	setdir=$setsdir/lists/$1
526	echo_if_exist $setdir/stl.mi
527	echo_if_exist $setdir/stl.${stlib}
528}
529
530list_set_lists_shl()
531{
532	setdir=$setsdir/lists/$1
533	[ "$shlib" != "no" ] || return
534	echo_if_exist $setdir/shl.mi
535	echo_if_exist $setdir/shl.${shlib}
536}
537
538list_set_lists_module()
539{
540	setdir=$setsdir/lists/$1
541	[ "$module" != "no" ] || return
542	echo_if_exist $setdir/module.mi
543	echo_if_exist $setdir/module.${MACHINE}
544	# XXX module never has .shl
545	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
546	list_set_lists_common_ad $1 module
547}
548
549list_set_lists_rescue()
550{
551	setdir=$setsdir/lists/$1
552	echo_if_exist $setdir/rescue.mi
553	echo_if_exist $setdir/rescue.${MACHINE}
554	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
555	list_set_lists_common_ad $1 rescue
556}
557
558list_set_lists_common_ad()
559{
560	setdir=$setsdir/lists/$1; _prefix=$2
561
562	[ -n "$_prefix" ] && prefix="$_prefix".
563
564	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
565	# <prefix>.ad.${MACHINE_CPU}, since the arch-
566	# specific one will be more specific than the
567	# cpu-specific one.
568	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
569	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
570	[ "$shlib" != "no" ] && \
571	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
572}
573
574echo_if_exist()
575{
576	[ -f $1 ] && echo $1
577	return $?
578}
579
580echo_if_exist_foreach()
581{
582	local _list=$1; shift
583	for _suffix in $@; do
584		echo_if_exist ${_list}.${_suffix}
585	done
586}
587
588print_set_lists()
589{
590	for setname; do
591		list=$(list_set_lists $setname)
592		for l in $list; do
593			echo $l
594			if $verbose; then
595				echo >&2 "DEBUG: list_set_files: $l"
596			fi
597		done
598	done | ${XARGS} ${SED} ${SUBST}
599}
600
601# arch_to_cpu mach
602#
603# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
604# as determined by <bsd.own.mk>.
605#
606arch_to_cpu()
607{
608	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
609.include <bsd.own.mk>
610all:
611	@echo \${MACHINE_CPU}
612EOMAKE
613}
614
615# arch_to_endian mach
616#
617# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
618# as determined by <bsd.endian.mk>.
619#
620arch_to_endian()
621{
622	MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
623.include <bsd.endian.mk>
624all:
625	@echo \${TARGET_ENDIANNESS}
626EOMAKE
627}
628
629#####
630
631# print_mkvars
632print_mkvars()
633{
634	for v in $MKVARS; do
635		eval echo $v=\$$v
636	done
637}
638
639# print_set_lists_{base,x,ext}
640# list_set_lists_{base,x,ext}
641# list_set_files_{base,x,ext}
642for func in print_set_lists list_set_lists list_set_files; do
643	for x in base x ext; do
644		if [ $x = base ]; then
645			list=nlists
646		else
647			list=${x}lists
648		fi
649		eval ${func}_${x} \(\) \{ $func \$$list \; \}
650	done
651done
652