diff mbox series

[Unstable/Lunar] UBUNTU: [Packaging] scripts/misc/kernelconfig: Rewrite

Message ID 20230116165827.501424-1-juerg.haefliger@canonical.com
State New
Headers show
Series [Unstable/Lunar] UBUNTU: [Packaging] scripts/misc/kernelconfig: Rewrite | expand

Commit Message

Juerg Haefliger Jan. 16, 2023, 4:58 p.m. UTC
The kernelconfig script evolved over a long time and accumulated quite
some cruft. With the switch to using annotations only, that got even
worse so it's time for a major overhaul. Rather than sending tons of
little patches, just rewrite the whole script and also ensure shellcheck
is happy.

No functional changes intended.

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
---
 debian/scripts/misc/kernelconfig | 219 +++++++++++++++----------------
 1 file changed, 107 insertions(+), 112 deletions(-)

Comments

Andrea Righi Jan. 17, 2023, 7:51 a.m. UTC | #1
On Mon, Jan 16, 2023 at 05:58:27PM +0100, Juerg Haefliger wrote:
> The kernelconfig script evolved over a long time and accumulated quite
> some cruft. With the switch to using annotations only, that got even
> worse so it's time for a major overhaul. Rather than sending tons of
> little patches, just rewrite the whole script and also ensure shellcheck
> is happy.
> 
> No functional changes intended.
> 
> Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
> ---

Applied to lunar/linux and lunar/linux-unstable.

Thanks,
-Andrea
diff mbox series

Patch

diff --git a/debian/scripts/misc/kernelconfig b/debian/scripts/misc/kernelconfig
index 37ad900a205c..0c412af2ec8e 100755
--- a/debian/scripts/misc/kernelconfig
+++ b/debian/scripts/misc/kernelconfig
@@ -1,157 +1,152 @@ 
-#!/bin/bash
+#!/bin/bash -u
 
-. debian/debian.env
+function cleanup()
+{
+	rm -rf build "${TMP_DIR}"
+}
 
-# We have to be in the top level kernel source directory
-if [ ! -f MAINTAINERS ] || [ ! -f Makefile ]; then
-	echo "This does not appear to be the kernel source directory." 1>&2
+# We have to be in the top level Ubuntu kernel source directory
+if ! [ -e debian/debian.env ] ; then
+	echo "ERROR: This is not an Ubuntu kernel source directory" >&2
 	exit 1
 fi
 
-mode=${1:?"Usage: $0 (updateconfigs|defaultconfigs|genconfigs)"}
-yes=0
-genconfigs=0
-case "$mode" in
-    update*configs)  mode='syncconfig' ;;
-    default*configs) mode='oldconfig'; yes=1 ;;
-    gen*configs)     mode='genconfigs'; genconfigs=1 ;;
-    *) echo "$0 called with invalid mode" 1>&2
-       exit 1 ;;
-esac
-
-if [ -z "$gcc" ]; then
-    echo "ERROR: gcc environment variable must be set"
-    exit 1
+if [ -z "${gcc:-}" ] ; then
+	echo "ERROR: 'gcc' environment variable must be set" >&2
+	exit 1
 fi
 
-kerneldir="`pwd`"
-confdir="$kerneldir/${DEBIAN}/config"
-variant="$2"
-
-# TODO: Drop this once all derivatives have migrated to the new annotations
-# scheme
-if [ -e $DEBIAN/etc/kernelconfig ] ; then
-	. $DEBIAN/etc/kernelconfig
+if [ ${#} -ne 1 ] ; then
+	echo "Usage: $0 updateconfigs|defaultconfigs|genconfigs"
+	exit 2
 fi
 
-bindir="`pwd`/${DROOT}/scripts/misc"
-tmpdir=`mktemp -d`
+mode=${1}
 
-if [ "$genconfigs" == "1" ]; then
-	mode="oldconfig"
-fi
+case "${mode}" in
+	updateconfigs)  target="syncconfig" ;;
+	defaultconfigs) target="olddefconfig" ;;
+	genconfigs)     target="oldconfig" ;;
+	*) echo "ERROR: Invalid mode: ${1}" >&2
+	   exit 1 ;;
+esac
 
-warning_partial=
+. debian/debian.env
 
-# Use annotations to generate configs
-ARCHES=$(sed -ne 's/^# ARCH: \(.*\)/\1/p' < ${confdir}/annotations)
-FLAVOURS=$(sed -ne 's/^# FLAVOUR: \(.*\)/\1/p' < ${confdir}/annotations)
+annotations_file=${DEBIAN}/config/annotations
+warning_partial=()
 
