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