test_build_system_cmake.sh 51 KB

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