test_build_system_cmake.sh 43 KB

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