1#! /bin/sh
2#
3# $NetBSD: regpkgset,v 1.19 2024/04/22 14:41:25 nia Exp $
4#
5# Copyright (c) 2003,2009 The NetBSD Foundation, Inc.
6# All rights reserved.
7#
8# This code is derived from software contributed to The NetBSD Foundation
9# by Alistair Crooks (agc@NetBSD.org)
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19#
20# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30# POSSIBILITY OF SUCH DAMAGE.
31#
32
33# Usage: regpkgset [options] set
34#
35# Options:
36#   -q              Quiet.
37#   -v              Verbose.
38#   -f              Force.
39#   -m              Ignore errors from missing files.
40#   -u              Update.
41#   -c              Cache some information in ${BUILD_INFO_CACHE}.
42#   -d destdir      Sets DESTDIR.
43#   -t binpkgdir Create a binary package (in *.tgz format) in the
44#                   specified directory.  Without this option, a binary
45#                   package is not created.
46#   -M metalog      Use the specified metalog file to override file
47#                   or directory attributes when creating a binary package.
48#   -N etcdir       Use the specified directory for passwd and group files.
49
50prog="${0##*/}"
51toppid=$$
52rundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"
53. "${rundir}/sets.subr"
54
55: ${TARGET_ENDIANNESS:="$(arch_to_endian "${MACHINE_ARCH}")"}
56export TARGET_ENDIANNESS
57
58bomb()
59{
60          kill ${toppid}                # in case we were invoked from a subshell
61          exit 1
62}
63
64# A literal newline
65nl='
66'
67
68#
69# cleanup() deletes temporary files.
70#
71es=0
72cleanup()
73{
74          trap - 0
75          [ x"${BUILD_INFO_CACHE}" != x ] && rm -f "${BUILD_INFO_CACHE}"
76          exit ${es}
77}
78trap 'es=128; cleanup' 1 2 3 13 15      # HUP INT QUIT PIPE TERM
79trap 'es=$?; cleanup' 0                 # EXIT
80
81#
82# Parse command line args.
83#
84verbose=false
85quiet=false
86force=false
87allowmissing=false
88update=false
89cache=false
90pkgdir=""
91metalog=""
92etcdir=""
93all_options=""
94while [ $# -gt 1 ]; do
95          # XXX: ${all_options} doesn't correctly handle args with
96          # embedded shell special characters.
97          case "$1" in
98          -q)       quiet=true; verbose=false ;;
99          -v)       verbose=true; quiet=false ;;
100          -f)       force=true ;;
101          -m)       allowmissing=true ;;
102          -u)       update=true ;;
103          -c)       cache=true ;;
104          -d)       DESTDIR="$2"; all_options="${all_options} $1"; shift ;;
105          -d*)      DESTDIR="${1#-?}" ;;
106          -t)       pkgdir="$2"; all_options="${all_options} $1"; shift ;;
107          -t*)      pkgdir="${1#-?}" ;;
108          -M)       metalog="$2"; all_options="${all_options} $1"; shift ;;
109          -M*)      metalog="${1#-?}" ;;
110          -N)       etcdir="$2"; all_options="${all_options} $1"; shift ;;
111          -N*)      etcdir="${1#-?}" ;;
112          -*)       echo "Usage: regpkgset [options] set ..."; bomb ;;
113          *)        break ;;
114          esac
115          all_options="${all_options} $1"
116          shift
117done
118export DESTDIR
119
120if [ $# -lt 1 ]; then
121          echo "Usage: regpkgset [options] set ..."
122          bomb
123fi
124
125case "$1" in
126all)      list="base base32 base64 comp dtb etc games gpufw man manhtml misc modules rescue tests text xbase xcomp xetc xfont xserver" ;;
127*)        list="$*" ;;
128esac
129
130if ${cache}; then
131          BUILD_INFO_CACHE="$(${MKTEMP} "/var/tmp/${prog}-BUILD_INFO.XXXXXX")"
132          export BUILD_INFO_CACHE
133          {
134          # These variables describe the build
135          # environment, not the target.
136          echo "OPSYS=$(${UNAME} -s)"
137          echo "OS_VERSION=$(${UNAME} -r)"
138          ${MAKE} -B -f- all <<EOF
139.include <bsd.own.mk>
140all:
141          @echo OBJECT_FMT=${OBJECT_FMT}
142          @echo MACHINE_ARCH=${MACHINE_ARCH}
143          @echo MACHINE_GNU_ARCH=${MACHINE_GNU_ARCH}
144EOF
145          } > "${BUILD_INFO_CACHE}"
146fi
147
148#
149# For each pkgset mentioned in ${list}, get a list of all pkgs in the pkgset.
150#
151# Sort all the pkgs into dependency order (with prerequisite pkgs before
152# pkgs that depend on them).
153#
154# Invoke ${rundir}/regpkg for each pkg, taking care to do it in dependency
155# order.  If there were any pkgs for which we failed to find dependency
156# information, handle them at the end.
157#
158pkgs="$(for pkgset in ${list}; do
159                    ${HOST_SH} "${rundir}/listpkgs" "${pkgset}" || bomb
160           done)"
161tsort_input="$(${AWK} '{print $2 " " $1}' <"${rundir}/deps" || bomb)"
162tsort_output="$(echo "${tsort_input}" | ${TSORT} || bomb)"
163for pkg in ${tsort_output}; do
164          case "${nl}${pkgs}${nl}" in
165          *"${nl}${pkg}${nl}"*)
166                    # We want this pkg.
167                    pkgset="${pkg%%-*}"
168                    ${verbose} && echo "${prog}: registering ${pkg}"
169                    ${HOST_SH} "${rundir}/regpkg" ${all_options} \
170                              "${pkgset}" "${pkg}" || bomb
171                    ;;
172          *)        # pkg is mentioned in ${tsort_output} but not in ${pkgs}.
173                    # We do not want this pkg.
174                    ;;
175          esac
176done
177for pkg in ${pkgs}; do
178          case "${nl}${tsort_output}${nl}" in
179          *"${nl}${pkg}${nl}"*)
180                    # pkg was in the tsort output, so it would have been
181                    # handled above.
182                    ;;
183          *)        # This pkg was not in the tsort output.
184                    # This is probably an error, but process the
185                    # pkg anyway.
186                    echo >&2 "${prog}: WARNING: ${pkg} is not mentioned in deps file"
187                    pkgset="${pkg%%-*}"
188                    ${verbose} && echo "${prog}: registering ${pkg}"
189                    ${HOST_SH} "${rundir}/regpkg" ${all_options} \
190                              "${pkgset}" "${pkg}" || bomb
191                    ;;
192          esac
193done
194
195exit 0
196