test_build_system_cmake.sh 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. # Some tests assume that ccache is not enabled
  35. unset IDF_CCACHE_ENABLE
  36. function get_file_size() {
  37. if [[ $OSTYPE == 'darwin'* ]]; then
  38. BINSIZE=$(stat -f "%z" ${1})
  39. else
  40. BINSIZE=$(stat -c "%s" ${1})
  41. fi
  42. }
  43. function run_tests()
  44. {
  45. FAILURES=
  46. STATUS="Starting"
  47. print_status "Checking prerequisites"
  48. [ -z ${IDF_PATH} ] && echo "IDF_PATH is not set. Need path to esp-idf installation." && exit 2
  49. print_status "Cloning template from ${ESP_IDF_TEMPLATE_GIT}..."
  50. git clone ${ESP_IDF_TEMPLATE_GIT} template
  51. cd template
  52. if [ -z $CHECKOUT_REF_SCRIPT ]; then
  53. git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
  54. else
  55. $CHECKOUT_REF_SCRIPT esp-idf-template .
  56. fi
  57. print_status "Try to clean fresh directory..."
  58. idf.py fullclean || exit $?
  59. # all relative to the build directory
  60. BOOTLOADER_BINS="bootloader/bootloader.elf bootloader/bootloader.bin"
  61. APP_BINS="app-template.elf app-template.bin"
  62. PARTITION_BIN="partition_table/partition-table.bin"
  63. PHY_INIT_BIN="phy_init_data.bin"
  64. BUILD_ARTIFACTS="project_description.json flasher_args.json config/kconfig_menus.json config/sdkconfig.json"
  65. IDF_COMPONENT_PREFIX="__idf"
  66. print_status "Initial clean build"
  67. # if build fails here, everything fails
  68. idf.py build || exit $?
  69. # check all the expected build artifacts from the clean build
  70. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} ${BUILD_ARTIFACTS}
  71. print_status "Updating component source file rebuilds component"
  72. # touch a file & do a build
  73. take_build_snapshot
  74. touch ${IDF_PATH}/components/esp_system/port/cpu_start.c
  75. idf.py build || failure "Failed to partial build"
  76. assert_rebuilt ${APP_BINS} esp-idf/esp_system/libesp_system.a esp-idf/esp_system/CMakeFiles/${IDF_COMPONENT_PREFIX}_esp_system.dir/port/cpu_start.c.obj
  77. assert_not_rebuilt esp-idf/lwip/liblwip.a esp-idf/freertos/libfreertos.a ${BOOTLOADER_BINS} ${PARTITION_BIN}
  78. print_status "Bootloader source file rebuilds bootloader"
  79. take_build_snapshot
  80. touch ${IDF_PATH}/components/bootloader/subproject/main/bootloader_start.c
  81. idf.py build || failure "Failed to partial build bootloader"
  82. assert_rebuilt ${BOOTLOADER_BINS} bootloader/esp-idf/main/CMakeFiles/${IDF_COMPONENT_PREFIX}_main.dir/bootloader_start.c.obj
  83. assert_not_rebuilt ${APP_BINS} ${PARTITION_BIN}
  84. print_status "Partition CSV file rebuilds partitions"
  85. take_build_snapshot
  86. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  87. idf.py build || failure "Failed to build partition table"
  88. assert_rebuilt ${PARTITION_BIN}
  89. assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
  90. print_status "Partial build doesn't compile anything by default"
  91. take_build_snapshot
  92. # verify no build files are refreshed by a partial make
  93. ALL_BUILD_FILES=$(find ${BUILD} -type f | ${SED} "s@${BUILD}/@@" | grep -v '^.')
  94. idf.py build || failure "Partial build failed"
  95. assert_not_rebuilt ${ALL_BUILD_FILES}
  96. print_status "Rebuild when app version was changed"
  97. clean_build_dir
  98. # App version
  99. echo "IDF_VER_0123456789_0123456789_0123456789" > ${IDF_PATH}/version.txt
  100. echo "project-version-1.0" > ${TESTDIR}/template/version.txt
  101. idf.py build || failure "Failed to build with app version"
  102. print_status "Change app version"
  103. take_build_snapshot
  104. echo "project-version-2.0(012345678901234567890123456789)" > ${TESTDIR}/template/version.txt
  105. idf.py build || failure "Failed to rebuild with changed app version"
  106. assert_rebuilt ${APP_BINS}
  107. assert_not_rebuilt ${BOOTLOADER_BINS} esp-idf/esp_system/libesp_system.a
  108. print_status "Re-building does not change app.bin"
  109. take_build_snapshot
  110. idf.py build
  111. assert_not_rebuilt ${APP_BINS} ${BOOTLOADER_BINS} esp-idf/esp_system/libesp_system.a
  112. rm -f ${IDF_PATH}/version.txt
  113. rm -f ${TESTDIR}/template/version.txt
  114. 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."
  115. idf.py reconfigure >> log.log || failure "Failed to build"
  116. version="App \"app-template\" version: "
  117. version+=$(git describe --always --tags --dirty)
  118. grep "${version}" log.log || failure "Project version should have a hash commit"
  119. print_status "Get the version of app from Kconfig option"
  120. idf.py clean > /dev/null
  121. rm -f sdkconfig.defaults
  122. rm -f sdkconfig
  123. echo "project_version_from_txt" > ${TESTDIR}/template/version.txt
  124. echo "CONFIG_APP_PROJECT_VER_FROM_CONFIG=y" >> sdkconfig.defaults
  125. echo 'CONFIG_APP_PROJECT_VER="project_version_from_Kconfig"' >> sdkconfig.defaults
  126. idf.py build >> log.log || failure "Failed to build"
  127. version="App \"app-template\" version: "
  128. version+="project_version_from_Kconfig"
  129. grep "${version}" log.log || failure "Project version should be from Kconfig"
  130. rm -f sdkconfig.defaults
  131. rm -f sdkconfig
  132. rm -f ${TESTDIR}/template/version.txt
  133. print_status "Use IDF version variables in component CMakeLists.txt file"
  134. clean_build_dir
  135. (echo -e "if (NOT IDF_VERSION_MAJOR)\n message(FATAL_ERROR \"IDF version not set\")\n endif()" \
  136. && cat main/CMakeLists.txt) > main/CMakeLists.new && mv main/CMakeLists.new main/CMakeLists.txt
  137. idf.py reconfigure || failure "Failed to use IDF_VERSION_MAJOR in component CMakeLists.txt"
  138. git checkout -- main/CMakeLists.txt
  139. print_status "Project is in ESP-IDF which has a custom tag"
  140. pushd ${IDF_PATH}/examples/get-started/hello_world
  141. GIT_COMMITTER_NAME="No One" GIT_COMMITTER_EMAIL="noone@espressif.com" git tag mytag -a -m "mytag" || failure "Git cannot create tag"
  142. idf.py reconfigure &> log.log || failure "Failed to build"
  143. str="App \"hello_world\" version: mytag"
  144. grep "${str}" log.log || { cat log.log ; failure "Project version should be the custom tag"; }
  145. idf_version=$(idf.py --version)
  146. if [[ "$idf_version" == *"mytag"* ]]; then
  147. failure "IDF version $idf_version should not contain mytag"
  148. fi
  149. git tag -d mytag
  150. rm -rf sdkconfig build
  151. popd
  152. print_status "Moving BUILD_DIR_BASE out of tree"
  153. clean_build_dir
  154. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  155. idf.py -B "${OUTOFTREE_BUILD}" build || failure "Failed to build with out-of-tree build dir"
  156. NEW_BUILD_FILES=$(find ${OUTOFTREE_BUILD} -type f)
  157. if [ -z "${NEW_BUILD_FILES}" ]; then
  158. failure "No files found in new build directory!"
  159. fi
  160. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  161. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  162. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  163. fi
  164. print_status "BUILD_DIR_BASE inside default build directory"
  165. clean_build_dir
  166. idf.py -B "build/subdirectory" build || failure "Failed to build with build dir as subdir"
  167. NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
  168. if [ -z "${NEW_BUILD_FILES}" ]; then
  169. failure "No files found in new build directory!"
  170. fi
  171. print_status "Can still clean build if all text files are CRLFs"
  172. clean_build_dir
  173. find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
  174. # make a copy of esp-idf and CRLFify it
  175. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  176. mkdir -p ${CRLF_ESPIDF}
  177. TESTDIR_REL=$($REALPATH ${TESTDIR} --relative-to ${IDF_PATH})
  178. # Note: trailing slash after ${IDF_PATH} avoids creating esp-idf directory inside ${CRLF_ESPIDF}
  179. rsync -a --exclude ${TESTDIR_REL} ${IDF_PATH}/ ${CRLF_ESPIDF}
  180. # don't CRLFify executable files, as Linux will fail to execute them
  181. find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
  182. IDF_PATH=${CRLF_ESPIDF} idf.py build || failure "Failed to build with CRLFs in source"
  183. # do the same checks we do for the clean build
  184. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  185. print_status "Updating rom ld file should re-link app and bootloader"
  186. clean_build_dir
  187. idf.py build
  188. take_build_snapshot
  189. sleep 1 # ninja may ignore if the timestamp delta is too low
  190. cp ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld .
  191. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld
  192. tail ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld
  193. idf.py build || failure "Failed to rebuild with modified linker script"
  194. assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
  195. mv esp32.rom.ld ${IDF_PATH}/components/esp_rom/esp32/ld/
  196. print_status "Updating app-only ld file should only re-link app"
  197. take_build_snapshot
  198. cp ${IDF_PATH}/components/esp_system/ld/esp32/sections.ld.in .
  199. sleep 1 # ninja may ignore if the timestamp delta is too low
  200. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp_system/ld/esp32/sections.ld.in
  201. idf.py build || failure "Failed to rebuild with modified linker script"
  202. assert_rebuilt ${APP_BINS}
  203. assert_not_rebuilt ${BOOTLOADER_BINS}
  204. mv sections.ld.in ${IDF_PATH}/components/esp_system/ld/esp32
  205. print_status "Updating ld file should only re-link app"
  206. take_build_snapshot
  207. cp ${IDF_PATH}/components/esp_system/ld/esp32/memory.ld .
  208. sleep 1 # ninja may ignore if the timestamp delta is too low
  209. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp_system/ld/esp32/memory.ld.in
  210. idf.py build || failure "Failed to rebuild with modified linker script"
  211. assert_rebuilt ${APP_BINS}
  212. assert_not_rebuilt ${BOOTLOADER_BINS}
  213. mv memory.ld ${IDF_PATH}/components/esp_system/ld/esp32/
  214. print_status "Updating fragment file should only re-link app" # only app linker script is generated by tool for now
  215. take_build_snapshot
  216. cp ${IDF_PATH}/components/esp_common/common.lf .
  217. sleep 1 # ninja may ignore if the timestamp delta is too low
  218. echo "# (Build test comment)" >> ${IDF_PATH}/components/esp_common/common.lf
  219. idf.py build || failure "Failed to rebuild with modified linker fragment file"
  220. assert_rebuilt ${APP_BINS}
  221. assert_not_rebuilt ${BOOTLOADER_BINS}
  222. mv common.lf ${IDF_PATH}/components/esp_common
  223. print_status "sdkconfig update triggers full recompile"
  224. clean_build_dir
  225. idf.py build
  226. take_build_snapshot
  227. # need to actually change config, or cmake is too smart to rebuild
  228. ${SED} -i.bak s/^\#\ CONFIG_FREERTOS_UNICORE\ is\ not\ set/CONFIG_FREERTOS_UNICORE=y/ sdkconfig
  229. idf.py build
  230. # check the sdkconfig.h file was rebuilt
  231. assert_rebuilt config/sdkconfig.h
  232. # pick one each of .c, .cpp, .S that #includes sdkconfig.h
  233. # and therefore should rebuild
  234. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/newlib_init.c.obj
  235. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  236. assert_rebuilt esp-idf/esp_system/CMakeFiles/${IDF_COMPONENT_PREFIX}_esp_system.dir/port/arch/xtensa/panic_handler_asm.S.obj
  237. mv sdkconfig.bak sdkconfig
  238. print_status "Updating project CMakeLists.txt triggers full recompile"
  239. clean_build_dir
  240. idf.py build
  241. take_build_snapshot
  242. # Need to actually change the build config, or CMake won't do anything
  243. cp CMakeLists.txt CMakeLists.bak
  244. ${SED} -i.bak 's/^project(/add_compile_options("-DUSELESS_MACRO_DOES_NOTHING=1")\nproject\(/' CMakeLists.txt
  245. idf.py build || failure "Build failed"
  246. mv CMakeLists.bak CMakeLists.txt
  247. # similar to previous test
  248. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/newlib_init.c.obj
  249. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  250. assert_rebuilt esp-idf/esp_system/CMakeFiles/${IDF_COMPONENT_PREFIX}_esp_system.dir/port/arch/xtensa/panic_handler_asm.S.obj
  251. mv sdkconfig.bak sdkconfig
  252. print_status "Can build with Ninja (no idf.py)"
  253. clean_build_dir
  254. (cd build && cmake -G Ninja .. && ninja) || failure "Ninja build failed"
  255. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  256. print_status "Can build with GNU Make (no idf.py)"
  257. clean_build_dir
  258. mkdir build
  259. (cd build && cmake -G "Unix Makefiles" .. && make) || failure "Make build failed"
  260. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  261. print_status "idf.py can build with Ninja"
  262. clean_build_dir
  263. idf.py -G Ninja build || failure "idf.py cannot build with Ninja"
  264. grep "CMAKE_GENERATOR:INTERNAL=Ninja" build/CMakeCache.txt || failure "Ninja is not set in CMakeCache.txt"
  265. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  266. print_status "idf.py can build with Unix Makefiles"
  267. clean_build_dir
  268. mkdir build
  269. idf.py -G "Unix Makefiles" build || failure "idf.py cannot build with Unix Makefiles"
  270. grep "CMAKE_GENERATOR:INTERNAL=Unix Makefiles" build/CMakeCache.txt || failure "Unix Makefiles are not set in CMakeCache.txt"
  271. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  272. print_status "Can build with IDF_PATH set via cmake cache not environment"
  273. clean_build_dir
  274. ${SED} -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  275. export IDF_PATH_BACKUP="$IDF_PATH"
  276. (unset IDF_PATH &&
  277. cd build &&
  278. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  279. ninja) || failure "Ninja build failed"
  280. mv CMakeLists.txt.bak CMakeLists.txt
  281. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  282. print_status "Can build with IDF_PATH unset and inferred by build system"
  283. clean_build_dir
  284. ${SED} -i.bak "s%\$ENV{IDF_PATH}%\${ci_idf_path}%" CMakeLists.txt # expand to a hardcoded path
  285. (ci_idf_path=${IDF_PATH} && unset IDF_PATH && cd build &&
  286. cmake -G Ninja -D ci_idf_path=${ci_idf_path} .. && ninja) || failure "Ninja build failed"
  287. mv CMakeLists.txt.bak CMakeLists.txt
  288. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  289. print_status "Can build with IDF_PATH unset and inferred by cmake when Kconfig needs it to be set"
  290. clean_build_dir
  291. ${SED} -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  292. export IDF_PATH_BACKUP="$IDF_PATH"
  293. mv main/Kconfig.projbuild main/Kconfig.projbuild_bak
  294. echo "source \"\$IDF_PATH/examples/wifi/getting_started/station/main/Kconfig.projbuild\"" > main/Kconfig.projbuild
  295. (unset IDF_PATH &&
  296. cd build &&
  297. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  298. ninja) || failure "Ninja build failed"
  299. mv CMakeLists.txt.bak CMakeLists.txt
  300. mv main/Kconfig.projbuild_bak main/Kconfig.projbuild
  301. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  302. print_status "can build with phy_init_data"
  303. idf.py clean > /dev/null
  304. idf.py fullclean > /dev/null
  305. rm -f sdkconfig.defaults
  306. rm -f sdkconfig
  307. echo "CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION=y" >> sdkconfig.defaults
  308. idf.py reconfigure > /dev/null
  309. idf.py build || failure "Failed to build with PHY_INIT_DATA"
  310. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} ${PHY_INIT_BIN}
  311. rm sdkconfig
  312. rm sdkconfig.defaults
  313. print_status "can build with ethernet component disabled"
  314. idf.py clean > /dev/null
  315. idf.py fullclean > /dev/null
  316. rm -f sdkconfig.defaults
  317. rm -f sdkconfig
  318. echo "CONFIG_ETH_USE_SPI_ETHERNET=" >> sdkconfig.defaults
  319. echo "CONFIG_ETH_USE_ESP32_EMAC=" >> sdkconfig.defaults
  320. idf.py reconfigure > /dev/null
  321. idf.py build || failure "Failed to build with ethernet component disabled"
  322. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  323. rm sdkconfig
  324. rm sdkconfig.defaults
  325. print_status "Compiler flags on build command line are taken into account"
  326. clean_build_dir
  327. # Backup original source file
  328. cp main/main.c main/main.c.bak
  329. # Alter source file to check user flag
  330. echo -e "\n#ifndef USER_FLAG \n \
  331. #error \"USER_FLAG is not defined!\" \n \
  332. #endif\n" >> main/main.c
  333. idf.py build -DCMAKE_C_FLAGS=-DUSER_FLAG || failure "User flags should have been taken into account"
  334. # Restore original file
  335. mv main/main.c.bak main/main.c
  336. print_status "Compiler flags cannot be overwritten"
  337. clean_build_dir
  338. # If the compiler flags are overriden, the following build command will
  339. # cause issues at link time.
  340. idf.py build -DCMAKE_C_FLAGS= -DCMAKE_CXX_FLAGS= || failure "CMake compiler flags have been overriden"
  341. # the next tests use the esp32s2 target
  342. export other_target=esp32s2
  343. print_status "Can override IDF_TARGET from environment"
  344. clean_build_dir
  345. rm sdkconfig
  346. export IDF_TARGET=$other_target
  347. (cd build && cmake -G Ninja .. ) || failure "Failed to configure with IDF_TARGET set in environment"
  348. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured for IDF_TARGET correctly"
  349. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt"
  350. unset IDF_TARGET
  351. print_status "Can set target using idf.py -D"
  352. clean_build_dir
  353. rm sdkconfig
  354. idf.py -DIDF_TARGET=$other_target reconfigure || failure "Failed to set target via idf.py"
  355. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py -D"
  356. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py -D"
  357. print_status "Can set target using -D as subcommand parameter for idf.py"
  358. clean_build_dir
  359. rm sdkconfig
  360. idf.py reconfigure -DIDF_TARGET=$other_target || failure "Failed to set target via idf.py subcommand -D parameter"
  361. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py reconfigure -D"
  362. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py reconfigure -D"
  363. print_status "Can set target using idf.py set-target"
  364. clean_build_dir
  365. rm sdkconfig
  366. idf.py set-target ${other_target} || failure "Failed to set target via idf.py set-target"
  367. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target"
  368. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target"
  369. print_status "idf.py understands alternative target names"
  370. clean_build_dir
  371. rm sdkconfig
  372. idf.py set-target ESP32-S2
  373. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target"
  374. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target"
  375. print_status "Can guess target from sdkconfig, if CMakeCache does not exist"
  376. idf.py fullclean || failure "Failed to clean the build directory"
  377. idf.py reconfigure || failure "Failed to reconfigure after fullclean"
  378. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  379. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  380. print_status "Can set the default target using sdkconfig.defaults"
  381. clean_build_dir
  382. rm sdkconfig
  383. echo "CONFIG_IDF_TARGET=\"${other_target}\"" > sdkconfig.defaults
  384. idf.py reconfigure || failure "Failed to reconfigure with default target set in sdkconfig.defaults"
  385. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  386. other_target_caps=$(tr 'a-z' 'A-Z' <<< "${other_target}")
  387. grep "CONFIG_IDF_TARGET_${other_target_caps}=y" sdkconfig || failure "Didn't find CONFIG_IDF_TARGET_${other_target_caps} value"
  388. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  389. rm sdkconfig.defaults
  390. print_status "IDF_TARGET takes precedence over the value of CONFIG_IDF_TARGET in sdkconfig.defaults"
  391. clean_build_dir
  392. rm sdkconfig
  393. echo "CONFIG_IDF_TARGET=\"${other_target}\"" > sdkconfig.defaults
  394. export IDF_TARGET=esp32
  395. idf.py reconfigure || failure "Failed to reconfigure with default target set in sdkconfig.defaults and different IDF_TARGET in the environment"
  396. grep "CONFIG_IDF_TARGET=\"esp32\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  397. grep "CONFIG_IDF_TARGET_ESP32=y" sdkconfig || failure "Didn't find CONFIG_IDF_TARGET_ESP32 value"
  398. grep "IDF_TARGET:STRING=esp32" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  399. rm sdkconfig.defaults
  400. unset IDF_TARGET
  401. print_status "idf.py fails if IDF_TARGET settings don't match in sdkconfig, CMakeCache.txt, and the environment"
  402. clean_build_dir
  403. rm sdkconfig
  404. idf.py set-target ${other_target} || failure "Couldn't set target to ${other_target}"
  405. # Change to a different IDF_TARGET in the environment
  406. export IDF_TARGET=esp32
  407. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET was set to an incompatible value in the environment"
  408. # Now make sdkconfig consistent with the environement (note: not really consistent, just for the purpose of the test)
  409. echo "CONFIG_IDF_TARGET=\"esp32\"" >> sdkconfig
  410. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET in CMakeCache.txt didn't match the environment"
  411. # Now unset IDF_TARGET in the environment, sdkconfig and CMakeCache.txt are still inconsistent
  412. unset IDF_TARGET
  413. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET in CMakeCache.txt didn't match the sdkconfig"
  414. unset other_target # done changing target from the default
  415. clean_build_dir
  416. rm sdkconfig
  417. print_status "Setting EXTRA_COMPONENT_DIRS works"
  418. clean_build_dir
  419. (idf.py reconfigure | grep "$PWD/main") || failure "Failed to verify original `main` directory"
  420. mkdir -p main/main/main # move main component contents to another directory
  421. mv main/* main/main/main
  422. cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  423. ${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
  424. (idf.py reconfigure | grep "$PWD/main/main/main") || failure "Failed to set EXTRA_COMPONENT_DIRS"
  425. mv CMakeLists.bak CMakeLists.txt # revert previous modifications
  426. mv main/main/main/* main
  427. rm -rf main/main
  428. print_status "Non-existent paths in EXTRA_COMPONENT_DIRS are not allowed"
  429. clean_build_dir
  430. ! idf.py -DEXTRA_COMPONENT_DIRS="extra_components" reconfigure || failure "Build should fail when non-existent component path is added"
  431. print_status "Component names may contain spaces"
  432. clean_build_dir
  433. mkdir -p "extra component"
  434. echo "idf_component_register" > "extra component/CMakeLists.txt"
  435. idf.py -DEXTRA_COMPONENT_DIRS="extra component;main" || failure "Build should succeed when a component name contains space"
  436. rm -rf "extra component"
  437. print_status "sdkconfig should have contents of all files: sdkconfig, sdkconfig.defaults, sdkconfig.defaults.IDF_TARGET"
  438. idf.py clean > /dev/null
  439. idf.py fullclean > /dev/null
  440. rm -f sdkconfig.defaults
  441. rm -f sdkconfig
  442. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults
  443. echo "CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y" >> sdkconfig.defaults.esp32
  444. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig
  445. idf.py reconfigure > /dev/null
  446. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults should be into sdkconfig"
  447. grep "CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y" sdkconfig || failure "The define from sdkconfig.defaults.esp32 should be into sdkconfig"
  448. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig should be into sdkconfig"
  449. rm sdkconfig sdkconfig.defaults sdkconfig.defaults.esp32
  450. print_status "Test if it can build the example to run on host"
  451. pushd $IDF_PATH/examples/build_system/cmake/idf_as_lib
  452. (set -euo pipefail && source build.sh)
  453. popd
  454. rm -r $IDF_PATH/examples/build_system/cmake/idf_as_lib/build
  455. print_status "Test build ESP-IDF as a library to a custom CMake projects for all targets"
  456. IDF_AS_LIB=$IDF_PATH/examples/build_system/cmake/idf_as_lib
  457. # note: we just need to run cmake
  458. for TARGET in "esp32" "esp32s2" "esp32s3" "esp32c3" "esp32h2" "esp32c2"
  459. do
  460. echo "Build idf_as_lib for $TARGET target"
  461. rm -rf build
  462. mkdir -p build && cd build
  463. cmake $IDF_AS_LIB -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-$TARGET.cmake -DTARGET=$TARGET || failure "Failed to generate idf_as_lib build files for target $TARGET"
  464. cmake --build . || failure "Failed to build idf_as_lib for target $TARGET"
  465. cd ..
  466. done
  467. print_status "Building a project with CMake library imported and PSRAM workaround, all files compile with workaround"
  468. # Test for libraries compiled within ESP-IDF
  469. rm -r build sdkconfig
  470. echo "CONFIG_SPIRAM=y" >> sdkconfig.defaults
  471. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  472. # note: we do 'reconfigure' here, as we just need to run cmake
  473. idf.py -C $IDF_PATH/examples/build_system/cmake/import_lib -B `pwd`/build -D SDKCONFIG_DEFAULTS="`pwd`/sdkconfig.defaults" reconfigure
  474. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  475. (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"
  476. rm -r build sdkconfig sdkconfig.defaults
  477. print_status "Test for external libraries in custom CMake projects with ESP-IDF components linked"
  478. mkdir build
  479. IDF_AS_LIB=$IDF_PATH/examples/build_system/cmake/idf_as_lib
  480. # note: we just need to run cmake
  481. (cd build && cmake $IDF_AS_LIB -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32)
  482. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  483. for strat in MEMW NOPS DUPLDST; do
  484. print_status "Test for external libraries in custom CMake projects with PSRAM strategy $strat"
  485. rm -r build sdkconfig sdkconfig.defaults sdkconfig.defaults.esp32
  486. stratlc=`echo $strat | tr A-Z a-z`
  487. echo "CONFIG_SPIRAM=y" > sdkconfig.defaults
  488. echo "CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_$strat=y" >> sdkconfig.defaults
  489. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  490. # note: we do 'reconfigure' here, as we just need to run cmake
  491. idf.py reconfigure
  492. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  493. (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"
  494. echo ${PWD}
  495. rm -r sdkconfig.defaults build
  496. done
  497. print_status "Cleaning Python bytecode"
  498. idf.py clean > /dev/null
  499. idf.py fullclean > /dev/null
  500. if [ "$(find $IDF_PATH -name "*.py[co]" | wc -l)" -eq 0 ]; then
  501. failure "No Python bytecode in IDF!"
  502. fi
  503. idf.py python-clean
  504. if [ "$(find $IDF_PATH -name "*.py[co]" | wc -l)" -gt 0 ]; then
  505. failure "Python bytecode isn't working!"
  506. fi
  507. print_status "Displays partition table when executing target partition_table"
  508. idf.py partition-table | grep -E "# ESP-IDF .+ Partition Table"
  509. rm -r build
  510. print_status "Make sure a full build never runs '/usr/bin/env python' or similar"
  511. OLDPATH="$PATH"
  512. PYTHON="$(which python)"
  513. rm -rf build
  514. cat > ./python << EOF
  515. #!/bin/sh
  516. echo "The build system has executed '/usr/bin/env python' or similar"
  517. exit 1
  518. EOF
  519. chmod +x ./python
  520. export PATH="$(pwd):$PATH"
  521. ${PYTHON} $IDF_PATH/tools/idf.py build || failure "build failed"
  522. export PATH="$OLDPATH"
  523. rm ./python
  524. print_status "Handling deprecated Kconfig options"
  525. idf.py clean > /dev/null
  526. rm -f sdkconfig.defaults
  527. rm -f sdkconfig
  528. echo "" > ${IDF_PATH}/sdkconfig.rename
  529. idf.py reconfigure > /dev/null
  530. echo "CONFIG_TEST_OLD_OPTION=y" >> sdkconfig
  531. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" >> ${IDF_PATH}/sdkconfig.rename
  532. echo -e "\n\
  533. menu \"test\"\n\
  534. config TEST_NEW_OPTION\n\
  535. bool \"test\"\n\
  536. default \"n\"\n\
  537. help\n\
  538. TEST_NEW_OPTION description\n\
  539. endmenu\n" >> ${IDF_PATH}/Kconfig
  540. idf.py reconfigure > /dev/null
  541. grep "CONFIG_TEST_OLD_OPTION=y" sdkconfig || failure "CONFIG_TEST_OLD_OPTION should be in sdkconfig for backward compatibility"
  542. grep "CONFIG_TEST_NEW_OPTION=y" sdkconfig || failure "CONFIG_TEST_NEW_OPTION should be now in sdkconfig"
  543. grep "#define CONFIG_TEST_NEW_OPTION 1" build/config/sdkconfig.h || failure "sdkconfig.h should contain the new macro"
  544. grep "#define CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" build/config/sdkconfig.h || failure "sdkconfig.h should contain the compatibility macro"
  545. grep "set(CONFIG_TEST_OLD_OPTION \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_OLD_OPTION should be in auto.conf for backward compatibility"
  546. grep "set(CONFIG_TEST_NEW_OPTION \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_NEW_OPTION should be now in auto.conf"
  547. rm -f sdkconfig sdkconfig.defaults
  548. pushd ${IDF_PATH}
  549. git checkout -- sdkconfig.rename Kconfig
  550. popd
  551. print_status "Handling deprecated Kconfig options in sdkconfig.defaults"
  552. idf.py clean
  553. rm -f sdkconfig
  554. echo "CONFIG_TEST_OLD_OPTION=7" > sdkconfig.defaults
  555. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" > ${IDF_PATH}/sdkconfig.rename
  556. echo -e "\n\
  557. menu \"test\"\n\
  558. config TEST_NEW_OPTION\n\
  559. int \"TEST_NEW_OPTION\"\n\
  560. range 0 10\n\
  561. default 5\n\
  562. help\n\
  563. TEST_NEW_OPTION description\n\
  564. endmenu\n" >> ${IDF_PATH}/Kconfig
  565. idf.py reconfigure > /dev/null
  566. grep "CONFIG_TEST_OLD_OPTION=7" sdkconfig || failure "CONFIG_TEST_OLD_OPTION=7 should be in sdkconfig for backward compatibility"
  567. grep "CONFIG_TEST_NEW_OPTION=7" sdkconfig || failure "CONFIG_TEST_NEW_OPTION=7 should be in sdkconfig"
  568. rm -f sdkconfig.defaults
  569. pushd ${IDF_PATH}
  570. git checkout -- sdkconfig.rename Kconfig
  571. popd
  572. echo "Can have multiple deprecated Kconfig options map to a single new option"
  573. idf.py clean > /dev/null
  574. rm -f sdkconfig.defaults
  575. rm -f sdkconfig
  576. echo "" > ${IDF_PATH}/sdkconfig.rename
  577. idf.py reconfigure > /dev/null
  578. echo "CONFIG_TEST_NEW_OPTION=y" >> sdkconfig
  579. echo "CONFIG_TEST_OLD_OPTION_1 CONFIG_TEST_NEW_OPTION" >> ${IDF_PATH}/sdkconfig.rename
  580. echo "CONFIG_TEST_OLD_OPTION_2 CONFIG_TEST_NEW_OPTION" >> ${IDF_PATH}/sdkconfig.rename
  581. echo -e "\n\
  582. menu \"test\"\n\
  583. config TEST_NEW_OPTION\n\
  584. bool \"test\"\n\
  585. default \"n\"\n\
  586. help\n\
  587. TEST_NEW_OPTION description\n\
  588. endmenu\n" >> ${IDF_PATH}/Kconfig
  589. idf.py reconfigure > /dev/null
  590. grep "CONFIG_TEST_OLD_OPTION_1=y" sdkconfig || failure "CONFIG_TEST_OLD_OPTION_1 should be in sdkconfig for backward compatibility"
  591. grep "CONFIG_TEST_OLD_OPTION_2=y" sdkconfig || failure "CONFIG_TEST_OLD_OPTION_2 should be in sdkconfig for backward compatibility"
  592. grep "#define CONFIG_TEST_OLD_OPTION_1 CONFIG_TEST_NEW_OPTION" build/config/sdkconfig.h || failure "sdkconfig.h should contain the compatibility macro"
  593. grep "#define CONFIG_TEST_OLD_OPTION_2 CONFIG_TEST_NEW_OPTION" build/config/sdkconfig.h || failure "sdkconfig.h should contain the compatibility macro"
  594. grep "set(CONFIG_TEST_OLD_OPTION_1 \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_OLD_OPTION_1 should be in auto.conf for backward compatibility"
  595. grep "set(CONFIG_TEST_OLD_OPTION_2 \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_OLD_OPTION_2 should be in auto.conf for backward compatibility"
  596. rm -rf sdkconfig sdkconfig.defaults build
  597. pushd ${IDF_PATH}
  598. git checkout -- sdkconfig.rename Kconfig
  599. popd
  600. echo "Can have target specific deprecated Kconfig options"
  601. idf.py clean
  602. rm -f sdkconfig
  603. echo "CONFIG_TEST_OLD_OPTION=y" > sdkconfig
  604. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" >> ${IDF_PATH}/components/esp_system/sdkconfig.rename.esp32s2
  605. echo -e "\n\
  606. menu \"test\"\n\
  607. config TEST_NEW_OPTION\n\
  608. bool \"TEST_NEW_OPTION\"\n\
  609. default y\n\
  610. help\n\
  611. TEST_NEW_OPTION description\n\
  612. endmenu\n" >> ${IDF_PATH}/Kconfig
  613. idf.py set-target esp32 > /dev/null
  614. grep "CONFIG_TEST_OLD_OPTION=y" sdkconfig && failure "CONFIG_TEST_OLD_OPTION=y should NOT be in sdkconfig"
  615. grep "CONFIG_TEST_NEW_OPTION=y" sdkconfig || failure "CONFIG_TEST_NEW_OPTION=y should be in sdkconfig"
  616. rm -f sdkconfig
  617. idf.py set-target esp32s2 > /dev/null
  618. grep "CONFIG_TEST_OLD_OPTION=y" sdkconfig || failure "CONFIG_TEST_OLD_OPTION=y should be in esp32s2's sdkconfig for backward compatibility"
  619. grep "CONFIG_TEST_NEW_OPTION=y" sdkconfig || failure "CONFIG_TEST_NEW_OPTION=y should be in sdkconfig"
  620. rm -rf sdkconfig sdkconfig.defaults build
  621. pushd ${IDF_PATH}
  622. git checkout -- components/esp_system/sdkconfig.rename.esp32s2 Kconfig
  623. popd
  624. print_status "Confserver can be invoked by idf.py"
  625. echo '{"version": 1}' | idf.py confserver || failure "Couldn't load confserver"
  626. print_status "Check ccache is used to build"
  627. touch ccache && chmod +x ccache # make sure that ccache is present for this test
  628. (export PATH=$PWD:$PATH && idf.py --ccache reconfigure | grep "ccache will be used") || failure "ccache should be used when --cache is specified"
  629. idf.py fullclean
  630. (export PATH=$PWD:$PATH && idf.py reconfigure| grep -c "ccache will be used" | grep -wq 0) \
  631. || failure "ccache should not be used even when present if --ccache is not specified"
  632. (export PATH=$PWD:$PATH && idf.py --no-ccache reconfigure| grep -c "ccache will be used" | grep -wq 0) \
  633. || failure "--no-ccache causes no issue for backward compatibility"
  634. rm -f ccache
  635. print_status "Custom bootloader overrides original"
  636. clean_build_dir
  637. (mkdir components && cd components && cp -r $IDF_PATH/components/bootloader .)
  638. idf.py bootloader
  639. grep "$PWD/components/bootloader/subproject/main/bootloader_start.c" build/bootloader/compile_commands.json \
  640. || failure "Custom bootloader source files should be built instead of the original's"
  641. rm -rf components
  642. print_status "Empty directory not treated as a component"
  643. clean_build_dir
  644. mkdir -p components/esp32 && idf.py reconfigure
  645. ! grep "$PWD/components/esp32" $PWD/build/project_description.json || failure "Failed to build with empty esp32 directory in components"
  646. rm -rf components
  647. print_status "If a component directory is added to COMPONENT_DIRS, its subdirectories are not added"
  648. clean_build_dir
  649. mkdir -p main/test
  650. echo "idf_component_register()" > main/test/CMakeLists.txt
  651. idf.py reconfigure
  652. ! grep "$PWD/main/test" $PWD/build/project_description.json || failure "COMPONENT_DIRS has added component subdirectory to the build"
  653. grep "$PWD/main" $PWD/build/project_description.json || failure "COMPONENT_DIRS parent component directory should be included in the build"
  654. rm -rf main/test
  655. print_status "If a component directory is added to COMPONENT_DIRS, its sibling directories are not added"
  656. clean_build_dir
  657. mkdir -p mycomponents/mycomponent
  658. echo "idf_component_register()" > mycomponents/mycomponent/CMakeLists.txt
  659. # first test by adding single component directory to EXTRA_COMPONENT_DIRS
  660. mkdir -p mycomponents/esp32
  661. echo "idf_component_register()" > mycomponents/esp32/CMakeLists.txt
  662. idf.py -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents/mycomponent reconfigure
  663. ! grep "$PWD/mycomponents/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  664. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  665. rm -rf mycomponents/esp32
  666. # now the same thing, but add a components directory
  667. mkdir -p esp32
  668. echo "idf_component_register()" > esp32/CMakeLists.txt
  669. idf.py -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents reconfigure
  670. ! grep "$PWD/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  671. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  672. rm -rf esp32
  673. rm -rf mycomponents
  674. print_status "toolchain prefix is set in project description file"
  675. clean_build_dir
  676. idf.py reconfigure
  677. grep "prefix.*esp.*elf-" $PWD/build/project_description.json || failure "toolchain prefix not set or determined by CMake"
  678. # idf.py subcommand options, (using monitor with as example)
  679. print_status "Can set options to subcommands: print_filter for monitor"
  680. clean_build_dir
  681. mv ${IDF_PATH}/tools/idf_monitor.py ${IDF_PATH}/tools/idf_monitor.py.tmp
  682. echo "import sys;print(sys.argv[1:])" > ${IDF_PATH}/tools/idf_monitor.py
  683. idf.py build || failure "Failed to build project"
  684. 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)"
  685. mv ${IDF_PATH}/tools/idf_monitor.py.tmp ${IDF_PATH}/tools/idf_monitor.py
  686. print_status "Fail on build time works"
  687. clean_build_dir
  688. cp CMakeLists.txt CMakeLists.txt.bak
  689. printf "\nif(NOT EXISTS \"\${CMAKE_CURRENT_LIST_DIR}/hello.txt\") \nfail_at_build_time(test_file \"hello.txt does not exists\") \nendif()" >> CMakeLists.txt
  690. ! idf.py build || failure "Build should fail if requirements are not satisfied"
  691. touch hello.txt
  692. idf.py build || failure "Build succeeds once requirements are satisfied"
  693. rm -rf hello.txt CMakeLists.txt
  694. mv CMakeLists.txt.bak CMakeLists.txt
  695. rm -rf CMakeLists.txt.bak
  696. print_status "Component properties are set"
  697. clean_build_dir
  698. cp CMakeLists.txt CMakeLists.txt.bak
  699. printf "\nidf_component_get_property(srcs main SRCS)\nmessage(STATUS SRCS:\${srcs})" >> CMakeLists.txt
  700. (idf.py reconfigure | grep "SRCS:$(${REALPATH} main/main.c)") || failure "Component properties should be set"
  701. rm -rf CMakeLists.txt
  702. mv CMakeLists.txt.bak CMakeLists.txt
  703. rm -rf CMakeLists.txt.bak
  704. print_status "should be able to specify multiple sdkconfig default files"
  705. idf.py clean > /dev/null
  706. idf.py fullclean > /dev/null
  707. rm -f sdkconfig.defaults
  708. rm -f sdkconfig
  709. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults1
  710. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig.defaults2
  711. idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults1;sdkconfig.defaults2" reconfigure > /dev/null
  712. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults1 should be in sdkconfig"
  713. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig.defaults2 should be in sdkconfig"
  714. rm sdkconfig.defaults1 sdkconfig.defaults2 sdkconfig
  715. git checkout sdkconfig.defaults
  716. print_status "Supports git worktree"
  717. clean_build_dir
  718. git branch test_build_system
  719. git worktree add ../esp-idf-template-test test_build_system
  720. diff <(idf.py reconfigure | grep "App \"app-template\" version: ") <(cd ../esp-idf-template-test && idf.py reconfigure | grep "App \"app-template\" version: ") \
  721. || failure "Version on worktree should have been properly resolved"
  722. git worktree remove ../esp-idf-template-test
  723. print_status "idf.py fallback to build system target"
  724. clean_build_dir
  725. msg="Custom target is running"
  726. echo "" >> CMakeLists.txt
  727. echo "add_custom_target(custom_target COMMAND \${CMAKE_COMMAND} -E echo \"${msg}\")" >> CMakeLists.txt
  728. idf.py custom_target 1>log.txt || failure "Could not invoke idf.py with custom target"
  729. grep "${msg}" log.txt 1>/dev/null || failure "Custom target did not produce expected output"
  730. git checkout CMakeLists.txt
  731. rm -f log.txt
  732. print_status "Build fails if partitions don't fit in flash"
  733. clean_build_dir
  734. sed -i.bak "s/CONFIG_ESPTOOLPY_FLASHSIZE.\+//" sdkconfig # remove all flashsize config
  735. echo "CONFIG_ESPTOOLPY_FLASHSIZE_1MB=y" >> sdkconfig # introduce undersize flash
  736. ( 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"
  737. mv sdkconfig.bak sdkconfig
  738. print_status "Warning is given if smallest partition is nearly full"
  739. clean_build_dir
  740. # Build a first time to get the binary size and to check that no warning is issued.
  741. ( idf.py build 2>&1 | grep "partition is nearly full" ) && failure "Warning for nearly full smallest partition was given when the condition is not fulfilled"
  742. # Get the size of the binary, in KB. Add 1 to the total.
  743. # The goal is to create an app partition which is slightly bigger than the binary itself
  744. get_file_size "build/app-template.bin"
  745. # Put the returned size ($BINSIZE) in a new variable, convert it to KB and add 1
  746. let size=${BINSIZE}/1024+1
  747. cp ${IDF_PATH}/components/partition_table/partitions_singleapp.csv partitions.csv
  748. ${SED} -i "s/factory, app, factory, , 1M/factory, app, factory, , ${size}K/" partitions.csv
  749. echo "CONFIG_PARTITION_TABLE_CUSTOM=y" > sdkconfig
  750. # don't use FreeRTOS SMP build for this test - follow up ticket: IDF-5386
  751. echo "CONFIG_FREERTOS_SMP=n" >> sdkconfig
  752. ( idf.py build 2>&1 | grep "partition is nearly full" ) || failure "No warning for nearly full smallest partition was given when the condition is fulfilled"
  753. rm -f partitions.csv sdkconfig
  754. print_status "Flash size is correctly set in the bootloader image header"
  755. # Build with the default 2MB setting
  756. rm sdkconfig
  757. idf.py bootloader || failure "Failed to build bootloader"
  758. bin_header_match build/bootloader/bootloader.bin "0210"
  759. # Change to 4MB
  760. sleep 1 # delay here to make sure sdkconfig modification time is different
  761. echo "CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y" > sdkconfig
  762. idf.py bootloader || failure "Failed to build bootloader"
  763. bin_header_match build/bootloader/bootloader.bin "0220"
  764. # Change to QIO, bootloader should still be DIO (will change to QIO in 2nd stage bootloader)
  765. sleep 1 # delay here to make sure sdkconfig modification time is different
  766. echo "CONFIG_FLASHMODE_QIO=y" > sdkconfig
  767. idf.py bootloader || failure "Failed to build bootloader"
  768. bin_header_match build/bootloader/bootloader.bin "0210"
  769. # Change to 80 MHz
  770. sleep 1 # delay here to make sure sdkconfig modification time is different
  771. echo "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" > sdkconfig
  772. idf.py bootloader || failure "Failed to build bootloader"
  773. bin_header_match build/bootloader/bootloader.bin "021f"
  774. rm sdkconfig
  775. print_status "DFU build works"
  776. rm -f -r build sdkconfig
  777. idf.py dfu &> tmp.log
  778. grep "command \"dfu\" is not known to idf.py and is not a Ninja target" tmp.log || (tail -n 100 tmp.log ; failure "DFU build should fail for default chip target")
  779. idf.py set-target esp32s2
  780. idf.py dfu &> tmp.log
  781. grep "build/dfu.bin\" has been written. You may proceed with DFU flashing." tmp.log || (tail -n 100 tmp.log ; failure "DFU build should succeed for esp32s2")
  782. rm tmp.log
  783. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} "dfu.bin"
  784. rm -rf build sdkconfig
  785. print_status "UF2 build works"
  786. rm -f -r build sdkconfig
  787. idf.py uf2 &> tmp.log
  788. grep "build/uf2.bin\" has been written." tmp.log || (tail -n 100 tmp.log ; failure "UF2 build works for esp32")
  789. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} "uf2.bin"
  790. idf.py uf2-app &> tmp.log
  791. grep "build/uf2-app.bin\" has been written." tmp.log || (tail -n 100 tmp.log ; failure "UF2 build works for application binary")
  792. assert_built "uf2-app.bin"
  793. idf.py set-target esp32s2
  794. idf.py uf2 &> tmp.log
  795. grep "build/uf2.bin\" has been written." tmp.log || (tail -n 100 tmp.log ; failure "UF2 build works for esp32s2")
  796. rm tmp.log
  797. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} "uf2.bin"
  798. rm -rf build sdkconfig
  799. print_status "Loadable ELF build works"
  800. echo "CONFIG_APP_BUILD_TYPE_ELF_RAM=y" > sdkconfig
  801. # Set recommend configs to reduce memory footprint
  802. echo "CONFIG_VFS_SUPPORT_TERMIOS=n" >> sdkconfig
  803. echo "CONFIG_NEWLIB_NANO_FORMAT=y" >> sdkconfig
  804. echo "CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y" >> sdkconfig
  805. echo "CONFIG_ESP_ERR_TO_NAME_LOOKUP=n" >> sdkconfig
  806. idf.py reconfigure || failure "Couldn't configure for loadable ELF file"
  807. test -f build/flasher_args.json && failure "flasher_args.json should not be generated in a loadable ELF build"
  808. idf.py build || failure "Couldn't build a loadable ELF file"
  809. print_status "Defaults set properly for unspecified idf_build_process args"
  810. pushd $IDF_PATH/examples/build_system/cmake/idf_as_lib
  811. cp CMakeLists.txt CMakeLists.txt.bak
  812. echo -e "\nidf_build_get_property(project_dir PROJECT_DIR)" >> CMakeLists.txt
  813. echo -e "\nmessage(\"Project directory: \${project_dir}\")" >> CMakeLists.txt
  814. mkdir build && cd build
  815. cmake .. -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32 &> log.txt
  816. grep "Project directory: $IDF_PATH/examples/build_system/cmake/idf_as_lib" log.txt || failure "PROJECT_DIR default was not set"
  817. cd ..
  818. mv CMakeLists.txt.bak CMakeLists.txt
  819. rm -rf build
  820. popd
  821. print_status "Getting component overriden dir"
  822. clean_build_dir
  823. mkdir -p components/hal
  824. echo "idf_component_get_property(overriden_dir \${COMPONENT_NAME} COMPONENT_OVERRIDEN_DIR)" >> components/hal/CMakeLists.txt
  825. echo "message(STATUS overriden_dir:\${overriden_dir})" >> components/hal/CMakeLists.txt
  826. (idf.py reconfigure | grep "overriden_dir:$IDF_PATH/components/hal") || failure "Failed to get overriden dir" # no registration, overrides registration as well
  827. print_status "Overriding Kconfig"
  828. echo "idf_component_register(KCONFIG \${overriden_dir}/Kconfig)" >> components/hal/CMakeLists.txt
  829. echo "idf_component_get_property(kconfig \${COMPONENT_NAME} KCONFIG)" >> components/hal/CMakeLists.txt
  830. echo "message(STATUS kconfig:\${overriden_dir}/Kconfig)" >> components/hal/CMakeLists.txt
  831. (idf.py reconfigure | grep "kconfig:$IDF_PATH/components/hal/Kconfig") || failure "Failed to verify original `main` directory"
  832. rm -rf components
  833. print_status "Project components prioritized over EXTRA_COMPONENT_DIRS"
  834. clean_build_dir
  835. mkdir -p extra_dir/my_component
  836. echo "idf_component_register()" > extra_dir/my_component/CMakeLists.txt
  837. cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  838. ${SED} -i "s%cmake_minimum_required(VERSION \([0-9]\+\).\([0-9]\+\))%cmake_minimum_required(VERSION \1.\2)\nset(EXTRA_COMPONENT_DIRS extra_dir)%" CMakeLists.txt
  839. (idf.py reconfigure | grep "$PWD/extra_dir/my_component") || failure "Unable to find component specified in EXTRA_COMPONENT_DIRS"
  840. mkdir -p components/my_component
  841. echo "idf_component_register()" > components/my_component/CMakeLists.txt
  842. (idf.py reconfigure | grep "$PWD/components/my_component") || failure "Project components should be prioritized over EXTRA_COMPONENT_DIRS"
  843. mv CMakeLists.bak CMakeLists.txt # revert previous modifications
  844. rm -rf extra_dir components
  845. print_status "Components in EXCLUDE_COMPONENTS not passed to idf_component_manager"
  846. clean_build_dir
  847. idf.py create-component -C components/ to_be_excluded || failure "Failed to create a component"
  848. echo "invalid syntax..." > components/to_be_excluded/idf_component.yml
  849. ! idf.py reconfigure || failure "Build should have failed due to invalid syntax in idf_component.yml"
  850. idf.py -DEXCLUDE_COMPONENTS=to_be_excluded reconfigure || failure "Build should have succeeded when the component is excluded"
  851. rm -rf components/to_be_excluded
  852. print_status "Create project using idf.py and build it"
  853. echo "Trying to create project."
  854. (idf.py -C projects create-project temp_test_project) || failure "Failed to create the project."
  855. cd "$IDF_PATH/projects/temp_test_project"
  856. echo "Building the project temp_test_project . . ."
  857. idf.py build || failure "Failed to build the project."
  858. cd "$IDF_PATH"
  859. rm -rf "$IDF_PATH/projects/temp_test_project"
  860. print_status "Create component using idf.py, create project using idf.py."
  861. print_status "Add the component to the created project and build the project."
  862. echo "Trying to create project . . ."
  863. (idf.py -C projects create-project temp_test_project) || failure "Failed to create the project."
  864. echo "Trying to create component . . ."
  865. (idf.py -C components create-component temp_test_component) || failure "Failed to create the component."
  866. ${SED} -i '5i\\tfunc();' "$IDF_PATH/projects/temp_test_project/main/temp_test_project.c"
  867. ${SED} -i '5i#include "temp_test_component.h"' "$IDF_PATH/projects/temp_test_project/main/temp_test_project.c"
  868. cd "$IDF_PATH/projects/temp_test_project"
  869. idf.py build || failure "Failed to build the project."
  870. cd "$IDF_PATH"
  871. rm -rf "$IDF_PATH/projects/temp_test_project"
  872. rm -rf "$IDF_PATH/components/temp_test_component"
  873. print_status "Check that command for creating new project will fail if the target folder is not empty."
  874. mkdir "$IDF_PATH/example_proj/"
  875. touch "$IDF_PATH/example_proj/tmp_130698"
  876. EXPECTED_EXIT_VALUE=3
  877. expected_failure $EXPECTED_EXIT_VALUE idf.py create-project --path "$IDF_PATH/example_proj/" temp_test_project || failure "Command exit value is wrong."
  878. rm -rf "$IDF_PATH/example_proj/"
  879. print_status "Check that command for creating new project will fail if the target path is file."
  880. touch "$IDF_PATH/example_proj"
  881. EXPECTED_EXIT_VALUE=4
  882. expected_failure $EXPECTED_EXIT_VALUE idf.py create-project --path "$IDF_PATH/example_proj" temp_test_project || failure "Command exit value is wrong."
  883. rm -rf "$IDF_PATH/example_proj"
  884. print_status "Check docs command"
  885. clean_build_dir
  886. idf.py build
  887. idf.py set-target esp32s2
  888. idf.py docs || failure "'idf.py docs' failed"
  889. idf.py docs --no-browser | grep "https://docs.espressif.com/projects/esp-idf/en" || failure "'idf.py docs --no-browser' failed"
  890. idf.py docs --no-browser --language en | grep "https://docs.espressif.com/projects/esp-idf/en" || failure "'idf.py docs --no-browser --language en' failed"
  891. idf.py docs --no-browser --language en --version v4.2.1 | grep "https://docs.espressif.com/projects/esp-idf/en/v4.2.1" || failure "'idf.py docs --no-browser --language en --version v4.2.1' failed"
  892. idf.py docs --no-browser --language en --version v4.2.1 --target esp32 | grep "https://docs.espressif.com/projects/esp-idf/en/v4.2.1/esp32" || failure "'idf.py docs --no-browser --language en --version v4.2.1 --target esp32' failed"
  893. idf.py docs --no-browser --language en --version v4.2.1 --target esp32 --starting-page get-started | grep "https://docs.espressif.com/projects/esp-idf/en/v4.2.1/esp32/get-started" || failure "'idf.py docs --no-browser --language en --version v4.2.1 --target esp32 --starting-page get-started' failed"
  894. print_status "Deprecation warning check"
  895. cd ${TESTDIR}/template
  896. # click warning
  897. idf.py post_debug &> tmp.log
  898. grep "Error: Command \"post_debug\" is deprecated since v4.4 and was removed in v5.0." tmp.log || failure "Missing deprecation warning with command \"post_debug\""
  899. rm tmp.log
  900. # cmake warning
  901. idf.py efuse_common_table &> tmp.log
  902. grep "Have you wanted to run \"efuse-common-table\" instead?" tmp.log || failure "Missing deprecation warning with command \"efuse_common_table\""
  903. rm tmp.log
  904. print_status "Save-defconfig checks"
  905. cd ${TESTDIR}/template
  906. rm -f sdkconfig.defaults
  907. rm -f sdkconfig
  908. idf.py fullclean > /dev/null
  909. echo "CONFIG_COMPILER_OPTIMIZATION_SIZE=y" >> sdkconfig
  910. echo "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" >> sdkconfig
  911. idf.py save-defconfig
  912. wc -l sdkconfig.defaults
  913. grep "CONFIG_IDF_TARGET" sdkconfig.defaults && failure "CONFIG_IDF_TARGET should not be in sdkconfig.defaults"
  914. grep "CONFIG_COMPILER_OPTIMIZATION_SIZE=y" sdkconfig.defaults || failure "Missing CONFIG_COMPILER_OPTIMIZATION_SIZE=y in sdkconfig.defaults"
  915. grep "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" sdkconfig.defaults || failure "Missing CONFIG_ESPTOOLPY_FLASHFREQ_80M=y in sdkconfig.defaults"
  916. idf.py fullclean > /dev/null
  917. rm -f sdkconfig.defaults
  918. rm -f sdkconfig
  919. idf.py set-target esp32c3
  920. echo "CONFIG_PARTITION_TABLE_OFFSET=0x8001" >> sdkconfig
  921. idf.py save-defconfig
  922. wc -l sdkconfig.defaults
  923. grep "CONFIG_IDF_TARGET=\"esp32c3\"" sdkconfig.defaults || failure "Missing CONFIG_IDF_TARGET=\"esp32c3\" in sdkconfig.defaults"
  924. grep "CONFIG_PARTITION_TABLE_OFFSET=0x8001" sdkconfig.defaults || failure "Missing CONFIG_PARTITION_TABLE_OFFSET=0x8001 in sdkconfig.defaults"
  925. idf.py fullclean > /dev/null
  926. rm -f sdkconfig.defaults
  927. rm -f sdkconfig
  928. print_status "All tests completed"
  929. if [ -n "${FAILURES}" ]; then
  930. echo "Some failures were detected:"
  931. echo -e "${FAILURES}"
  932. exit 1
  933. else
  934. echo "Build tests passed."
  935. fi
  936. }
  937. function print_status()
  938. {
  939. echo "******** $1"
  940. STATUS="$1"
  941. }
  942. function failure()
  943. {
  944. echo "!!!!!!!!!!!!!!!!!!!"
  945. echo "FAILURE: $1"
  946. echo "!!!!!!!!!!!!!!!!!!!"
  947. FAILURES="${FAILURES}${STATUS} :: $1\n"
  948. }
  949. function expected_failure() {
  950. "${@:2}"
  951. EXIT_VALUE=$?
  952. if [ $EXIT_VALUE != "$1" ]; then
  953. echo "[ERROR] Exit value of executed command is $EXIT_VALUE (expected $1)"; return 1
  954. else return 0
  955. fi
  956. }
  957. TESTDIR=${PWD}/build_system_tests_$$
  958. mkdir -p ${TESTDIR}
  959. # set NOCLEANUP=1 if you want to keep the test directory around
  960. # for post-mortem debugging
  961. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  962. SNAPSHOT=${TESTDIR}/snapshot
  963. BUILD=${TESTDIR}/template/build
  964. IS_DARWIN=
  965. export SED=sed
  966. export REALPATH=realpath
  967. if [ "$(uname -s)" = "Darwin" ]; then
  968. IS_DARWIN=1
  969. export SED=gsed
  970. export REALPATH=grealpath
  971. fi
  972. # copy all the build output to a snapshot directory
  973. function take_build_snapshot()
  974. {
  975. rm -rf ${SNAPSHOT}
  976. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  977. if [ -n "$IS_DARWIN" ]; then
  978. # wait at least 1 second before the next build, for the test in
  979. # file_was_rebuilt to work
  980. sleep 1
  981. fi
  982. }
  983. # verify that all the arguments are present in the build output directory
  984. function assert_built()
  985. {
  986. until [ -z "$1" ]; do
  987. if [ ! -f "${BUILD}/$1" ]; then
  988. failure "File $1 should be in the build output directory"
  989. fi
  990. shift
  991. done
  992. }
  993. # Test if a file has been rebuilt.
  994. function file_was_rebuilt()
  995. {
  996. if [ -z "$IS_DARWIN" ]; then
  997. # can't use [ a -ot b ] here as -ot only gives second resolution
  998. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  999. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  1000. return 0
  1001. else
  1002. return 1
  1003. fi
  1004. else
  1005. # macOS: work around 1-second resolution by adding a sleep in take_build_snapshot
  1006. if [ ${SNAPSHOT}/$1 -ot ${BUILD}/$1 ]; then
  1007. return 0
  1008. else
  1009. return 1
  1010. fi
  1011. fi
  1012. }
  1013. # verify all the arguments passed in were rebuilt relative to the snapshot
  1014. function assert_rebuilt()
  1015. {
  1016. until [ -z "$1" ]; do
  1017. assert_built "$1"
  1018. if [ ! -f "${SNAPSHOT}/$1" ]; then
  1019. failure "File $1 should be in original build snapshot"
  1020. fi
  1021. if ! file_was_rebuilt "$1"; then
  1022. failure "File $1 should have been rebuilt"
  1023. fi
  1024. shift
  1025. done
  1026. }
  1027. # verify all the arguments are in the build directory & snapshot,
  1028. # but were not rebuilt
  1029. function assert_not_rebuilt()
  1030. {
  1031. until [ -z "$1" ]; do
  1032. assert_built "$1"
  1033. if [ ! -f "${SNAPSHOT}/$1" ]; then
  1034. failure "File $1 should be in snapshot build directory"
  1035. fi
  1036. if file_was_rebuilt "$1"; then
  1037. failure "File $1 should not have been rebuilt"
  1038. fi
  1039. shift
  1040. done
  1041. }
  1042. # do a "clean" that doesn't depend on idf.py
  1043. function clean_build_dir()
  1044. {
  1045. PRESERVE_ROOT_ARG=
  1046. if [ -z "$IS_DARWIN" ]; then
  1047. PRESERVE_ROOT_ARG=--preserve-root
  1048. fi
  1049. rm -rf $PRESERVE_ROOT_ARG ${BUILD}/* ${BUILD}/.*
  1050. }
  1051. # check the bytes 3-4 of the binary image header. e.g.:
  1052. # bin_header_match app.bin 0210
  1053. function bin_header_match()
  1054. {
  1055. expected=$2
  1056. filename=$1
  1057. actual=$(xxd -s 2 -l 2 -ps $1)
  1058. if [ ! "$expected" = "$actual" ]; then
  1059. failure "Incorrect binary image header, expected $expected got $actual"
  1060. fi
  1061. }
  1062. cd ${TESTDIR}
  1063. run_tests