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