t40c term[] count fix
[minix.git] / distrib / sets / sets.subr
blob901d4a89fabe5f80b51cde54ec8116866acf3754
1 #       $NetBSD: sets.subr,v 1.138 2012/08/08 14:08:01 christos Exp $
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)
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:
29 # The following variables refer to tools that are used when building sets:
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}
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.)
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 ".".
76 case "$(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         ;;
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         ;;
115 esac
117 #####
119 oIFS=$IFS
120 IFS="
123 for x in $( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars ); do
124         eval export $x
125 done
127 IFS=$oIFS
129 MKVARS="$( ${MAKE} -B -f ${rundir}/mkvars.mk mkvars | ${SED} -e 's,=.*,,' | ${XARGS} )"
131 #####
133 setsdir=${rundir}
134 obsolete=0
135 if [ "${MKKMOD}" = "no" ]; then
136         module=no                       # MODULEs are off.
137         modset=""
138 else
139         module=yes
140         modset="modules"
142 if [ "${MKATF}" = "no" ]; then
143         testset=""
144 else
145         testset="tests"
147 # Determine lib type. Do this first so stlib also gets set.
148 if [ "${OBJECT_FMT}" = "ELF" ]; then
149         shlib=elf
150 else
151         shlib=aout
153 stlib=$shlib
154 # Now check for MKPIC or specials and turn off shlib if need be.
155 if [ "${MKPIC}" = "no" ]; then
156         shlib=no
158 # LSC for Minix
159 #nlists="base comp etc games man misc $modset $testset text"
160 nlists="minix"
161 xlists="xbase xcomp xetc xfont xserver"
162 extlists="extbase extcomp extetc"
164 OSRELEASE=$(${HOST_SH} ${NETBSDSRCDIR}/sys/conf/osrelease.sh -k)
165 MODULEDIR="stand/${MACHINE}/${OSRELEASE}/modules"
166 SUBST="s#@MODULEDIR@#${MODULEDIR}#g"
167 SUBST="${SUBST};s#@OSRELEASE@#${OSRELEASE}#g"
168 SUBST="${SUBST};s#@MACHINE@#${MACHINE}#g"
171 # list_set_files setfile [...]
173 # Produce a packing list for setfile(s).
174 # In each file, a record consists of a path and a System Package name,
175 # separated by whitespace. E.g.,
177 #       # $NetBSD: sets.subr,v 1.138 2012/08/08 14:08:01 christos Exp $
178 #       .                       base-sys-root   [keyword[,...]]
179 #       ./altroot               base-sys-root
180 #       ./bin                   base-sys-root
181 #       ./bin/[                 base-util-root
182 #       ./bin/cat               base-util-root
183 #               [...]
185 # A # in the first column marks a comment.
187 # If ${obsolete} != 0, only entries with an "obsolete" keyword will
188 # be printed.  All other keywords must be present.
190 # The third field is an optional comma separated list of keywords to
191 # control if a record is printed; every keyword listed must be enabled
192 # for the record to be printed.  The following keywords are available:
193 #       dummy                   dummy entry (ignored)
194 #       obsolete                file is obsolete, and only printed if 
195 #                               ${obsolete} != 0
197 #       atf                     ${MKATF} != no
198 #       bfd                     obsolete, use binutils.
199 #       binutils                ${MKBINUTILS} != no
200 #       bsdgrep                 ${MKBSDGREP} != no
201 #       catpages                ${MKCATPAGES} != no
202 #       compat                  ${MKCOMPAT} != no
203 #       crypto                  ${MKCRYPTO} != 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 #       makemandb               ${MKMAKEMANDB} != no
228 #       man                     ${MKMAN} != no
229 #       manpages                ${MKMANPAGES} != no
230 #       manz                    ${MKMANZ} != no
231 #       mdns                    ${MKMDNS} != no
232 #       nls                     ${MKNLS} != no
233 #       nvi                     ${MKNVI} != no
234 #       pam                     ${MKPAM} != no
235 #       pcc                     ${MKPCC} != no
236 #       pf                      ${MKPF} != no
237 #       pic                     ${MKPIC} != no
238 #       postfix                 ${MKPOSTFIX} != no
239 #       profile                 ${MKPROFILE} != no
240 #       perfuse                 ${MKPERFUSE} != no
241 #       rump                    ${MKRUMP} != no
242 #       share                   ${MKSHARE} != no
243 #       skey                    ${MKSKEY} != no
244 #       solaris                 ${MKDTRACE} != no or ${MKZFS} != no
245 #       ssp                     ${HAS_SSP} != no
246 #       x11                     ${MKX11} != no && ${X11FLAVOUR} != "Xorg"
247 #       xorg                    ${MKX11} != no && ${X11FLAVOUR} == "Xorg"
248 #       yp                      ${MKYP} != no
249 #       zfs                     ${MKZFS} != no
251 #       binutils=<n>            <n> = value of ${HAVE_BINUTILS}
252 #       gcc=<n>                 <n> = value of ${HAVE_GCC}
253 #       gdb=<n>                 <n> = value of ${HAVE_GDB}
255 #       use_inet6               ${USE_INET6} != no
256 #       use_kerberos            ${USE_KERBEROS} != no
257 #       use_yp                  ${USE_YP} != no
259 #       .cat                    if ${MKMANZ} != "no" && ${MKCATPAGES} != "no"
260 #                                 automatically append ".gz" to the filename
262 #       .man                    if ${MKMANZ} != "no" && ${MKMAN} != "no"
263 #                                 automatically append ".gz" to the filename
265 list_set_files()
267         if [ ${MAKEVERBOSE:-2} -lt 3 ]; then
268                 verbose=false
269         else
270                 verbose=true
271         fi
272         print_set_lists "$@" | \
273         ${AWK} -v obsolete=${obsolete} '
274                 BEGIN {
275                         if (obsolete)
276                                 wanted["obsolete"] = 1
277                 
278                         split("'"${MKVARS}"'", needvars)
279                         for (vi in needvars) {
280                                 nv = needvars[vi]
281                                 kw = tolower(nv)
282                                 sub(/^mk/, "", kw)
283                                 sub(/^has_/, "", kw)
284                                 if (ENVIRON[nv] != "no")
285                                         wanted[kw] = 1 
286                         }
288                         if ("'"${TOOLCHAIN_MISSING}"'" != "yes") {
289                                 if ("binutils" in wanted)
290                                         wanted["binutils=" "'"${HAVE_BINUTILS}"'"] = 1
291                                 if ("gcc" in wanted)
292                                         wanted["gcc=" "'"${HAVE_GCC}"'"] = 1
293                                 if ("gdb" in wanted)
294                                         wanted["gdb=" "'"${HAVE_GDB}"'"] = 1
295                         }
296                         if (("man" in wanted) && ("catpages" in wanted))
297                                 wanted[".cat"] = 1
298                         if (("man" in wanted) && ("manpages" in wanted))
299                                 wanted[".man"] = 1
300                 }
302                 /^#/ {
303                         next;
304                 }
306                 /^-/ {
307                         notwanted[substr($1, 2)] = 1;
308                         delete list [substr($1, 2)];
309                         next;
310                 }
311                                 
313                 NF > 2 && $3 != "-" {
314                         if (notwanted[$1] != "")
315                                 next;
316                         split($3, keywords, ",")
317                         show = 1
318                         haveobs = 0
319                         for (ki in keywords) {
320                                 kw = keywords[ki]
321                                 if (("manz" in wanted) &&
322                                     (kw == ".cat" || kw == ".man"))
323                                         $1 = $1 ".gz"
324                                 if (substr(kw, 1, 1) == "!") {
325                                         kw = substr(kw, 2)
326                                         if (kw in wanted)
327                                                 show = 0
328                                 } else {
329                                         if (! (kw in wanted))
330                                                 show = 0
331                                 }
332                                 if (kw == "obsolete")
333                                         haveobs = 1
334                         }
335                         if (obsolete && ! haveobs)
336                                 next
337                         if (show)
338                                 list[$1] = $0
339                         next
340                 }
342                 {
343                         if (notwanted[$1] != "")
344                                 next;
345                         if (! obsolete)
346                                 list[$1] = $0
347                 }
349                 END {
350                         for (i in list) {
351                                 print list[i]
352                         }
353                 }'
358 # list_set_lists setname
360 # Print to stdout a list of files, one filename per line, which
361 # concatenate to create the packing list for setname. E.g.,
363 #       .../lists/base/mi
364 #       .../lists/base/rescue.mi
365 #       .../lists/base/md.i386
366 #               [...]
368 # For a given setname $set, the following files may be selected from
369 # .../list/$set:
370 #       mi
371 #       mi.ext.*
372 #       ad.${MACHINE_ARCH}
373 # (or)  ad.${MACHINE_CPU}
374 #       ad.${MACHINE_CPU}.shl
375 #       md.${MACHINE}.${MACHINE_ARCH}
376 # (or)  md.${MACHINE}
377 #       stl.mi
378 #       stl.${stlib}
379 #       shl.mi
380 #       shl.mi.ext.*
381 #       shl.${shlib}
382 #       shl.${shlib}.ext.*
383 #       module.mi                       if ${module} != no
384 #       module.${MACHINE}               if ${module} != no
385 #       module.ad.${MACHINE_ARCH}       if ${module} != no
386 # (or)  module.ad.${MACHINE_CPU}        if ${module} != no
387 #       rescue.shl
388 #       rescue.${MACHINE}
389 #       rescue.ad.${MACHINE_ARCH}
390 # (or)  rescue.ad.${MACHINE_CPU}
391 #       rescue.ad.${MACHINE_CPU}.shl
393 # Environment:
394 #       shlib
395 #       stlib
397 list_set_lists()
399         setname=$1
401         list_set_lists_mi $setname
402         list_set_lists_ad $setname
403         list_set_lists_md $setname
404         list_set_lists_stl $setname
405         list_set_lists_shl $setname
406         list_set_lists_module $setname
407         list_set_lists_rescue $setname
408         return 0
411 list_set_lists_mi()
413         setdir=$setsdir/lists/$1
414         # always exist!
415         echo $setdir/mi
418 list_set_lists_ad()
420         setdir=$setsdir/lists/$1
421         [ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
422         list_set_lists_common_ad $1
425 list_set_lists_md()
427         setdir=$setsdir/lists/$1
428         echo_if_exist $setdir/md.${MACHINE}.${MACHINE_ARCH} || \
429         echo_if_exist $setdir/md.${MACHINE}
432 list_set_lists_stl()
434         setdir=$setsdir/lists/$1
435         echo_if_exist $setdir/stl.mi
436         echo_if_exist $setdir/stl.${stlib}
439 list_set_lists_shl()
441         setdir=$setsdir/lists/$1
442         [ "$shlib" != "no" ] || return
443         echo_if_exist $setdir/shl.mi
444         echo_if_exist $setdir/shl.${shlib}
447 list_set_lists_module()
449         setdir=$setsdir/lists/$1
450         [ "$module" != "no" ] || return
451         echo_if_exist $setdir/module.mi
452         echo_if_exist $setdir/module.${MACHINE}
453         # XXX module never has .shl
454         [ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
455         list_set_lists_common_ad $1 module
458 list_set_lists_rescue()
460         setdir=$setsdir/lists/$1
461         echo_if_exist $setdir/rescue.mi
462         echo_if_exist $setdir/rescue.${MACHINE}
463         [ "${MACHINE}" != "${MACHINE_ARCH}" ] && \
464         list_set_lists_common_ad $1 rescue
467 list_set_lists_common_ad()
469         setdir=$setsdir/lists/$1; _prefix=$2
471         [ -n "$_prefix" ] && prefix="$_prefix".
473         # Prefer a <prefix>.ad.${MACHINE_ARCH} over a
474         # <prefix>.ad.${MACHINE_CPU}, since the arch-
475         # specific one will be more specific than the
476         # cpu-specific one.
477         echo_if_exist $setdir/${prefix}ad.${MACHINE_ARCH} || \
478         echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}
479         [ "$shlib" != "no" ] && \
480         echo_if_exist $setdir/${prefix}ad.${MACHINE_CPU}.shl
483 echo_if_exist()
485         [ -f $1 ] && echo $1
486         return $?
489 echo_if_exist_foreach()
491         local _list=$1; shift
492         for _suffix in $@; do
493                 echo_if_exist ${_list}.${_suffix}
494         done
497 print_set_lists()
499         for setname; do
500                 list=$(list_set_lists $setname)
501                 for l in $list; do
502                         echo $l
503                         if $verbose; then
504                                 echo >&2 "DEBUG: list_set_files: $l"
505                         fi
506                 done
507         done | ${XARGS} ${SED} ${SUBST}
510 # arch_to_cpu mach
512 # Print the ${MACHINE_CPU} for ${MACHINE_ARCH}=mach,
513 # as determined by <bsd.own.mk>.
515 arch_to_cpu()
517         MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
518 .include <bsd.own.mk>
519 all:
520         @echo \${MACHINE_CPU}
521 EOMAKE
524 # arch_to_endian mach
526 # Print the ${TARGET_ENDIANNESS} for ${MACHINE_ARCH}=mach,
527 # as determined by <bsd.endian.mk>.
529 arch_to_endian()
531         MACHINE_ARCH=${1} ${MAKE} -B -f- all <<EOMAKE
532 .include <bsd.endian.mk>
533 all:
534         @echo \${TARGET_ENDIANNESS}
535 EOMAKE
538 #####
540 # print_mkvars
541 print_mkvars()
543         for v in $MKVARS; do
544                 eval echo $v=\$$v
545         done
548 # print_set_lists_{base,x,ext}
549 # list_set_lists_{base,x,ext}
550 # list_set_files_{base,x,ext}
551 for func in print_set_lists list_set_lists list_set_files; do
552         for x in base x ext; do
553                 if [ $x = base ]; then
554                         list=nlists
555                 else
556                         list=${x}lists
557                 fi
558                 eval ${func}_${x} \(\) \{ $func \$$list \; \}
559         done
560 done