Blame SOURCES/process_configs.sh

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