test_build_system_cmake.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #!/bin/bash
  2. #
  3. # Test the build system for basic consistency (Cmake/idf.py version)
  4. #
  5. # A bash script that tests some likely build 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. export PATH="$IDF_PATH/tools:$PATH" # for idf.py
  34. function run_tests()
  35. {
  36. FAILURES=
  37. STATUS="Starting"
  38. print_status "Checking prerequisites"
  39. [ -z ${IDF_PATH} ] && echo "IDF_PATH is not set. Need path to esp-idf installation." && exit 2
  40. print_status "Cloning template from ${ESP_IDF_TEMPLATE_GIT}..."
  41. git clone ${ESP_IDF_TEMPLATE_GIT} template
  42. cd template
  43. if [ -z $CHECKOUT_REF_SCRIPT ]; then
  44. git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
  45. else
  46. $CHECKOUT_REF_SCRIPT esp-idf-template
  47. fi
  48. print_status "Try to clean fresh directory..."
  49. idf.py fullclean || exit $?
  50. # all relative to the build directory
  51. BOOTLOADER_BINS="bootloader/bootloader.elf bootloader/bootloader.bin"
  52. APP_BINS="app-template.elf app-template.bin"
  53. PARTITION_BIN="partition_table/partition-table.bin"
  54. IDF_COMPONENT_PREFIX="idf_component"
  55. print_status "Initial clean build"
  56. # if build fails here, everything fails
  57. idf.py build || exit $?
  58. # check all the expected build artifacts from the clean build
  59. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  60. print_status "Updating component source file rebuilds component"
  61. # touch a file & do a build
  62. take_build_snapshot
  63. touch ${IDF_PATH}/components/esp32/cpu_start.c
  64. idf.py build || failure "Failed to partial build"
  65. assert_rebuilt ${APP_BINS} esp-idf/esp32/libesp32.a esp-idf/esp32/CMakeFiles/${IDF_COMPONENT_PREFIX}_esp32.dir/cpu_start.c.obj
  66. assert_not_rebuilt esp-idf/lwip/liblwip.a esp-idf/freertos/libfreertos.a ${BOOTLOADER_BINS} ${PARTITION_BIN}
  67. print_status "Bootloader source file rebuilds bootloader"
  68. take_build_snapshot
  69. touch ${IDF_PATH}/components/bootloader/subproject/main/bootloader_start.c
  70. idf.py build || failure "Failed to partial build bootloader"
  71. assert_rebuilt ${BOOTLOADER_BINS} bootloader/esp-idf/main/CMakeFiles/${IDF_COMPONENT_PREFIX}_main.dir/bootloader_start.c.obj
  72. assert_not_rebuilt ${APP_BINS} ${PARTITION_BIN}
  73. print_status "Partition CSV file rebuilds partitions"
  74. take_build_snapshot
  75. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  76. idf.py build || failure "Failed to build partition table"
  77. assert_rebuilt ${PARTITION_BIN}
  78. assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
  79. print_status "Partial build doesn't compile anything by default"
  80. take_build_snapshot
  81. # verify no build files are refreshed by a partial make
  82. ALL_BUILD_FILES=$(find ${BUILD} -type f | sed "s@${BUILD}/@@" | grep -v '^.')
  83. idf.py build || failure "Partial build failed"
  84. assert_not_rebuilt ${ALL_BUILD_FILES}
  85. print_status "Rebuild when app version was changed"
  86. clean_build_dir
  87. # App version
  88. echo "project-version-1.0" > ${TESTDIR}/template/version.txt
  89. idf.py build || failure "Failed to build with app version"
  90. print_status "Change app version"
  91. take_build_snapshot
  92. echo "project-version-2.0(012345678901234567890123456789)" > ${TESTDIR}/template/version.txt
  93. idf.py build || failure "Failed to rebuild with changed app version"
  94. assert_rebuilt ${APP_BINS}
  95. assert_not_rebuilt ${BOOTLOADER_BINS} esp-idf/esp32/libesp32.a
  96. rm -f ${TESTDIR}/template/version.txt
  97. print_status "Moving BUILD_DIR_BASE out of tree"
  98. clean_build_dir
  99. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  100. idf.py -B "${OUTOFTREE_BUILD}" build || failure "Failed to build with out-of-tree build dir"
  101. NEW_BUILD_FILES=$(find ${OUTOFREE_BUILD} -type f)
  102. if [ -z "${NEW_BUILD_FILES}" ]; then
  103. failure "No files found in new build directory!"
  104. fi
  105. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  106. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  107. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  108. fi
  109. print_status "BUILD_DIR_BASE inside default build directory"
  110. clean_build_dir
  111. idf.py -B "build/subdirectory" build || failure "Failed to build with build dir as subdir"
  112. NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
  113. if [ -z "${NEW_BUILD_FILES}" ]; then
  114. failure "No files found in new build directory!"
  115. fi
  116. print_status "Can still clean build if all text files are CRLFs"
  117. clean_build_dir
  118. find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
  119. # make a copy of esp-idf and CRLFify it
  120. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  121. mkdir -p ${CRLF_ESPIDF}
  122. cp -r ${IDF_PATH}/* ${CRLF_ESPIDF}
  123. # don't CRLFify executable files, as Linux will fail to execute them
  124. find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
  125. IDF_PATH=${CRLF_ESPIDF} idf.py build || failure "Failed to build with CRLFs in source"
  126. # do the same checks we do for the clean build
  127. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  128. print_status "Updating rom ld file should re-link app and bootloader"
  129. clean_build_dir
  130. idf.py build
  131. take_build_snapshot
  132. sleep 1 # ninja may ignore if the timestamp delta is too low
  133. cp ${IDF_PATH}/components/esp32/ld/esp32.rom.ld .
  134. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp32/ld/esp32.rom.ld
  135. tail ${IDF_PATH}/components/esp32/ld/esp32.rom.ld
  136. idf.py build || failure "Failed to rebuild with modified linker script"
  137. assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
  138. mv esp32.rom.ld ${IDF_PATH}/components/esp32/ld/
  139. print_status "Updating app-only ld file should only re-link app"
  140. take_build_snapshot
  141. cp ${IDF_PATH}/components/esp32/ld/esp32.common.ld.in .
  142. sleep 1 # ninja may ignore if the timestamp delta is too low
  143. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp32/ld/esp32.common.ld.in
  144. idf.py build || failure "Failed to rebuild with modified linker script"
  145. assert_rebuilt ${APP_BINS}
  146. assert_not_rebuilt ${BOOTLOADER_BINS}
  147. mv esp32.common.ld.in ${IDF_PATH}/components/esp32/ld/
  148. print_status "Updating fragment file should only re-link app" # only app linker script is generated by tool for now
  149. take_build_snapshot
  150. cp ${IDF_PATH}/components/esp32/ld/esp32_fragments.lf .
  151. sleep 1 # ninja may ignore if the timestamp delta is too low
  152. echo "# (Build test comment)" >> ${IDF_PATH}/components/esp32/ld/esp32_fragments.lf
  153. idf.py build || failure "Failed to rebuild with modified linker fragment file"
  154. assert_rebuilt ${APP_BINS}
  155. assert_not_rebuilt ${BOOTLOADER_BINS}
  156. mv esp32_fragments.lf ${IDF_PATH}/components/esp32/ld/
  157. print_status "sdkconfig update triggers full recompile"
  158. clean_build_dir
  159. idf.py build
  160. take_build_snapshot
  161. # need to actually change config, or cmake is too smart to rebuild
  162. sed -i.bak s/^\#\ CONFIG_FREERTOS_UNICORE\ is\ not\ set/CONFIG_FREERTOS_UNICORE=y/ sdkconfig
  163. idf.py build
  164. # check the sdkconfig.h file was rebuilt
  165. assert_rebuilt config/sdkconfig.h
  166. # pick one each of .c, .cpp, .S that #includes sdkconfig.h
  167. # and therefore should rebuild
  168. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/syscall_table.c.obj
  169. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  170. assert_rebuilt esp-idf/freertos/CMakeFiles/${IDF_COMPONENT_PREFIX}_freertos.dir/xtensa_vectors.S.obj
  171. mv sdkconfig.bak sdkconfig
  172. print_status "Updating project CMakeLists.txt triggers full recompile"
  173. clean_build_dir
  174. idf.py build
  175. take_build_snapshot
  176. # Need to actually change the build config, or CMake won't do anything
  177. cp CMakeLists.txt CMakeLists.bak
  178. sed -i.bak 's/^project(/add_compile_options("-DUSELESS_MACRO_DOES_NOTHING=1")\nproject\(/' CMakeLists.txt
  179. idf.py build || failure "Build failed"
  180. mv CMakeLists.bak CMakeLists.txt
  181. # similar to previous test
  182. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/syscall_table.c.obj
  183. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  184. assert_rebuilt esp-idf/freertos/CMakeFiles/${IDF_COMPONENT_PREFIX}_freertos.dir/xtensa_vectors.S.obj
  185. mv sdkconfig.bak sdkconfig
  186. print_status "Can build with Ninja (no idf.py)"
  187. clean_build_dir
  188. (cd build && cmake -G Ninja .. && ninja) || failure "Ninja build failed"
  189. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  190. print_status "Can build with GNU Make (no idf.py)"
  191. clean_build_dir
  192. mkdir build
  193. (cd build && cmake -G "Unix Makefiles" .. && make) || failure "Make build failed"
  194. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  195. print_status "Can build with IDF_PATH set via cmake cache not environment"
  196. clean_build_dir
  197. sed -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  198. export IDF_PATH_BACKUP="$IDF_PATH"
  199. (unset IDF_PATH &&
  200. cd build &&
  201. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  202. ninja) || failure "Ninja build failed"
  203. mv CMakeLists.txt.bak CMakeLists.txt
  204. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  205. print_status "Can build with IDF_PATH unset and inferred by build system"
  206. clean_build_dir
  207. sed -i.bak "s%\$ENV{IDF_PATH}%\${ci_idf_path}%" CMakeLists.txt # expand to a hardcoded path
  208. (ci_idf_path=${IDF_PATH} && unset IDF_PATH && cd build &&
  209. cmake -G Ninja -D ci_idf_path=${ci_idf_path} .. && ninja) || failure "Ninja build failed"
  210. mv CMakeLists.txt.bak CMakeLists.txt
  211. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  212. # Next two tests will use this fake 'esp31b' target
  213. export fake_target=esp31b
  214. mkdir -p components/$fake_target
  215. touch components/$fake_target/CMakeLists.txt
  216. cp ${IDF_PATH}/tools/cmake/toolchain-esp32.cmake components/$fake_target/toolchain-$fake_target.cmake
  217. sed -i.bak '/cmake_minimum_required/ a\
  218. set(COMPONENTS esptool_py)' CMakeLists.txt
  219. print_status "Can override IDF_TARGET from environment"
  220. clean_build_dir
  221. rm sdkconfig
  222. export IDF_TARGET=$fake_target
  223. (cd build && cmake -G Ninja .. ) || failure "Failed to configure with IDF_TARGET set in environment"
  224. grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured for IDF_TARGET correctly"
  225. grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt"
  226. unset IDF_TARGET
  227. print_status "Can set target using idf.py -D"
  228. clean_build_dir
  229. rm sdkconfig
  230. idf.py -DIDF_TARGET=$fake_target reconfigure || failure "Failed to set target via idf.py"
  231. grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured correctly using idf.py -D"
  232. grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py -D"
  233. # Clean up modifications for the fake target
  234. mv CMakeLists.txt.bak CMakeLists.txt
  235. rm -rf components
  236. print_status "Can find toolchain file in component directory"
  237. clean_build_dir
  238. mv ${IDF_PATH}/tools/cmake/toolchain-esp32.cmake ${IDF_PATH}/components/esp32/
  239. idf.py build || failure "Failed to build with toolchain file in component directory"
  240. mv ${IDF_PATH}/components/esp32/toolchain-esp32.cmake ${IDF_PATH}/tools/cmake/
  241. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  242. print_status "Can build with auto generated CMakeLists.txt"
  243. clean_build_dir
  244. mv CMakeLists.txt CMakeLists.bak
  245. ${IDF_PATH}/tools/cmake/convert_to_cmake.py .
  246. idf.py build || failure "Auto generated CMakeLists.txt build failed"
  247. mv CMakeLists.bak CMakeLists.txt
  248. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  249. print_status "Setting EXTRA_COMPONENT_DIRS works"
  250. clean_build_dir
  251. mkdir -p main/main/main # move main component contents to another directory
  252. mv main/* main/main/main
  253. cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  254. sed -i "s%cmake_minimum_required(VERSION \([0-9]\+\).\([0-9]\+\))%cmake_minimum_required(VERSION \1.\2)\nset(EXTRA_COMPONENT_DIRS main/main/main)%" CMakeLists.txt
  255. idf.py build || failure "Build with EXTRA_COMPONENT_DIRS set failed"
  256. mv CMakeLists.bak CMakeLists.txt # revert previous modifications
  257. mv main/main/main/* main
  258. rm -rf main/main
  259. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  260. print_status "All tests completed"
  261. if [ -n "${FAILURES}" ]; then
  262. echo "Some failures were detected:"
  263. echo -e "${FAILURES}"
  264. exit 1
  265. else
  266. echo "Build tests passed."
  267. fi
  268. }
  269. function print_status()
  270. {
  271. echo "******** $1"
  272. STATUS="$1"
  273. }
  274. function failure()
  275. {
  276. echo "!!!!!!!!!!!!!!!!!!!"
  277. echo "FAILURE: $1"
  278. echo "!!!!!!!!!!!!!!!!!!!"
  279. FAILURES="${FAILURES}${STATUS} :: $1\n"
  280. }
  281. TESTDIR=${PWD}/build_system_tests_$$
  282. mkdir -p ${TESTDIR}
  283. # set NOCLEANUP=1 if you want to keep the test directory around
  284. # for post-mortem debugging
  285. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  286. SNAPSHOT=${TESTDIR}/snapshot
  287. BUILD=${TESTDIR}/template/build
  288. # copy all the build output to a snapshot directory
  289. function take_build_snapshot()
  290. {
  291. rm -rf ${SNAPSHOT}
  292. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  293. }
  294. # verify that all the arguments are present in the build output directory
  295. function assert_built()
  296. {
  297. until [ -z "$1" ]; do
  298. if [ ! -f "${BUILD}/$1" ]; then
  299. failure "File $1 should be in the build output directory"
  300. fi
  301. shift
  302. done
  303. }
  304. # Test if a file has been rebuilt.
  305. function file_was_rebuilt()
  306. {
  307. # can't use [ a -ot b ] here as -ot only gives second resolution
  308. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  309. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  310. return 0
  311. else
  312. return 1
  313. fi
  314. }
  315. # verify all the arguments passed in were rebuilt relative to the snapshot
  316. function assert_rebuilt()
  317. {
  318. until [ -z "$1" ]; do
  319. assert_built "$1"
  320. if [ ! -f "${SNAPSHOT}/$1" ]; then
  321. failure "File $1 should be in original build snapshot"
  322. fi
  323. if ! file_was_rebuilt "$1"; then
  324. failure "File $1 should have been rebuilt"
  325. fi
  326. shift
  327. done
  328. }
  329. # verify all the arguments are in the build directory & snapshot,
  330. # but were not rebuilt
  331. function assert_not_rebuilt()
  332. {
  333. until [ -z "$1" ]; do
  334. assert_built "$1"
  335. if [ ! -f "${SNAPSHOT}/$1" ]; then
  336. failure "File $1 should be in snapshot build directory"
  337. fi
  338. if file_was_rebuilt "$1"; then
  339. failure "File $1 should not have been rebuilt"
  340. fi
  341. shift
  342. done
  343. }
  344. # do a "clean" that doesn't depend on idf.py
  345. function clean_build_dir()
  346. {
  347. rm -rf --preserve-root ${BUILD}/* ${BUILD}/.*
  348. }
  349. cd ${TESTDIR}
  350. run_tests