Blame SOURCES/process_configs.sh

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