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
{
63fe04
	count=$3
63fe04
	variant=$4
63fe04
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
		}
63fe04
	' "$1" "$2" > .mismatches${count}
44a40b
44a40b
	checkoptions_error=false
63fe04
	if test -s .mismatches${count}
44a40b
	then
44a40b
		while read -r LINE
44a40b
		do
63fe04
			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
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
63fe04
		done < .mismatches${count}
44a40b
44a40b
		! $checkoptions_error && return
44a40b
63fe04
		sed -i "1s/^/Error: Mismatches found in configuration files for ${arch} ${variant}\n/" .mismatches${count}
63fe04
	else
63fe04
		rm -f .mismatches${count}
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
63fe04
function process_config()
63fe04
{
63fe04
	local cfg
63fe04
	local arch
63fe04
	local cfgtmp
63fe04
	local cfgorig
63fe04
	local count
63fe04
	local variant
63fe04
63fe04
	cfg=$1
63fe04
	count=$2
63fe04
63fe04
	arch=$(head -1 "$cfg" | cut -b 3-)
63fe04
63fe04
	if [ "$arch" = "EMPTY" ]
63fe04
	then
63fe04
		# This arch is intentionally left blank
63fe04
		return
63fe04
	fi
63fe04
63fe04
	variant=$(basename "$cfg" | cut -d"-" -f3- | cut -d"." -f1)
63fe04
63fe04
	cfgtmp="${cfg}.tmp"
63fe04
	cfgorig="${cfg}.orig"
63fe04
	cat "$cfg" > "$cfgorig"
63fe04
63fe04
	echo "Processing $cfg ... "
63fe04
63fe04
	make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE=$(get_cross_compile $arch) KCONFIG_CONFIG="$cfgorig" listnewconfig >& .listnewconfig${count}
63fe04
	grep -E 'CONFIG_' .listnewconfig${count} > .newoptions${count}
63fe04
	if test -n "$NEWOPTIONS" && test -s .newoptions${count}
63fe04
	then
63fe04
		echo "Found unset config items in ${arch} ${variant}, please set them to an appropriate value" >> .errors${count}
63fe04
		cat .newoptions${count} >> .errors${count}
63fe04
		rm .newoptions${count}
63fe04
		RETURNCODE=1
63fe04
	fi
63fe04
	rm .newoptions${count}
63fe04
63fe04
	grep -E 'config.*warning' .listnewconfig${count} > .warnings${count}
63fe04
	if test -n "$CHECKWARNINGS" && test -s .warnings${count}
63fe04
	then
63fe04
		echo "Found misconfigured config items in ${arch} ${variant}, please set them to an appropriate value" >> .errors${count}
63fe04
		cat .warnings${count} >> .errors${count}
63fe04
		rm .warnings${count}
63fe04
	fi
63fe04
	rm .warnings${count}
63fe04
63fe04
	rm .listnewconfig${count}
63fe04
63fe04
	make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE=$(get_cross_compile $arch) KCONFIG_CONFIG="$cfgorig" olddefconfig > /dev/null || exit 1
63fe04
	echo "# $arch" > "$cfgtmp"
63fe04
	cat "$cfgorig" >> "$cfgtmp"
63fe04
	if test -n "$CHECKOPTIONS"
63fe04
	then
63fe04
		checkoptions "$cfg" "$cfgtmp" "$count" "$variant"
63fe04
	fi
63fe04
	# if test run, don't overwrite original
63fe04
	if test -n "$TESTRUN"
63fe04
	then
63fe04
		rm -f "$cfgtmp"
63fe04
	else
63fe04
		mv "$cfgtmp" "$cfg"
63fe04
	fi
63fe04
	rm -f "$cfgorig"
63fe04
	echo "Processing $cfg complete"
63fe04
}
63fe04
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
63fe04
	# The next line is throwaway code for transition to parallel
63fe04
	# processing.  Leaving this line in place is harmless, but it can be
63fe04
	# removed the next time anyone updates this function.
63fe04
	[ -f .mismatches ] && rm -f .mismatches
63fe04
63fe04
	count=0
44a40b
	for cfg in "$SCRIPT_DIR/${PACKAGE_NAME}${KVERREL}${SUBARCH}"*.config
44a40b
	do
63fe04
		if [ "$count" -eq 0 ]; then
63fe04
			# do the first one by itself so that tools are built
63fe04
			process_config "$cfg" "$count"
44a40b
		fi
63fe04
		process_config "$cfg" "$count" &
63fe04
		waitpids[${count}]=$!
63fe04
		((count++))
63fe04
		while [ "$(jobs | grep Running | wc -l)" -ge $RHJOBS ]; do :; done
63fe04
	done
63fe04
	for pid in ${waitpids[*]}; do
63fe04
		wait ${pid}
63fe04
	done
44a40b
63fe04
	rm "$SCRIPT_DIR"/*.config*.old
44a40b
63fe04
	if ls .errors* 1> /dev/null 2>&1; then
63fe04
		RETURNCODE=1
63fe04
		cat .errors*
63fe04
		rm .errors* -f
63fe04
	fi
63fe04
	if ls .mismatches* 1> /dev/null 2>&1; then
63fe04
		RETURNCODE=1
63fe04
		cat .mismatches*
63fe04
		rm .mismatches* -f
63fe04
	fi
44a40b
44a40b
	popd > /dev/null
44a40b
63fe04
	[ $RETURNCODE -eq 0 ] && echo "Processed config files are in $SCRIPT_DIR"
44a40b
}
44a40b
44a40b
CHECKOPTIONS=""
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
			NEWOPTIONS="x"
44a40b
			CHECKWARNINGS="x"
44a40b
			;;
44a40b
		-c)
44a40b
			CHECKOPTIONS="x"
44a40b
			;;
44a40b
		-h)
44a40b
			usage
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")"
63fe04
RHJOBS="$(test -n "$5" && echo "$5" || nproc --all)"
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