blob: 7606193102451a11f0906ae67993b54c1a8a3e52 [file] [log] [blame]
Masahiro Yamadac9c2b752014-08-21 11:44:34 +09001#!/bin/sh
2#
3# A wrapper script to adjust Kconfig for U-Boot
4#
5# Instead of touching various parts under the scripts/kconfig/ directory,
6# pushing necessary adjustments into this single script would be better
7# for code maintainance. All the make targets related to the configuration
8# (make %config) should be invoked via this script.
9# See doc/README.kconfig for further information of Kconfig.
10#
11# Copyright (C) 2014, Masahiro Yamada <yamada.m@jp.panasonic.com>
12#
13# SPDX-License-Identifier: GPL-2.0+
14#
15
16set -e
17
18# Set "DEBUG" enavironment variable to show debug messages
19debug () {
20 if [ $DEBUG ]; then
21 echo "$@"
22 fi
23}
24
25# Useful shorthands
26build () {
27 debug $progname: $MAKE -f $srctree/scripts/Makefile.build obj="$@"
28 $MAKE -f $srctree/scripts/Makefile.build obj="$@"
29}
30
31autoconf () {
32 debug $progname: $MAKE -f $srctree/scripts/Makefile.autoconf obj="$@"
33 $MAKE -f $srctree/scripts/Makefile.autoconf obj="$@"
34}
35
36# Make a configuration target
37# Usage:
38# run_make_config <target> <objdir>
39# <target>: Make target such as "config", "menuconfig", "defconfig", etc.
40# <objdir>: Target directory where the make command is run.
41# Typically "", "spl", "tpl" for Normal, SPL, TPL, respectively.
42run_make_config () {
43 target=$1
44 objdir=$2
45
46 # Linux expects defconfig files in arch/$(SRCARCH)/configs/ directory,
47 # but U-Boot has them in configs/ directory.
48 # Give SRCARCH=.. to fake scripts/kconfig/Makefile.
49 options="SRCARCH=.. KCONFIG_OBJDIR=$objdir"
50 if [ "$objdir" ]; then
51 options="$options KCONFIG_CONFIG=$objdir/$KCONFIG_CONFIG"
52 mkdir -p $objdir
53 fi
54
55 build scripts/kconfig $options $target
56}
57
58# Parse .config file to detect if CONFIG_SPL, CONFIG_TPL is enabled
59# and returns:
60# "" if neither CONFIG_SPL nor CONFIG_TPL is defined
61# "spl" if CONFIG_SPL is defined but CONFIG_TPL is not
62# "spl tpl" if both CONFIG_SPL and CONFIG_TPL are defined
63get_enabled_subimages() {
64 if [ ! -r "$KCONFIG_CONFIG" ]; then
65 # This should never happen
66 echo "$progname: $KCONFIG_CONFIG not found" >&2
67 exit 1
68 fi
69
70 # CONFIG_SPL=y -> spl
71 # CONFIG_TPL=y -> tpl
72 sed -n -e 's/^CONFIG_\(SPL\|TPL\)=y$/\1/p' $KCONFIG_CONFIG | \
73 tr '[A-Z]' '[a-z]'
74}
75
76do_silentoldconfig () {
77 run_make_config silentoldconfig
78 subimages=$(get_enabled_subimages)
79
80 for obj in $subimages
81 do
82 mkdir -p $obj/include/config $obj/include/generated
83 run_make_config silentoldconfig $obj
84 done
85
86 # If the following part fails, include/config/auto.conf should be
87 # deleted so "make silentoldconfig" will be re-run on the next build.
88 autoconf include include/autoconf.mk include/autoconf.mk.dep || {
89 rm -f include/config/auto.conf
90 exit 1
91 }
92
93 # include/config.h has been updated after "make silentoldconfig".
94 # We need to touch include/config/auto.conf so it gets newer
95 # than include/config.h.
96 # Otherwise, 'make silentoldconfig' would be invoked twice.
97 touch include/config/auto.conf
98
99 for obj in $subimages
100 do
101 autoconf $obj/include $obj/include/autoconf.mk || {
102 rm -f include/config/auto.conf
103 exit 1
104 }
105 done
106}
107
108cleanup_after_defconfig () {
109 rm -f configs/.tmp_defconfig
110 # ignore 'Directory not empty' error
111 # without using non-POSIX option '--ignore-fail-on-non-empty'
112 rmdir arch configs 2>/dev/null || true
113}
114
115# Usage:
116# do_board_defconfig <board>_defconfig
117do_board_defconfig () {
118 defconfig_path=$srctree/configs/$1
119 tmp_defconfig_path=configs/.tmp_defconfig
120
121 mkdir -p arch configs
122 # defconfig for Normal:
123 # pick lines without prefixes and lines starting '+' prefix
124 # and rip the prefixes off.
125 sed -n -e '/^[+A-Z]*:/!p' -e 's/^+[A-Z]*://p' $defconfig_path \
126 > configs/.tmp_defconfig
127
128 run_make_config .tmp_defconfig || {
129 cleanup_after_defconfig
130 exit 1
131 }
132
133 for img in $(get_enabled_subimages)
134 do
135 symbol=$(echo $img | cut -c 1 | tr '[a-z]' '[A-Z]')
136 # defconfig for SPL, TPL:
137 # pick lines with 'S', 'T' prefix and rip the prefixes off
138 sed -n -e 's/^[+A-Z]*'$symbol'[A-Z]*://p' $defconfig_path \
139 > configs/.tmp_defconfig
140 run_make_config .tmp_defconfig $img || {
141 cleanup_after_defconfig
142 exit 1
143 }
144 done
145
146 cleanup_after_defconfig
147}
148
149do_defconfig () {
150 if [ "$KBUILD_DEFCONFIG" ]; then
151 do_board_defconfig $KBUILD_DEFCONFIG
152 echo "*** Default configuration is based on '$KBUILD_DEFCONFIG'"
153 else
154 run_make_config defconfig
155 fi
156}
157
158do_savedefconfig () {
159 if [ -r "$KCONFIG_CONFIG" ]; then
160 subimages=$(get_enabled_subimages)
161 else
162 subimages=
163 fi
164
165 run_make_config savedefconfig
166
167 output_lines=
168
169 # -r option is necessay because some string-type configs may include
170 # backslashes as an escape character
171 while read -r line
172 do
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900173 output_lines="$output_lines%$line"
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900174 done < defconfig
175
176 for img in $subimages
177 do
178 run_make_config savedefconfig $img
179
180 symbol=$(echo $img | cut -c 1 | tr '[a-z]' '[A-Z]')
181 unmatched=
182
183 while read -r line
184 do
185 tmp=
186 match=
187
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900188 # "# CONFIG_FOO is not set" should not be divided.
189 # Use "%" as a separator, instead of a whitespace.
190 # "%" is unlikely to appear in defconfig context.
191 save_IFS=$IFS
192 IFS=%
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900193 # coalesce common lines together
194 for i in $output_lines
195 do
196 case "$i" in
197 "[+A-Z]*:$line")
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900198 tmp="$tmp%$unmatched"
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900199 i=$(echo "$i" | \
200 sed -e "s/^\([^:]\)*/\1$symbol/")
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900201 tmp="$tmp%$i"
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900202 match=1
203 ;;
204 "$line")
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900205 tmp="$tmp%$unmatched"
206 tmp="$tmp%+$symbol:$i"
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900207 match=1
208 ;;
209 *)
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900210 tmp="$tmp%$i"
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900211 ;;
212 esac
213 done
214
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900215 # Restore the default separator for the outer for loop.
216 IFS=$save_IFS
217
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900218 if [ "$match" ]; then
219 output_lines="$tmp"
220 unmatched=
221 else
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900222 unmatched="$unmatched%$symbol:$line"
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900223 fi
224 done < defconfig
225 done
226
227 rm -f defconfig
Masahiro Yamada12094822014-09-04 05:41:31 +0900228 touch defconfig
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900229
230 save_IFS=$IFS
231 IFS=%
232
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900233 for line in $output_lines
234 do
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900235 case "$line" in
236 "")
237 # do not output blank lines
238 ;;
239 *)
240 echo $line >> defconfig
241 ;;
242 esac
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900243 done
Masahiro Yamada6a8b5bb2014-09-04 05:41:32 +0900244
245 IFS=$save_IFS
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900246}
247
248# Usage:
249# do_others <objdir>/<target>
250# The field "<objdir>/" is typically empy, "spl/", "tpl/" for Normal, SPL, TPL,
251# respectively.
252# The field "<target>" is a configuration target such as "config",
253# "menuconfig", etc.
254do_others () {
255 target=${1##*/}
256
257 if [ "$target" = "$1" ]; then
258 objdir=
259 else
260 objdir=${1%/*}
261 fi
262
263 run_make_config $target $objdir
264}
265
266progname=$(basename $0)
267target=$1
268
269case $target in
270*_defconfig)
271 do_board_defconfig $target;;
272*_config)
Masahiro Yamadacc533e92014-08-28 10:56:55 +0900273 # backward compatibility
Masahiro Yamadac9c2b752014-08-21 11:44:34 +0900274 do_board_defconfig ${target%_config}_defconfig;;
275silentoldconfig)
276 do_silentoldconfig;;
277defconfig)
278 do_defconfig;;
279savedefconfig)
280 do_savedefconfig;;
281*)
282 do_others $target;;
283esac