test_build_system.sh 10 KB

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