Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 1 | #!/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 | |
| 16 | set -e |
| 17 | |
| 18 | # Set "DEBUG" enavironment variable to show debug messages |
| 19 | debug () { |
| 20 | if [ $DEBUG ]; then |
| 21 | echo "$@" |
| 22 | fi |
| 23 | } |
| 24 | |
| 25 | # Useful shorthands |
| 26 | build () { |
| 27 | debug $progname: $MAKE -f $srctree/scripts/Makefile.build obj="$@" |
| 28 | $MAKE -f $srctree/scripts/Makefile.build obj="$@" |
| 29 | } |
| 30 | |
| 31 | autoconf () { |
| 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. |
| 42 | run_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 |
| 63 | get_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 |
Jeroen Hofstee | 2699758 | 2014-10-01 17:22:58 +0200 | [diff] [blame] | 72 | sed -n -e 's/^CONFIG_SPL=y$/spl/p' -e 's/^CONFIG_TPL=y$/tpl/p' \ |
| 73 | $KCONFIG_CONFIG |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | do_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 | |
| 108 | cleanup_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 |
| 117 | do_board_defconfig () { |
| 118 | defconfig_path=$srctree/configs/$1 |
| 119 | tmp_defconfig_path=configs/.tmp_defconfig |
| 120 | |
Masahiro Yamada | f396a72 | 2014-09-04 22:16:15 +0900 | [diff] [blame] | 121 | if [ ! -r $defconfig_path ]; then |
| 122 | echo >&2 "***" |
York Sun | 65071d5 | 2014-10-01 08:44:55 -0700 | [diff] [blame] | 123 | echo >&2 "*** Can't find default configuration \"configs/$1\"!" |
Masahiro Yamada | f396a72 | 2014-09-04 22:16:15 +0900 | [diff] [blame] | 124 | echo >&2 "***" |
| 125 | exit 1 |
| 126 | fi |
| 127 | |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 128 | mkdir -p arch configs |
| 129 | # defconfig for Normal: |
| 130 | # pick lines without prefixes and lines starting '+' prefix |
| 131 | # and rip the prefixes off. |
| 132 | sed -n -e '/^[+A-Z]*:/!p' -e 's/^+[A-Z]*://p' $defconfig_path \ |
| 133 | > configs/.tmp_defconfig |
| 134 | |
| 135 | run_make_config .tmp_defconfig || { |
| 136 | cleanup_after_defconfig |
| 137 | exit 1 |
| 138 | } |
| 139 | |
| 140 | for img in $(get_enabled_subimages) |
| 141 | do |
| 142 | symbol=$(echo $img | cut -c 1 | tr '[a-z]' '[A-Z]') |
| 143 | # defconfig for SPL, TPL: |
| 144 | # pick lines with 'S', 'T' prefix and rip the prefixes off |
| 145 | sed -n -e 's/^[+A-Z]*'$symbol'[A-Z]*://p' $defconfig_path \ |
| 146 | > configs/.tmp_defconfig |
| 147 | run_make_config .tmp_defconfig $img || { |
| 148 | cleanup_after_defconfig |
| 149 | exit 1 |
| 150 | } |
| 151 | done |
| 152 | |
| 153 | cleanup_after_defconfig |
| 154 | } |
| 155 | |
| 156 | do_defconfig () { |
| 157 | if [ "$KBUILD_DEFCONFIG" ]; then |
| 158 | do_board_defconfig $KBUILD_DEFCONFIG |
| 159 | echo "*** Default configuration is based on '$KBUILD_DEFCONFIG'" |
| 160 | else |
| 161 | run_make_config defconfig |
| 162 | fi |
| 163 | } |
| 164 | |
| 165 | do_savedefconfig () { |
| 166 | if [ -r "$KCONFIG_CONFIG" ]; then |
| 167 | subimages=$(get_enabled_subimages) |
| 168 | else |
| 169 | subimages= |
| 170 | fi |
| 171 | |
| 172 | run_make_config savedefconfig |
| 173 | |
| 174 | output_lines= |
| 175 | |
| 176 | # -r option is necessay because some string-type configs may include |
| 177 | # backslashes as an escape character |
| 178 | while read -r line |
| 179 | do |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 180 | output_lines="$output_lines%$line" |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 181 | done < defconfig |
| 182 | |
| 183 | for img in $subimages |
| 184 | do |
| 185 | run_make_config savedefconfig $img |
| 186 | |
| 187 | symbol=$(echo $img | cut -c 1 | tr '[a-z]' '[A-Z]') |
| 188 | unmatched= |
| 189 | |
| 190 | while read -r line |
| 191 | do |
| 192 | tmp= |
| 193 | match= |
| 194 | |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 195 | # "# CONFIG_FOO is not set" should not be divided. |
| 196 | # Use "%" as a separator, instead of a whitespace. |
| 197 | # "%" is unlikely to appear in defconfig context. |
| 198 | save_IFS=$IFS |
| 199 | IFS=% |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 200 | # coalesce common lines together |
| 201 | for i in $output_lines |
| 202 | do |
| 203 | case "$i" in |
Masahiro Yamada | d2b5bbd | 2014-09-04 05:41:33 +0900 | [diff] [blame] | 204 | [+A-Z]*:$line) |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 205 | tmp="$tmp%$unmatched" |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 206 | i=$(echo "$i" | \ |
Masahiro Yamada | d2b5bbd | 2014-09-04 05:41:33 +0900 | [diff] [blame] | 207 | sed -e "s/^\([^:]*\)/\1$symbol/") |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 208 | tmp="$tmp%$i" |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 209 | match=1 |
| 210 | ;; |
Masahiro Yamada | d2b5bbd | 2014-09-04 05:41:33 +0900 | [diff] [blame] | 211 | $line) |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 212 | tmp="$tmp%$unmatched" |
| 213 | tmp="$tmp%+$symbol:$i" |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 214 | match=1 |
| 215 | ;; |
| 216 | *) |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 217 | tmp="$tmp%$i" |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 218 | ;; |
| 219 | esac |
| 220 | done |
| 221 | |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 222 | # Restore the default separator for the outer for loop. |
| 223 | IFS=$save_IFS |
| 224 | |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 225 | if [ "$match" ]; then |
| 226 | output_lines="$tmp" |
| 227 | unmatched= |
| 228 | else |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 229 | unmatched="$unmatched%$symbol:$line" |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 230 | fi |
| 231 | done < defconfig |
Masahiro Yamada | 6fcbbe6 | 2014-09-26 18:42:36 +0900 | [diff] [blame] | 232 | |
| 233 | output_lines="$output_lines%$unmatched" |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 234 | done |
| 235 | |
| 236 | rm -f defconfig |
Masahiro Yamada | 1209482 | 2014-09-04 05:41:31 +0900 | [diff] [blame] | 237 | touch defconfig |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 238 | |
| 239 | save_IFS=$IFS |
| 240 | IFS=% |
| 241 | |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 242 | for line in $output_lines |
| 243 | do |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 244 | case "$line" in |
| 245 | "") |
| 246 | # do not output blank lines |
| 247 | ;; |
| 248 | *) |
| 249 | echo $line >> defconfig |
| 250 | ;; |
| 251 | esac |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 252 | done |
Masahiro Yamada | 6a8b5bb | 2014-09-04 05:41:32 +0900 | [diff] [blame] | 253 | |
| 254 | IFS=$save_IFS |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 255 | } |
| 256 | |
Masahiro Yamada | bbd6365 | 2014-09-10 18:13:10 +0900 | [diff] [blame] | 257 | # Some sanity checks before running "make <objdir>/<target>", |
| 258 | # where <objdir> should be either "spl" or "tpl". |
| 259 | # Doing "make spl/menuconfig" etc. on a non-SPL board makes no sense. |
| 260 | # It should be allowed only when ".config" exists and "CONFIG_SPL" is enabled. |
| 261 | # |
| 262 | # Usage: |
| 263 | # check_enabled_sumbimage <objdir>/<target> <objdir> |
| 264 | check_enabled_subimage () { |
| 265 | |
| 266 | case $2 in |
| 267 | spl|tpl) ;; |
| 268 | *) |
| 269 | echo >&2 "***" |
| 270 | echo >&2 "*** \"make $1\" is not supported." |
| 271 | echo >&2 "***" |
| 272 | exit 1 |
| 273 | ;; |
| 274 | esac |
| 275 | test -r "$KCONFIG_CONFIG" && get_enabled_subimages | grep -q $2 || { |
| 276 | config=CONFIG_$(echo $2 | tr '[a-z]' '[A-Z]') |
| 277 | |
| 278 | echo >&2 "***" |
| 279 | echo >&2 "*** Create \"$KCONFIG_CONFIG\" with \"$config\" enabled" |
| 280 | echo >&2 "*** before \"make $1\"." |
| 281 | echo >&2 "***" |
| 282 | exit 1 |
| 283 | } |
| 284 | } |
| 285 | |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 286 | # Usage: |
| 287 | # do_others <objdir>/<target> |
| 288 | # The field "<objdir>/" is typically empy, "spl/", "tpl/" for Normal, SPL, TPL, |
| 289 | # respectively. |
| 290 | # The field "<target>" is a configuration target such as "config", |
| 291 | # "menuconfig", etc. |
| 292 | do_others () { |
| 293 | target=${1##*/} |
| 294 | |
| 295 | if [ "$target" = "$1" ]; then |
| 296 | objdir= |
| 297 | else |
| 298 | objdir=${1%/*} |
Masahiro Yamada | bbd6365 | 2014-09-10 18:13:10 +0900 | [diff] [blame] | 299 | check_enabled_subimage $1 $objdir |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 300 | fi |
| 301 | |
| 302 | run_make_config $target $objdir |
| 303 | } |
| 304 | |
| 305 | progname=$(basename $0) |
| 306 | target=$1 |
| 307 | |
| 308 | case $target in |
| 309 | *_defconfig) |
| 310 | do_board_defconfig $target;; |
| 311 | *_config) |
Masahiro Yamada | cc533e9 | 2014-08-28 10:56:55 +0900 | [diff] [blame] | 312 | # backward compatibility |
Masahiro Yamada | c9c2b75 | 2014-08-21 11:44:34 +0900 | [diff] [blame] | 313 | do_board_defconfig ${target%_config}_defconfig;; |
| 314 | silentoldconfig) |
| 315 | do_silentoldconfig;; |
| 316 | defconfig) |
| 317 | do_defconfig;; |
| 318 | savedefconfig) |
| 319 | do_savedefconfig;; |
| 320 | *) |
| 321 | do_others $target;; |
| 322 | esac |