test_build_system.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.)
  25. # Set up some variables
  26. #
  27. [ -z ${TMP} ] && TMP="/tmp"
  28. # override ESP_IDF_TEMPLATE_GIT to point to a local dir if you're testing and want fast iterations
  29. [ -z ${ESP_IDF_TEMPLATE_GIT} ] && ESP_IDF_TEMPLATE_GIT=https://github.com/espressif/esp-idf-template.git
  30. export V=1
  31. function run_tests()
  32. {
  33. FAILURES=
  34. STATUS="Starting"
  35. print_status "Checking prerequisites"
  36. [ -z ${IDF_PATH} ] && echo "IDF_PATH is not set. Need path to esp-idf installation." && exit 2
  37. print_status "Cloning template from ${ESP_IDF_TEMPLATE_GIT}..."
  38. git clone ${ESP_IDF_TEMPLATE_GIT} template
  39. cd template
  40. git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
  41. print_status "Updating template config..."
  42. make defconfig || exit $?
  43. BOOTLOADER_BINS="bootloader/bootloader.elf bootloader/bootloader.bin"
  44. APP_BINS="app-template.elf app-template.bin"
  45. print_status "Initial clean build"
  46. # if make fails here, everything fails
  47. make || exit $?
  48. # check all the expected build artifacts from the clean build
  49. assert_built ${APP_BINS} ${BOOTLOADER_BINS} partitions_singleapp.bin
  50. [ -f ${BUILD}/partition*.bin ] || failure "A partition table should have been built"
  51. print_status "Updating component source file rebuilds component"
  52. # touch a file & do a build
  53. take_build_snapshot
  54. touch ${IDF_PATH}/components/esp32/syscalls.c
  55. make || failure "Failed to partial build"
  56. assert_rebuilt ${APP_BINS} esp32/libesp32.a esp32/syscalls.o
  57. assert_not_rebuilt lwip/liblwip.a freertos/libfreertos.a ${BOOTLOADER_BINS} partitions_singleapp.bin
  58. print_status "Bootloader source file rebuilds bootloader"
  59. take_build_snapshot
  60. touch ${IDF_PATH}/components/bootloader/src/main/bootloader_start.c
  61. make bootloader || failure "Failed to partial build bootloader"
  62. assert_rebuilt ${BOOTLOADER_BINS} bootloader/main/bootloader_start.o
  63. assert_not_rebuilt ${APP_BINS} partitions_singleapp.bin
  64. print_status "Partition CSV file rebuilds partitions"
  65. take_build_snapshot
  66. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  67. make partition_table || failure "Failed to build partition table"
  68. assert_rebuilt partitions_singleapp.bin
  69. assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
  70. print_status "Partial build doesn't compile anything by default"
  71. take_build_snapshot
  72. # verify no build files are refreshed by a partial make
  73. ALL_BUILD_FILES=$(find ${BUILD} -type f | sed "s@${BUILD}/@@")
  74. make || failure "Partial build failed"
  75. assert_not_rebuilt ${ALL_BUILD_FILES}
  76. print_status "Cleaning should remove all files from build"
  77. make clean || failure "Failed to make clean"
  78. ALL_BUILD_FILES=$(find ${BUILD} -type f)
  79. if [ -n "${ALL_BUILD_FILES}" ]; then
  80. failure "Files weren't cleaned: ${ALL_BUILD_FILES}"
  81. fi
  82. print_status "Moving BUILD_DIR_BASE out of tree should still build OK"
  83. rm -rf --preserve-root ${BUILD}/*
  84. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  85. make BUILD_DIR_BASE=${OUTOFTREE_BUILD} || failure "Failed to build with BUILD_DIR_BASE overriden"
  86. NEW_BUILD_FILES=$(find ${OUTOFREE_BUILD} -type f)
  87. if [ -z "${NEW_BUILD_FILES}" ]; then
  88. failure "No files found in new build directory!"
  89. fi
  90. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  91. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  92. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  93. fi
  94. print_status "Can still clean build if all text files are CRLFs"
  95. make clean || failure "Unexpected failure to make clean"
  96. find . -exec unix2dos {} \; # CRLFify template dir
  97. # make a copy of esp-idf and CRLFify it
  98. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  99. mkdir -p ${CRLF_ESPIDF}
  100. cp -rv ${IDF_PATH}/* ${CRLF_ESPIDF}
  101. # don't CRLFify executable files, as Linux will fail to execute them
  102. find ${CRLF_ESPIDF} -type f ! -perm 755 -exec unix2dos {} \;
  103. make IDF_PATH=${CRLF_ESPIDF} || failure "Failed to build with CRLFs in source"
  104. # do the same checks we do for the clean build
  105. assert_built ${APP_BINS} ${BOOTLOADER_BINS} partitions_singleapp.bin
  106. [ -f ${BUILD}/partition*.bin ] || failure "A partition table should have been built in CRLF mode"
  107. print_status "All tests completed"
  108. if [ -n "${FAILURES}" ]; then
  109. echo "Some failures were detected:"
  110. echo -e "${FAILURES}"
  111. exit 1
  112. else
  113. echo "Build tests passed."
  114. fi
  115. }
  116. function print_status()
  117. {
  118. echo "******** $1"
  119. STATUS="$1"
  120. }
  121. function failure()
  122. {
  123. echo "!!!!!!!!!!!!!!!!!!!"
  124. echo "FAILURE: $1"
  125. echo "!!!!!!!!!!!!!!!!!!!"
  126. FAILURES="${FAILURES}${STATUS} :: $1\n"
  127. }
  128. TESTDIR=${TMP}/build_system_tests_$$
  129. mkdir -p ${TESTDIR}
  130. # set NOCLEANUP=1 if you want to keep the test directory around
  131. # for post-mortem debugging
  132. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  133. SNAPSHOT=${TESTDIR}/snapshot
  134. BUILD=${TESTDIR}/template/build
  135. # copy all the build output to a snapshot directory
  136. function take_build_snapshot()
  137. {
  138. rm -rf ${SNAPSHOT}
  139. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  140. }
  141. # verify that all the arguments are present in the build output directory
  142. function assert_built()
  143. {
  144. until [ -z "$1" ]; do
  145. if [ ! -f "${BUILD}/$1" ]; then
  146. failure "File $1 should be in the build output directory"
  147. fi
  148. shift
  149. done
  150. }
  151. # Test if a file has been rebuilt.
  152. function file_was_rebuilt()
  153. {
  154. # can't use [ a -ot b ] here as -ot only gives second resolution
  155. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  156. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  157. return 0
  158. else
  159. return 1
  160. fi
  161. }
  162. # verify all the arguments passed in were rebuilt relative to the snapshot
  163. function assert_rebuilt()
  164. {
  165. until [ -z "$1" ]; do
  166. assert_built "$1"
  167. if [ ! -f "${SNAPSHOT}/$1" ]; then
  168. failure "File $1 should have been original build snapshot"
  169. fi
  170. if ! file_was_rebuilt "$1"; then
  171. failure "File $1 should have been rebuilt"
  172. fi
  173. shift
  174. done
  175. }
  176. # verify all the arguments are in the build directory & snapshot,
  177. # but were not rebuilt
  178. function assert_not_rebuilt()
  179. {
  180. until [ -z "$1" ]; do
  181. assert_built "$1"
  182. if [ ! -f "${SNAPSHOT}/$1" ]; then
  183. failure "File $1 should be in snapshot build directory"
  184. fi
  185. if file_was_rebuilt "$1"; then
  186. failure "File $1 should not have been rebuilt"
  187. fi
  188. shift
  189. done
  190. }
  191. cd ${TESTDIR}
  192. run_tests