-for flavour in ${FLAVOURS}; do
-	arch=$(echo $flavour    | sed 's/\([^-]*\)-\(.*\)/\1/')
-	flavour=$(echo $flavour | sed 's/\([^-]*\)-\(.*\)/\2/')
-	conf_file=${arch}-config.flavour.${flavour}
+TMP_DIR=$(mktemp -d)
+trap cleanup EXIT
 
-	rm -rf build
-	mkdir build
+# Use annotations to generate configs
+FLAVOURS=$(sed -ne 's/^# FLAVOUR: //p' "${annotations_file}")
 
-	# Generate .config from annotations (if a previosly generate config is
-	# available use that).
-	if [ -e "${tmpdir}/${conf_file}" ]; then
-		cat ${tmpdir}/${conf_file} > build/.config
-	else
-		python3 ${bindir}/annotations -f ${confdir}/annotations --arch ${arch} --flavour ${flavour} --export > build/.config
-	fi
+for arch_flavour in ${FLAVOURS} ; do
+	arch=${arch_flavour%%-*}
+	flavour=${arch_flavour#*-}
+	tmp_conf_file=${TMP_DIR}/${arch}-config.flavour.${flavour}
 
 	# Map debian archs to kernel archs
-	case "$arch" in
-		ppc64|ppc64el)	kernarch="powerpc"	;;
-		amd64)		kernarch="x86_64"	;;
-		lpia)		kernarch="x86" 		;;
-		sparc)		kernarch="sparc64"	;;
-		armel|armhf)	kernarch="arm"		;;
-		s390x)		kernarch="s390"		;;
-		riscv64)	kernarch="riscv"	;;
-		*)		kernarch="$arch"	;;
+	case "${arch}" in
+		amd64)   kern_arch="x86_64" ;;
+		arm64)   kern_arch="arm64" ;;
+		armhf)   kern_arch="arm" ;;
+		ppc64el) kern_arch="powerpc" ;;
+		riscv64) kern_arch="riscv" ;;
+		s390x)   kern_arch="s390" ;;
+		*)       echo "WARNING: Unsupported architecture: ${arch}"
+		         warning_partial+=("${arch}")
+		         continue ;;
 	esac
 
 	# Determine cross toolchain to use for Kconfig compiler tests
-	cross_compile="$(dpkg-architecture -qDEB_HOST_GNU_TYPE -a$arch 2>/dev/null)-"
+	cross_compile="$(dpkg-architecture -qDEB_HOST_GNU_TYPE -a"${arch}" 2>/dev/null)-"
 
 	# Arch-specific compiler, if any
-	archgcc=$(echo -e "show-%:\n\t@echo \$(\$*)\ninclude $DEBIAN/rules.d/$arch.mk" | make -s -f - show-gcc)
-
-	# Environment variables for 'make *config'. We omit CROSS_COMPILE
-	# for i386 since it is no longer supported after 19.04, however
-	# we maintain the configs for hwe.
-	modify_config=true
-	env="ARCH=$kernarch DEB_ARCH=$arch"
-	compiler_path=$(which "${cross_compile}${archgcc:-$gcc}" || true)
-	if [ "$compiler_path" != '' ]; then
-		env="$env CROSS_COMPILE=$cross_compile CC=$compiler_path"
-	else
-		echo "WARNING: ${cross_compile}gcc not installed"
-		modify_config=
-		warning_partial="$warning_partial $arch"
+	arch_gcc=$(cat <<EOF | make -s -f - all
+include ${DEBIAN}/rules.d/${arch}.mk
+all:
+	@echo \$(if \$(gcc),\$(gcc),${gcc})
+EOF
+			)
+	gcc_path=$(which "${cross_compile}${arch_gcc}" || true)
+	if [ -z "${gcc_path}" ] ; then
+		echo "WARNING: ${cross_compile}${arch_gcc} not installed"
+		warning_partial+=("${arch}")
+		continue
 	fi
 
