test_build_system_cmake.sh 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. PHY_INIT_BIN="phy_init_data.bin"
  55. BUILD_ARTIFACTS="project_description.json flasher_args.json config/kconfig_menus.json config/sdkconfig.json"
  56. IDF_COMPONENT_PREFIX="__idf"
  57. print_status "Initial clean build"
  58. # if build fails here, everything fails
  59. idf.py build || exit $?
  60. # check all the expected build artifacts from the clean build
  61. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} ${BUILD_ARTIFACTS}
  62. print_status "Updating component source file rebuilds component"
  63. # touch a file & do a build
  64. take_build_snapshot
  65. touch ${IDF_PATH}/components/esp32/cpu_start.c
  66. idf.py build || failure "Failed to partial build"
  67. assert_rebuilt ${APP_BINS} esp-idf/esp32/libesp32.a esp-idf/esp32/CMakeFiles/${IDF_COMPONENT_PREFIX}_esp32.dir/cpu_start.c.obj
  68. assert_not_rebuilt esp-idf/lwip/liblwip.a esp-idf/freertos/libfreertos.a ${BOOTLOADER_BINS} ${PARTITION_BIN}
  69. print_status "Bootloader source file rebuilds bootloader"
  70. take_build_snapshot
  71. touch ${IDF_PATH}/components/bootloader/subproject/main/bootloader_start.c
  72. idf.py build || failure "Failed to partial build bootloader"
  73. assert_rebuilt ${BOOTLOADER_BINS} bootloader/esp-idf/main/CMakeFiles/${IDF_COMPONENT_PREFIX}_main.dir/bootloader_start.c.obj
  74. assert_not_rebuilt ${APP_BINS} ${PARTITION_BIN}
  75. print_status "Partition CSV file rebuilds partitions"
  76. take_build_snapshot
  77. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  78. idf.py build || failure "Failed to build partition table"
  79. assert_rebuilt ${PARTITION_BIN}
  80. assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
  81. print_status "Partial build doesn't compile anything by default"
  82. take_build_snapshot
  83. # verify no build files are refreshed by a partial make
  84. ALL_BUILD_FILES=$(find ${BUILD} -type f | ${SED} "s@${BUILD}/@@" | grep -v '^.')
  85. idf.py build || failure "Partial build failed"
  86. assert_not_rebuilt ${ALL_BUILD_FILES}
  87. print_status "Rebuild when app version was changed"
  88. clean_build_dir
  89. # App version
  90. echo "IDF_VER_0123456789_0123456789_0123456789" > ${IDF_PATH}/version.txt
  91. echo "project-version-1.0" > ${TESTDIR}/template/version.txt
  92. idf.py build || failure "Failed to build with app version"
  93. print_status "Change app version"
  94. take_build_snapshot
  95. echo "project-version-2.0(012345678901234567890123456789)" > ${TESTDIR}/template/version.txt
  96. idf.py build || failure "Failed to rebuild with changed app version"
  97. assert_rebuilt ${APP_BINS}
  98. assert_not_rebuilt ${BOOTLOADER_BINS} esp-idf/esp32/libesp32.a
  99. print_status "Re-building does not change app.bin"
  100. take_build_snapshot
  101. idf.py build
  102. assert_not_rebuilt ${APP_BINS} ${BOOTLOADER_BINS} esp-idf/esp32/libesp32.a
  103. rm -f ${IDF_PATH}/version.txt
  104. rm -f ${TESTDIR}/template/version.txt
  105. print_status "Get the version of app from git describe. Project is not inside IDF and do not have a tag only a hash commit."
  106. idf.py reconfigure >> log.log || failure "Failed to build"
  107. version="App \"app-template\" version: "
  108. version+=$(git describe --always --tags --dirty)
  109. grep "${version}" log.log || failure "Project version should have a hash commit"
  110. print_status "Get the version of app from Kconfig option"
  111. idf.py clean > /dev/null
  112. rm -f sdkconfig.defaults
  113. rm -f sdkconfig
  114. echo "project_version_from_txt" > ${TESTDIR}/template/version.txt
  115. echo "CONFIG_APP_PROJECT_VER_FROM_CONFIG=y" >> sdkconfig.defaults
  116. echo 'CONFIG_APP_PROJECT_VER="project_version_from_Kconfig"' >> sdkconfig.defaults
  117. idf.py build >> log.log || failure "Failed to build"
  118. version="App \"app-template\" version: "
  119. version+="project_version_from_Kconfig"
  120. grep "${version}" log.log || failure "Project version should be from Kconfig"
  121. rm -f sdkconfig.defaults
  122. rm -f sdkconfig
  123. rm -f ${TESTDIR}/template/version.txt
  124. print_status "Moving BUILD_DIR_BASE out of tree"
  125. clean_build_dir
  126. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  127. idf.py -B "${OUTOFTREE_BUILD}" build || failure "Failed to build with out-of-tree build dir"
  128. NEW_BUILD_FILES=$(find ${OUTOFTREE_BUILD} -type f)
  129. if [ -z "${NEW_BUILD_FILES}" ]; then
  130. failure "No files found in new build directory!"
  131. fi
  132. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  133. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  134. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  135. fi
  136. print_status "BUILD_DIR_BASE inside default build directory"
  137. clean_build_dir
  138. idf.py -B "build/subdirectory" build || failure "Failed to build with build dir as subdir"
  139. NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
  140. if [ -z "${NEW_BUILD_FILES}" ]; then
  141. failure "No files found in new build directory!"
  142. fi
  143. print_status "Can still clean build if all text files are CRLFs"
  144. clean_build_dir
  145. find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
  146. # make a copy of esp-idf and CRLFify it
  147. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  148. mkdir -p ${CRLF_ESPIDF}
  149. TESTDIR_REL=$($REALPATH ${TESTDIR} --relative-to ${IDF_PATH})
  150. # Note: trailing slash after ${IDF_PATH} avoids creating esp-idf directory inside ${CRLF_ESPIDF}
  151. rsync -a --exclude ${TESTDIR_REL} ${IDF_PATH}/ ${CRLF_ESPIDF}
  152. # don't CRLFify executable files, as Linux will fail to execute them
  153. find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
  154. IDF_PATH=${CRLF_ESPIDF} idf.py build || failure "Failed to build with CRLFs in source"
  155. # do the same checks we do for the clean build
  156. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  157. print_status "Updating rom ld file should re-link app and bootloader"
  158. clean_build_dir
  159. idf.py build
  160. take_build_snapshot
  161. sleep 1 # ninja may ignore if the timestamp delta is too low
  162. cp ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld .
  163. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld
  164. tail ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld
  165. idf.py build || failure "Failed to rebuild with modified linker script"
  166. assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
  167. mv esp32.rom.ld ${IDF_PATH}/components/esp_rom/esp32/ld/
  168. print_status "Updating app-only ld file should only re-link app"
  169. take_build_snapshot
  170. cp ${IDF_PATH}/components/esp32/ld/esp32.project.ld.in .
  171. sleep 1 # ninja may ignore if the timestamp delta is too low
  172. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp32/ld/esp32.project.ld.in
  173. idf.py build || failure "Failed to rebuild with modified linker script"
  174. assert_rebuilt ${APP_BINS}
  175. assert_not_rebuilt ${BOOTLOADER_BINS}
  176. mv esp32.project.ld.in ${IDF_PATH}/components/esp32/ld/
  177. print_status "Updating fragment file should only re-link app" # only app linker script is generated by tool for now
  178. take_build_snapshot
  179. cp ${IDF_PATH}/components/esp32/ld/esp32_fragments.lf .
  180. sleep 1 # ninja may ignore if the timestamp delta is too low
  181. echo "# (Build test comment)" >> ${IDF_PATH}/components/esp32/ld/esp32_fragments.lf
  182. idf.py build || failure "Failed to rebuild with modified linker fragment file"
  183. assert_rebuilt ${APP_BINS}
  184. assert_not_rebuilt ${BOOTLOADER_BINS}
  185. mv esp32_fragments.lf ${IDF_PATH}/components/esp32/ld/
  186. print_status "sdkconfig update triggers full recompile"
  187. clean_build_dir
  188. idf.py build
  189. take_build_snapshot
  190. # need to actually change config, or cmake is too smart to rebuild
  191. ${SED} -i.bak s/^\#\ CONFIG_FREERTOS_UNICORE\ is\ not\ set/CONFIG_FREERTOS_UNICORE=y/ sdkconfig
  192. idf.py build
  193. # check the sdkconfig.h file was rebuilt
  194. assert_rebuilt config/sdkconfig.h
  195. # pick one each of .c, .cpp, .S that #includes sdkconfig.h
  196. # and therefore should rebuild
  197. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/syscall_table.c.obj
  198. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  199. assert_rebuilt esp-idf/freertos/CMakeFiles/${IDF_COMPONENT_PREFIX}_freertos.dir/xtensa/xtensa_vectors.S.obj
  200. mv sdkconfig.bak sdkconfig
  201. print_status "Updating project CMakeLists.txt triggers full recompile"
  202. clean_build_dir
  203. idf.py build
  204. take_build_snapshot
  205. # Need to actually change the build config, or CMake won't do anything
  206. cp CMakeLists.txt CMakeLists.bak
  207. ${SED} -i.bak 's/^project(/add_compile_options("-DUSELESS_MACRO_DOES_NOTHING=1")\nproject\(/' CMakeLists.txt
  208. idf.py build || failure "Build failed"
  209. mv CMakeLists.bak CMakeLists.txt
  210. # similar to previous test
  211. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/syscall_table.c.obj
  212. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  213. assert_rebuilt esp-idf/freertos/CMakeFiles/${IDF_COMPONENT_PREFIX}_freertos.dir/xtensa/xtensa_vectors.S.obj
  214. mv sdkconfig.bak sdkconfig
  215. print_status "Can build with Ninja (no idf.py)"
  216. clean_build_dir
  217. (cd build && cmake -G Ninja .. && ninja) || failure "Ninja build failed"
  218. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  219. print_status "Can build with GNU Make (no idf.py)"
  220. clean_build_dir
  221. mkdir build
  222. (cd build && cmake -G "Unix Makefiles" .. && make) || failure "Make build failed"
  223. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  224. print_status "idf.py can build with Ninja"
  225. clean_build_dir
  226. idf.py -G Ninja build || failure "idf.py cannot build with Ninja"
  227. grep "CMAKE_GENERATOR:INTERNAL=Ninja" build/CMakeCache.txt || failure "Ninja is not set in CMakeCache.txt"
  228. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  229. print_status "idf.py can build with Unix Makefiles"
  230. clean_build_dir
  231. mkdir build
  232. idf.py -G "Unix Makefiles" build || failure "idf.py cannot build with Unix Makefiles"
  233. grep "CMAKE_GENERATOR:INTERNAL=Unix Makefiles" build/CMakeCache.txt || failure "Unix Makefiles are not set in CMakeCache.txt"
  234. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  235. print_status "Can build with IDF_PATH set via cmake cache not environment"
  236. clean_build_dir
  237. ${SED} -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  238. export IDF_PATH_BACKUP="$IDF_PATH"
  239. (unset IDF_PATH &&
  240. cd build &&
  241. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  242. ninja) || failure "Ninja build failed"
  243. mv CMakeLists.txt.bak CMakeLists.txt
  244. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  245. print_status "Can build with IDF_PATH unset and inferred by build system"
  246. clean_build_dir
  247. ${SED} -i.bak "s%\$ENV{IDF_PATH}%\${ci_idf_path}%" CMakeLists.txt # expand to a hardcoded path
  248. (ci_idf_path=${IDF_PATH} && unset IDF_PATH && cd build &&
  249. cmake -G Ninja -D ci_idf_path=${ci_idf_path} .. && ninja) || failure "Ninja build failed"
  250. mv CMakeLists.txt.bak CMakeLists.txt
  251. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  252. print_status "Can build with IDF_PATH unset and inferred by cmake when Kconfig needs it to be set"
  253. clean_build_dir
  254. ${SED} -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  255. export IDF_PATH_BACKUP="$IDF_PATH"
  256. mv main/Kconfig.projbuild main/Kconfig.projbuild_bak
  257. echo "source \"\$IDF_PATH/examples/wifi/getting_started/station/main/Kconfig.projbuild\"" > main/Kconfig.projbuild
  258. (unset IDF_PATH &&
  259. cd build &&
  260. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  261. ninja) || failure "Ninja build failed"
  262. mv CMakeLists.txt.bak CMakeLists.txt
  263. mv main/Kconfig.projbuild_bak main/Kconfig.projbuild
  264. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  265. print_status "can build with phy_init_data"
  266. idf.py clean > /dev/null
  267. idf.py fullclean > /dev/null
  268. rm -f sdkconfig.defaults
  269. rm -f sdkconfig
  270. echo "CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION=y" >> sdkconfig.defaults
  271. idf.py reconfigure > /dev/null
  272. idf.py build || failure "Failed to build with PHY_INIT_DATA"
  273. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} ${PHY_INIT_BIN}
  274. rm sdkconfig
  275. rm sdkconfig.defaults
  276. print_status "can build with ethernet component disabled"
  277. idf.py clean > /dev/null
  278. idf.py fullclean > /dev/null
  279. rm -f sdkconfig.defaults
  280. rm -f sdkconfig
  281. echo "CONFIG_ETH_USE_SPI_ETHERNET=" >> sdkconfig.defaults
  282. echo "CONFIG_ETH_USE_ESP32_EMAC=" >> sdkconfig.defaults
  283. idf.py reconfigure > /dev/null
  284. idf.py build || failure "Failed to build with ethernet component disabled"
  285. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  286. rm sdkconfig
  287. rm sdkconfig.defaults
  288. # the next tests use the esp32s2 target
  289. export other_target=esp32s2
  290. print_status "Can override IDF_TARGET from environment"
  291. clean_build_dir
  292. rm sdkconfig
  293. export IDF_TARGET=$other_target
  294. (cd build && cmake -G Ninja .. ) || failure "Failed to configure with IDF_TARGET set in environment"
  295. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured for IDF_TARGET correctly"
  296. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt"
  297. unset IDF_TARGET
  298. print_status "Can set target using idf.py -D"
  299. clean_build_dir
  300. rm sdkconfig
  301. idf.py -DIDF_TARGET=$other_target reconfigure || failure "Failed to set target via idf.py"
  302. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py -D"
  303. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py -D"
  304. print_status "Can set target using -D as subcommand parameter for idf.py"
  305. clean_build_dir
  306. rm sdkconfig
  307. idf.py reconfigure -DIDF_TARGET=$other_target || failure "Failed to set target via idf.py subcommand -D parameter"
  308. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py reconfigure -D"
  309. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py reconfigure -D"
  310. print_status "Can set target using idf.py set-target"
  311. clean_build_dir
  312. rm sdkconfig
  313. idf.py set-target ${other_target} || failure "Failed to set target via idf.py set-target"
  314. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target"
  315. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target"
  316. print_status "Can guess target from sdkconfig, if CMakeCache does not exist"
  317. idf.py fullclean || failure "Failed to clean the build directory"
  318. idf.py reconfigure || failure "Failed to reconfigure after fullclean"
  319. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  320. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  321. print_status "Can set the default target using sdkconfig.defaults"
  322. clean_build_dir
  323. rm sdkconfig
  324. echo "CONFIG_IDF_TARGET=\"${other_target}\"" > sdkconfig.defaults
  325. idf.py reconfigure || failure "Failed to reconfigure with default target set in sdkconfig.defaults"
  326. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  327. other_target_caps=$(tr 'a-z' 'A-Z' <<< "${other_target}")
  328. grep "CONFIG_IDF_TARGET_${other_target_caps}=y" sdkconfig || failure "Didn't find CONFIG_IDF_TARGET_${other_target_caps} value"
  329. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  330. rm sdkconfig.defaults
  331. print_status "IDF_TARGET takes precedence over the value of CONFIG_IDF_TARGET in sdkconfig.defaults"
  332. clean_build_dir
  333. rm sdkconfig
  334. echo "CONFIG_IDF_TARGET=\"${other_target}\"" > sdkconfig.defaults
  335. export IDF_TARGET=esp32
  336. idf.py reconfigure || failure "Failed to reconfigure with default target set in sdkconfig.defaults and different IDF_TARGET in the environment"
  337. grep "CONFIG_IDF_TARGET=\"esp32\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  338. grep "CONFIG_IDF_TARGET_ESP32=y" sdkconfig || failure "Didn't find CONFIG_IDF_TARGET_ESP32 value"
  339. grep "IDF_TARGET:STRING=esp32" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  340. rm sdkconfig.defaults
  341. unset IDF_TARGET
  342. print_status "idf.py fails if IDF_TARGET settings don't match in sdkconfig, CMakeCache.txt, and the environment"
  343. clean_build_dir
  344. rm sdkconfig
  345. idf.py set-target ${other_target} || failure "Couldn't set target to ${other_target}"
  346. # Change to a different IDF_TARGET in the environment
  347. export IDF_TARGET=esp32
  348. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET was set to an incompatible value in the environment"
  349. # Now make sdkconfig consistent with the environement (note: not really consistent, just for the purpose of the test)
  350. echo "CONFIG_IDF_TARGET=\"esp32\"" >> sdkconfig
  351. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET in CMakeCache.txt didn't match the environment"
  352. # Now unset IDF_TARGET in the environment, sdkconfig and CMakeCache.txt are still inconsistent
  353. unset IDF_TARGET
  354. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET in CMakeCache.txt didn't match the sdkconfig"
  355. unset other_target # done changing target from the default
  356. clean_build_dir
  357. rm sdkconfig
  358. print_status "Can build with auto generated CMakeLists.txt"
  359. clean_build_dir
  360. mv CMakeLists.txt CMakeLists.bak
  361. ${IDF_PATH}/tools/cmake/convert_to_cmake.py .
  362. idf.py build || failure "Auto generated CMakeLists.txt build failed"
  363. mv CMakeLists.bak CMakeLists.txt
  364. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  365. print_status "Can find toolchain file in component directory"
  366. clean_build_dir
  367. mv ${IDF_PATH}/tools/cmake/toolchain-esp32.cmake ${IDF_PATH}/components/esp32/
  368. (idf.py reconfigure > /dev/null && grep "${IDF_PATH}/components/esp32/toolchain-esp32.cmake" build/CMakeCache.txt) || failure "Failed to find toolchain file in component directory"
  369. mv ${IDF_PATH}/components/esp32/toolchain-esp32.cmake ${IDF_PATH}/tools/cmake/
  370. print_status "Setting EXTRA_COMPONENT_DIRS works"
  371. clean_build_dir
  372. (idf.py reconfigure | grep "$PWD/main") || failure "Failed to verify original `main` directory"
  373. mkdir -p main/main/main # move main component contents to another directory
  374. mv main/* main/main/main
  375. cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  376. ${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
  377. (idf.py reconfigure | grep "$PWD/main/main/main") || failure "Failed to set EXTRA_COMPONENT_DIRS"
  378. mv CMakeLists.bak CMakeLists.txt # revert previous modifications
  379. mv main/main/main/* main
  380. rm -rf main/main
  381. print_status "sdkconfig should have contents of all files: sdkconfig, sdkconfig.defaults, sdkconfig.defaults.IDF_TARGET"
  382. idf.py clean > /dev/null
  383. idf.py fullclean > /dev/null
  384. rm -f sdkconfig.defaults
  385. rm -f sdkconfig
  386. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults
  387. echo "CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y" >> sdkconfig.defaults.esp32
  388. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig
  389. idf.py reconfigure > /dev/null
  390. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults should be into sdkconfig"
  391. grep "CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y" sdkconfig || failure "The define from sdkconfig.defaults.esp32 should be into sdkconfig"
  392. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig should be into sdkconfig"
  393. rm sdkconfig sdkconfig.defaults sdkconfig.defaults.esp32
  394. print_status "Building a project with CMake library imported and PSRAM workaround, all files compile with workaround"
  395. # Test for libraries compiled within ESP-IDF
  396. rm -rf build
  397. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" >> sdkconfig.defaults
  398. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  399. # note: we do 'reconfigure' here, as we just need to run cmake
  400. idf.py -C $IDF_PATH/examples/build_system/cmake/import_lib -B `pwd`/build -D SDKCONFIG_DEFAULTS="`pwd`/sdkconfig.defaults" reconfigure
  401. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  402. (grep '"command"' build/compile_commands.json | grep -v mfix-esp32-psram-cache-issue) && failure "All commands in compile_commands.json should use PSRAM cache workaround"
  403. rm -r sdkconfig.defaults build
  404. # Test for external libraries in custom CMake projects with ESP-IDF components linked
  405. mkdir build && touch build/sdkconfig
  406. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" >> build/sdkconfig
  407. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> build/sdkconfig
  408. # note: we just need to run cmake
  409. (cd build && cmake $IDF_PATH/examples/build_system/cmake/idf_as_lib -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32)
  410. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  411. (grep '"command"' build/compile_commands.json | grep -v mfix-esp32-psram-cache-issue) && failure "All commands in compile_commands.json should use PSRAM cache workaround"
  412. rm -r build
  413. #Test for various strategies
  414. for strat in MEMW NOPS DUPLDST; do
  415. rm -r build sdkconfig.defaults sdkconfig sdkconfig.defaults.esp32
  416. stratlc=`echo $strat | tr A-Z a-z`
  417. mkdir build && touch build/sdkconfig
  418. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" > sdkconfig.defaults
  419. echo "CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_$strat=y" >> sdkconfig.defaults
  420. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  421. # note: we do 'reconfigure' here, as we just need to run cmake
  422. idf.py reconfigure
  423. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  424. (grep '"command"' build/compile_commands.json | grep -v mfix-esp32-psram-cache-strategy=$stratlc) && failure "All commands in compile_commands.json should use PSRAM cache workaround strategy $strat when selected"
  425. echo ${PWD}
  426. rm -r sdkconfig.defaults build
  427. done
  428. print_status "Displays partition table when executing target partition_table"
  429. idf.py partition_table | grep -E "# ESP-IDF .+ Partition Table"
  430. rm -r build
  431. print_status "Make sure a full build never runs '/usr/bin/env python' or similar"
  432. OLDPATH="$PATH"
  433. PYTHON="$(which python)"
  434. rm -rf build
  435. cat > ./python << EOF
  436. #!/bin/sh
  437. echo "The build system has executed '/usr/bin/env python' or similar"
  438. exit 1
  439. EOF
  440. chmod +x ./python
  441. export PATH="$(pwd):$PATH"
  442. ${PYTHON} $IDF_PATH/tools/idf.py build || failure "build failed"
  443. export PATH="$OLDPATH"
  444. rm ./python
  445. print_status "Handling deprecated Kconfig options"
  446. idf.py clean > /dev/null
  447. rm -f sdkconfig.defaults
  448. rm -f sdkconfig
  449. echo "" > ${IDF_PATH}/sdkconfig.rename
  450. idf.py reconfigure > /dev/null
  451. echo "CONFIG_TEST_OLD_OPTION=y" >> sdkconfig
  452. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" >> ${IDF_PATH}/sdkconfig.rename
  453. echo -e "\n\
  454. menu \"test\"\n\
  455. config TEST_NEW_OPTION\n\
  456. bool \"test\"\n\
  457. default \"n\"\n\
  458. help\n\
  459. TEST_NEW_OPTION description\n\
  460. endmenu\n" >> ${IDF_PATH}/Kconfig
  461. idf.py reconfigure > /dev/null
  462. grep "CONFIG_TEST_OLD_OPTION=y" sdkconfig || failure "CONFIG_TEST_OLD_OPTION should be in sdkconfig for backward compatibility"
  463. grep "CONFIG_TEST_NEW_OPTION=y" sdkconfig || failure "CONFIG_TEST_NEW_OPTION should be now in sdkconfig"
  464. grep "#define CONFIG_TEST_NEW_OPTION 1" build/config/sdkconfig.h || failure "sdkconfig.h should contain the new macro"
  465. grep "#define CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" build/config/sdkconfig.h || failure "sdkconfig.h should contain the compatibility macro"
  466. grep "set(CONFIG_TEST_OLD_OPTION \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_OLD_OPTION should be in auto.conf for backward compatibility"
  467. grep "set(CONFIG_TEST_NEW_OPTION \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_NEW_OPTION should be now in auto.conf"
  468. rm -f sdkconfig sdkconfig.defaults
  469. pushd ${IDF_PATH}
  470. git checkout -- sdkconfig.rename Kconfig
  471. popd
  472. print_status "Handling deprecated Kconfig options in sdkconfig.defaults"
  473. idf.py clean
  474. rm -f sdkconfig
  475. echo "CONFIG_TEST_OLD_OPTION=7" > sdkconfig.defaults
  476. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" > ${IDF_PATH}/sdkconfig.rename
  477. echo -e "\n\
  478. menu \"test\"\n\
  479. config TEST_NEW_OPTION\n\
  480. int \"TEST_NEW_OPTION\"\n\
  481. range 0 10\n\
  482. default 5\n\
  483. help\n\
  484. TEST_NEW_OPTION description\n\
  485. endmenu\n" >> ${IDF_PATH}/Kconfig
  486. idf.py reconfigure > /dev/null
  487. grep "CONFIG_TEST_OLD_OPTION=7" sdkconfig || failure "CONFIG_TEST_OLD_OPTION=7 should be in sdkconfig for backward compatibility"
  488. grep "CONFIG_TEST_NEW_OPTION=7" sdkconfig || failure "CONFIG_TEST_NEW_OPTION=7 should be in sdkconfig"
  489. rm -f sdkconfig.defaults
  490. pushd ${IDF_PATH}
  491. git checkout -- sdkconfig.rename Kconfig
  492. popd
  493. print_status "Confserver can be invoked by idf.py"
  494. echo '{"version": 1}' | idf.py confserver || failure "Couldn't load confserver"
  495. print_status "Check ccache is used to build"
  496. touch ccache && chmod +x ccache # make sure that ccache is present for this test
  497. (export PATH=$PWD:$PATH && idf.py --ccache reconfigure | grep "ccache will be used") || failure "ccache should be used when --cache is specified"
  498. idf.py fullclean
  499. (export PATH=$PWD:$PATH && idf.py reconfigure| grep -c "ccache will be used" | grep -wq 0) \
  500. || failure "ccache should not be used even when present if --ccache is not specified"
  501. (export PATH=$PWD:$PATH && idf.py --no-ccache reconfigure| grep -c "ccache will be used" | grep -wq 0) \
  502. || failure "--no-ccache causes no issue for backward compatibility"
  503. rm -f ccache
  504. print_status "Custom bootloader overrides original"
  505. clean_build_dir
  506. (mkdir components && cd components && cp -r $IDF_PATH/components/bootloader .)
  507. idf.py bootloader
  508. grep "$PWD/components/bootloader/subproject/main/bootloader_start.c" build/bootloader/compile_commands.json \
  509. || failure "Custom bootloader source files should be built instead of the original's"
  510. rm -rf components
  511. print_status "Empty directory not treated as a component"
  512. clean_build_dir
  513. mkdir -p components/esp32 && idf.py reconfigure
  514. ! grep "$PWD/components/esp32" $PWD/build/project_description.json || failure "Failed to build with empty esp32 directory in components"
  515. rm -rf components
  516. print_status "If a component directory is added to COMPONENT_DIRS, its subdirectories are not added"
  517. clean_build_dir
  518. mkdir -p main/test
  519. echo "idf_component_register()" > main/test/CMakeLists.txt
  520. idf.py reconfigure
  521. ! grep "$PWD/main/test" $PWD/build/project_description.json || failure "COMPONENT_DIRS has added component subdirectory to the build"
  522. grep "$PWD/main" $PWD/build/project_description.json || failure "COMPONENT_DIRS parent component directory should be included in the build"
  523. rm -rf main/test
  524. print_status "If a component directory is added to COMPONENT_DIRS, its sibling directories are not added"
  525. clean_build_dir
  526. mkdir -p mycomponents/mycomponent
  527. echo "idf_component_register()" > mycomponents/mycomponent/CMakeLists.txt
  528. # first test by adding single component directory to EXTRA_COMPONENT_DIRS
  529. mkdir -p mycomponents/esp32
  530. echo "idf_component_register()" > mycomponents/esp32/CMakeLists.txt
  531. idf.py -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents/mycomponent reconfigure
  532. ! grep "$PWD/mycomponents/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  533. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  534. rm -rf mycomponents/esp32
  535. # now the same thing, but add a components directory
  536. mkdir -p esp32
  537. echo "idf_component_register()" > esp32/CMakeLists.txt
  538. idf.py -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents reconfigure
  539. ! grep "$PWD/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  540. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  541. rm -rf esp32
  542. rm -rf mycomponents
  543. # idf.py subcommand options, (using monitor with as example)
  544. print_status "Can set options to subcommands: print_filter for monitor"
  545. mv ${IDF_PATH}/tools/idf_monitor.py ${IDF_PATH}/tools/idf_monitor.py.tmp
  546. echo "import sys;print(sys.argv[1:])" > ${IDF_PATH}/tools/idf_monitor.py
  547. idf.py build || "Failed to build project"
  548. idf.py monitor --print-filter="*:I" -p tty.fake | grep "'--print_filter', '\*:I'" || failure "It should process options for subcommands (and pass print-filter to idf_monitor.py)"
  549. mv ${IDF_PATH}/tools/idf_monitor.py.tmp ${IDF_PATH}/tools/idf_monitor.py
  550. print_status "Fail on build time works"
  551. clean_build_dir
  552. cp CMakeLists.txt CMakeLists.txt.bak
  553. printf "\nif(NOT EXISTS \"\${CMAKE_CURRENT_LIST_DIR}/hello.txt\") \nfail_at_build_time(test_file \"hello.txt does not exists\") \nendif()" >> CMakeLists.txt
  554. ! idf.py build || failure "Build should fail if requirements are not satisfied"
  555. touch hello.txt
  556. idf.py build || failure "Build succeeds once requirements are satisfied"
  557. rm -rf hello.txt CMakeLists.txt
  558. mv CMakeLists.txt.bak CMakeLists.txt
  559. rm -rf CMakeLists.txt.bak
  560. print_status "Component properties are set"
  561. clean_build_dir
  562. cp CMakeLists.txt CMakeLists.txt.bak
  563. printf "\nidf_component_get_property(srcs main SRCS)\nmessage(STATUS SRCS:\${srcs})" >> CMakeLists.txt
  564. (idf.py reconfigure | grep "SRCS:$(${REALPATH} main/main.c)") || failure "Component properties should be set"
  565. rm -rf CMakeLists.txt
  566. mv CMakeLists.txt.bak CMakeLists.txt
  567. rm -rf CMakeLists.txt.bak
  568. print_status "should be able to specify multiple sdkconfig default files"
  569. idf.py clean > /dev/null
  570. idf.py fullclean > /dev/null
  571. rm -f sdkconfig.defaults
  572. rm -f sdkconfig
  573. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults1
  574. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig.defaults2
  575. idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults1;sdkconfig.defaults2" reconfigure > /dev/null
  576. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults1 should be in sdkconfig"
  577. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig.defaults2 should be in sdkconfig"
  578. rm sdkconfig.defaults1 sdkconfig.defaults2 sdkconfig
  579. git checkout sdkconfig.defaults
  580. print_status "Supports git worktree"
  581. clean_build_dir
  582. git branch test_build_system
  583. git worktree add ../esp-idf-template-test test_build_system
  584. diff <(idf.py reconfigure | grep "App \"app-template\" version: ") <(cd ../esp-idf-template-test && idf.py reconfigure | grep "App \"app-template\" version: ") \
  585. || failure "Version on worktree should have been properly resolved"
  586. git worktree remove ../esp-idf-template-test
  587. print_status "idf.py fallback to build system target"
  588. clean_build_dir
  589. msg="Custom target is running"
  590. echo "" >> CMakeLists.txt
  591. echo "add_custom_target(custom_target COMMAND \${CMAKE_COMMAND} -E echo \"${msg}\")" >> CMakeLists.txt
  592. idf.py custom_target 1>log.txt || failure "Could not invoke idf.py with custom target"
  593. grep "${msg}" log.txt 1>/dev/null || failure "Custom target did not produce expected output"
  594. git checkout CMakeLists.txt
  595. rm -f log.txt
  596. print_status "Compiles with dependencies delivered by component manager"
  597. clean_build_dir
  598. printf "\n#include \"test_component.h\"\n" >> main/main.c
  599. printf "dependencies:\n test_component:\n path: test_component\n git: ${COMPONENT_MANAGER_TEST_REPO}\n" >> idf_project.yml
  600. ! idf.py build || failure "Build should fail if dependencies are not installed"
  601. pip install ${COMPONENT_MANAGER_REPO} || failure "Failed to install the component manager"
  602. idf.py reconfigure build || failure "Build didn't succeed with required components installed by package manager"
  603. pip uninstall -y idf_component_manager
  604. rm idf_project.yml
  605. git checkout main/main.c
  606. print_status "Build fails if partitions don't fit in flash"
  607. sed -i.bak "s/CONFIG_ESPTOOLPY_FLASHSIZE.\+//" sdkconfig # remove all flashsize config
  608. echo "CONFIG_ESPTOOLPY_FLASHSIZE_1MB=y" >> sdkconfig # introduce undersize flash
  609. ( idf.py build 2>&1 | grep "does not fit in configured flash size 1MB" ) || failure "Build didn't fail with expected flash size failure message"
  610. mv sdkconfig.bak sdkconfig
  611. print_status "Flash size is correctly set in the bootloader image header"
  612. # Build with the default 2MB setting
  613. rm sdkconfig
  614. idf.py bootloader || failure "Failed to build bootloader"
  615. bin_header_match build/bootloader/bootloader.bin "0210"
  616. # Change to 4MB
  617. sleep 1 # delay here to make sure sdkconfig modification time is different
  618. echo "CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y" > sdkconfig
  619. idf.py bootloader || failure "Failed to build bootloader"
  620. bin_header_match build/bootloader/bootloader.bin "0220"
  621. # Change to QIO, bootloader should still be DIO (will change to QIO in 2nd stage bootloader)
  622. sleep 1 # delay here to make sure sdkconfig modification time is different
  623. echo "CONFIG_FLASHMODE_QIO=y" > sdkconfig
  624. idf.py bootloader || failure "Failed to build bootloader"
  625. bin_header_match build/bootloader/bootloader.bin "0210"
  626. # Change to 80 MHz
  627. sleep 1 # delay here to make sure sdkconfig modification time is different
  628. echo "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" > sdkconfig
  629. idf.py bootloader || failure "Failed to build bootloader"
  630. bin_header_match build/bootloader/bootloader.bin "021f"
  631. rm sdkconfig
  632. print_status "All tests completed"
  633. if [ -n "${FAILURES}" ]; then
  634. echo "Some failures were detected:"
  635. echo -e "${FAILURES}"
  636. exit 1
  637. else
  638. echo "Build tests passed."
  639. fi
  640. }
  641. function print_status()
  642. {
  643. echo "******** $1"
  644. STATUS="$1"
  645. }
  646. function failure()
  647. {
  648. echo "!!!!!!!!!!!!!!!!!!!"
  649. echo "FAILURE: $1"
  650. echo "!!!!!!!!!!!!!!!!!!!"
  651. FAILURES="${FAILURES}${STATUS} :: $1\n"
  652. }
  653. TESTDIR=${PWD}/build_system_tests_$$
  654. mkdir -p ${TESTDIR}
  655. # set NOCLEANUP=1 if you want to keep the test directory around
  656. # for post-mortem debugging
  657. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  658. SNAPSHOT=${TESTDIR}/snapshot
  659. BUILD=${TESTDIR}/template/build
  660. IS_DARWIN=
  661. export SED=sed
  662. export REALPATH=realpath
  663. if [ "$(uname -s)" = "Darwin" ]; then
  664. IS_DARWIN=1
  665. export SED=gsed
  666. export REALPATH=grealpath
  667. fi
  668. # copy all the build output to a snapshot directory
  669. function take_build_snapshot()
  670. {
  671. rm -rf ${SNAPSHOT}
  672. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  673. if [ -n "$IS_DARWIN" ]; then
  674. # wait at least 1 second before the next build, for the test in
  675. # file_was_rebuilt to work
  676. sleep 1
  677. fi
  678. }
  679. # verify that all the arguments are present in the build output directory
  680. function assert_built()
  681. {
  682. until [ -z "$1" ]; do
  683. if [ ! -f "${BUILD}/$1" ]; then
  684. failure "File $1 should be in the build output directory"
  685. fi
  686. shift
  687. done
  688. }
  689. # Test if a file has been rebuilt.
  690. function file_was_rebuilt()
  691. {
  692. if [ -z "$IS_DARWIN" ]; then
  693. # can't use [ a -ot b ] here as -ot only gives second resolution
  694. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  695. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  696. return 0
  697. else
  698. return 1
  699. fi
  700. else
  701. # macOS: work around 1-second resolution by adding a sleep in take_build_snapshot
  702. if [ ${SNAPSHOT}/$1 -ot ${BUILD}/$1 ]; then
  703. return 0
  704. else
  705. return 1
  706. fi
  707. fi
  708. }
  709. # verify all the arguments passed in were rebuilt relative to the snapshot
  710. function assert_rebuilt()
  711. {
  712. until [ -z "$1" ]; do
  713. assert_built "$1"
  714. if [ ! -f "${SNAPSHOT}/$1" ]; then
  715. failure "File $1 should be in original build snapshot"
  716. fi
  717. if ! file_was_rebuilt "$1"; then
  718. failure "File $1 should have been rebuilt"
  719. fi
  720. shift
  721. done
  722. }
  723. # verify all the arguments are in the build directory & snapshot,
  724. # but were not rebuilt
  725. function assert_not_rebuilt()
  726. {
  727. until [ -z "$1" ]; do
  728. assert_built "$1"
  729. if [ ! -f "${SNAPSHOT}/$1" ]; then
  730. failure "File $1 should be in snapshot build directory"
  731. fi
  732. if file_was_rebuilt "$1"; then
  733. failure "File $1 should not have been rebuilt"
  734. fi
  735. shift
  736. done
  737. }
  738. # do a "clean" that doesn't depend on idf.py
  739. function clean_build_dir()
  740. {
  741. PRESERVE_ROOT_ARG=
  742. if [ -z "$IS_DARWIN" ]; then
  743. PRESERVE_ROOT_ARG=--preserve-root
  744. fi
  745. rm -rf $PRESERVE_ROOT_ARG ${BUILD}/* ${BUILD}/.*
  746. }
  747. # check the bytes 3-4 of the binary image header. e.g.:
  748. # bin_header_match app.bin 0210
  749. function bin_header_match()
  750. {
  751. expected=$2
  752. filename=$1
  753. actual=$(xxd -s 2 -l 2 -ps $1)
  754. if [ ! "$expected" = "$actual" ]; then
  755. failure "Incorrect binary image header, expected $expected got $actual"
  756. fi
  757. }
  758. cd ${TESTDIR}
  759. run_tests