Blame SOURCES/process_configs.sh

9713b0
#!/bin/bash
9713b0
#
9713b0
# This script takes the merged config files and processes them through oldconfig
9713b0
# and listnewconfig
9713b0
#
9713b0
# Globally disable suggestion of appending '|| exit' or '|| return' to cd/pushd/popd commands
9713b0
# shellcheck disable=SC2164
9713b0
bd1bf5
test -n "$RHTEST" && exit 0
bd1bf5
9713b0
usage()
9713b0
{
9713b0
	# alphabetical order please
9713b0
	echo "process_configs.sh [ options ] package_name kernel_version"
9713b0
	echo "     -a: report all errors, equivalent to [-c -n -w -i]"
9713b0
	echo "     -c: error on mismatched config options"
9713b0
	echo "     -i: continue on error"
9713b0
	echo "     -n: error on unset config options"
9713b0
	echo "     -t: test run, do not overwrite original config"
9713b0
	echo "     -w: error on misconfigured config options"
9713b0
	echo "     -z: commit new configs to pending directory"
9713b0
	echo ""
9713b0
	echo "     A special CONFIG file tag, process_configs_known_broken can be added as a"
9713b0
	echo "     comment to any CONFIG file.  This tag indicates that there is no way to "
9713b0
	echo "     fix a CONFIG's entry.  This tag should only be used in extreme cases"
9713b0
	echo "     and is not to be used as a workaround to solve CONFIG problems."
9713b0
	exit 1
9713b0
}
9713b0
9713b0
die()
9713b0
{
9713b0
	echo "$1"
9713b0
	exit 1
9713b0
}
9713b0
9713b0
get_cross_compile()
9713b0
{
9713b0
	arch=$1
9713b0
	if [[ "$CC_IS_CLANG" -eq 1 ]]; then
9713b0
		echo "$arch"
9713b0
	else
9713b0
		echo "scripts/dummy-tools/"
9713b0
	fi
9713b0
}
9713b0
9713b0
# stupid function to find top of tree to do kernel make configs
9713b0
switch_to_toplevel()
9713b0
{
9713b0
	path="$(pwd)"
9713b0
	while test -n "$path"
9713b0
	do
9713b0
		test -e "$path"/MAINTAINERS && \
9713b0
			test -d "$path"/drivers && \
9713b0
			break
9713b0
9713b0
		path=$(dirname "$path")
9713b0
	done
9713b0
9713b0
	test -n "$path"  || die "Can't find toplevel"
9713b0
	echo "$path"
9713b0
}
9713b0
9713b0
checkoptions()
9713b0
{
a39c23
	count=$3
a39c23
	variant=$4
a39c23
9713b0
	/usr/bin/awk '
9713b0
9713b0
		/is not set/ {
9713b0
			split ($0, a, "#");
9713b0
			split(a[2], b);
9713b0
			if (NR==FNR) {
9713b0
				configs[b[1]]="is not set";
9713b0
			} else {
9713b0
				if (configs[b[1]] != "" && configs[b[1]] != "is not set")
9713b0
					 print "Found # "b[1] " is not set, after generation, had " b[1] " " configs[b[1]] " in Source tree";
9713b0
			}
9713b0
		}
9713b0
9713b0
		/=/     {
9713b0
			split ($0, a, "=");
9713b0
			if (NR==FNR) {
9713b0
				configs[a[1]]=a[2];
9713b0
			} else {
9713b0
				if (configs[a[1]] != "" && configs[a[1]] != a[2])
9713b0
					 print "Found "a[1]"="a[2]" after generation, had " a[1]"="configs[a[1]]" in Source tree";
9713b0
			}
9713b0
		}
bd1bf5
	' "$1" "$2" > .mismatches"${count}"
9713b0
9713b0
	checkoptions_error=false
bd1bf5
	if test -s .mismatches"${count}"
9713b0
	then
9713b0
		while read -r LINE
9713b0
		do
a39c23
			if find "${REDHAT}"/configs -name "$(echo "$LINE" | awk -F "=" ' { print $1 } ' | awk ' { print $2 }')" -print0 | xargs -0 grep ^ | grep -q "process_configs_known_broken"; then
9713b0
				# This is a known broken config.
9713b0
				# See script help warning.
9713b0
				checkoptions_error=false
9713b0
			else
9713b0
				checkoptions_error=true
9713b0
				break
9713b0
			fi
bd1bf5
		done < .mismatches"${count}"
9713b0
9713b0
		! $checkoptions_error && return
9713b0
bd1bf5
		sed -i "1s/^/Error: Mismatches found in configuration files for ${arch} ${variant}\n/" .mismatches"${count}"
a39c23
	else
bd1bf5
		rm -f .mismatches"${count}"
9713b0
	fi
9713b0
}
9713b0
9713b0
parsenewconfigs()
9713b0
{
9713b0
	tmpdir=$(mktemp -d)
9713b0
9713b0
	# This awk script reads the output of make listnewconfig
9713b0
	# and puts it into CONFIG_FOO files. Using the output of
9713b0
	# listnewconfig is much easier to ensure we get the default
9713b0
	# output.
9713b0
        /usr/bin/awk -v BASE="$tmpdir" '
9713b0
                /is not set/ {
9713b0
                        split ($0, a, "#");
9713b0
                        split(a[2], b);
9713b0
                        OUT_FILE=BASE"/"b[1];
9713b0
                        print $0 >> OUT_FILE;
9713b0
                }
9713b0
9713b0
                /=/     {
9713b0
                        split ($0, a, "=");
9713b0
                        OUT_FILE=BASE"/"a[1];
9713b0
                        if (a[2] == "n")
9713b0
                                print "# " a[1] " is not set" >> OUT_FILE;
9713b0
                        else
9713b0
                                print $0 >> OUT_FILE;
9713b0
                }
9713b0
9713b0
        ' .newoptions
9713b0
9713b0
	# This awk script parses the output of helpnewconfig.
9713b0
	# Each option is separated between ----- markers
9713b0
	# The goal is to put all the help text as a comment in
9713b0
	# each CONFIG_FOO file. Because of how awk works
9713b0
	# there's a lot of moving files around and catting to
9713b0
	# get what we need.
9713b0
        /usr/bin/awk -v BASE="$tmpdir" '
9713b0
                BEGIN { inpatch=0;
9713b0
			outfile="none";
9713b0
                        symbol="none"; }
9713b0
                /^Symbol: .*$/ {
9713b0
                        split($0, a, " ");
9713b0
                        symbol="CONFIG_"a[2];
9713b0
                        outfile=BASE "/fake_"symbol
9713b0
                }
9713b0
                /-----/ {
9713b0
			if (inpatch == 0) {
9713b0
				inpatch = 1;
9713b0
			}
9713b0
                        else {
9713b0
                                if (symbol != "none") {
9713b0
                                    system("cat " outfile " " BASE "/" symbol " > " BASE "/tmpf");
9713b0
                                    system("mv " BASE "/tmpf " BASE "/" symbol);
9713b0
                                    symbol="none"
9713b0
				}
9713b0
                                outfile="none"
9713b0
				inpatch = 0;
9713b0
                        }
9713b0
                }
9713b0
                !/-----/ {
9713b0
                        if (inpatch == 1 && outfile != "none") {
9713b0
                                print "# "$0 >> outfile;
9713b0
                        }
9713b0
                }
9713b0
9713b0
9713b0
        ' .helpnewconfig
9713b0
9713b0
	pushd "$tmpdir" &> /dev/null
9713b0
	rm fake_*
9713b0
	popd &> /dev/null
9713b0
	for f in "$tmpdir"/*; do
9713b0
		[[ -e "$f" ]] || break
9713b0
		cp "$f" "$SCRIPT_DIR/pending$FLAVOR/generic/"
9713b0
	done
9713b0
9713b0
	rm -rf "$tmpdir"
9713b0
}
9713b0
9713b0
function commit_new_configs()
9713b0
{
9713b0
	# assume we are in $source_tree/configs, need to get to top level
9713b0
	pushd "$(switch_to_toplevel)" &>/dev/null
9713b0
bd1bf5
	for cfg in "$SCRIPT_DIR/${PACKAGE_NAME}${KVERREL}"*.config
9713b0
	do
9713b0
		arch=$(head -1 "$cfg" | cut -b 3-)
9713b0
		cfgtmp="${cfg}.tmp"
9713b0
		cfgorig="${cfg}.orig"
9713b0
		cat "$cfg" > "$cfgorig"
9713b0
9713b0
		if [ "$arch" = "EMPTY" ]
9713b0
		then
9713b0
			# This arch is intentionally left blank
9713b0
			continue
9713b0
		fi
9713b0
		echo -n "Checking for new configs in $cfg ... "
9713b0
bd1bf5
		# shellcheck disable=SC2086
bd1bf5
		make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE="$(get_cross_compile "$arch")" KCONFIG_CONFIG="$cfgorig" listnewconfig >& .listnewconfig
9713b0
		grep -E 'CONFIG_' .listnewconfig > .newoptions
9713b0
		if test -s .newoptions
9713b0
		then
bd1bf5
		# shellcheck disable=SC2086
bd1bf5
			make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE="$(get_cross_compile "$arch")" KCONFIG_CONFIG="$cfgorig" helpnewconfig >& .helpnewconfig
9713b0
			parsenewconfigs
9713b0
		fi
9713b0
		rm .newoptions
9713b0
		echo "done"
9713b0
	done
9713b0
9713b0
	git add "$SCRIPT_DIR/pending$FLAVOR"
9713b0
	git commit -m "[redhat] AUTOMATIC: New configs"
9713b0
}
9713b0
a39c23
function process_config()
a39c23
{
a39c23
	local cfg
a39c23
	local arch
a39c23
	local cfgtmp
a39c23
	local cfgorig
a39c23
	local count
a39c23
	local variant
a39c23
a39c23
	cfg=$1
a39c23
	count=$2
a39c23
a39c23
	arch=$(head -1 "$cfg" | cut -b 3-)
a39c23
a39c23
	if [ "$arch" = "EMPTY" ]
a39c23
	then
a39c23
		# This arch is intentionally left blank
a39c23
		return
a39c23
	fi
a39c23
a39c23
	variant=$(basename "$cfg" | cut -d"-" -f3- | cut -d"." -f1)
a39c23
a39c23
	cfgtmp="${cfg}.tmp"
a39c23
	cfgorig="${cfg}.orig"
a39c23
	cat "$cfg" > "$cfgorig"
a39c23
a39c23
	echo "Processing $cfg ... "
a39c23
bd1bf5
	# shellcheck disable=SC2086
bd1bf5
	make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE="$(get_cross_compile "$arch")" KCONFIG_CONFIG="$cfgorig" listnewconfig >& .listnewconfig"${count}"
bd1bf5
	grep -E 'CONFIG_' .listnewconfig"${count}" > .newoptions"${count}"
bd1bf5
	if test -n "$NEWOPTIONS" && test -s .newoptions"${count}"
a39c23
	then
bd1bf5
		echo "Found unset config items in ${arch} ${variant}, please set them to an appropriate value" >> .errors"${count}"
bd1bf5
		cat .newoptions"${count}" >> .errors"${count}"
bd1bf5
		rm .newoptions"${count}"
a39c23
		RETURNCODE=1
a39c23
	fi
bd1bf5
	rm -f .newoptions"${count}"
a39c23
bd1bf5
	grep -E 'config.*warning' .listnewconfig"${count}" > .warnings"${count}"
bd1bf5
	if test -n "$CHECKWARNINGS" && test -s .warnings"${count}"
a39c23
	then
bd1bf5
		echo "Found misconfigured config items in ${arch} ${variant}, please set them to an appropriate value" >> .errors"${count}"
bd1bf5
		cat .warnings"${count}" >> .errors"${count}"
a39c23
	fi
bd1bf5
	rm .warnings"${count}"
a39c23
bd1bf5
	rm .listnewconfig"${count}"
a39c23
bd1bf5
	# shellcheck disable=SC2086
bd1bf5
	make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE="$(get_cross_compile "$arch")" KCONFIG_CONFIG="$cfgorig" olddefconfig > /dev/null || exit 1
a39c23
	echo "# $arch" > "$cfgtmp"
a39c23
	cat "$cfgorig" >> "$cfgtmp"
a39c23
	if test -n "$CHECKOPTIONS"
a39c23
	then
a39c23
		checkoptions "$cfg" "$cfgtmp" "$count" "$variant"
a39c23
	fi
a39c23
	# if test run, don't overwrite original
a39c23
	if test -n "$TESTRUN"
a39c23
	then
a39c23
		rm -f "$cfgtmp"
a39c23
	else
a39c23
		mv "$cfgtmp" "$cfg"
a39c23
	fi
a39c23
	rm -f "$cfgorig"
a39c23
	echo "Processing $cfg complete"
a39c23
}
a39c23
9713b0
function process_configs()
9713b0
{
9713b0
	# assume we are in $source_tree/configs, need to get to top level
9713b0
	pushd "$(switch_to_toplevel)" &>/dev/null
9713b0
a39c23
	# The next line is throwaway code for transition to parallel
a39c23
	# processing.  Leaving this line in place is harmless, but it can be
a39c23
	# removed the next time anyone updates this function.
a39c23
	[ -f .mismatches ] && rm -f .mismatches
a39c23
a39c23
	count=0
bd1bf5
	for cfg in "$SCRIPT_DIR/${PACKAGE_NAME}${KVERREL}"*.config
9713b0
	do
a39c23
		if [ "$count" -eq 0 ]; then
a39c23
			# do the first one by itself so that tools are built
a39c23
			process_config "$cfg" "$count"
9713b0
		fi
a39c23
		process_config "$cfg" "$count" &
a39c23
		waitpids[${count}]=$!
a39c23
		((count++))
bd1bf5
		while [ "$(jobs | grep -c Running)" -ge "$RHJOBS" ]; do :; done
a39c23
	done
a39c23
	for pid in ${waitpids[*]}; do
a39c23
		wait ${pid}
a39c23
	done
9713b0
a39c23
	rm "$SCRIPT_DIR"/*.config*.old
9713b0
a39c23
	if ls .errors* 1> /dev/null 2>&1; then
a39c23
		RETURNCODE=1
a39c23
		cat .errors*
a39c23
		rm .errors* -f
a39c23
	fi
a39c23
	if ls .mismatches* 1> /dev/null 2>&1; then
a39c23
		RETURNCODE=1
a39c23
		cat .mismatches*
a39c23
		rm .mismatches* -f
a39c23
	fi
9713b0
9713b0
	popd > /dev/null
9713b0
a39c23
	[ $RETURNCODE -eq 0 ] && echo "Processed config files are in $SCRIPT_DIR"
9713b0
}
9713b0
9713b0
CHECKOPTIONS=""
9713b0
NEWOPTIONS=""
9713b0
TESTRUN=""
9713b0
CHECKWARNINGS=""
9713b0
MAKEOPTS=""
9713b0
CC_IS_CLANG=0
9713b0
9713b0
RETURNCODE=0
9713b0
9713b0
while [[ $# -gt 0 ]]
9713b0
do
9713b0
	key="$1"
9713b0
	case $key in
9713b0
		-a)
9713b0
			CHECKOPTIONS="x"
9713b0
			NEWOPTIONS="x"
9713b0
			CHECKWARNINGS="x"
9713b0
			;;
9713b0
		-c)
9713b0
			CHECKOPTIONS="x"
9713b0
			;;
9713b0
		-h)
9713b0
			usage
9713b0
			;;
9713b0
		-n)
9713b0
			NEWOPTIONS="x"
9713b0
			;;
9713b0
		-t)
9713b0
			TESTRUN="x"
9713b0
			;;
9713b0
		-w)
9713b0
			CHECKWARNINGS="x"
9713b0
			;;
9713b0
		-z)
9713b0
			COMMITNEWCONFIGS="x"
9713b0
			;;
9713b0
		-m)
9713b0
			shift
bd1bf5
			if [ "$1" = "CC=clang" ] || [ "$1" = "LLVM=1" ]; then
9713b0
				CC_IS_CLANG=1
9713b0
			fi
9713b0
			MAKEOPTS="$MAKEOPTS $1"
9713b0
			;;
9713b0
		*)
9713b0
			break;;
9713b0
	esac
9713b0
	shift
9713b0
done
9713b0
bd1bf5
KVERREL="$(test -n "$1" && echo "-$1" || echo "")"
bd1bf5
FLAVOR="$(test -n "$2" && echo "-$2" || echo "-ark")"
bd1bf5
# shellcheck disable=SC2015
9713b0
SCRIPT=$(readlink -f "$0")
9713b0
SCRIPT_DIR=$(dirname "$SCRIPT")
9713b0
bd1bf5
# Config options for RHEL should target the pending-ark directory, not pending-common.
9713b0
if [ "$FLAVOR" = "-rhel" ]
9713b0
then
bd1bf5
	FLAVOR="-ark"
9713b0
fi
9713b0
9713b0
# to handle this script being a symlink
9713b0
cd "$SCRIPT_DIR"
9713b0
9713b0
if test -n "$COMMITNEWCONFIGS"; then
9713b0
	commit_new_configs
9713b0
else
9713b0
	process_configs
9713b0
fi
9713b0
9713b0
exit $RETURNCODE