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