11.1Sagc#! /bin/sh
21.1Sagc#
31.19Snia# $NetBSD: regpkgset,v 1.19 2024/04/22 14:41:25 nia Exp $
41.1Sagc#
51.11Sagc# Copyright (c) 2003,2009 The NetBSD Foundation, Inc.
61.11Sagc# All rights reserved.
71.11Sagc#
81.11Sagc# This code is derived from software contributed to The NetBSD Foundation
91.11Sagc# by Alistair Crooks (agc@NetBSD.org)
101.1Sagc#
111.1Sagc# Redistribution and use in source and binary forms, with or without
121.1Sagc# modification, are permitted provided that the following conditions
131.1Sagc# are met:
141.1Sagc# 1. Redistributions of source code must retain the above copyright
151.1Sagc#    notice, this list of conditions and the following disclaimer.
161.1Sagc# 2. Redistributions in binary form must reproduce the above copyright
171.1Sagc#    notice, this list of conditions and the following disclaimer in the
181.1Sagc#    documentation and/or other materials provided with the distribution.
191.11Sagc#
201.11Sagc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
211.11Sagc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
221.11Sagc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
231.11Sagc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
241.11Sagc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
251.11Sagc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
261.11Sagc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
271.11Sagc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
281.11Sagc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
291.11Sagc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
301.11Sagc# POSSIBILITY OF SUCH DAMAGE.
311.1Sagc#
321.1Sagc
331.6Sapb# Usage: regpkgset [options] set
341.6Sapb#
351.6Sapb# Options:
361.6Sapb#   -q		Quiet.
371.6Sapb#   -v		Verbose.
381.6Sapb#   -f		Force.
391.6Sapb#   -m		Ignore errors from missing files.
401.6Sapb#   -u		Update.
411.6Sapb#   -c		Cache some information in ${BUILD_INFO_CACHE}.
421.6Sapb#   -d destdir	Sets DESTDIR.
431.6Sapb#   -t binpkgdir Create a binary package (in *.tgz format) in the
441.6Sapb#		specified directory.  Without this option, a binary
451.6Sapb#		package is not created.
461.6Sapb#   -M metalog	Use the specified metalog file to override file
471.6Sapb#		or directory attributes when creating a binary package.
481.6Sapb#   -N etcdir	Use the specified directory for passwd and group files.
491.1Sagc
501.6Sapbprog="${0##*/}"
511.6Sapbtoppid=$$
521.5Sapbrundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"
531.6Sapb. "${rundir}/sets.subr"
541.6Sapb
551.8Sapb: ${TARGET_ENDIANNESS:="$(arch_to_endian "${MACHINE_ARCH}")"}
561.7Sapbexport TARGET_ENDIANNESS
571.7Sapb
581.6Sapbbomb()
591.6Sapb{
601.6Sapb	kill ${toppid}		# in case we were invoked from a subshell
611.6Sapb	exit 1
621.6Sapb}
631.3Serh
641.6Sapb# A literal newline
651.6Sapbnl='
661.6Sapb'
671.1Sagc
681.6Sapb#
691.6Sapb# cleanup() deletes temporary files.
701.6Sapb#
711.6Sapbes=0
721.8Sapbcleanup()
731.6Sapb{
741.6Sapb	trap - 0
751.6Sapb	[ x"${BUILD_INFO_CACHE}" != x ] && rm -f "${BUILD_INFO_CACHE}"
761.6Sapb	exit ${es}
771.6Sapb}
781.6Sapbtrap 'es=128; cleanup' 1 2 3 13 15	# HUP INT QUIT PIPE TERM
791.6Sapbtrap 'es=$?; cleanup' 0 		# EXIT
801.6Sapb
811.6Sapb#
821.6Sapb# Parse command line args.
831.6Sapb#
841.6Sapbverbose=false
851.6Sapbquiet=false
861.6Sapbforce=false
871.6Sapballowmissing=false
881.6Sapbupdate=false
891.6Sapbcache=false
901.6Sapbpkgdir=""
911.6Sapbmetalog=""
921.6Sapbetcdir=""
931.6Sapball_options=""
941.1Sagcwhile [ $# -gt 1 ]; do
951.6Sapb	# XXX: ${all_options} doesn't correctly handle args with
961.6Sapb	# embedded shell special characters.
971.5Sapb	case "$1" in
981.8Sapb	-q)	quiet=true; verbose=false ;;
991.8Sapb	-v)	verbose=true; quiet=false ;;
1001.6Sapb	-f)	force=true ;;
1011.6Sapb	-m)	allowmissing=true ;;
1021.6Sapb	-u)	update=true ;;
1031.6Sapb	-c)	cache=true ;;
1041.8Sapb	-d)	DESTDIR="$2"; all_options="${all_options} $1"; shift ;;
1051.6Sapb	-d*)	DESTDIR="${1#-?}" ;;
1061.8Sapb	-t)	pkgdir="$2"; all_options="${all_options} $1"; shift ;;
1071.6Sapb	-t*)	pkgdir="${1#-?}" ;;
1081.8Sapb	-M)	metalog="$2"; all_options="${all_options} $1"; shift ;;
1091.6Sapb	-M*)	metalog="${1#-?}" ;;
1101.8Sapb	-N)	etcdir="$2"; all_options="${all_options} $1"; shift ;;
1111.6Sapb	-N*)	etcdir="${1#-?}" ;;
1121.8Sapb	-*)	echo "Usage: regpkgset [options] set ..."; bomb ;;
1131.1Sagc	*)	break ;;
1141.1Sagc	esac
1151.6Sapb	all_options="${all_options} $1"
1161.1Sagc	shift
1171.1Sagcdone
1181.6Sapbexport DESTDIR
1191.1Sagc
1201.1Sagcif [ $# -lt 1 ]; then
1211.6Sapb	echo "Usage: regpkgset [options] set ..."
1221.6Sapb	bomb
1231.1Sagcfi
1241.1Sagc
1251.5Sapbcase "$1" in
1261.19Sniaall)	list="base base32 base64 comp dtb etc games gpufw man manhtml misc modules rescue tests text xbase xcomp xetc xfont xserver" ;;
1271.5Sapb*)	list="$*" ;;
1281.1Sagcesac
1291.1Sagc
1301.8Sapbif ${cache}; then
1311.6Sapb	BUILD_INFO_CACHE="$(${MKTEMP} "/var/tmp/${prog}-BUILD_INFO.XXXXXX")"
1321.2Sdyoung	export BUILD_INFO_CACHE
1331.2Sdyoung	{
1341.6Sapb	# These variables describe the build
1351.6Sapb	# environment, not the target.
1361.5Sapb	echo "OPSYS=$(${UNAME} -s)"
1371.5Sapb	echo "OS_VERSION=$(${UNAME} -r)"
1381.9Sapb	${MAKE} -B -f- all <<EOF
1391.2Sdyoung.include <bsd.own.mk>
1401.2Sdyoungall:
1411.2Sdyoung	@echo OBJECT_FMT=${OBJECT_FMT}
1421.2Sdyoung	@echo MACHINE_ARCH=${MACHINE_ARCH}
1431.2Sdyoung	@echo MACHINE_GNU_ARCH=${MACHINE_GNU_ARCH}
1441.2SdyoungEOF
1451.5Sapb	} > "${BUILD_INFO_CACHE}"
1461.2Sdyoungfi
1471.2Sdyoung
1481.6Sapb#
1491.6Sapb# For each pkgset mentioned in ${list}, get a list of all pkgs in the pkgset.
1501.6Sapb#
1511.6Sapb# Sort all the pkgs into dependency order (with prerequisite pkgs before
1521.6Sapb# pkgs that depend on them).
1531.6Sapb#
1541.6Sapb# Invoke ${rundir}/regpkg for each pkg, taking care to do it in dependency
1551.6Sapb# order.  If there were any pkgs for which we failed to find dependency
1561.6Sapb# information, handle them at the end.
1571.6Sapb#
1581.8Sapbpkgs="$(for pkgset in ${list}; do
1591.6Sapb		${HOST_SH} "${rundir}/listpkgs" "${pkgset}" || bomb
1601.8Sapb	 done)"
1611.8Sapbtsort_input="$(${AWK} '{print $2 " " $1}' <"${rundir}/deps" || bomb)"
1621.8Sapbtsort_output="$(echo "${tsort_input}" | ${TSORT} || bomb)"
1631.8Sapbfor pkg in ${tsort_output}; do
1641.6Sapb	case "${nl}${pkgs}${nl}" in
1651.6Sapb	*"${nl}${pkg}${nl}"*)
1661.6Sapb		# We want this pkg.
1671.6Sapb		pkgset="${pkg%%-*}"
1681.6Sapb		${verbose} && echo "${prog}: registering ${pkg}"
1691.6Sapb		${HOST_SH} "${rundir}/regpkg" ${all_options} \
1701.6Sapb			"${pkgset}" "${pkg}" || bomb
1711.6Sapb		;;
1721.6Sapb	*)	# pkg is mentioned in ${tsort_output} but not in ${pkgs}.
1731.6Sapb		# We do not want this pkg.
1741.6Sapb		;;
1751.6Sapb	esac
1761.6Sapbdone
1771.8Sapbfor pkg in ${pkgs}; do
1781.6Sapb	case "${nl}${tsort_output}${nl}" in
1791.6Sapb	*"${nl}${pkg}${nl}"*)
1801.6Sapb		# pkg was in the tsort output, so it would have been
1811.6Sapb		# handled above.
1821.6Sapb		;;
1831.6Sapb	*)	# This pkg was not in the tsort output.
1841.6Sapb		# This is probably an error, but process the
1851.6Sapb		# pkg anyway.
1861.6Sapb		echo >&2 "${prog}: WARNING: ${pkg} is not mentioned in deps file"
1871.6Sapb		pkgset="${pkg%%-*}"
1881.6Sapb		${verbose} && echo "${prog}: registering ${pkg}"
1891.6Sapb		${HOST_SH} "${rundir}/regpkg" ${all_options} \
1901.6Sapb			"${pkgset}" "${pkg}" || bomb
1911.6Sapb		;;
1921.6Sapb	esac
1931.1Sagcdone
1941.1Sagc
1951.1Sagcexit 0
196