test_build_system_cmake.sh 50 KB

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