1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4#  merge_config.sh - Takes a list of config fragment values, and merges
5#  them one by one. Provides warnings on overridden values, and specified
6#  values that did not make it to the resulting .config file (due to missed
7#  dependencies or config symbol removal).
8#
9#  Portions reused from kconf_check and generate_cfg:
10#  http://git.yoctoproject.org/cgit/cgit.cgi/yocto-kernel-tools/tree/tools/kconf_check
11#  http://git.yoctoproject.org/cgit/cgit.cgi/yocto-kernel-tools/tree/tools/generate_cfg
12#
13#  Copyright (c) 2009-2010 Wind River Systems, Inc.
14#  Copyright 2011 Linaro
15
16set -e
17
18clean_up() {
19	rm -f $TMP_FILE
20	rm -f $MERGE_FILE
21}
22
23usage() {
24	echo "Usage: $0 [OPTIONS] [CONFIG [...]]"
25	echo "  -h    display this help text"
26	echo "  -m    only merge the fragments, do not execute the make command"
27	echo "  -n    use allnoconfig instead of alldefconfig"
28	echo "  -r    list redundant entries when merging fragments"
29	echo "  -y    make builtin have precedence over modules"
30	echo "  -O    dir to put generated output files.  Consider setting \$KCONFIG_CONFIG instead."
31	echo
32	echo "Used prefix: '$CONFIG_PREFIX'. You can redefine it with \$CONFIG_ environment variable."
33}
34
35RUNMAKE=true
36ALLTARGET=alldefconfig
37WARNREDUN=false
38BUILTIN=false
39OUTPUT=.
40CONFIG_PREFIX=${CONFIG_-CONFIG_}
41
42while true; do
43	case $1 in
44	"-n")
45		ALLTARGET=allnoconfig
46		shift
47		continue
48		;;
49	"-m")
50		RUNMAKE=false
51		shift
52		continue
53		;;
54	"-h")
55		usage
56		exit
57		;;
58	"-r")
59		WARNREDUN=true
60		shift
61		continue
62		;;
63	"-y")
64		BUILTIN=true
65		shift
66		continue
67		;;
68	"-O")
69		if [ -d $2 ];then
70			OUTPUT=$(echo $2 | sed 's/\/*$//')
71		else
72			echo "output directory $2 does not exist" 1>&2
73			exit 1
74		fi
75		shift 2
76		continue
77		;;
78	*)
79		break
80		;;
81	esac
82done
83
84if [ "$#" -lt 1 ] ; then
85	usage
86	exit
87fi
88
89if [ -z "$KCONFIG_CONFIG" ]; then
90	if [ "$OUTPUT" != . ]; then
91		KCONFIG_CONFIG=$(readlink -m -- "$OUTPUT/.config")
92	else
93		KCONFIG_CONFIG=.config
94	fi
95fi
96
97INITFILE=$1
98shift;
99
100if [ ! -r "$INITFILE" ]; then
101	echo "The base file '$INITFILE' does not exist.  Exit." >&2
102	exit 1
103fi
104
105MERGE_LIST=$*
106SED_CONFIG_EXP1="s/^\(${CONFIG_PREFIX}[a-zA-Z0-9_]*\)=.*/\1/p"
107SED_CONFIG_EXP2="s/^# \(${CONFIG_PREFIX}[a-zA-Z0-9_]*\) is not set$/\1/p"
108
109TMP_FILE=$(mktemp ./.tmp.config.XXXXXXXXXX)
110MERGE_FILE=$(mktemp ./.merge_tmp.config.XXXXXXXXXX)
111
112echo "Using $INITFILE as base"
113
114trap clean_up EXIT
115
116cat $INITFILE > $TMP_FILE
117
118# Merge files, printing warnings on overridden values
119for ORIG_MERGE_FILE in $MERGE_LIST ; do
120	echo "Merging $ORIG_MERGE_FILE"
121	if [ ! -r "$ORIG_MERGE_FILE" ]; then
122		echo "The merge file '$ORIG_MERGE_FILE' does not exist.  Exit." >&2
123		exit 1
124	fi
125	cat $ORIG_MERGE_FILE > $MERGE_FILE
126	CFG_LIST=$(sed -n -e "$SED_CONFIG_EXP1" -e "$SED_CONFIG_EXP2" $MERGE_FILE)
127
128	for CFG in $CFG_LIST ; do
129		grep -q -w $CFG $TMP_FILE || continue
130		PREV_VAL=$(grep -w $CFG $TMP_FILE)
131		NEW_VAL=$(grep -w $CFG $MERGE_FILE)
132		BUILTIN_FLAG=false
133		if [ "$BUILTIN" = "true" ] && [ "${NEW_VAL#CONFIG_*=}" = "m" ] && [ "${PREV_VAL#CONFIG_*=}" = "y" ]; then
134			echo Previous  value: $PREV_VAL
135			echo New value:       $NEW_VAL
136			echo -y passed, will not demote y to m
137			echo
138			BUILTIN_FLAG=true
139		elif [ "x$PREV_VAL" != "x$NEW_VAL" ] ; then
140			echo Value of $CFG is redefined by fragment $ORIG_MERGE_FILE:
141			echo Previous  value: $PREV_VAL
142			echo New value:       $NEW_VAL
143			echo
144		elif [ "$WARNREDUN" = "true" ]; then
145			echo Value of $CFG is redundant by fragment $ORIG_MERGE_FILE:
146		fi
147		if [ "$BUILTIN_FLAG" = "false" ]; then
148			sed -i "/$CFG[ =]/d" $TMP_FILE
149		else
150			sed -i "/$CFG[ =]/d" $MERGE_FILE
151		fi
152	done
153	cat $MERGE_FILE >> $TMP_FILE
154done
155
156if [ "$RUNMAKE" = "false" ]; then
157	cp -T -- "$TMP_FILE" "$KCONFIG_CONFIG"
158	echo "#"
159	echo "# merged configuration written to $KCONFIG_CONFIG (needs make)"
160	echo "#"
161	exit
162fi
163
164# If we have an output dir, setup the O= argument, otherwise leave
165# it blank, since O=. will create an unnecessary ./source softlink
166OUTPUT_ARG=""
167if [ "$OUTPUT" != "." ] ; then
168	OUTPUT_ARG="O=$OUTPUT"
169fi
170
171
172# Use the merged file as the starting point for:
173# alldefconfig: Fills in any missing symbols with Kconfig default
174# allnoconfig: Fills in any missing symbols with # CONFIG_* is not set
175make KCONFIG_ALLCONFIG=$TMP_FILE $OUTPUT_ARG $ALLTARGET
176
177
178# Check all specified config values took (might have missed-dependency issues)
179for CFG in $(sed -n -e "$SED_CONFIG_EXP1" -e "$SED_CONFIG_EXP2" $TMP_FILE); do
180
181	REQUESTED_VAL=$(grep -w -e "$CFG" $TMP_FILE)
182	ACTUAL_VAL=$(grep -w -e "$CFG" "$KCONFIG_CONFIG" || true)
183	if [ "x$REQUESTED_VAL" != "x$ACTUAL_VAL" ] ; then
184		echo "Value requested for $CFG not in final .config"
185		echo "Requested value:  $REQUESTED_VAL"
186		echo "Actual value:     $ACTUAL_VAL"
187		echo ""
188	fi
189done
190