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