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