check_defconfigs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. # SPDX-License-Identifier: GPL-2.0
  2. #
  3. #!/bin/bash
  4. function usage ()
  5. {
  6. echo $0 'Check for kernel defconfig mismacth'
  7. echo 'Usage:'
  8. echo ' '$0' [-c] [-u] [defconfig-files]'
  9. echo ' -c Check changed defconfigs from last git commit'
  10. echo ' -u Update defconfig if no mismatch is found'
  11. echo ' -h This help'
  12. exit 0
  13. }
  14. function build_config_parse ()
  15. {
  16. build_config=$1
  17. CC=
  18. LD=
  19. cc=`cat ${build_config} | grep ^CC= | cut -d'=' -f 2`
  20. ld=`cat ${build_config} | grep ^LD= | cut -d'=' -f 2`
  21. if [ $cc == clang ]; then
  22. toolchain=`cat ${build_config} | grep CLANG_PREBUILT_BIN | cut -d'=' -f 2`
  23. CC="${PWD}/../${toolchain}/${cc}"
  24. LD="${PWD}/../${toolchain}/${ld}"
  25. else
  26. toolchain=`cat ${build_config} | grep LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN | cut -d'=' -f 2`
  27. cross_compile=`cat ${build_config} | grep ^CROSS_COMPILE | cut -d'=' -f 2`
  28. CC="${PWD}/../${toolchain}/${cross_compile}${cc}"
  29. fi
  30. }
  31. function build_config_read ()
  32. {
  33. build_config_parse ${ARM_CONFIG}
  34. CC_ARM="${CC}"
  35. LD_ARM="${LD}"
  36. build_config_parse ${ARM64_CONFIG}
  37. CC_ARM64="${CC}"
  38. LD_ARM64="${LD}"
  39. build_config_parse ${ARM64_GCC_CONFIG}
  40. CC_ARM64_GCC="${CC}"
  41. LD_ARM64_GCC="${LD}"
  42. build_config_parse ${KASAN_CONFIG}
  43. CC_KASAN="${CC}"
  44. LD_KASAN="${LD}"
  45. build_config_parse ${UBSAN_CONFIG}
  46. CC_UBSAN="${CC}"
  47. LD_UBSAN="${LD}"
  48. }
  49. function toolchain_check ()
  50. {
  51. if [ ! -d "../${TOOLCHAIN_CLANG_PATH}/linux-x86" ]; then
  52. echo "CLANG does not exist, clone it"
  53. mkdir -p "../${TOOLCHAIN_CLANG_PATH}"
  54. cd "../${TOOLCHAIN_CLANG_PATH}"
  55. git clone "http://gerrit.mediatek.inc:8080/${TOOLCHAIN_CLANG_REPO}"
  56. cd linux-x86
  57. git reset --hard ${TOOLCHAIN_CLANG_COMMIT}
  58. else
  59. echo "update CLANG to specific commit"
  60. cd "../${TOOLCHAIN_CLANG_PATH}/linux-x86"
  61. git reset --hard ${TOOLCHAIN_CLANG_COMMIT}
  62. fi
  63. cd ${KERNEL_PATH}
  64. if [ ! -d "../${TOOLCHAIN_GCC_PATH}/bin" ]; then
  65. echo "GCC does not exist, clone it"
  66. rm -rf "../${TOOLCHAIN_GCC_PATH}"
  67. mkdir -p "../prebuilts/gcc/linux-x86/aarch64"
  68. cd "../prebuilts/gcc/linux-x86/aarch64"
  69. git clone "http://gerrit.mediatek.inc:8080/${TOOLCHAIN_GCC_REPO}" -b alps-trunk-r0.basic
  70. else
  71. echo "update GCC to the latest codebase"
  72. cd "../${TOOLCHAIN_GCC_PATH}"
  73. git pull
  74. fi
  75. cd ${KERNEL_PATH}
  76. }
  77. CONFIGS=
  78. UPDATE=0
  79. CHECK_COMMIT=0
  80. CHECK_SERVICE=0
  81. CC=
  82. LD=
  83. CC_ARM=
  84. CC_ARM64=
  85. CC_ARM64_GCC=
  86. CC_KASAN=
  87. CC_UBSAN=
  88. LD_ARM=
  89. LD_ARM64=
  90. LD_ARM64_GCC=
  91. LD_KASAN=
  92. LD_UBSAN=
  93. ARM_CONFIG='build.config.mtk.arm'
  94. ARM64_CONFIG='build.config.mtk.aarch64'
  95. ARM64_GCC_CONFIG='build.config.mtk.aarch64.gcc'
  96. KASAN_CONFIG='build.config.mtk.aarch64.kasan'
  97. UBSAN_CONFIG='build.config.mtk.aarch64.ubsan'
  98. TOOLCHAIN_CLANG_PATH='prebuilts/clang/host'
  99. TOOLCHAIN_GCC_PATH='prebuilts/gcc/linux-x86/aarch64/aarch64-linux-gnu-6.3.1'
  100. TOOLCHAIN_CLANG_REPO='platform/prebuilts/clang/host/linux-x86'
  101. TOOLCHAIN_GCC_REPO='alps/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-gnu-6.3.1'
  102. TOOLCHAIN_CLANG_COMMIT='252aba16f513a857bc923172f67b0e55e23de35f'
  103. KERNEL_PATH=${PWD}
  104. build_config_read
  105. echo "CC_ARM=${CC_ARM}"
  106. echo "CC_ARM64=${CC_ARM64}"
  107. echo "CC_ARM64_GCC=${CC_ARM64_GCC}"
  108. echo "CC_KASAN=${CC_KASAN}"
  109. echo "CC_UBSAN=${CC_UBSAN}"
  110. echo "LD_ARM=${LD_ARM}"
  111. echo "LD_ARM64=${LD_ARM64}"
  112. echo "LD_ARM64_GCC=${LD_ARM64_GCC}"
  113. echo "LD_KASAN=${LD_KASAN}"
  114. echo "LD_UBSAN=${LD_UBSAN}"
  115. while getopts "cuh" opt; do
  116. case $opt in
  117. c)
  118. if [ $# == 2 ]; then
  119. CHECK_COMMIT=$2
  120. if [ ${CHECK_COMMIT} == FETCH_HEAD ]; then
  121. CHECK_SERVICE=1
  122. echo Checking from check service
  123. fi
  124. else
  125. CHECK_COMMIT="HEAD"
  126. fi
  127. KCONFIG=`git show ${CHECK_COMMIT} --stat | gawk '/\/Kconfig.* +\| +[0-9]*/{print $1}'`
  128. if [ ".$KCONFIG" != "." ]; then
  129. echo From git commit: Checking all defconfig due to Kconfig is changed.
  130. CONFIGS=`grep -rl "CONFIG_MTK_PLATFORM" arch/arm/configs arch/arm64/configs`
  131. fi
  132. CONFIGS+=" `git show ${CHECK_COMMIT} --stat-width=500 | gawk '/arch\/(arm64|arm)\/configs\/.*_defconfig +\| +[0-9]*/{print $1}'`"
  133. CONFIGS=`echo $CONFIGS | xargs -n1 | sort -u | xargs`
  134. if [ ".$CONFIGS" != "." ]; then
  135. echo From git commit: Checking $CONFIGS
  136. else
  137. echo "Nothing to check"
  138. exit 0
  139. fi
  140. ;;
  141. u)
  142. UPDATE=1
  143. ;;
  144. h)
  145. usage
  146. ;;
  147. esac
  148. done
  149. if [ $# == 0 ]; then
  150. usage
  151. fi
  152. if [ $CHECK_COMMIT == 0 ]; then
  153. shift $((OPTIND-1))
  154. CONFIGS="${CONFIGS} $*"
  155. fi
  156. check_defconfig() {
  157. CONFIG=${1}
  158. UPDATE=${2}
  159. CC_ARM=${3}
  160. CC_ARM64=${4}
  161. CC_ARM64_GCC=${5}
  162. CC_KASAN=${6}
  163. CC_UBSAN=${7}
  164. LD_ARM=${8}
  165. LD_ARM64=${9}
  166. LD_ARM64_GCC=${10}
  167. LD_KASAN=${11}
  168. LD_UBSAN=${12}
  169. FAIL=0
  170. echo "Checking ${CONFIG}"
  171. if [ ! -e ${CONFIG} ]; then
  172. echo warning: defconfig file $CONFIG not found
  173. return $FAIL
  174. fi
  175. echo $CONFIG | grep arch/arm64 > /dev/null
  176. RETVAL=$?
  177. if [ $RETVAL != 0 ]; then
  178. ARCH=arm
  179. CC=${CC_ARM}
  180. LD=${LD_ARM}
  181. else
  182. ARCH=arm64
  183. CC=${CC_ARM64}
  184. LD=${LD_ARM64}
  185. fi
  186. echo $CONFIG | grep kasan > /dev/null
  187. RETVAL=$?
  188. if [ $RETVAL == 0 ]; then
  189. CC=${CC_KASAN}
  190. LD=${LD_KASAN}
  191. fi
  192. echo $CONFIG | grep ubsan > /dev/null
  193. RETVAL=$?
  194. if [ $RETVAL == 0 ]; then
  195. CC=${CC_UBSAN}
  196. LD=${LD_UBSAN}
  197. fi
  198. OUT=out-$$
  199. mkdir ${OUT}
  200. ARGS=()
  201. if [ -n "${CC}" ]; then
  202. ARGS+=("CC=${CC}")
  203. fi
  204. if [ -n "${LD}" ]; then
  205. ARGS+=("LD=${LD}")
  206. fi
  207. CMD="make ARCH=${ARCH} ${ARGS[@]} O=${OUT} `basename ${CONFIG}` savedefconfig 2>&1"
  208. echo $CMD
  209. OUTPUT="$(${CMD})"
  210. RETVAL=$?
  211. echo "$OUTPUT"
  212. if [ $RETVAL != 0 ]; then
  213. echo error: Make ${CONFIG} error
  214. rm -rf ${OUT}
  215. FAIL=1
  216. return $FAIL
  217. fi
  218. # Check if there is a warning message
  219. if echo "$OUTPUT" | grep -q "^warning:"; then
  220. echo error: there is a warning message with Kconfig
  221. rm -rf ${OUT}
  222. FAIL=1
  223. return $FAIL
  224. fi
  225. # Check option mismatch
  226. gawk 'BEGIN{lastf="";} { if (lastf != FILENAME) { if (lastf != "") CHECK=1; lastf=FILENAME; } } \
  227. /CONFIG_/ { match($0, /CONFIG_[^ =]*/); option=substr($0, RSTART, RLENGTH); \
  228. if (CHECK==1) { if (option in opts && opts[option]==$0) delete opts[option]; } \
  229. else { if (option in opts && opts[option]!=$0) dups[option]=$0; opts[option]=$0; } } \
  230. END { C=0; RET=0; for (i in dups) { RET=1; C++; printf("error: %s duplicate in defconfig\n", i); } \
  231. for (i in opts) { RET=1; C++; printf("error: %s mismatch\n", i); } exit(RET);}' ${CONFIG} ${OUT}/.config
  232. RETVAL=$?
  233. if [ $RETVAL != 0 ]; then
  234. echo error: ${CONFIG}: defconfig mismatch. Please check Kconfig and follow SOP to update _defconfig.
  235. rm -rf ${OUT}
  236. FAIL=1
  237. return $FAIL
  238. fi
  239. if [ $UPDATE == 0 ]; then
  240. # Compare output, make sure format is correct
  241. OUTPUT="$(diff -u ${OUT}/defconfig ${CONFIG} 2>&1)"
  242. RETVAL=$?
  243. if [ $RETVAL != 0 ]; then
  244. echo error: ${CONFIG} compare error. Please follow SOP to update _defconfig
  245. echo "$OUTPUT"
  246. rm -rf ${OUT}
  247. FAIL=1
  248. return $FAIL
  249. fi
  250. else
  251. cp ${OUT}/defconfig ${CONFIG}
  252. fi
  253. rm -rf ${OUT}
  254. return $FAIL
  255. }
  256. export -f check_defconfig
  257. toolchain_check
  258. parallel check_defconfig ::: $CONFIGS ::: $UPDATE ::: $CC_ARM ::: $CC_ARM64 ::: $CC_ARM64_GCC ::: $CC_KASAN ::: $CC_UBSAN ::: $LD_ARM ::: $LD_ARM64 ::: $LD_ARM64_GCC ::: $LD_KASAN ::: $LD_UBSAN
  259. RETVAL=$?
  260. if [ $RETVAL != 0 ]; then
  261. echo
  262. echo Please check the following wiki for detail, thanks!
  263. echo http://wiki.mediatek.inc/display/KernelStandardization/Check+defconfig+in+kernel+preflight
  264. echo http://wiki.mediatek.inc/display/KernelStandardization/SOP+to+update+kernel+config
  265. fi
  266. if [ ${CHECK_SERVICE} == 1 ]; then
  267. rm -rf /tmp/prebuilts
  268. echo Remove toolchain in check service
  269. fi
  270. exit $RETVAL