test_build_system.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. if [ -z $CHECKOUT_REF_SCRIPT ]; then
  43. git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
  44. else
  45. $CHECKOUT_REF_SCRIPT esp-idf-template
  46. fi
  47. print_status "Updating template config..."
  48. make defconfig || exit $?
  49. print_status "Try to clean fresh directory..."
  50. MAKEFLAGS= make clean || exit $?
  51. BOOTLOADER_BINS="bootloader/bootloader.elf bootloader/bootloader.bin"
  52. APP_BINS="app-template.elf app-template.bin"
  53. print_status "Initial clean build"
  54. # if make fails here, everything fails
  55. make || exit $?
  56. # check all the expected build artifacts from the clean build
  57. assert_built ${APP_BINS} ${BOOTLOADER_BINS} partitions_singleapp.bin
  58. [ -f ${BUILD}/partition*.bin ] || failure "A partition table should have been built"
  59. print_status "Updating component source file rebuilds component"
  60. # touch a file & do a build
  61. take_build_snapshot
  62. touch ${IDF_PATH}/components/esp32/cpu_start.c
  63. make || failure "Failed to partial build"
  64. assert_rebuilt ${APP_BINS} esp32/libesp32.a esp32/cpu_start.o
  65. assert_not_rebuilt lwip/liblwip.a freertos/libfreertos.a ${BOOTLOADER_BINS} partitions_singleapp.bin
  66. print_status "Bootloader source file rebuilds bootloader"
  67. take_build_snapshot
  68. touch ${IDF_PATH}/components/bootloader/subproject/main/bootloader_start.c
  69. make bootloader || failure "Failed to partial build bootloader"
  70. assert_rebuilt ${BOOTLOADER_BINS} bootloader/main/bootloader_start.o
  71. assert_not_rebuilt ${APP_BINS} partitions_singleapp.bin
  72. print_status "Partition CSV file rebuilds partitions"
  73. take_build_snapshot
  74. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  75. make partition_table || failure "Failed to build partition table"
  76. assert_rebuilt partitions_singleapp.bin
  77. assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
  78. print_status "Partial build doesn't compile anything by default"
  79. take_build_snapshot
  80. # verify no build files are refreshed by a partial make
  81. ALL_BUILD_FILES=$(find ${BUILD} -type f | sed "s@${BUILD}/@@")
  82. make || failure "Partial build failed"
  83. assert_not_rebuilt ${ALL_BUILD_FILES}
  84. print_status "Cleaning should remove all files from build"
  85. make clean || failure "Failed to make clean"
  86. ALL_BUILD_FILES=$(find ${BUILD} -type f)
  87. if [ -n "${ALL_BUILD_FILES}" ]; then
  88. failure "Files weren't cleaned: ${ALL_BUILD_FILES}"
  89. fi
  90. print_status "Bootloader build shouldn't leave build output anywhere else"
  91. clean_build_dir
  92. make bootloader
  93. # find wizardry: find any file not named sdkconfig.h that
  94. # isn't in the "bootloader" or "config" directories
  95. 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"
  96. print_status "Moving BUILD_DIR_BASE out of tree"
  97. clean_build_dir
  98. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  99. make BUILD_DIR_BASE=${OUTOFTREE_BUILD} || failure "Failed to build with BUILD_DIR_BASE overriden"
  100. NEW_BUILD_FILES=$(find ${OUTOFREE_BUILD} -type f)
  101. if [ -z "${NEW_BUILD_FILES}" ]; then
  102. failure "No files found in new build directory!"
  103. fi
  104. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  105. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  106. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  107. fi
  108. print_status "BUILD_DIR_BASE inside default build directory"
  109. clean_build_dir
  110. make BUILD_DIR_BASE=build/subdirectory || failure "Failed to build with BUILD_DIR_BASE as subdir"
  111. NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
  112. if [ -z "${NEW_BUILD_FILES}" ]; then
  113. failure "No files found in new build directory!"
  114. fi
  115. print_status "Parallel builds should work OK"
  116. clean_build_dir
  117. (make -j5 2>&1 | tee ${TESTDIR}/parallel_build.log) || failure "Failed to build in parallel"
  118. if grep -q "warning: jobserver unavailable" ${TESTDIR}/parallel_build.log; then
  119. failure "Parallel build prints 'warning: jobserver unavailable' errors"
  120. fi
  121. print_status "Can still clean build if all text files are CRLFs"
  122. make clean || failure "Unexpected failure to make clean"
  123. find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
  124. # make a copy of esp-idf and CRLFify it
  125. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  126. mkdir -p ${CRLF_ESPIDF}
  127. cp -r ${IDF_PATH}/* ${CRLF_ESPIDF}
  128. # don't CRLFify executable files, as Linux will fail to execute them
  129. find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
  130. make IDF_PATH=${CRLF_ESPIDF} || failure "Failed to build with CRLFs in source"
  131. # do the same checks we do for the clean build
  132. assert_built ${APP_BINS} ${BOOTLOADER_BINS} partitions_singleapp.bin
  133. [ -f ${BUILD}/partition*.bin ] || failure "A partition table should have been built in CRLF mode"
  134. print_status "Touching rom ld file should re-link app and bootloader"
  135. make
  136. take_build_snapshot
  137. touch ${IDF_PATH}/components/esp32/ld/esp32.rom.ld
  138. make
  139. assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
  140. print_status "Touching app-only template ld file should only re-link app"
  141. take_build_snapshot
  142. touch ${IDF_PATH}/components/esp32/ld/esp32.common.ld.in
  143. make
  144. assert_rebuilt ${APP_BINS}
  145. assert_not_rebuilt ${BOOTLOADER_BINS}
  146. print_status "Touching a linker fragment file should trigger re-link of app" # only app linker script is generated by tool for now
  147. take_build_snapshot
  148. touch ${IDF_PATH}/components/esp32/linker.lf
  149. make
  150. assert_rebuilt ${APP_BINS}
  151. assert_not_rebuilt ${BOOTLOADER_BINS}
  152. print_status "sdkconfig update triggers full recompile"
  153. make
  154. take_build_snapshot
  155. touch sdkconfig
  156. make
  157. # check the component_project_vars.mk file was rebuilt
  158. assert_rebuilt esp32/component_project_vars.mk
  159. # pick one each of .c, .cpp, .S that #includes sdkconfig.h
  160. # and therefore should rebuild
  161. assert_rebuilt newlib/syscall_table.o
  162. assert_rebuilt nvs_flash/src/nvs_api.o
  163. assert_rebuilt freertos/xtensa_vectors.o
  164. print_status "Updating project Makefile triggers full recompile"
  165. make
  166. take_build_snapshot
  167. touch Makefile
  168. make
  169. # similar to previous test
  170. assert_rebuilt newlib/syscall_table.o
  171. assert_rebuilt nvs_flash/src/nvs_api.o
  172. assert_rebuilt freertos/xtensa_vectors.o
  173. print_status "print_flash_cmd target should produce one line of output"
  174. make
  175. test $(make print_flash_cmd V=0 | wc -l | tr -d ' ') -eq 1
  176. print_status "Can include/exclude object files"
  177. echo "#error This file should not compile" > main/excluded_file.c
  178. echo "int required_global;" > main/included_file.c
  179. echo "COMPONENT_OBJEXCLUDE := excluded_file.o" >> main/component.mk
  180. echo "COMPONENT_OBJINCLUDE := included_file.o" >> main/component.mk
  181. echo "COMPONENT_ADD_LDFLAGS := -l\$(COMPONENT_NAME) -u required_global" >> main/component.mk
  182. make
  183. git checkout main/component.mk
  184. rm main/{included,excluded}_file.c
  185. print_status "Can include/exclude object files outside of component tree"
  186. mkdir -p extra_source_dir
  187. echo "#error This file should not compile" > extra_source_dir/excluded_file.c
  188. echo "int required_global;" > extra_source_dir/included_file.c
  189. echo "COMPONENT_SRCDIRS := . ../extra_source_dir" >> main/component.mk
  190. echo "COMPONENT_OBJEXCLUDE := ../extra_source_dir/excluded_file.o" >> main/component.mk
  191. echo "COMPONENT_OBJINCLUDE := ../extra_source_dir/included_file.o" >> main/component.mk
  192. echo "COMPONENT_ADD_LDFLAGS := -l\$(COMPONENT_NAME) -u required_global" >> main/component.mk
  193. make
  194. git checkout main/component.mk
  195. rm -rf extra_source_dir
  196. print_status "Can build without git installed on system"
  197. clean_build_dir
  198. # Make provision for getting IDF version
  199. echo "custom-version-x.y" > ${IDF_PATH}/version.txt
  200. echo "project-version-w.z" > ${TESTDIR}/template/version.txt
  201. # Hide .gitmodules so that submodule check is avoided
  202. [ -f ${IDF_PATH}/.gitmodules ] && mv ${IDF_PATH}/.gitmodules ${IDF_PATH}/.gitmodules_backup
  203. # Overload `git` command
  204. echo -e '#!/bin/bash\ntouch ${IDF_PATH}/git_invoked' > git
  205. chmod +x git
  206. OLD_PATH=$PATH
  207. export PATH="$PWD:$PATH"
  208. make
  209. [ -f ${IDF_PATH}/git_invoked ] && rm ${IDF_PATH}/git_invoked && failure "git should not have been invoked in this case"
  210. rm -f ${IDF_PATH}/version.txt git
  211. rm -f ${TESTDIR}/template/version.txt
  212. [ -f ${IDF_PATH}/.gitmodules_backup ] && mv ${IDF_PATH}/.gitmodules_backup ${IDF_PATH}/.gitmodules
  213. export PATH=$OLD_PATH
  214. print_status "Rebuild when app version was changed"
  215. take_build_snapshot
  216. # App version
  217. echo "project-version-1.0" > ${TESTDIR}/template/version.txt
  218. make
  219. assert_rebuilt ${APP_BINS}
  220. print_status "Change app version"
  221. take_build_snapshot
  222. echo "project-version-2.0(012345678901234567890123456789)" > ${TESTDIR}/template/version.txt
  223. make
  224. assert_rebuilt ${APP_BINS}
  225. assert_not_rebuilt ${BOOTLOADER_BINS} esp32/libesp32.a
  226. rm -f ${TESTDIR}/template/version.txt
  227. print_status "Build fails if partitions don't fit in flash"
  228. sed -i.bak "s/CONFIG_ESPTOOLPY_FLASHSIZE.\+//" sdkconfig # remove all flashsize config
  229. echo "CONFIG_ESPTOOLPY_FLASHSIZE_1MB=y" >> sdkconfig # introduce undersize flash
  230. make defconfig || failure "Failed to reconfigure with smaller flash"
  231. ( make 2>&1 | grep "does not fit in configured flash size 1MB" ) || failure "Build didn't fail with expected flash size failure message"
  232. mv sdkconfig.bak sdkconfig
  233. print_status "All tests completed"
  234. if [ -n "${FAILURES}" ]; then
  235. echo "Some failures were detected:"
  236. echo -e "${FAILURES}"
  237. exit 1
  238. else
  239. echo "Build tests passed."
  240. fi
  241. }
  242. function print_status()
  243. {
  244. echo "******** $1"
  245. STATUS="$1"
  246. }
  247. function failure()
  248. {
  249. echo "!!!!!!!!!!!!!!!!!!!"
  250. echo "FAILURE: $1"
  251. echo "!!!!!!!!!!!!!!!!!!!"
  252. FAILURES="${FAILURES}${STATUS} :: $1\n"
  253. }
  254. TESTDIR=${PWD}/build_system_tests_$$
  255. mkdir -p ${TESTDIR}
  256. # set NOCLEANUP=1 if you want to keep the test directory around
  257. # for post-mortem debugging
  258. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  259. SNAPSHOT=${TESTDIR}/snapshot
  260. BUILD=${TESTDIR}/template/build
  261. # copy all the build output to a snapshot directory
  262. function take_build_snapshot()
  263. {
  264. rm -rf ${SNAPSHOT}
  265. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  266. }
  267. # verify that all the arguments are present in the build output directory
  268. function assert_built()
  269. {
  270. until [ -z "$1" ]; do
  271. if [ ! -f "${BUILD}/$1" ]; then
  272. failure "File $1 should be in the build output directory"
  273. fi
  274. shift
  275. done
  276. }
  277. # Test if a file has been rebuilt.
  278. function file_was_rebuilt()
  279. {
  280. # can't use [ a -ot b ] here as -ot only gives second resolution
  281. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  282. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  283. return 0
  284. else
  285. return 1
  286. fi
  287. }
  288. # verify all the arguments passed in were rebuilt relative to the snapshot
  289. function assert_rebuilt()
  290. {
  291. until [ -z "$1" ]; do
  292. assert_built "$1"
  293. if [ ! -f "${SNAPSHOT}/$1" ]; then
  294. failure "File $1 should have been original build snapshot"
  295. fi
  296. if ! file_was_rebuilt "$1"; then
  297. failure "File $1 should have been rebuilt"
  298. fi
  299. shift
  300. done
  301. }
  302. # verify all the arguments are in the build directory & snapshot,
  303. # but were not rebuilt
  304. function assert_not_rebuilt()
  305. {
  306. until [ -z "$1" ]; do
  307. assert_built "$1"
  308. if [ ! -f "${SNAPSHOT}/$1" ]; then
  309. failure "File $1 should be in snapshot build directory"
  310. fi
  311. if file_was_rebuilt "$1"; then
  312. failure "File $1 should not have been rebuilt"
  313. fi
  314. shift
  315. done
  316. }
  317. # do a "clean" that doesn't depend on 'make clean'
  318. function clean_build_dir()
  319. {
  320. rm -rf --preserve-root ${BUILD}/*
  321. }
  322. cd ${TESTDIR}
  323. run_tests