aboutsummaryrefslogtreecommitdiff
path: root/scripts/multiconfig.sh
blob: 70f3a5df6f711081d9808cb67bd807a843164df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/sh
#
# A wrapper script to adjust Kconfig for U-Boot
#
# Instead of touching various parts under the scripts/kconfig/ directory,
# pushing necessary adjustments into this single script would be better
# for code maintainance.  All the make targets related to the configuration
# (make %config) should be invoked via this script.
# See doc/README.kconfig for further information of Kconfig.
#
# Copyright (C) 2014, Masahiro Yamada <yamada.m@jp.panasonic.com>
#
# SPDX-License-Identifier:	GPL-2.0+
#

set -e

# Set "DEBUG" enavironment variable to show debug messages
debug () {
	if [ $DEBUG ]; then
		echo "$@"
	fi
}

# Useful shorthands
build () {
	debug $progname: $MAKE -f $srctree/scripts/Makefile.build obj="$@"
	$MAKE -f $srctree/scripts/Makefile.build obj="$@"
}

autoconf () {
	debug $progname: $MAKE -f $srctree/scripts/Makefile.autoconf obj="$@"
	$MAKE -f $srctree/scripts/Makefile.autoconf obj="$@"
}

# Make a configuration target
# Usage:
#   run_make_config <target> <objdir>
# <target>: Make target such as "config", "menuconfig", "defconfig", etc.
# <objdir>: Target directory where the make command is run.
#           Typically "", "spl", "tpl" for Normal, SPL, TPL, respectively.
run_make_config () {
	target=$1
	objdir=$2

	# Linux expects defconfig files in arch/$(SRCARCH)/configs/ directory,
	# but U-Boot has them in configs/ directory.
	# Give SRCARCH=.. to fake scripts/kconfig/Makefile.
	options="SRCARCH=.. KCONFIG_OBJDIR=$objdir"
	if [ "$objdir" ]; then
		options="$options KCONFIG_CONFIG=$objdir/$KCONFIG_CONFIG"
		mkdir -p $objdir
	fi

	build scripts/kconfig $options $target
}

# Parse .config file to detect if CONFIG_SPL, CONFIG_TPL is enabled
# and returns:
#   ""        if neither CONFIG_SPL nor CONFIG_TPL is defined
#   "spl"     if CONFIG_SPL is defined but CONFIG_TPL is not
#   "spl tpl" if both CONFIG_SPL and CONFIG_TPL are defined
get_enabled_subimages() {
	if [ ! -r "$KCONFIG_CONFIG" ]; then
		# This should never happen
		echo "$progname: $KCONFIG_CONFIG not found" >&2
		exit 1
	fi

	# CONFIG_SPL=y -> spl
	# CONFIG_TPL=y -> tpl
	sed -n -e 's/^CONFIG_SPL=y$/spl/p' -e 's/^CONFIG_TPL=y$/tpl/p' \
							 $KCONFIG_CONFIG
}

do_silentoldconfig () {
	run_make_config silentoldconfig
	subimages=$(get_enabled_subimages)

	for obj in $subimages
	do
		mkdir -p $obj/include/config $obj/include/generated
		run_make_config silentoldconfig $obj
	done

	# If the following part fails, include/config/auto.conf should be
	# deleted so "make silentoldconfig" will be re-run on the next build.
	autoconf include include/autoconf.mk include/autoconf.mk.dep || {
		rm -f include/config/auto.conf
		exit 1
	}

	# include/config.h has been updated after "make silentoldconfig".
	# We need to touch include/config/auto.conf so it gets newer
	# than include/config.h.
	# Otherwise, 'make silentoldconfig' would be invoked twice.
	touch include/config/auto.conf

	for obj in $subimages
	do
		autoconf $obj/include $obj/include/autoconf.mk || {
			rm -f include/config/auto.conf
			exit 1
		}
	done
}

cleanup_after_defconfig () {
	rm -f configs/.tmp_defconfig
	# ignore 'Directory not empty' error
	# without using non-POSIX option '--ignore-fail-on-non-empty'
	rmdir arch configs 2>/dev/null || true
}

# Usage:
#  do_board_defconfig <board>_defconfig
do_board_defconfig () {
	defconfig_path=$srctree/configs/$1
	tmp_defconfig_path=configs/.tmp_defconfig

	if [ ! -r $defconfig_path ]; then
		echo >&2 "***"
		echo >&2 "*** Can't find default configuration \"configs/$1\"!"
		echo >&2 "***"
		exit 1
	fi

	mkdir -p arch configs
	# defconfig for Normal:
	#  pick lines without prefixes and lines starting '+' prefix
	#  and rip the prefixes off.
	sed -n -e '/^[+A-Z]*:/!p' -e 's/^+[A-Z]*://p' $defconfig_path \
						> configs/.tmp_defconfig

	run_make_config .tmp_defconfig || {
		cleanup_after_defconfig
		exit 1
	}

	for img in $(get_enabled_subimages)
	do
		symbol=$(echo $img | cut -c 1 | tr '[a-z]' '[A-Z]')
		# defconfig for SPL, TPL:
		#   pick lines with 'S', 'T' prefix and rip the prefixes off
		sed -n -e 's/^[+A-Z]*'$symbol'[A-Z]*://p' $defconfig_path \
						> configs/.tmp_defconfig
		run_make_config .tmp_defconfig $img || {
			cleanup_after_defconfig
			exit 1
		}
	done

	cleanup_after_defconfig
}

