test_build_system.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/bin/bash
  2. #
  3. # Test the build system for basic consistency
  4. #
  5. # A bash script that tests some likely make failure scenarios in a row
  6. #
  7. # Assumes PWD is an out-of-tree build directory, and will create a
  8. # subdirectory inside it to run build tests in.
  9. #
  10. # Environment variables:
  11. # IDF_PATH - must be set
  12. # ESP_IDF_TEMPLATE_GIT - Can override git clone source for template app. Otherwise github.
  13. # NOCLEANUP - Set to '1' if you want the script to leave its temporary directory when done, for post-mortem.
  14. #
  15. #
  16. # Internals:
  17. # * The tests run in sequence & the system keeps track of all failures to print at the end.
  18. # * BUILD directory is set to default BUILD_DIR_BASE
  19. # * The "print_status" function both prints a status line to the log and keeps track of which test is running.
  20. # * Calling the "failure" function prints a failure message to the log and also adds to the list of failures to print at the end.
  21. # * The function "assert_built" tests for a file relative to the BUILD directory.
  22. # * The function "take_build_snapshot" can be paired with the functions "assert_rebuilt" and "assert_not_rebuilt" to compare file timestamps and verify if they were rebuilt or not since the snapshot was taken.
  23. #
  24. # To add a new test case, add it to the end of the run_tests function. Note that not all test cases do comprehensive cleanup
  25. # (although very invasive ones like appending CRLFs to all files take a copy of the esp-idf tree), however the clean_build_dir
  26. # function can be used to force-delete all files from the build output directory.
  27. # Set up some variables
  28. #
  29. # override ESP_IDF_TEMPLATE_GIT to point to a local dir if you're testing and want fast iterations
  30. [ -z ${ESP_IDF_TEMPLATE_GIT} ] && ESP_IDF_TEMPLATE_GIT=https://github.com/espressif/esp-idf-template.git
  31. # uncomment next line to produce a lot more debug output
  32. #export V=1
  33. function run_tests()
  34. {
  35. FAILURES=
  36. STATUS="Starting"
  37. print_status "Checking prerequisites"
  38. [ -z ${IDF_PATH} ] && echo "IDF_PATH is not set. Need path to esp-idf installation." && exit 2
  39. print_status "Cloning template from ${ESP_IDF_TEMPLATE_GIT}..."
  40. git clone ${ESP_IDF_TEMPLATE_GIT} template
  41. cd template
  42. git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
  43. print_status "Updating template config..."
  44. make defconfig || exit $?
  45. print_status "Try to clean fresh directory..."
  46. MAKEFLAGS= make clean || exit $?
  47. BOOTLOADER_BINS="bootloader/bootloader.elf bootloader/bootloader.bin"
  48. APP_BINS="app-template.elf app-template.bin"
  49. print_status "Initial clean build"
  50. # if make fails here, everything fails
  51. make || exit $?
  52. # check all the expected build artifacts from the clean build
  53. assert_built ${APP_BINS} ${BOOTLOADER_BINS} partitions_singleapp.bin
  54. [ -f ${BUILD}/partition*.bin ] || failure "A partition table should have been built"
  55. print_status "Updating component source file rebuilds component"
  56. # touch a file & do a build
  57. take_build_snapshot
  58. touch ${IDF_PATH}/components/esp32/cpu_start.c
  59. make || failure "Failed to partial build"
  60. assert_rebuilt ${APP_BINS} esp32/libesp32.a esp32/cpu_start.o
  61. assert_not_rebuilt lwip/liblwip.a freertos/libfreertos.a ${BOOTLOADER_BINS} partitions_singleapp.bin
  62. print_status "Bootloader source file rebuilds bootloader"
  63. take_build_snapshot
  64. touch ${IDF_PATH}/components/bootloader/subproject/main/bootloader_start.c
  65. make bootloader || failure "Failed to partial build bootloader"
  66. assert_rebuilt ${BOOTLOADER_BINS} bootloader/main/bootloader_start.o
  67. assert_not_rebuilt ${APP_BINS} partitions_singleapp.bin
  68. print_status "Partition CSV file rebuilds partitions"
  69. take_build_snapshot
  70. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  71. make partition_table || failure "Failed to build partition table"
  72. assert_rebuilt partitions_singleapp.bin
  73. assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
  74. print_status "Partial build doesn't compile anything by default"
  75. take_build_snapshot
  76. # verify no build files are refreshed by a partial make
  77. ALL_BUILD_FILES=$(find ${BUILD} -type f | sed "s@${BUILD}/@@")
  78. make || failure "Partial build failed"
  79. assert_not_rebuilt ${ALL_BUILD_FILES}
  80. print_status "Cleaning should remove all files from build"
  81. make clean || failure "Failed to make clean"
  82. ALL_BUILD_FILES=$(find ${BUILD} -type f)
  83. if [ -n "${ALL_BUILD_FILES}" ]; then
  84. failure "Files weren't cleaned: ${ALL_BUILD_FILES}"
  85. fi
  86. print_status "Bootloader build shouldn't leave build output anywhere else"
  87. clean_build_dir
  88. make bootloader
  89. # find wizardry: find any file not named sdkconfig.h that
  90. # isn't in the "bootloader" or "config" directories
  91. find ${BUILD} -type d \( -name bootloader -o -name config \) -prune , -type f ! -name sdkconfig.h || failure "Bootloader built files outside the bootloader or config directories"
  92. print_status "Moving BUILD_DIR_BASE out of tree"
  93. clean_build_dir
  94. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  95. make BUILD_DIR_BASE=${OUTOFTREE_BUILD} || failure "Failed to build with BUILD_DIR_BASE overriden"
  96. NEW_BUILD_FILES=$(find ${OUTOFREE_BUILD} -type f)
  97. if [ -z "${NEW_BUILD_FILES}" ]; then
  98. failure "No files found in new build directory!"
  99. fi
  100. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  101. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  102. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  103. fi
  104. print_status "BUILD_DIR_BASE inside default build directory"
  105. clean_build_dir
  106. make BUILD_DIR_BASE=build/subdirectory || failure "Failed to build with BUILD_DIR_BASE as subdir"
  107. NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
  108. if [ -z "${NEW_BUILD_FILES}" ]; then
  109. failure "No files found in new build directory!"
  110. fi
  111. print_status "Parallel builds should work OK"
  112. clean_build_dir
  113. (make -j5 2>&1 | tee ${TESTDIR}/parallel_build.log) || failure "Failed to build in parallel"
  114. if grep -q "warning: jobserver unavailable" ${TESTDIR}/parallel_build.log; then
  115. failure "Parallel build prints 'warning: jobserver unavailable' errors"
  116. fi
  117. print_status "Can still clean build if all text files are CRLFs"
  118. make clean || failure "Unexpected failure to make clean"
  119. find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
  120. # make a copy of esp-idf and CRLFify it
  121. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  122. mkdir -p ${CRLF_ESPIDF}
  123. cp -r ${IDF_PATH}/* ${CRLF_ESPIDF}
  124. # don't CRLFify executable files, as Linux will fail to execute them
  125. find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
  126. make IDF_PATH=${CRLF_ESPIDF} || failure "Failed to build with CRLFs in source"
  127. # do the same checks we do for the clean build
  128. assert_built ${APP_BINS} ${BOOTLOADER_BINS} partitions_singleapp.bin
  129. [ -f ${BUILD}/partition*.bin ] || failure "A partition table should have been built in CRLF mode"
  130. print_status "Touching rom ld file should re-link app and bootloader"
  131. make
  132. take_build_snapshot
  133. touch ${IDF_PATH}/components/esp32/ld/esp32.rom.ld
  134. make
  135. assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
  136. print_status "Touching app-only ld file should only re-link app"
  137. take_build_snapshot
  138. touch ${IDF_PATH}/components/esp32/ld/esp32.common.ld
  139. make
  140. assert_rebuilt ${APP_BINS}
  141. assert_not_rebuilt ${BOOTLOADER_BINS}
  142. print_status "sdkconfig update triggers full recompile"
  143. make
  144. take_build_snapshot
  145. touch sdkconfig
  146. make
  147. # check the component_project_vars.mk file was rebuilt
  148. assert_rebuilt esp32/component_project_vars.mk
  149. # pick one each of .c, .cpp, .S that #includes sdkconfig.h
  150. # and therefore should rebuild
  151. assert_rebuilt newlib/syscall_table.o
  152. assert_rebuilt nvs_flash/src/nvs_api.o
  153. assert_rebuilt freertos/xtensa_vectors.o
  154. print_status "Updating project Makefile triggers full recompile"
  155. make
  156. take_build_snapshot
  157. touch Makefile
  158. make
  159. # similar to previous test
  160. assert_rebuilt newlib/syscall_table.o
  161. assert_rebuilt nvs_flash/src/nvs_api.o
  162. assert_rebuilt freertos/xtensa_vectors.o
  163. print_status "All tests completed"
  164. if [ -n "${FAILURES}" ]; then
  165. echo "Some failures were detected:"
  166. echo -e "${FAILURES}"
  167. exit 1
  168. else
  169. echo "Build tests passed."
  170. fi
  171. }
  172. function print_status()
  173. {
  174. echo "******** $1"
  175. STATUS="$1"
  176. }
  177. function failure()
  178. {
  179. echo "!!!!!!!!!!!!!!!!!!!"
  180. echo "FAILURE: $1"
  181. echo "!!!!!!!!!!!!!!!!!!!"
  182. FAILURES="${FAILURES}${STATUS} :: $1\n"
  183. }
  184. TESTDIR=${PWD}/build_system_tests_$$
  185. mkdir -p ${TESTDIR}
  186. # set NOCLEANUP=1 if you want to keep the test directory around
  187. # for post-mortem debugging
  188. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  189. SNAPSHOT=${TESTDIR}/snapshot
  190. BUILD=${TESTDIR}/template/build
  191. # copy all the build output to a snapshot directory
  192. function take_build_snapshot()
  193. {
  194. rm -rf ${SNAPSHOT}
  195. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  196. }
  197. # verify that all the arguments are present in the build output directory
  198. function assert_built()
  199. {
  200. until [ -z "$1" ]; do
  201. if [ ! -f "${BUILD}/$1" ]; then
  202. failure "File $1 should be in the build output directory"
  203. fi
  204. shift
  205. done
  206. }
  207. # Test if a file has been rebuilt.
  208. function file_was_rebuilt()
  209. {
  210. # can't use [ a -ot b ] here as -ot only gives second resolution
  211. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  212. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  213. return 0
  214. else
  215. return 1
  216. fi
  217. }
  218. # verify all the arguments passed in were rebuilt relative to the snapshot
  219. function assert_rebuilt()
  220. {
  221. until [ -z "$1" ]; do
  222. assert_built "$1"
  223. if [ ! -f "${SNAPSHOT}/$1" ]; then
  224. failure "File $1 should have been original build snapshot"
  225. fi
  226. if ! file_was_rebuilt "$1"; then
  227. failure "File $1 should have been rebuilt"
  228. fi
  229. shift
  230. done
  231. }
  232. # verify all the arguments are in the build directory & snapshot,
  233. # but were not rebuilt
  234. function assert_not_rebuilt()
  235. {
  236. until [ -z "$1" ]; do
  237. assert_built "$1"
  238. if [ ! -f "${SNAPSHOT}/$1" ]; then
  239. failure "File $1 should be in snapshot build directory"
  240. fi
  241. if file_was_rebuilt "$1"; then
  242. failure "File $1 should not have been rebuilt"
  243. fi
  244. shift
  245. done
  246. }
  247. # do a "clean" that doesn't depend on 'make clean'
  248. function clean_build_dir()
  249. {
  250. rm -rf --preserve-root ${BUILD}/*
  251. }
  252. cd ${TESTDIR}
  253. run_tests