pengqianheng / rpms / kernel

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