process_configs.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #!/bin/bash
  2. #
  3. # This script takes the merged config files and processes them through oldconfig
  4. # and listnewconfig
  5. #
  6. # Globally disable suggestion of appending '|| exit' or '|| return' to cd/pushd/popd commands
  7. # shellcheck disable=SC2164
  8. test -n "$RHTEST" && exit 0
  9. usage()
  10. {
  11. # alphabetical order please
  12. echo "process_configs.sh [ options ] package_name kernel_version"
  13. echo " -a: report all errors, equivalent to [-c -n -w -i]"
  14. echo " -c: error on mismatched config options"
  15. echo " -i: continue on error"
  16. echo " -n: error on unset config options"
  17. echo " -t: test run, do not overwrite original config"
  18. echo " -w: error on misconfigured config options"
  19. echo " -z: commit new configs to pending directory"
  20. echo ""
  21. echo " A special CONFIG file tag, process_configs_known_broken can be added as a"
  22. echo " comment to any CONFIG file. This tag indicates that there is no way to "
  23. echo " fix a CONFIG's entry. This tag should only be used in extreme cases"
  24. echo " and is not to be used as a workaround to solve CONFIG problems."
  25. exit 1
  26. }
  27. die()
  28. {
  29. echo "$1"
  30. exit 1
  31. }
  32. get_cross_compile()
  33. {
  34. arch=$1
  35. if [[ "$CC_IS_CLANG" -eq 1 ]]; then
  36. echo "$arch"
  37. else
  38. echo "scripts/dummy-tools/"
  39. fi
  40. }
  41. # stupid function to find top of tree to do kernel make configs
  42. switch_to_toplevel()
  43. {
  44. path="$(pwd)"
  45. while test -n "$path"
  46. do
  47. test -e "$path"/MAINTAINERS && \
  48. test -d "$path"/drivers && \
  49. break
  50. path=$(dirname "$path")
  51. done
  52. test -n "$path" || die "Can't find toplevel"
  53. echo "$path"
  54. }
  55. checkoptions()
  56. {
  57. count=$3
  58. variant=$4
  59. /usr/bin/awk '
  60. /is not set/ {
  61. split ($0, a, "#");
  62. split(a[2], b);
  63. if (NR==FNR) {
  64. configs[b[1]]="is not set";
  65. } else {
  66. if (configs[b[1]] != "" && configs[b[1]] != "is not set")
  67. print "Found # "b[1] " is not set, after generation, had " b[1] " " configs[b[1]] " in Source tree";
  68. }
  69. }
  70. /=/ {
  71. split ($0, a, "=");
  72. if (NR==FNR) {
  73. configs[a[1]]=a[2];
  74. } else {
  75. if (configs[a[1]] != "" && configs[a[1]] != a[2])
  76. print "Found "a[1]"="a[2]" after generation, had " a[1]"="configs[a[1]]" in Source tree";
  77. }
  78. }
  79. ' "$1" "$2" > .mismatches"${count}"
  80. checkoptions_error=false
  81. if test -s .mismatches"${count}"
  82. then
  83. while read -r LINE
  84. do
  85. if find "${REDHAT}"/configs -name "$(echo "$LINE" | awk -F "=" ' { print $1 } ' | awk ' { print $2 }')" -print0 | xargs -0 grep ^ | grep -q "process_configs_known_broken"; then
  86. # This is a known broken config.
  87. # See script help warning.
  88. checkoptions_error=false
  89. else
  90. checkoptions_error=true
  91. break
  92. fi
  93. done < .mismatches"${count}"
  94. ! $checkoptions_error && return
  95. sed -i "1s/^/Error: Mismatches found in configuration files for ${arch} ${variant}\n/" .mismatches"${count}"
  96. else
  97. rm -f .mismatches"${count}"
  98. fi
  99. }
  100. parsenewconfigs()
  101. {
  102. tmpdir=$(mktemp -d)
  103. # This awk script reads the output of make listnewconfig
  104. # and puts it into CONFIG_FOO files. Using the output of
  105. # listnewconfig is much easier to ensure we get the default
  106. # output.
  107. /usr/bin/awk -v BASE="$tmpdir" '
  108. /is not set/ {
  109. split ($0, a, "#");
  110. split(a[2], b);
  111. OUT_FILE=BASE"/"b[1];
  112. print $0 >> OUT_FILE;
  113. }
  114. /=/ {
  115. split ($0, a, "=");
  116. OUT_FILE=BASE"/"a[1];
  117. if (a[2] == "n")
  118. print "# " a[1] " is not set" >> OUT_FILE;
  119. else
  120. print $0 >> OUT_FILE;
  121. }
  122. ' .newoptions
  123. # This awk script parses the output of helpnewconfig.
  124. # Each option is separated between ----- markers
  125. # The goal is to put all the help text as a comment in
  126. # each CONFIG_FOO file. Because of how awk works
  127. # there's a lot of moving files around and catting to
  128. # get what we need.
  129. /usr/bin/awk -v BASE="$tmpdir" '
  130. BEGIN { inpatch=0;
  131. outfile="none";
  132. symbol="none"; }
  133. /^Symbol: .*$/ {
  134. split($0, a, " ");
  135. symbol="CONFIG_"a[2];
  136. outfile=BASE "/fake_"symbol
  137. }
  138. /-----/ {
  139. if (inpatch == 0) {
  140. inpatch = 1;
  141. }
  142. else {
  143. if (symbol != "none") {
  144. system("cat " outfile " " BASE "/" symbol " > " BASE "/tmpf");
  145. system("mv " BASE "/tmpf " BASE "/" symbol);
  146. symbol="none"
  147. }
  148. outfile="none"
  149. inpatch = 0;
  150. }
  151. }
  152. !/-----/ {
  153. if (inpatch == 1 && outfile != "none") {
  154. print "# "$0 >> outfile;
  155. }
  156. }
  157. ' .helpnewconfig
  158. pushd "$tmpdir" &> /dev/null
  159. rm fake_*
  160. popd &> /dev/null
  161. for f in "$tmpdir"/*; do
  162. [[ -e "$f" ]] || break
  163. cp "$f" "$SCRIPT_DIR/pending$FLAVOR/generic/"
  164. done
  165. rm -rf "$tmpdir"
  166. }
  167. function commit_new_configs()
  168. {
  169. # assume we are in $source_tree/configs, need to get to top level
  170. pushd "$(switch_to_toplevel)" &>/dev/null
  171. for cfg in "$SCRIPT_DIR/${SPECPACKAGE_NAME}${KVERREL}"*.config
  172. do
  173. arch=$(head -1 "$cfg" | cut -b 3-)
  174. cfgtmp="${cfg}.tmp"
  175. cfgorig="${cfg}.orig"
  176. cat "$cfg" > "$cfgorig"
  177. if [ "$arch" = "EMPTY" ]
  178. then
  179. # This arch is intentionally left blank
  180. continue
  181. fi
  182. echo -n "Checking for new configs in $cfg ... "
  183. # shellcheck disable=SC2086
  184. make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE="$(get_cross_compile "$arch")" KCONFIG_CONFIG="$cfgorig" listnewconfig >& .listnewconfig
  185. grep -E 'CONFIG_' .listnewconfig > .newoptions
  186. if test -s .newoptions
  187. then
  188. # shellcheck disable=SC2086
  189. make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE="$(get_cross_compile "$arch")" KCONFIG_CONFIG="$cfgorig" helpnewconfig >& .helpnewconfig
  190. parsenewconfigs
  191. fi
  192. rm .newoptions
  193. echo "done"
  194. done
  195. git add "$SCRIPT_DIR/pending$FLAVOR"
  196. git commit -m "[redhat] AUTOMATIC: New configs"
  197. }
  198. function process_config()
  199. {
  200. local cfg
  201. local arch
  202. local cfgtmp
  203. local cfgorig
  204. local count
  205. local variant
  206. cfg=$1
  207. count=$2
  208. arch=$(head -1 "$cfg" | cut -b 3-)
  209. if [ "$arch" = "EMPTY" ]
  210. then
  211. # This arch is intentionally left blank
  212. return
  213. fi
  214. variant=$(basename "$cfg" | cut -d"-" -f3- | cut -d"." -f1)
  215. cfgtmp="${cfg}.tmp"
  216. cfgorig="${cfg}.orig"
  217. cat "$cfg" > "$cfgorig"
  218. echo "Processing $cfg ... "
  219. # shellcheck disable=SC2086
  220. make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE="$(get_cross_compile "$arch")" KCONFIG_CONFIG="$cfgorig" listnewconfig >& .listnewconfig"${count}"
  221. grep -E 'CONFIG_' .listnewconfig"${count}" > .newoptions"${count}"
  222. if test -n "$NEWOPTIONS" && test -s .newoptions"${count}"
  223. then
  224. echo "Found unset config items in ${arch} ${variant}, please set them to an appropriate value" >> .errors"${count}"
  225. cat .newoptions"${count}" >> .errors"${count}"
  226. rm .newoptions"${count}"
  227. RETURNCODE=1
  228. fi
  229. rm -f .newoptions"${count}"
  230. grep -E 'config.*warning' .listnewconfig"${count}" > .warnings"${count}"
  231. if test -n "$CHECKWARNINGS" && test -s .warnings"${count}"
  232. then
  233. echo "Found misconfigured config items in ${arch} ${variant}, please set them to an appropriate value" >> .errors"${count}"
  234. cat .warnings"${count}" >> .errors"${count}"
  235. fi
  236. rm .warnings"${count}"
  237. rm .listnewconfig"${count}"
  238. # shellcheck disable=SC2086
  239. make ${MAKEOPTS} ARCH="$arch" CROSS_COMPILE="$(get_cross_compile "$arch")" KCONFIG_CONFIG="$cfgorig" olddefconfig > /dev/null || exit 1
  240. echo "# $arch" > "$cfgtmp"
  241. cat "$cfgorig" >> "$cfgtmp"
  242. if test -n "$CHECKOPTIONS"
  243. then
  244. checkoptions "$cfg" "$cfgtmp" "$count" "$variant"
  245. fi
  246. # if test run, don't overwrite original
  247. if test -n "$TESTRUN"
  248. then
  249. rm -f "$cfgtmp"
  250. else
  251. mv "$cfgtmp" "$cfg"
  252. fi
  253. rm -f "$cfgorig"
  254. echo "Processing $cfg complete"
  255. }
  256. function process_configs()
  257. {
  258. # assume we are in $source_tree/configs, need to get to top level
  259. pushd "$(switch_to_toplevel)" &>/dev/null
  260. # The next line is throwaway code for transition to parallel
  261. # processing. Leaving this line in place is harmless, but it can be
  262. # removed the next time anyone updates this function.
  263. [ -f .mismatches ] && rm -f .mismatches
  264. count=0
  265. for cfg in "$SCRIPT_DIR/${SPECPACKAGE_NAME}${KVERREL}"*.config
  266. do
  267. if [ "$count" -eq 0 ]; then
  268. # do the first one by itself so that tools are built
  269. process_config "$cfg" "$count"
  270. fi
  271. process_config "$cfg" "$count" &
  272. # shellcheck disable=SC2004
  273. waitpids[${count}]=$!
  274. ((count++))
  275. while [ "$(jobs | grep -c Running)" -ge "$RHJOBS" ]; do :; done
  276. done
  277. # shellcheck disable=SC2048
  278. for pid in ${waitpids[*]}; do
  279. wait "${pid}"
  280. done
  281. rm "$SCRIPT_DIR"/*.config*.old
  282. if ls .errors* 1> /dev/null 2>&1; then
  283. RETURNCODE=1
  284. cat .errors*
  285. rm .errors* -f
  286. fi
  287. if ls .mismatches* 1> /dev/null 2>&1; then
  288. RETURNCODE=1
  289. cat .mismatches*
  290. rm .mismatches* -f
  291. fi
  292. popd > /dev/null
  293. [ $RETURNCODE -eq 0 ] && echo "Processed config files are in $SCRIPT_DIR"
  294. }
  295. CHECKOPTIONS=""
  296. NEWOPTIONS=""
  297. TESTRUN=""
  298. CHECKWARNINGS=""
  299. MAKEOPTS=""
  300. CC_IS_CLANG=0
  301. RETURNCODE=0
  302. while [[ $# -gt 0 ]]
  303. do
  304. key="$1"
  305. case $key in
  306. -a)
  307. CHECKOPTIONS="x"
  308. NEWOPTIONS="x"
  309. CHECKWARNINGS="x"
  310. ;;
  311. -c)
  312. CHECKOPTIONS="x"
  313. ;;
  314. -h)
  315. usage
  316. ;;
  317. -n)
  318. NEWOPTIONS="x"
  319. ;;
  320. -t)
  321. TESTRUN="x"
  322. ;;
  323. -w)
  324. CHECKWARNINGS="x"
  325. ;;
  326. -z)
  327. COMMITNEWCONFIGS="x"
  328. ;;
  329. -m)
  330. shift
  331. if [ "$1" = "CC=clang" ] || [ "$1" = "LLVM=1" ]; then
  332. CC_IS_CLANG=1
  333. fi
  334. MAKEOPTS="$MAKEOPTS $1"
  335. ;;
  336. *)
  337. break;;
  338. esac
  339. shift
  340. done
  341. KVERREL="$(test -n "$1" && echo "-$1" || echo "")"
  342. FLAVOR="$(test -n "$2" && echo "-$2" || echo "-rhel")"
  343. # shellcheck disable=SC2015
  344. SCRIPT=$(readlink -f "$0")
  345. SCRIPT_DIR=$(dirname "$SCRIPT")
  346. # Config options for RHEL should target the pending-rhel directory, not pending-common.
  347. if [ "$FLAVOR" = "-rhel" ]
  348. then
  349. FLAVOR="-rhel"
  350. fi
  351. # to handle this script being a symlink
  352. cd "$SCRIPT_DIR"
  353. if test -n "$COMMITNEWCONFIGS"; then
  354. commit_new_configs
  355. else
  356. process_configs
  357. fi
  358. exit $RETURNCODE