sets.subr revision 1.183
1#	$NetBSD: sets.subr,v 1.183 2018/02/08 03:27:50 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#	HAVE_OPENSSL
24#	TOOLCHAIN_MISSING
25#	OBJECT_FMT
26# as well as:
27#
28
29#
30# The following variables refer to tools that are used when building sets:
31#
32: ${AWK:=awk}
33: ${CKSUM:=cksum}
34: ${COMM:=comm}
35: ${DATE:=date}
36: ${DB:=db}
37: ${EGREP:=egrep}
38: ${ENV_CMD:=env}       # ${ENV} is special to sh(1), ksh(1), etc.
39: ${FGREP:=fgrep}
40: ${FIND:=find}
41: ${GREP:=grep}
42: ${GZIP_CMD:=gzip}     # ${GZIP} is special to gzip(1)
43: ${HOSTNAME_CMD:=hostname}	# ${HOSTNAME} is special to bash(1)
44: ${HOST_SH:=sh}
45: ${IDENT:=ident}
46: ${JOIN:=join}
47: ${LS:=ls}
48: ${MAKE:=make}
49: ${MKTEMP:=mktemp}
50: ${MTREE:=mtree}
51: ${PASTE:=paste}
52: ${PAX:=pax}
53: ${PRINTF:=printf}
54: ${SED:=sed}
55: ${SORT:=sort}
56: ${STAT:=stat}
57: ${TSORT:=tsort}
58: ${UNAME:=uname}
59: ${WC:=wc}
60: ${XARGS:=xargs}
61
62#
63# If printf is a shell builtin command, then we can
64# implement cheaper versions of basename and dirname
65# that do not involve any fork/exec overhead.
66# If printf is not builtin, approximate it using echo,
67# and hope there are no weird file names that cause
68# some versions of echo to do the wrong thing.
69# (Converting to this version of dirname speeded up the
70# syspkgdeps script by an order of magnitude, from 68
71# seconds to 6.3 seconds on one particular host.)
72#
73# Note that naive approximations for dirname
74# using ${foo%/*} do not do the right thing in cases
75# where the result should be "/" or ".".
76#
77case "$(type printf)" in
78*builtin*)
79	basename ()
80	{
81		local bn
82		bn="${1##*/}"
83		bn="${bn%$2}"
84		printf "%s\n" "$bn"
85	}
86	dirname ()
87	{
88		local dn
89		case "$1" in
90		?*/*)	dn="${1%/*}" ;;
91		/*)	dn=/ ;;
92		*)	dn=. ;;
93		esac
94		printf "%s\n" "$dn"
95	}
96	;;
97*)
98	basename ()
99	{
100		local bn
101		bn="${1##*/}"
102		bn="${bn%$2}"
103		echo "$bn"
104	}
105	dirname ()
106	{
107		local dn
108		case "$1" in
109		?*/*)	dn="${1%/*}" ;;
110		/*)	dn=/ ;;
111		*)	dn=. ;;
112		esac
113		echo "$dn"
114	}
115	;;
116esac
117
118#####
119
120oIFS=$IFS
121IFS="
122"
123
124for x in $( MAKEVERBOSE= ${MAKE} -B -f ${rundir}/mkvars.mk mkvars ); do
125	eval export $x
126done
127
128IFS=$oIFS
129
130MKVARS="$( MAKEVERBOSE= ${MAKE} -B -f ${rundir}/mkvars.mk mkvars | ${SED} -e 's,=.*,,' | ${XARGS} )"
131
132#####
133
134setsdir=${rundir}
135obsolete=0
136if [ "${MKKMOD}" = "no" ]; then
137	module=no			# MODULEs are off.
138	modset=""
139else
140	module=yes
141	modset="modules"
142fi
143if [ "${MKATF}" = "no" ]; then
144	testset=""
145else
146	testset="tests"
147fi
148if [ "${MKDEBUG}" = "no" -a "${MKDEBUGLIB}" = "no" ]; then
149	debugset=""
150	xdebugset=""
151else
152	debugset="debug"
153	xdebugset="xdebug"
154fi
155# Determine lib type. Do this first so stlib also gets set.
156if [ "${OBJECT_FMT}" = "ELF" ]; then
157	shlib=elf
158else
159	shlib=aout
160fi
161stlib=$shlib
162# Now check for MKPIC or specials and turn off shlib if need be.
163if [ "${MKPIC}" = "no" ]; then
164	shlib=no
165fi
166nlists="base comp $debugset etc games man misc $modset $testset text"
167xlists="xbase xcomp $xdebugset xetc xfont xserver"
168extlists="extbase extcomp extetc"
169
170OSRELEASE=$(${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh -k)
171MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
172SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
173SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
174SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
175
176#
177# list_set_files setfile [...]
178# 
179# Produce a packing list for setfile(s).
180# In each file, a record consists of a path and a System Package name,
181# separated by whitespace. E.g.,
182#
183# 	# $NetBSD: sets.subr,v 1.183 2018/02/08 03:27:50 christos Exp $
184# 	.			base-sys-root	[keyword[,...]]
185# 	./altroot		base-sys-root
186# 	./bin			base-sys-root
187# 	./bin/[			base-util-root
188# 	./bin/cat		base-util-root
189#		[...]
190#
191# A # in the first column marks a comment.
192#
193# If ${obsolete} != 0, only entries with an "obsolete" keyword will
194# be printed.  All other keywords must be present.
195#
196# The third field is an optional comma separated list of keywords to
197# control if a record is printed; every keyword listed must be enabled
198# for the record to be printed. The list of all avalaible make variables
199# that can be turned on or off can be found by running in this directory:
200#
201#	make -f mkvars.mk mkvarsyesno
202#
203# These MK<NAME> variables can be used as selectors in the sets as <name>.
204# 
205# The following extra keywords are also available, listed by:
206#
207#	make -f mkvars.mk mkextravars
208#
209# These are:
210#    1. The HAVE_<name>:
211#	ssp			${HAVE_SSP} != no
212#	libgcc_eh		${HAVE_LIBGCC_EH} != no
213#	binutils=<n>		<n> = value of ${HAVE_BINUTILS}
214#	gcc=<n>			<n> = value of ${HAVE_GCC}
215#	gdb=<n>			<n> = value of ${HAVE_GDB}
216#	openssl=<n>		<n> = value of ${HAVE_OPENSSL}
217#	xorg_server_ver=<n>	<n> = value of ${HAVE_XORG_SERVER_VER}
218#
219#    2. The USE_<name>:
220#	use_inet6		${USE_INET6} != no
221#	use_kerberos		${USE_KERBEROS} != no
222#	use_ldap		${USE_LDAP} != no
223#	use_yp			${USE_YP} != no
224#
225#    3. Finally:
226#	dummy			dummy entry (ignored)
227#	obsolete		file is obsolete, and only printed if 
228#				${obsolete} != 0
229#
230#	solaris			${MKDTRACE} != no or ${MKZFS} != no or ${MKCTF} != no
231#
232#
233#	endian=<n>		<n> = value of ${TARGET_ENDIANNESS}
234#
235#
236#	.cat			if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
237#				  automatically append ".gz" to the filename
238#
239#	.man			if ${MKMANZ} != "no" && ${MKMAN} != "no"
240#				  automatically append ".gz" to the filename
241#
242list_set_files()
243{
244	if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
245		verbose=false
246	else
247		verbose=true
248	fi
249	print_set_lists "$@" | \
250	${AWK} -v obsolete=${obsolete} '
251		BEGIN {
252			if (obsolete)
253				wanted["obsolete"] = 1
254		
255			split("'"${MKVARS}"'", needvars)
256			doingcompat = 0
257			doingcompattests = 0
258			ignoredkeywords["compatdir"] = 1
259			ignoredkeywords["compatfile"] = 1
260			ignoredkeywords["compattestdir"] = 1
261			ignoredkeywords["compattestfile"] = 1
262			ignoredkeywords["compatx11dir"] = 1
263			ignoredkeywords["compatx11file"] = 1
264			for (vi in needvars) {
265				nv = needvars[vi]
266				kw = tolower(nv)
267				sub(/^mk/, "", kw)
268				sub(/^have_/, "", kw)
269				sub(/^target_endianness/, "endian", kw)
270				if (nv != "HAVE_GCC" && nv != "HAVE_GDB" && ENVIRON[nv] != "no" && nv != "COMPATARCHDIRS" && nv != "KMODARCHDIRS") {
271					wanted[kw] = 1 
272				}
273			}
274
275			if ("compat" in wanted) {
276				doingcompat = 1;
277				split("'"${COMPATARCHDIRS}"'", compatarchdirs, ",");
278				compatdirkeywords["compatdir"] = 1
279				compatfilekeywords["compatfile"] = 1
280
281				if (wanted["compattests"]) {
282					doingcompattests = 1;
283					compatdirkeywords["compattestdir"] = 1
284					compatfilekeywords["compattestfile"] = 1
285				}
286				if (wanted["compatx11"]) {
287					doingcompatx11 = 1;
288					compatdirkeywords["compatx11dir"] = 1
289					compatfilekeywords["compatx11file"] = 1
290				}
291			}
292
293			if (("kmod" in wanted) && ("compatmodules" in wanted)) {
294				split("'"${KMODARCHDIRS}"'", kmodarchdirs, ",");
295				kmodpat = "./stand/" ENVIRON["MACHINE"]
296				l_kmodpat = length(kmodpat)
297			}
298
299			if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
300				if ("binutils" in wanted)
301					wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
302				if ("gcc" in wanted)
303					wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
304				if ("gdb" in wanted)
305					wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
306			}
307			if ("openssl" in wanted) {
308				wanted["openssl=" "'"${HAVE_OPENSSL}"'"] = 1
309			}
310			if ("xorg_server_ver" in wanted) {
311				wanted["xorg_server_ver=" "'"${HAVE_XORG_SERVER_VER}"'"] = 1
312			}
313			if (("man" in wanted) && ("catpages" in wanted))
314				wanted[".cat"] = 1
315			if (("man" in wanted) && ("manpages" in wanted))
316				wanted[".man"] = 1
317			if ("endian" in wanted)
318				wanted["endian=" "'"${TARGET_ENDIANNESS}"'"] = 1
319			if ("machine" in wanted)
320				wanted["machine=" "'"${MACHINE}"'"] = 1
321			if ("machine_arch" in wanted)
322				wanted["machine_arch=" "'"${MACHINE_ARCH}"'"] = 1
323			if ("machine_cpu" in wanted)
324				wanted["machine_cpu=" "'"${MACHINE_CPU}"'"] = 1
325		}
326
327		/^#/ {
328			next;
329		}
330
331		/^-/ {
332			notwanted[substr($1, 2)] = 1;
333			delete list [substr($1, 2)];
334			next;
335		}
336				
337
338		NF > 2 && $3 != "-" {
339			if (notwanted[$1] != "")
340				next;
341			split($3, keywords, ",")
342			show = 1
343			haveobs = 0
344			iscompatfile = 0
345			havekmod = 0
346			iscompatdir = 0
347			for (ki in keywords) {
348				kw = keywords[ki]
349				if (("manz" in wanted) &&
350				    (kw == ".cat" || kw == ".man"))
351					$1 = $1 ".gz"
352				if (substr(kw, 1, 1) == "!") {
353					kw = substr(kw, 2)
354					if (kw in wanted)
355						show = 0
356				} else if (kw in compatdirkeywords) {
357					iscompatdir = 1
358				} else if (kw in compatfilekeywords) {
359					iscompatfile = 1
360				} else if (kw == "nocompatmodules") {
361					havekmod = -1
362				} else if (kw in ignoredkeywords) {
363					# ignore
364				} else if (! (kw in wanted)) {
365					show = 0
366				} else if (kw == "kmod" && havekmod == 0) {
367					havekmod = 1
368				}
369				if (kw == "obsolete")
370					haveobs = 1
371			}
372			if (obsolete && ! haveobs)
373				next
374			if (!show)
375				next
376
377			list[$1] = $0
378			if (havekmod > 0 && substr($1,1,l_kmodpat) == kmodpat) {
379				for (d in kmodarchdirs) {
380					xd = "./stand/" kmodarchdirs[d]
381					xfile = xd substr($1, l_kmodpat+1)
382					tmp = xd substr($0, l_kmodpat+1)
383					list[xfile] = tmp;
384				}
385				next
386			}
387
388			if (!doingcompat || !(iscompatfile || iscompatdir))
389				next
390
391			if (iscompatfile) {
392				emitcompat[$1] = 1;
393				next
394			}
395			for (d in cpaths) {
396				if (cpaths[d] == $1 "/")
397					next
398			}
399			cpaths[ncpaths++] = $1 "/"
400			for (d in compatarchdirs) {
401				tmp = $0
402				xfile = $1 "/" compatarchdirs[d]
403				tmp = xfile substr(tmp, length($1) + 1)
404				if (xfile in notwanted)
405					continue;
406				sub("compatdir","compat",tmp);
407				sub("compattestdir","compat",tmp);
408				list[xfile] = tmp
409			}
410			next
411		}
412
413		{
414			if ($1 in notwanted)
415				next;
416			if (! obsolete)
417				list[$1] = $0
418		}
419
420		END {
421			for (i in list) {
422				print list[i]
423				if (! (i in emitcompat))
424					continue;
425				l_i = length(i)
426				l = 0
427				for (j in cpaths) {
428					lx = length(cpaths[j])
429					if (lx >= l_i || cpaths[j] != substr(i, 1, lx)) {
430						continue;
431					}
432					if (lx > l) {
433						l = lx;
434						cpath = cpaths[j];
435					}
436				}
437				for (d in compatarchdirs) {
438					tmp = list[i]
439					extrapath = compatarchdirs[d] "/"
440					xfile = cpath extrapath substr(i, l + 1)
441					if (xfile in notwanted)
442						continue;
443					sub("compatfile","compat",tmp);
444					sub("compattestfile","compat",tmp);
445					tmp = xfile substr(tmp, l_i + 1)
446					print tmp;
447				}
448			}
449		}'
450
451}
452
453#
454# list_set_lists setname
455# 
456# Print to stdout a list of files, one filename per line, which
457# concatenate to create the packing list for setname. E.g.,
458#
459# 	.../lists/base/mi
460# 	.../lists/base/rescue.mi
461# 	.../lists/base/md.i386
462#		[...]
463#
464# For a given setname $set, the following files may be selected from
465# .../list/$set:
466#	mi
467#	mi.ext.*
468#	ad.${MACHINE_ARCH}
469# (or)	ad.${MACHINE_CPU}
470#	ad.${MACHINE_CPU}.shl
471#	md.${MACHINE}.${MACHINE_ARCH}
472# (or)	md.${MACHINE}
473#	stl.mi
474#	stl.${stlib}
475#	shl.mi
476#	shl.mi.ext.*
477#	shl.${shlib}
478#	shl.${shlib}.ext.*
479#	module.mi			if ${module} != no
480#	module.${MACHINE}		if ${module} != no
481#	module.ad.${MACHINE_ARCH}	if ${module} != no
482# (or)	module.ad.${MACHINE_CPU}	if ${module} != no
483#	rescue.shl
484#	rescue.${MACHINE}
485#	rescue.ad.${MACHINE_ARCH}
486# (or)	rescue.ad.${MACHINE_CPU}
487# 	rescue.ad.${MACHINE_CPU}.shl
488#
489# Environment:
490# 	shlib
491# 	stlib
492#
493list_set_lists()
494{
495	setname=$1
496
497	list_set_lists_mi $setname
498	list_set_lists_ad $setname
499	list_set_lists_md $setname
500	list_set_lists_stl $setname
501	list_set_lists_shl $setname
502	list_set_lists_module $setname
503	list_set_lists_rescue $setname
504	return 0
505}
506
507list_set_lists_mi()
508{
509	setdir=$setsdir/lists/$1
510	# always exist!
511	echo $setdir/mi
512}
513
514list_set_lists_ad()
515{
516	setdir=$setsdir/lists/$1
517	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
518	list_set_lists_common_ad $1
519}
520
521list_set_lists_md()
522{
523	setdir=$setsdir/lists/$1
524	echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
525	echo_if_exist $setdir/md.${MACHINE}
526}
527
528list_set_lists_stl()
529{
530	setdir=$setsdir/lists/$1
531	echo_if_exist $setdir/stl.mi
532	echo_if_exist $setdir/stl.${stlib}
533}
534
535list_set_lists_shl()
536{
537	setdir=$setsdir/lists/$1
538	[ "$shlib" != "no" ] || return
539	echo_if_exist $setdir/shl.mi
540	echo_if_exist $setdir/shl.${shlib}
541}
542
543list_set_lists_module()
544{
545	setdir=$setsdir/lists/$1
546	[ "$module" != "no" ] || return
547	echo_if_exist $setdir/module.mi
548	echo_if_exist $setdir/module.${MACHINE}
549	# XXX module never has .shl
550	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
551	list_set_lists_common_ad $1 module
552}
553
554list_set_lists_rescue()
555{
556	setdir=$setsdir/lists/$1
557	echo_if_exist $setdir/rescue.mi
558	echo_if_exist $setdir/rescue.${MACHINE}
559	[ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
560	list_set_lists_common_ad $1 rescue
561}
562
563list_set_lists_common_ad()
564{
565	setdir=$setsdir/lists/$1; _prefix=$2
566
567	[ -n "$_prefix" ] && prefix="$_prefix".
568
569	# Prefer a <prefix>.ad.${MACHINE_ARCH} over a
570	# <prefix>.ad.${MACHINE_CPU}, since the arch-
571	# specific one will be more specific than the
572	# cpu-specific one.
573	echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
574	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
575	[ "$shlib" != "no" ] && \
576	echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
577}
578
579echo_if_exist()
580{
581	[ -f $1 ] && echo $1
582	return $?
583}
584
585echo_if_exist_foreach()
586{
587	local _list=$1; shift
588	for _suffix in $@; do
589		echo_if_exist ${_list}.${_suffix}
590	done
591}
592
593print_set_lists()
594{
595	for setname; do
596		list=$(list_set_lists $setname)
597		for l in $list; do
598			echo $l
599			if $verbose; then
600				echo >&2 "DEBUG: list_set_files: $l"
601			fi
602		done
603	done | ${XARGS} ${SED} ${SUBST}
604}
605
606# arch_to_cpu mach
607#
608# Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
609# as determined by <bsd.own.mk>.
610#
611arch_to_cpu()
612{
613	MACHINE_ARCH=${1} MAKEFLAGS= \
614	${MAKE} -m ${NETBSDSRCDIR}/share/mk \
615		-f ${NETBSDSRCDIR}/share/mk/bsd.own.mk \
616		-V '${MACHINE_CPU}'
617}
618
619# arch_to_endian mach
620#
621# Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
622# as determined by <bsd.endian.mk>.
623#
624arch_to_endian()
625{
626	MACHINE_ARCH=${1} MAKEFLAGS= \
627	${MAKE} -m ${NETBSDSRCDIR}/share/mk \
628		-f ${NETBSDSRCDIR}/share/mk/bsd.endian.mk \
629		-V '${TARGET_ENDIANNESS}'
630}
631
632#####
633
634# print_mkvars
635print_mkvars()
636{
637	for v in $MKVARS; do
638		eval echo $v=\$$v
639	done
640}
641
642# print_set_lists_{base,x,ext}
643# list_set_lists_{base,x,ext}
644# list_set_files_{base,x,ext}
645for func in print_set_lists list_set_lists list_set_files; do
646	for x in base x ext; do
647		if [ $x = base ]; then
648			list=nlists
649		else
650			list=${x}lists
651		fi
652		eval ${func}_${x} \(\) \{ $func \$$list \; \}
653	done
654done
655