merge_config.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. set -e
  16. clean_up() {
  17. rm -f "$TMP_FILE"
  18. rm -f "$TMP_FILE.new"
  19. }
  20. usage() {
  21. echo "Usage: $0 [OPTIONS] [CONFIG [...]]"
  22. echo " -h display this help text"
  23. echo " -m only merge the fragments, do not execute the make command"
  24. echo " -n use allnoconfig instead of alldefconfig"
  25. echo " -r list redundant entries when merging fragments"
  26. echo " -y make builtin have precedence over modules"
  27. echo " -O dir to put generated output files. Consider setting \$KCONFIG_CONFIG instead."
  28. echo " -s strict mode. Fail if the fragment redefines any value."
  29. echo " -Q disable warning messages for overridden options."
  30. echo
  31. echo "Used prefix: '$CONFIG_PREFIX'. You can redefine it with \$CONFIG_ environment variable."
  32. }
  33. RUNMAKE=true
  34. ALLTARGET=alldefconfig
  35. WARNREDUN=false
  36. BUILTIN=false
  37. OUTPUT=.
  38. STRICT=false
  39. CONFIG_PREFIX=${CONFIG_-CONFIG_}
  40. WARNOVERRIDE=echo
  41. if [ -z "$AWK" ]; then
  42. AWK=awk
  43. fi
  44. while true; do
  45. case $1 in
  46. "-n")
  47. ALLTARGET=allnoconfig
  48. shift
  49. continue
  50. ;;
  51. "-m")
  52. RUNMAKE=false
  53. shift
  54. continue
  55. ;;
  56. "-h")
  57. usage
  58. exit
  59. ;;
  60. "-r")
  61. WARNREDUN=true
  62. shift
  63. continue
  64. ;;
  65. "-y")
  66. BUILTIN=true
  67. shift
  68. continue
  69. ;;
  70. "-O")
  71. if [ -d $2 ];then
  72. OUTPUT=$(echo $2 | sed 's/\/*$//')
  73. else
  74. echo "output directory $2 does not exist" 1>&2
  75. exit 1
  76. fi
  77. shift 2
  78. continue
  79. ;;
  80. "-s")
  81. STRICT=true
  82. shift
  83. continue
  84. ;;
  85. "-Q")
  86. WARNOVERRIDE=true
  87. shift
  88. continue
  89. ;;
  90. *)
  91. break
  92. ;;
  93. esac
  94. done
  95. if [ "$#" -lt 1 ] ; then
  96. usage
  97. exit
  98. fi
  99. if [ -z "$KCONFIG_CONFIG" ]; then
  100. if [ "$OUTPUT" != . ]; then
  101. KCONFIG_CONFIG=$(readlink -m -- "$OUTPUT/.config")
  102. else
  103. KCONFIG_CONFIG=.config
  104. fi
  105. fi
  106. INITFILE=$1
  107. shift;
  108. if [ ! -r "$INITFILE" ]; then
  109. echo "The base file '$INITFILE' does not exist. Creating one..." >&2
  110. touch "$INITFILE"
  111. fi
  112. MERGE_LIST=$*
  113. TMP_FILE=$(mktemp ./.tmp.config.XXXXXXXXXX)
  114. echo "Using $INITFILE as base"
  115. trap clean_up EXIT
  116. cat $INITFILE > $TMP_FILE
  117. PROCESSED_FILES=""
  118. # Merge files, printing warnings on overridden values
  119. for 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. # Check for duplicate input files
  126. case " $PROCESSED_FILES " in
  127. *" $ORIG_MERGE_FILE "*)
  128. ${WARNOVERRIDE} "WARNING: Input file provided multiple times: $ORIG_MERGE_FILE"
  129. ;;
  130. esac
  131. # Use awk for single-pass processing instead of per-symbol grep/sed
  132. if ! "$AWK" -v prefix="$CONFIG_PREFIX" \
  133. -v warnoverride="$WARNOVERRIDE" \
  134. -v strict="$STRICT" \
  135. -v outfile="$TMP_FILE.new" \
  136. -v builtin="$BUILTIN" \
  137. -v warnredun="$WARNREDUN" '
  138. BEGIN {
  139. strict_violated = 0
  140. cfg_regex = "^" prefix "[a-zA-Z0-9_]+"
  141. notset_regex = "^# " prefix "[a-zA-Z0-9_]+ is not set$"
  142. }
  143. # Extract config name from a line, returns "" if not a config line
  144. function get_cfg(line) {
  145. if (match(line, cfg_regex)) {
  146. return substr(line, RSTART, RLENGTH)
  147. } else if (match(line, notset_regex)) {
  148. # Extract CONFIG_FOO from "# CONFIG_FOO is not set"
  149. sub(/^# /, "", line)
  150. sub(/ is not set$/, "", line)
  151. return line
  152. }
  153. return ""
  154. }
  155. function warn_builtin(cfg, prev, new) {
  156. if (warnoverride == "true") return
  157. print cfg ": -y passed, will not demote y to m"
  158. print "Previous value: " prev
  159. print "New value: " new
  160. print ""
  161. }
  162. function warn_redefined(cfg, prev, new) {
  163. if (warnoverride == "true") return
  164. print "Value of " cfg " is redefined by fragment " mergefile ":"
  165. print "Previous value: " prev
  166. print "New value: " new
  167. print ""
  168. }
  169. function warn_redundant(cfg) {
  170. if (warnredun != "true" || warnoverride == "true") return
  171. print "Value of " cfg " is redundant by fragment " mergefile ":"
  172. }
  173. # First pass: read merge file, store all lines and index
  174. FILENAME == ARGV[1] {
  175. mergefile = FILENAME
  176. merge_lines[FNR] = $0
  177. merge_total = FNR
  178. cfg = get_cfg($0)
  179. if (cfg != "") {
  180. merge_cfg[cfg] = $0
  181. merge_cfg_line[cfg] = FNR
  182. }
  183. next
  184. }
  185. # Second pass: process base file (TMP_FILE)
  186. FILENAME == ARGV[2] {
  187. cfg = get_cfg($0)
  188. # Not a config or not in merge file - keep it
  189. if (cfg == "" || !(cfg in merge_cfg)) {
  190. print $0 >> outfile
  191. next
  192. }
  193. prev_val = $0
  194. new_val = merge_cfg[cfg]
  195. # BUILTIN: do not demote y to m
  196. if (builtin == "true" && new_val ~ /=m$/ && prev_val ~ /=y$/) {
  197. warn_builtin(cfg, prev_val, new_val)
  198. print $0 >> outfile
  199. skip_merge[merge_cfg_line[cfg]] = 1
  200. next
  201. }
  202. # Values equal - redundant
  203. if (prev_val == new_val) {
  204. warn_redundant(cfg)
  205. next
  206. }
  207. # "=n" is the same as "is not set"
  208. if (prev_val ~ /=n$/ && new_val ~ / is not set$/) {
  209. print $0 >> outfile
  210. next
  211. }
  212. # Values differ - redefined
  213. warn_redefined(cfg, prev_val, new_val)
  214. if (strict == "true") {
  215. strict_violated = 1
  216. }
  217. }
  218. END {
  219. # Newline in case base file lacks trailing newline
  220. print "" >> outfile
  221. # Append merge file, skipping lines marked for builtin preservation
  222. for (i = 1; i <= merge_total; i++) {
  223. if (!(i in skip_merge)) {
  224. print merge_lines[i] >> outfile
  225. }
  226. }
  227. if (strict_violated) {
  228. exit 1
  229. }
  230. }' \
  231. "$ORIG_MERGE_FILE" "$TMP_FILE"; then
  232. # awk exited non-zero, strict mode was violated
  233. STRICT_MODE_VIOLATED=true
  234. fi
  235. mv "$TMP_FILE.new" "$TMP_FILE"
  236. PROCESSED_FILES="$PROCESSED_FILES $ORIG_MERGE_FILE"
  237. done
  238. if [ "$STRICT_MODE_VIOLATED" = "true" ]; then
  239. echo "The fragment redefined a value and strict mode had been passed."
  240. exit 1
  241. fi
  242. if [ "$RUNMAKE" = "false" ]; then
  243. cp -T -- "$TMP_FILE" "$KCONFIG_CONFIG"
  244. echo "#"
  245. echo "# merged configuration written to $KCONFIG_CONFIG (needs make)"
  246. echo "#"
  247. exit
  248. fi
  249. # If we have an output dir, setup the O= argument, otherwise leave
  250. # it blank, since O=. will create an unnecessary ./source softlink
  251. OUTPUT_ARG=""
  252. if [ "$OUTPUT" != "." ] ; then
  253. OUTPUT_ARG="O=$OUTPUT"
  254. fi
  255. # Use the merged file as the starting point for:
  256. # alldefconfig: Fills in any missing symbols with Kconfig default
  257. # allnoconfig: Fills in any missing symbols with # CONFIG_* is not set
  258. make KCONFIG_ALLCONFIG=$TMP_FILE $OUTPUT_ARG $ALLTARGET
  259. # Check all specified config values took effect (might have missed-dependency issues)
  260. if ! "$AWK" -v prefix="$CONFIG_PREFIX" \
  261. -v warnoverride="$WARNOVERRIDE" \
  262. -v strict="$STRICT" \
  263. -v warnredun="$WARNREDUN" '
  264. BEGIN {
  265. strict_violated = 0
  266. cfg_regex = "^" prefix "[a-zA-Z0-9_]+"
  267. notset_regex = "^# " prefix "[a-zA-Z0-9_]+ is not set$"
  268. }
  269. # Extract config name from a line, returns "" if not a config line
  270. function get_cfg(line) {
  271. if (match(line, cfg_regex)) {
  272. return substr(line, RSTART, RLENGTH)
  273. } else if (match(line, notset_regex)) {
  274. # Extract CONFIG_FOO from "# CONFIG_FOO is not set"
  275. sub(/^# /, "", line)
  276. sub(/ is not set$/, "", line)
  277. return line
  278. }
  279. return ""
  280. }
  281. function warn_mismatch(cfg, merged, final) {
  282. if (warnredun == "true") return
  283. if (final == "" && !(merged ~ / is not set$/ || merged ~ /=n$/)) {
  284. print "WARNING: Value requested for " cfg " not in final .config"
  285. print "Requested value: " merged
  286. print "Actual value: " final
  287. } else if (final == "" && merged ~ / is not set$/) {
  288. # not set, pass
  289. } else if (merged == "" && final != "") {
  290. print "WARNING: " cfg " not in merged config but added in final .config:"
  291. print "Requested value: " merged
  292. print "Actual value: " final
  293. } else {
  294. print "WARNING: " cfg " differs:"
  295. print "Requested value: " merged
  296. print "Actual value: " final
  297. }
  298. }
  299. # First pass: read effective config file, store all lines
  300. FILENAME == ARGV[1] {
  301. cfg = get_cfg($0)
  302. if (cfg != "") {
  303. config_cfg[cfg] = $0
  304. }
  305. next
  306. }
  307. # Second pass: process merged config and compare against effective config
  308. {
  309. cfg = get_cfg($0)
  310. if (cfg == "") next
  311. # strip trailing comment
  312. sub(/[[:space:]]+#.*/, "", $0)
  313. merged_val = $0
  314. final_val = config_cfg[cfg]
  315. if (merged_val == final_val) next
  316. if (merged_val ~ /=n$/ && final_val ~ / is not set$/) next
  317. if (merged_val ~ /=n$/ && final_val == "") next
  318. warn_mismatch(cfg, merged_val, final_val)
  319. if (strict == "true") {
  320. strict_violated = 1
  321. }
  322. }
  323. END {
  324. if (strict_violated) {
  325. exit 1
  326. }
  327. }' \
  328. "$KCONFIG_CONFIG" "$TMP_FILE"; then
  329. # awk exited non-zero, strict mode was violated
  330. STRICT_MODE_VIOLATED=true
  331. fi
  332. if [ "$STRICT" = "true" ] && [ "$STRICT_MODE_VIOLATED" = "true" ]; then
  333. echo "Requested and effective config differ"
  334. exit 1
  335. fi