ba564c
#!/bin/bash
ba564c
#
ba564c
# This script takes the merged config files and processes them through oldconfig
ba564c
# and listnewconfig
ba564c
#
ba564c
ba564c
usage()
ba564c
{
ba564c
	echo "process_configs.sh [ -n|-c|-t ] package_name kernel_version"
ba564c
	echo "     -n: error on unset config options"
ba564c
	echo "     -c: error on mismatched config options"
ba564c
	echo "     -t: test run, do not overwrite original config"
ba564c
	echo "     -w: error on misconfigured config options"
ba564c
	exit 1
ba564c
}
ba564c
ba564c
die()
ba564c
{
ba564c
	echo "$1"
ba564c
	exit 1
ba564c
}
ba564c
ba564c
# stupid function to find top of tree to do kernel make configs
ba564c
switch_to_toplevel()
ba564c
{
ba564c
	path="$(pwd)"
ba564c
	while test -n "$path"
ba564c
	do
ba564c
		test -d $path/firmware && \
ba564c
			test -e $path/MAINTAINERS && \
ba564c
			test -d $path/drivers && \
ba564c
			break
ba564c
ba564c
		path="$(dirname $path)"
ba564c
	done
ba564c
ba564c
	test -n "$path"  || die "Can't find toplevel"
ba564c
	echo "$path"
ba564c
}
ba564c
ba564c
checkoptions()
ba564c
{
ba564c
	/usr/bin/awk '
ba564c
ba564c
		/is not set/ {
ba564c
			split ($0, a, "#");
ba564c
			split(a[2], b);
ba564c
			if (NR==FNR) {
ba564c
				configs[b[1]]="is not set";
ba564c
			} else {
ba564c
				if (configs[b[1]] != "" && configs[b[1]] != "is not set")
ba564c
					 print "Found # "b[1] " is not set, after generation, had " b[1] " " configs[b[1]] " in Source tree";
ba564c
			}
ba564c
		}
ba564c
ba564c
		/=/     {
ba564c
			split ($0, a, "=");
ba564c
			if (NR==FNR) {
ba564c
				configs[a[1]]=a[2];
ba564c
			} else {
ba564c
				if (configs[a[1]] != "" && configs[a[1]] != a[2])
ba564c
					 print "Found "a[1]"="a[2]" after generation, had " a[1]"="configs[a[1]]" in Source tree";
ba564c
			}
ba564c
		}
ba564c
	' $1 $2 > .mismatches
ba564c
ba564c
	if test -s .mismatches
ba564c
	then
ba564c
		echo "Error: Mismatches found in configuration files"
ba564c
		cat .mismatches
ba564c
		exit 1
ba564c
	fi
ba564c
}
ba564c
ba564c
function process_configs()
ba564c
{
ba564c
	# assume we are in $source_tree/configs, need to get to top level
ba564c
	pushd $(switch_to_toplevel) &>/dev/null
ba564c
ba564c
	for cfg in $SCRIPT_DIR/${PACKAGE_NAME}${KVERREL}${SUBARCH}*.config
ba564c
	do
ba564c
		arch=$(head -1 $cfg | cut -b 3-)
ba564c
		cfgtmp="${cfg}.tmp"
ba564c
		cfgorig="${cfg}.orig"
ba564c
		cat $cfg > $cfgorig
ba564c
ba564c
		echo -n "Processing $cfg ... "
ba564c
ba564c
		make ARCH=$arch KCONFIG_CONFIG=$cfgorig listnewconfig >& .listnewconfig
ba564c
		grep -E 'CONFIG_' .listnewconfig > .newoptions
ba564c
		if test -n "$NEWOPTIONS" && test -s .newoptions
ba564c
		then
ba564c
			echo "Found unset config items, please set them to an appropriate value"
ba564c
			cat .newoptions
ba564c
			rm .newoptions
ba564c
			exit 1
ba564c
		fi
ba564c
		rm .newoptions
ba564c
ba564c
		grep -E 'config.*warning' .listnewconfig > .warnings
ba564c
		if test -n "$CHECKWARNINGS" && test -s .warnings
ba564c
		then
ba564c
			echo "Found misconfigured config items, please set them to an appropriate value"
ba564c
			cat .warnings
ba564c
			rm .warnings
ba564c
			exit 1
ba564c
		fi
ba564c
		rm .warnings
ba564c
ba564c
		rm .listnewconfig
ba564c
ba564c
		make ARCH=$arch KCONFIG_CONFIG=$cfgorig oldnoconfig > /dev/null || exit 1
ba564c
		echo "# $arch" > ${cfgtmp}
ba564c
		cat "${cfgorig}" >> ${cfgtmp}
ba564c
		if test -n "$CHECKOPTIONS"
ba564c
		then
ba564c
			checkoptions $cfg $cfgtmp
ba564c
		fi
ba564c
		# if test run, don't overwrite original
ba564c
		if test -n "$TESTRUN"
ba564c
		then
ba564c
			rm ${cfgtmp}
ba564c
		else
ba564c
			mv ${cfgtmp} ${cfg}
ba564c
		fi
ba564c
		rm ${cfgorig}
ba564c
		echo "done"
ba564c
	done
ba564c
	rm "$SCRIPT_DIR"/*.config*.old
ba564c
	popd > /dev/null
ba564c
ba564c
	echo "Processed config files are in $SCRIPT_DIR"
ba564c
}
ba564c
ba564c
NEWOPTIONS=""
ba564c
CHECKOPTIONS=""
ba564c
CHECKWARNINGS=""
ba564c
TESTRUN=""
ba564c
ba564c
while [[ $# -gt 0 ]]
ba564c
do
ba564c
	key="$1"
ba564c
	case $key in
ba564c
		-h)
ba564c
			usage
ba564c
			;;
ba564c
		-n)
ba564c
			NEWOPTIONS="x"
ba564c
			;;
ba564c
		-c)
ba564c
			CHECKOPTIONS="x"
ba564c
			;;
ba564c
		-t)
ba564c
			TESTRUN="x"
ba564c
			;;
ba564c
		-w)
ba564c
			CHECKWARNINGS="x"
ba564c
			;;
ba564c
		*)
ba564c
			break;;
ba564c
	esac
ba564c
	shift
ba564c
done
ba564c
ba564c
PACKAGE_NAME="${1:-kernel}" # defines the package name used
ba564c
KVERREL="$(test -n "$2" && echo "-$2" || echo "")"
ba564c
SUBARCH="$(test -n "$3" && echo "-$3" || echo "")"
ba564c
SCRIPT="$(readlink -f $0)"
ba564c
OUTPUT_DIR="$PWD"
ba564c
SCRIPT_DIR="$(dirname $SCRIPT)"
ba564c
ba564c
# to handle this script being a symlink
ba564c
cd $SCRIPT_DIR
ba564c
ba564c
process_configs