test_build_system.sh 9.2 KB

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