-	# Call oldconfig or syncconfig
-	if [ "$modify_config" ]; then
-		echo "* Run $mode (yes=$yes) on $arch/$flavour ..."
-		if [ "$yes" -eq 1 ]; then
-			yes "" | make O=`pwd`/build $conc_level $env "$mode"
-		else
-			make O=`pwd`/build $conc_level $env "$mode"
-		fi
+	rm -rf build
+	mkdir build
+
+	# Generate .config from annotations
+	python3 debian/scripts/misc/annotations -f "${annotations_file}" \
+			--arch "${arch}" --flavour "${flavour}" --export > build/.config
+
+	# Environment variables for 'make *config'
+	env=(ARCH="${kern_arch}"
+		 DEB_ARCH="${arch}"
+		 CROSS_COMPILE="${cross_compile}"
+		 CC="${gcc_path}")
+
+	# Concurrency level
+	if [ -n "${conc_level:-}" ] ; then
+		env+=("${conc_level}")
 	fi
 
-	# Export config for config-check (or genconfigs)
-	cat build/.config > ${tmpdir}/${conf_file}
+	# Call config target
+	echo
+	echo "* Run ${target} on ${arch}/${flavour} ..."
+	make O=build "${env[@]}" "${target}"
+
+	# Move config for further processing
+	mv build/.config "${tmp_conf_file}"
 done
 
-echo ""
+echo
 echo "Running config-check for all configurations ..."
-echo ""
 fail=0
-for flavour in ${FLAVOURS}; do
-	arch=$(echo $flavour    | sed 's/\([^-]*\)-\(.*\)/\1/')
-	flavour=$(echo $flavour | sed 's/\([^-]*\)-\(.*\)/\2/')
-	conf_file=${arch}-config.flavour.${flavour}
-
-	echo "Running config-check for ${arch}-${flavour}"
-	python3 ${bindir}/annotations -f ${confdir}/annotations --arch ${arch} --flavour ${flavour} --check ${tmpdir}/${conf_file} || let "fail=$fail+1"
+for arch_flavour in ${FLAVOURS} ; do
+	arch=${arch_flavour%%-*}
+	flavour=${arch_flavour#*-}
+	tmp_conf_file=${TMP_DIR}/${arch}-config.flavour.${flavour}
+
+	echo
+	echo "* Run config-check for ${arch}-${flavour} ..."
+	python3 debian/scripts/misc/annotations -f "${annotations_file}" \
+			--arch "${arch}" --flavour "${flavour}" --check "${tmp_conf_file}" || \
+		fail=$((fail + 1))
 done
 
 rc=0
-if [ "$fail" != 0 ]; then
+if [ ${fail} -gt 0 ] ; then
 	rc=1
-	echo ""
-	echo "*** ERROR: $fail config-check failures detected"
-	echo ""
+	echo "ERROR: ${fail} config-check failures detected" >&2
 fi
 
-rm -rf build
-
-if [ "$warning_partial" ]; then
+if [ ${#warning_partial[@]} -gt 0 ] ; then
 	rc=1
-	echo ""
-	echo "WARNING: configuration operation applied only to a subset of architectures (skipped$warning_partial)" 1>&2
-	echo ""
+	echo "ERROR: Config operation not applied to all architectures (skipped ${warning_partial[*]})" >&2
 fi
 
-if [ "$genconfigs" == "1" ]; then
+# Recreate the annotations file
+if [ "${mode}" = "genconfigs" ] ; then
 	rm -rf CONFIGS
-	mv ${tmpdir} CONFIGS
+	mv "${TMP_DIR}" CONFIGS
 else
-	# Automatically import configs back into annotations
-	for flavour in ${FLAVOURS}; do
-		arch=$(echo $flavour    | sed 's/\([^-]*\)-\(.*\)/\1/')
-		flavour=$(echo $flavour | sed 's/\([^-]*\)-\(.*\)/\2/')
-		conf_file=${arch}-config.flavour.${flavour}
-
-		python3 ${bindir}/annotations -f ${confdir}/annotations --arch ${arch} --flavour ${flavour} --import ${tmpdir}/${conf_file}
+	echo
+	echo "Importing all configurations ..."
+	echo
+	for arch_flavour in ${FLAVOURS} ; do
+		arch=${arch_flavour%%-*}
+		flavour=${arch_flavour#*-}
+		tmp_conf_file=${TMP_DIR}/${arch}-config.flavour.${flavour}
+
+		echo "* Import configs for ${arch}-${flavour} ..."
+		python3 debian/scripts/misc/annotations -f "${annotations_file}" \
+				--arch "${arch}" --flavour "${flavour}" --import "${tmp_conf_file}"
 	done
-	rm -rf ${tmpdir}
 fi
 
 exit "${rc}"