do_defconfig () {
	if [ "$KBUILD_DEFCONFIG" ]; then
		do_board_defconfig $KBUILD_DEFCONFIG
		echo "*** Default configuration is based on '$KBUILD_DEFCONFIG'"
	else
		run_make_config defconfig
	fi
}

do_board_felconfig () {
    do_board_defconfig ${1%%_felconfig}_defconfig
    if ! grep -q CONFIG_ARCH_SUNXI=y .config || ! grep -q CONFIG_SPL=y .config ; then
	echo "$progname: Cannot felconfig a non-sunxi or non-SPL platform" >&2
	exit 1
    fi
    sed -i -e 's/\# CONFIG_SPL_FEL is not set/CONFIG_SPL_FEL=y/g' \
	.config spl/.config
}

do_savedefconfig () {
	if [ -r "$KCONFIG_CONFIG" ]; then
		subimages=$(get_enabled_subimages)
	else
		subimages=
	fi

	run_make_config savedefconfig

	output_lines=

	# -r option is necessay because some string-type configs may include
	# backslashes as an escape character
	while read -r line
	do
		output_lines="$output_lines%$line"
	done < defconfig

	for img in $subimages
	do
		run_make_config savedefconfig $img

		symbol=$(echo $img | cut -c 1 | tr '[a-z]' '[A-Z]')
		unmatched=

		while read -r line
		do
			tmp=
			match=

			# "# CONFIG_FOO is not set" should not be divided.
			# Use "%" as a separator, instead of a whitespace.
			# "%" is unlikely to appear in defconfig context.
			save_IFS=$IFS
			IFS=%
			# coalesce common lines together
			for i in $output_lines
			do
				case "$i" in
				[+A-Z]*:$line)
					tmp="$tmp%$unmatched"
					i=$(echo "$i" | \
					    sed -e "s/^\([^:]*\)/\1$symbol/")
					tmp="$tmp%$i"
					match=1
					;;
				$line)
					tmp="$tmp%$unmatched"
					tmp="$tmp%+$symbol:$i"
					match=1
					;;
				*)
					tmp="$tmp%$i"
					;;
				esac
			done

			# Restore the default separator for the outer for loop.
			IFS=$save_IFS

			if [ "$match" ]; then
				output_lines="$tmp"
				unmatched=
			else
				unmatched="$unmatched%$symbol:$line"
			fi
		done < defconfig

		output_lines="$output_lines%$unmatched"
	done

	rm -f defconfig
	touch defconfig

	save_IFS=$IFS
	IFS=%

	for line in $output_lines
	do
		case "$line" in
		"")
			# do not output blank lines
			;;
		*)
			echo $line >> defconfig
			;;
		esac
	done

	IFS=$save_IFS
}

# Some sanity checks before running "make <objdir>/<target>",
# where <objdir> should be either "spl" or "tpl".
# Doing "make spl/menuconfig" etc. on a non-SPL board makes no sense.
# It should be allowed only when ".config" exists and "CONFIG_SPL" is enabled.
#
# Usage:
#   check_enabled_sumbimage <objdir>/<target> <objdir>
check_enabled_subimage () {

	case $2 in
	spl|tpl) ;;
	*)
		echo >&2 "***"
		echo >&2 "*** \"make $1\" is not supported."
		echo >&2 "***"
		exit 1
		;;
	esac
	test -r "$KCONFIG_CONFIG" && get_enabled_subimages | grep -q $2 || {
		config=CONFIG_$(echo $2 | tr '[a-z]' '[A-Z]')

		echo >&2 "***"
		echo >&2 "*** Create \"$KCONFIG_CONFIG\" with \"$config\" enabled"
		echo >&2 "*** before \"make $1\"."
		echo >&2 "***"
		exit 1
	}
}

# Usage:
#   do_others <objdir>/<target>
# The field "<objdir>/" is typically empy, "spl/", "tpl/" for Normal, SPL, TPL,
# respectively.
# The field "<target>" is a configuration target such as "config",
# "menuconfig", etc.
do_others () {
	target=${1##*/}

	if [ "$target" = "$1" ]; then
		objdir=
	else
		objdir=${1%/*}
		check_enabled_subimage $1 $objdir

		if [ -f "$objdir/$KCONFIG_CONFIG" ]; then
			timestamp_before=$(stat --printf="%Y" \
						$objdir/$KCONFIG_CONFIG)
		fi
	fi

	run_make_config $target $objdir

	if [ "$timestamp_before" -a -f "$objdir/$KCONFIG_CONFIG" ]; then
		timestamp_after=$(stat --printf="%Y" $objdir/$KCONFIG_CONFIG)

		if [ "$timestamp_after" -gt "$timestamp_before" ]; then
			# $objdir/.config has been updated.
			# touch .config to invoke "make silentoldconfig"
			touch $KCONFIG_CONFIG
		fi
	fi
}

progname=$(basename $0)
target=$1

case $target in
*_defconfig)
	do_board_defconfig $target;;
*_felconfig)
	do_board_felconfig $target;;
*_config)
	# backward compatibility
	do_board_defconfig ${target%_config}_defconfig;;
silentoldconfig)
	do_silentoldconfig;;
defconfig)
	do_defconfig;;
savedefconfig)
	do_savedefconfig;;
*)
	do_others $target;;
esac