test_build_system_cmake.sh 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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/FreeRTOS-Kernel/portable/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/FreeRTOS-Kernel/portable/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. print_status "Compiler flags on build command line are taken into account"
  319. clean_build_dir
  320. # Backup original source file
  321. cp main/main.c main/main.c.bak
  322. # Alter source file to check user flag
  323. echo -e "\n#ifndef USER_FLAG \n \
  324. #error \"USER_FLAG is not defined!\" \n \
  325. #endif\n" >> main/main.c
  326. idf.py build -DCMAKE_C_FLAGS=-DUSER_FLAG || failure "User flags should have been taken into account"
  327. # Restore original file
  328. mv main/main.c.bak main/main.c
  329. print_status "Compiler flags cannot be overwritten"
  330. clean_build_dir
  331. # If the compiler flags are overriden, the following build command will
  332. # cause issues at link time.
  333. idf.py build -DCMAKE_C_FLAGS= -DCMAKE_CXX_FLAGS= || failure "CMake compiler flags have been overriden"
  334. # the next tests use the esp32s2 target
  335. export other_target=esp32s2
  336. print_status "Can override IDF_TARGET from environment"
  337. clean_build_dir
  338. rm sdkconfig
  339. export IDF_TARGET=$other_target
  340. (cd build && cmake -G Ninja .. ) || failure "Failed to configure with IDF_TARGET set in environment"
  341. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured for IDF_TARGET correctly"
  342. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt"
  343. unset IDF_TARGET
  344. print_status "Can set target using idf.py -D"
  345. clean_build_dir
  346. rm sdkconfig
  347. idf.py -DIDF_TARGET=$other_target reconfigure || failure "Failed to set target via idf.py"
  348. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py -D"
  349. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py -D"
  350. print_status "Can set target using -D as subcommand parameter for idf.py"
  351. clean_build_dir
  352. rm sdkconfig
  353. idf.py reconfigure -DIDF_TARGET=$other_target || failure "Failed to set target via idf.py subcommand -D parameter"
  354. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py reconfigure -D"
  355. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py reconfigure -D"
  356. print_status "Can set target using idf.py set-target"
  357. clean_build_dir
  358. rm sdkconfig
  359. idf.py set-target ${other_target} || failure "Failed to set target via idf.py set-target"
  360. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target"
  361. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target"
  362. print_status "idf.py understands alternative target names"
  363. clean_build_dir
  364. rm sdkconfig
  365. idf.py set-target ESP32-S2
  366. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target"
  367. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target"
  368. print_status "Can guess target from sdkconfig, if CMakeCache does not exist"
  369. idf.py fullclean || failure "Failed to clean the build directory"
  370. idf.py reconfigure || failure "Failed to reconfigure after fullclean"
  371. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  372. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  373. print_status "Can set the default target using sdkconfig.defaults"
  374. clean_build_dir
  375. rm sdkconfig
  376. echo "CONFIG_IDF_TARGET=\"${other_target}\"" > sdkconfig.defaults
  377. idf.py reconfigure || failure "Failed to reconfigure with default target set in sdkconfig.defaults"
  378. grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  379. other_target_caps=$(tr 'a-z' 'A-Z' <<< "${other_target}")
  380. grep "CONFIG_IDF_TARGET_${other_target_caps}=y" sdkconfig || failure "Didn't find CONFIG_IDF_TARGET_${other_target_caps} value"
  381. grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  382. rm sdkconfig.defaults
  383. print_status "IDF_TARGET takes precedence over the value of CONFIG_IDF_TARGET in sdkconfig.defaults"
  384. clean_build_dir
  385. rm sdkconfig
  386. echo "CONFIG_IDF_TARGET=\"${other_target}\"" > sdkconfig.defaults
  387. export IDF_TARGET=esp32
  388. idf.py reconfigure || failure "Failed to reconfigure with default target set in sdkconfig.defaults and different IDF_TARGET in the environment"
  389. grep "CONFIG_IDF_TARGET=\"esp32\"" sdkconfig || failure "Didn't find the expected CONFIG_IDF_TARGET value"
  390. grep "CONFIG_IDF_TARGET_ESP32=y" sdkconfig || failure "Didn't find CONFIG_IDF_TARGET_ESP32 value"
  391. grep "IDF_TARGET:STRING=esp32" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt after fullclean and reconfigure"
  392. rm sdkconfig.defaults
  393. unset IDF_TARGET
  394. print_status "idf.py fails if IDF_TARGET settings don't match in sdkconfig, CMakeCache.txt, and the environment"
  395. clean_build_dir
  396. rm sdkconfig
  397. idf.py set-target ${other_target} || failure "Couldn't set target to ${other_target}"
  398. # Change to a different IDF_TARGET in the environment
  399. export IDF_TARGET=esp32
  400. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET was set to an incompatible value in the environment"
  401. # Now make sdkconfig consistent with the environement (note: not really consistent, just for the purpose of the test)
  402. echo "CONFIG_IDF_TARGET=\"esp32\"" >> sdkconfig
  403. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET in CMakeCache.txt didn't match the environment"
  404. # Now unset IDF_TARGET in the environment, sdkconfig and CMakeCache.txt are still inconsistent
  405. unset IDF_TARGET
  406. ! idf.py reconfigure || failure "Build did't fail when IDF_TARGET in CMakeCache.txt didn't match the sdkconfig"
  407. unset other_target # done changing target from the default
  408. clean_build_dir
  409. rm sdkconfig
  410. print_status "Setting EXTRA_COMPONENT_DIRS works"
  411. clean_build_dir
  412. (idf.py reconfigure | grep "$PWD/main") || failure "Failed to verify original `main` directory"
  413. mkdir -p main/main/main # move main component contents to another directory
  414. mv main/* main/main/main
  415. cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  416. ${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
  417. (idf.py reconfigure | grep "$PWD/main/main/main") || failure "Failed to set EXTRA_COMPONENT_DIRS"
  418. mv CMakeLists.bak CMakeLists.txt # revert previous modifications
  419. mv main/main/main/* main
  420. rm -rf main/main
  421. print_status "Non-existent paths in EXTRA_COMPONENT_DIRS are not allowed"
  422. clean_build_dir
  423. ! idf.py -DEXTRA_COMPONENT_DIRS="extra_components" reconfigure || failure "Build should fail when non-existent component path is added"
  424. print_status "Component names may contain spaces"
  425. clean_build_dir
  426. mkdir -p "extra component"
  427. echo "idf_component_register" > "extra component/CMakeLists.txt"
  428. idf.py -DEXTRA_COMPONENT_DIRS="extra component;main" || failure "Build should succeed when a component name contains space"
  429. rm -rf "extra component"
  430. print_status "sdkconfig should have contents of all files: sdkconfig, sdkconfig.defaults, sdkconfig.defaults.IDF_TARGET"
  431. idf.py clean > /dev/null
  432. idf.py fullclean > /dev/null
  433. rm -f sdkconfig.defaults
  434. rm -f sdkconfig
  435. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults
  436. echo "CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y" >> sdkconfig.defaults.esp32
  437. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig
  438. idf.py reconfigure > /dev/null
  439. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults should be into sdkconfig"
  440. grep "CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y" sdkconfig || failure "The define from sdkconfig.defaults.esp32 should be into sdkconfig"
  441. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig should be into sdkconfig"
  442. rm sdkconfig sdkconfig.defaults sdkconfig.defaults.esp32
  443. print_status "Test if it can build the example to run on host"
  444. pushd $IDF_PATH/examples/build_system/cmake/idf_as_lib
  445. (set -euo pipefail && source build.sh)
  446. popd
  447. rm -r $IDF_PATH/examples/build_system/cmake/idf_as_lib/build
  448. print_status "Test build ESP-IDF as a library to a custom CMake projects for all targets"
  449. IDF_AS_LIB=$IDF_PATH/examples/build_system/cmake/idf_as_lib
  450. # note: we just need to run cmake
  451. for TARGET in "esp32" "esp32s2" "esp32s3" "esp32c3" "esp32h2" "esp8684"
  452. do
  453. echo "Build idf_as_lib for $TARGET target"
  454. rm -rf build
  455. mkdir -p build && cd build
  456. cmake $IDF_AS_LIB -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-$TARGET.cmake -DTARGET=$TARGET || failure "Failed to generate idf_as_lib build files for target $TARGET"
  457. cmake --build . || failure "Failed to build idf_as_lib for target $TARGET"
  458. cd ..
  459. done
  460. print_status "Building a project with CMake library imported and PSRAM workaround, all files compile with workaround"
  461. # Test for libraries compiled within ESP-IDF
  462. rm -r build sdkconfig
  463. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" >> sdkconfig.defaults
  464. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  465. # note: we do 'reconfigure' here, as we just need to run cmake
  466. idf.py -C $IDF_PATH/examples/build_system/cmake/import_lib -B `pwd`/build -D SDKCONFIG_DEFAULTS="`pwd`/sdkconfig.defaults" reconfigure
  467. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  468. (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"
  469. rm -r build sdkconfig sdkconfig.defaults
  470. print_status "Test for external libraries in custom CMake projects with ESP-IDF components linked"
  471. mkdir build
  472. IDF_AS_LIB=$IDF_PATH/examples/build_system/cmake/idf_as_lib
  473. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" > $IDF_AS_LIB/sdkconfig
  474. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> $IDF_AS_LIB/sdkconfig
  475. # note: we just need to run cmake
  476. (cd build && cmake $IDF_AS_LIB -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32)
  477. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  478. (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"
  479. for strat in MEMW NOPS DUPLDST; do
  480. print_status "Test for external libraries in custom CMake projects with PSRAM strategy $strat"
  481. rm -r build sdkconfig sdkconfig.defaults sdkconfig.defaults.esp32
  482. stratlc=`echo $strat | tr A-Z a-z`
  483. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" > sdkconfig.defaults
  484. echo "CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_$strat=y" >> sdkconfig.defaults
  485. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  486. # note: we do 'reconfigure' here, as we just need to run cmake
  487. idf.py reconfigure
  488. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  489. (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"
  490. echo ${PWD}
  491. rm -r sdkconfig.defaults build
  492. done
  493. print_status "Cleaning Python bytecode"
  494. idf.py clean > /dev/null
  495. idf.py fullclean > /dev/null
  496. if [ "$(find $IDF_PATH -name "*.py[co]" | wc -l)" -eq 0 ]; then
  497. failure "No Python bytecode in IDF!"
  498. fi
  499. idf.py python-clean
  500. if [ "$(find $IDF_PATH -name "*.py[co]" | wc -l)" -gt 0 ]; then
  501. failure "Python bytecode isn't working!"
  502. fi
  503. print_status "Displays partition table when executing target partition_table"
  504. idf.py partition-table | grep -E "# ESP-IDF .+ Partition Table"
  505. rm -r build
  506. print_status "Make sure a full build never runs '/usr/bin/env python' or similar"
  507. OLDPATH="$PATH"
  508. PYTHON="$(which python)"
  509. rm -rf build
  510. cat > ./python << EOF
  511. #!/bin/sh
  512. echo "The build system has executed '/usr/bin/env python' or similar"
  513. exit 1
  514. EOF
  515. chmod +x ./python
  516. export PATH="$(pwd):$PATH"
  517. ${PYTHON} $IDF_PATH/tools/idf.py build || failure "build failed"
  518. export PATH="$OLDPATH"
  519. rm ./python
  520. print_status "Handling deprecated Kconfig options"
  521. idf.py clean > /dev/null
  522. rm -f sdkconfig.defaults
  523. rm -f sdkconfig
  524. echo "" > ${IDF_PATH}/sdkconfig.rename
  525. idf.py reconfigure > /dev/null
  526. echo "CONFIG_TEST_OLD_OPTION=y" >> sdkconfig
  527. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" >> ${IDF_PATH}/sdkconfig.rename
  528. echo -e "\n\
  529. menu \"test\"\n\
  530. config TEST_NEW_OPTION\n\
  531. bool \"test\"\n\
  532. default \"n\"\n\
  533. help\n\
  534. TEST_NEW_OPTION description\n\
  535. endmenu\n" >> ${IDF_PATH}/Kconfig
  536. idf.py reconfigure > /dev/null
  537. grep "CONFIG_TEST_OLD_OPTION=y" sdkconfig || failure "CONFIG_TEST_OLD_OPTION should be in sdkconfig for backward compatibility"
  538. grep "CONFIG_TEST_NEW_OPTION=y" sdkconfig || failure "CONFIG_TEST_NEW_OPTION should be now in sdkconfig"
  539. grep "#define CONFIG_TEST_NEW_OPTION 1" build/config/sdkconfig.h || failure "sdkconfig.h should contain the new macro"
  540. grep "#define CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" build/config/sdkconfig.h || failure "sdkconfig.h should contain the compatibility macro"
  541. grep "set(CONFIG_TEST_OLD_OPTION \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_OLD_OPTION should be in auto.conf for backward compatibility"
  542. grep "set(CONFIG_TEST_NEW_OPTION \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_NEW_OPTION should be now in auto.conf"
  543. rm -f sdkconfig sdkconfig.defaults
  544. pushd ${IDF_PATH}
  545. git checkout -- sdkconfig.rename Kconfig
  546. popd
  547. print_status "Handling deprecated Kconfig options in sdkconfig.defaults"
  548. idf.py clean
  549. rm -f sdkconfig
  550. echo "CONFIG_TEST_OLD_OPTION=7" > sdkconfig.defaults
  551. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" > ${IDF_PATH}/sdkconfig.rename
  552. echo -e "\n\
  553. menu \"test\"\n\
  554. config TEST_NEW_OPTION\n\
  555. int \"TEST_NEW_OPTION\"\n\
  556. range 0 10\n\
  557. default 5\n\
  558. help\n\
  559. TEST_NEW_OPTION description\n\
  560. endmenu\n" >> ${IDF_PATH}/Kconfig
  561. idf.py reconfigure > /dev/null
  562. grep "CONFIG_TEST_OLD_OPTION=7" sdkconfig || failure "CONFIG_TEST_OLD_OPTION=7 should be in sdkconfig for backward compatibility"
  563. grep "CONFIG_TEST_NEW_OPTION=7" sdkconfig || failure "CONFIG_TEST_NEW_OPTION=7 should be in sdkconfig"
  564. rm -f sdkconfig.defaults
  565. pushd ${IDF_PATH}
  566. git checkout -- sdkconfig.rename Kconfig
  567. popd
  568. print_status "Confserver can be invoked by idf.py"
  569. echo '{"version": 1}' | idf.py confserver || failure "Couldn't load confserver"
  570. print_status "Check ccache is used to build"
  571. touch ccache && chmod +x ccache # make sure that ccache is present for this test
  572. (export PATH=$PWD:$PATH && idf.py --ccache reconfigure | grep "ccache will be used") || failure "ccache should be used when --cache is specified"
  573. idf.py fullclean
  574. (export PATH=$PWD:$PATH && idf.py reconfigure| grep -c "ccache will be used" | grep -wq 0) \
  575. || failure "ccache should not be used even when present if --ccache is not specified"
  576. (export PATH=$PWD:$PATH && idf.py --no-ccache reconfigure| grep -c "ccache will be used" | grep -wq 0) \
  577. || failure "--no-ccache causes no issue for backward compatibility"
  578. rm -f ccache
  579. print_status "Custom bootloader overrides original"
  580. clean_build_dir
  581. (mkdir components && cd components && cp -r $IDF_PATH/components/bootloader .)
  582. idf.py bootloader
  583. grep "$PWD/components/bootloader/subproject/main/bootloader_start.c" build/bootloader/compile_commands.json \
  584. || failure "Custom bootloader source files should be built instead of the original's"
  585. rm -rf components
  586. print_status "Empty directory not treated as a component"
  587. clean_build_dir
  588. mkdir -p components/esp32 && idf.py reconfigure
  589. ! grep "$PWD/components/esp32" $PWD/build/project_description.json || failure "Failed to build with empty esp32 directory in components"
  590. rm -rf components
  591. print_status "If a component directory is added to COMPONENT_DIRS, its subdirectories are not added"
  592. clean_build_dir
  593. mkdir -p main/test
  594. echo "idf_component_register()" > main/test/CMakeLists.txt
  595. idf.py reconfigure
  596. ! grep "$PWD/main/test" $PWD/build/project_description.json || failure "COMPONENT_DIRS has added component subdirectory to the build"
  597. grep "$PWD/main" $PWD/build/project_description.json || failure "COMPONENT_DIRS parent component directory should be included in the build"
  598. rm -rf main/test
  599. print_status "If a component directory is added to COMPONENT_DIRS, its sibling directories are not added"
  600. clean_build_dir
  601. mkdir -p mycomponents/mycomponent
  602. echo "idf_component_register()" > mycomponents/mycomponent/CMakeLists.txt
  603. # first test by adding single component directory to EXTRA_COMPONENT_DIRS
  604. mkdir -p mycomponents/esp32
  605. echo "idf_component_register()" > mycomponents/esp32/CMakeLists.txt
  606. idf.py -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents/mycomponent reconfigure
  607. ! grep "$PWD/mycomponents/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  608. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  609. rm -rf mycomponents/esp32
  610. # now the same thing, but add a components directory
  611. mkdir -p esp32
  612. echo "idf_component_register()" > esp32/CMakeLists.txt
  613. idf.py -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents reconfigure
  614. ! grep "$PWD/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  615. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  616. rm -rf esp32
  617. rm -rf mycomponents
  618. # idf.py subcommand options, (using monitor with as example)
  619. print_status "Can set options to subcommands: print_filter for monitor"
  620. clean_build_dir
  621. mv ${IDF_PATH}/tools/idf_monitor.py ${IDF_PATH}/tools/idf_monitor.py.tmp
  622. echo "import sys;print(sys.argv[1:])" > ${IDF_PATH}/tools/idf_monitor.py
  623. idf.py build || failure "Failed to build project"
  624. 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)"
  625. mv ${IDF_PATH}/tools/idf_monitor.py.tmp ${IDF_PATH}/tools/idf_monitor.py
  626. print_status "Fail on build time works"
  627. clean_build_dir
  628. cp CMakeLists.txt CMakeLists.txt.bak
  629. printf "\nif(NOT EXISTS \"\${CMAKE_CURRENT_LIST_DIR}/hello.txt\") \nfail_at_build_time(test_file \"hello.txt does not exists\") \nendif()" >> CMakeLists.txt
  630. ! idf.py build || failure "Build should fail if requirements are not satisfied"
  631. touch hello.txt
  632. idf.py build || failure "Build succeeds once requirements are satisfied"
  633. rm -rf hello.txt CMakeLists.txt
  634. mv CMakeLists.txt.bak CMakeLists.txt
  635. rm -rf CMakeLists.txt.bak
  636. print_status "Component properties are set"
  637. clean_build_dir
  638. cp CMakeLists.txt CMakeLists.txt.bak
  639. printf "\nidf_component_get_property(srcs main SRCS)\nmessage(STATUS SRCS:\${srcs})" >> CMakeLists.txt
  640. (idf.py reconfigure | grep "SRCS:$(${REALPATH} main/main.c)") || failure "Component properties should be set"
  641. rm -rf CMakeLists.txt
  642. mv CMakeLists.txt.bak CMakeLists.txt
  643. rm -rf CMakeLists.txt.bak
  644. print_status "should be able to specify multiple sdkconfig default files"
  645. idf.py clean > /dev/null
  646. idf.py fullclean > /dev/null
  647. rm -f sdkconfig.defaults
  648. rm -f sdkconfig
  649. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults1
  650. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig.defaults2
  651. idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults1;sdkconfig.defaults2" reconfigure > /dev/null
  652. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults1 should be in sdkconfig"
  653. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig.defaults2 should be in sdkconfig"
  654. rm sdkconfig.defaults1 sdkconfig.defaults2 sdkconfig
  655. git checkout sdkconfig.defaults
  656. print_status "Supports git worktree"
  657. clean_build_dir
  658. git branch test_build_system
  659. git worktree add ../esp-idf-template-test test_build_system
  660. diff <(idf.py reconfigure | grep "App \"app-template\" version: ") <(cd ../esp-idf-template-test && idf.py reconfigure | grep "App \"app-template\" version: ") \
  661. || failure "Version on worktree should have been properly resolved"
  662. git worktree remove ../esp-idf-template-test
  663. print_status "idf.py fallback to build system target"
  664. clean_build_dir
  665. msg="Custom target is running"
  666. echo "" >> CMakeLists.txt
  667. echo "add_custom_target(custom_target COMMAND \${CMAKE_COMMAND} -E echo \"${msg}\")" >> CMakeLists.txt
  668. idf.py custom_target 1>log.txt || failure "Could not invoke idf.py with custom target"
  669. grep "${msg}" log.txt 1>/dev/null || failure "Custom target did not produce expected output"
  670. git checkout CMakeLists.txt
  671. rm -f log.txt
  672. print_status "Build fails if partitions don't fit in flash"
  673. clean_build_dir
  674. sed -i.bak "s/CONFIG_ESPTOOLPY_FLASHSIZE.\+//" sdkconfig # remove all flashsize config
  675. echo "CONFIG_ESPTOOLPY_FLASHSIZE_1MB=y" >> sdkconfig # introduce undersize flash
  676. ( 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"
  677. mv sdkconfig.bak sdkconfig
  678. print_status "Flash size is correctly set in the bootloader image header"
  679. # Build with the default 2MB setting
  680. rm sdkconfig
  681. idf.py bootloader || failure "Failed to build bootloader"
  682. bin_header_match build/bootloader/bootloader.bin "0210"
  683. # Change to 4MB
  684. sleep 1 # delay here to make sure sdkconfig modification time is different
  685. echo "CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y" > sdkconfig
  686. idf.py bootloader || failure "Failed to build bootloader"
  687. bin_header_match build/bootloader/bootloader.bin "0220"
  688. # Change to QIO, bootloader should still be DIO (will change to QIO in 2nd stage bootloader)
  689. sleep 1 # delay here to make sure sdkconfig modification time is different
  690. echo "CONFIG_FLASHMODE_QIO=y" > sdkconfig
  691. idf.py bootloader || failure "Failed to build bootloader"
  692. bin_header_match build/bootloader/bootloader.bin "0210"
  693. # Change to 80 MHz
  694. sleep 1 # delay here to make sure sdkconfig modification time is different
  695. echo "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" > sdkconfig
  696. idf.py bootloader || failure "Failed to build bootloader"
  697. bin_header_match build/bootloader/bootloader.bin "021f"
  698. rm sdkconfig
  699. print_status "DFU build works"
  700. rm -f -r build sdkconfig
  701. idf.py dfu &> tmp.log
  702. 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")
  703. idf.py set-target esp32s2
  704. idf.py dfu &> tmp.log
  705. 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")
  706. rm tmp.log
  707. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} "dfu.bin"
  708. rm -rf build sdkconfig
  709. print_status "UF2 build works"
  710. rm -f -r build sdkconfig
  711. idf.py uf2 &> tmp.log
  712. grep "build/uf2.bin\" has been written." tmp.log || (tail -n 100 tmp.log ; failure "UF2 build works for esp32")
  713. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} "uf2.bin"
  714. idf.py uf2-app &> tmp.log
  715. grep "build/uf2-app.bin\" has been written." tmp.log || (tail -n 100 tmp.log ; failure "UF2 build works for application binary")
  716. assert_built "uf2-app.bin"
  717. idf.py set-target esp32s2
  718. idf.py uf2 &> tmp.log
  719. grep "build/uf2.bin\" has been written." tmp.log || (tail -n 100 tmp.log ; failure "UF2 build works for esp32s2")
  720. rm tmp.log
  721. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} "uf2.bin"
  722. rm -rf build sdkconfig
  723. print_status "Loadable ELF build works"
  724. echo "CONFIG_APP_BUILD_TYPE_ELF_RAM=y" > sdkconfig
  725. idf.py reconfigure || failure "Couldn't configure for loadable ELF file"
  726. test -f build/flasher_args.json && failure "flasher_args.json should not be generated in a loadable ELF build"
  727. idf.py build || failure "Couldn't build a loadable ELF file"
  728. print_status "Defaults set properly for unspecified idf_build_process args"
  729. pushd $IDF_PATH/examples/build_system/cmake/idf_as_lib
  730. cp CMakeLists.txt CMakeLists.txt.bak
  731. echo -e "\nidf_build_get_property(project_dir PROJECT_DIR)" >> CMakeLists.txt
  732. echo -e "\nmessage(\"Project directory: \${project_dir}\")" >> CMakeLists.txt
  733. mkdir build && cd build
  734. cmake .. -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32 &> log.txt
  735. grep "Project directory: $IDF_PATH/examples/build_system/cmake/idf_as_lib" log.txt || failure "PROJECT_DIR default was not set"
  736. cd ..
  737. mv CMakeLists.txt.bak CMakeLists.txt
  738. rm -rf build
  739. popd
  740. print_status "Getting component overriden dir"
  741. clean_build_dir
  742. mkdir -p components/esp32
  743. echo "idf_component_get_property(overriden_dir \${COMPONENT_NAME} COMPONENT_OVERRIDEN_DIR)" >> components/esp32/CMakeLists.txt
  744. echo "message(STATUS overriden_dir:\${overriden_dir})" >> components/esp32/CMakeLists.txt
  745. (idf.py reconfigure | grep "overriden_dir:$IDF_PATH/components/esp32") || failure "Failed to get overriden dir" # no registration, overrides registration as well
  746. print_status "Overriding Kconfig"
  747. echo "idf_component_register(KCONFIG \${overriden_dir}/Kconfig)" >> components/esp32/CMakeLists.txt
  748. echo "idf_component_get_property(kconfig \${COMPONENT_NAME} KCONFIG)" >> components/esp32/CMakeLists.txt
  749. echo "message(STATUS kconfig:\${overriden_dir}/Kconfig)" >> components/esp32/CMakeLists.txt
  750. (idf.py reconfigure | grep "kconfig:$IDF_PATH/components/esp32/Kconfig") || failure "Failed to verify original `main` directory"
  751. rm -rf components
  752. print_status "Project components prioritized over EXTRA_COMPONENT_DIRS"
  753. clean_build_dir
  754. mkdir -p extra_dir/my_component
  755. echo "idf_component_register()" > extra_dir/my_component/CMakeLists.txt
  756. cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  757. ${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
  758. (idf.py reconfigure | grep "$PWD/extra_dir/my_component") || failure "Unable to find component specified in EXTRA_COMPONENT_DIRS"
  759. mkdir -p components/my_component
  760. echo "idf_component_register()" > components/my_component/CMakeLists.txt
  761. (idf.py reconfigure | grep "$PWD/components/my_component") || failure "Project components should be prioritized over EXTRA_COMPONENT_DIRS"
  762. mv CMakeLists.bak CMakeLists.txt # revert previous modifications
  763. rm -rf extra_dir components
  764. print_status "Create project using idf.py and build it"
  765. echo "Trying to create project."
  766. (idf.py -C projects create-project temp_test_project) || failure "Failed to create the project."
  767. cd "$IDF_PATH/projects/temp_test_project"
  768. echo "Building the project temp_test_project . . ."
  769. idf.py build || failure "Failed to build the project."
  770. cd "$IDF_PATH"
  771. rm -rf "$IDF_PATH/projects/temp_test_project"
  772. print_status "Create component using idf.py, create project using idf.py."
  773. print_status "Add the component to the created project and build the project."
  774. echo "Trying to create project . . ."
  775. (idf.py -C projects create-project temp_test_project) || failure "Failed to create the project."
  776. echo "Trying to create component . . ."
  777. (idf.py -C components create-component temp_test_component) || failure "Failed to create the component."
  778. ${SED} -i '5i\\tfunc();' "$IDF_PATH/projects/temp_test_project/main/temp_test_project.c"
  779. ${SED} -i '5i#include "temp_test_component.h"' "$IDF_PATH/projects/temp_test_project/main/temp_test_project.c"
  780. cd "$IDF_PATH/projects/temp_test_project"
  781. idf.py build || failure "Failed to build the project."
  782. cd "$IDF_PATH"
  783. rm -rf "$IDF_PATH/projects/temp_test_project"
  784. rm -rf "$IDF_PATH/components/temp_test_component"
  785. print_status "Check that command for creating new project will fail if the target folder is not empty."
  786. mkdir "$IDF_PATH/example_proj/"
  787. touch "$IDF_PATH/example_proj/tmp_130698"
  788. EXPECTED_EXIT_VALUE=3
  789. expected_failure $EXPECTED_EXIT_VALUE idf.py create-project --path "$IDF_PATH/example_proj/" temp_test_project || failure "Command exit value is wrong."
  790. rm -rf "$IDF_PATH/example_proj/"
  791. print_status "Check that command for creating new project will fail if the target path is file."
  792. touch "$IDF_PATH/example_proj"
  793. EXPECTED_EXIT_VALUE=4
  794. expected_failure $EXPECTED_EXIT_VALUE idf.py create-project --path "$IDF_PATH/example_proj" temp_test_project || failure "Command exit value is wrong."
  795. rm -rf "$IDF_PATH/example_proj"
  796. print_status "Check docs command"
  797. clean_build_dir
  798. idf.py build
  799. idf.py set-target esp32s2
  800. idf.py docs || failure "'idf.py docs' failed"
  801. idf.py docs --no-browser | grep "https://docs.espressif.com/projects/esp-idf/en" || failure "'idf.py docs --no-browser' failed"
  802. 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"
  803. 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"
  804. 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"
  805. 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"
  806. print_status "Deprecation warning check"
  807. cd ${TESTDIR}/template
  808. # click warning
  809. idf.py post_debug &> tmp.log
  810. grep "Warning: Command \"post_debug\" is deprecated and will be removed in v5.0." tmp.log || failure "Missing deprecation warning with command \"post_debug\""
  811. rm tmp.log
  812. # cmake warning
  813. idf.py efuse_common_table &> tmp.log
  814. 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\""
  815. rm tmp.log
  816. print_status "Save-defconfig checks"
  817. cd ${TESTDIR}/template
  818. rm -f sdkconfig.defaults
  819. rm -f sdkconfig
  820. idf.py fullclean > /dev/null
  821. echo "CONFIG_COMPILER_OPTIMIZATION_SIZE=y" >> sdkconfig
  822. echo "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" >> sdkconfig
  823. idf.py save-defconfig
  824. wc -l sdkconfig.defaults
  825. grep "CONFIG_IDF_TARGET" sdkconfig.defaults && failure "CONFIG_IDF_TARGET should not be in sdkconfig.defaults"
  826. grep "CONFIG_COMPILER_OPTIMIZATION_SIZE=y" sdkconfig.defaults || failure "Missing CONFIG_COMPILER_OPTIMIZATION_SIZE=y in sdkconfig.defaults"
  827. grep "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" sdkconfig.defaults || failure "Missing CONFIG_ESPTOOLPY_FLASHFREQ_80M=y in sdkconfig.defaults"
  828. idf.py fullclean > /dev/null
  829. rm -f sdkconfig.defaults
  830. rm -f sdkconfig
  831. idf.py set-target esp32c3
  832. echo "CONFIG_PARTITION_TABLE_OFFSET=0x8001" >> sdkconfig
  833. idf.py save-defconfig
  834. wc -l sdkconfig.defaults
  835. grep "CONFIG_IDF_TARGET=\"esp32c3\"" sdkconfig.defaults || failure "Missing CONFIG_IDF_TARGET=\"esp32c3\" in sdkconfig.defaults"
  836. grep "CONFIG_PARTITION_TABLE_OFFSET=0x8001" sdkconfig.defaults || failure "Missing CONFIG_PARTITION_TABLE_OFFSET=0x8001 in sdkconfig.defaults"
  837. idf.py fullclean > /dev/null
  838. rm -f sdkconfig.defaults
  839. rm -f sdkconfig
  840. print_status "All tests completed"
  841. if [ -n "${FAILURES}" ]; then
  842. echo "Some failures were detected:"
  843. echo -e "${FAILURES}"
  844. exit 1
  845. else
  846. echo "Build tests passed."
  847. fi
  848. }
  849. function print_status()
  850. {
  851. echo "******** $1"
  852. STATUS="$1"
  853. }
  854. function failure()
  855. {
  856. echo "!!!!!!!!!!!!!!!!!!!"
  857. echo "FAILURE: $1"
  858. echo "!!!!!!!!!!!!!!!!!!!"
  859. FAILURES="${FAILURES}${STATUS} :: $1\n"
  860. }
  861. function expected_failure() {
  862. "${@:2}"
  863. EXIT_VALUE=$?
  864. if [ $EXIT_VALUE != "$1" ]; then
  865. echo "[ERROR] Exit value of executed command is $EXIT_VALUE (expected $1)"; return 1
  866. else return 0
  867. fi
  868. }
  869. TESTDIR=${PWD}/build_system_tests_$$
  870. mkdir -p ${TESTDIR}
  871. # set NOCLEANUP=1 if you want to keep the test directory around
  872. # for post-mortem debugging
  873. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  874. SNAPSHOT=${TESTDIR}/snapshot
  875. BUILD=${TESTDIR}/template/build
  876. IS_DARWIN=
  877. export SED=sed
  878. export REALPATH=realpath
  879. if [ "$(uname -s)" = "Darwin" ]; then
  880. IS_DARWIN=1
  881. export SED=gsed
  882. export REALPATH=grealpath
  883. fi
  884. # copy all the build output to a snapshot directory
  885. function take_build_snapshot()
  886. {
  887. rm -rf ${SNAPSHOT}
  888. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  889. if [ -n "$IS_DARWIN" ]; then
  890. # wait at least 1 second before the next build, for the test in
  891. # file_was_rebuilt to work
  892. sleep 1
  893. fi
  894. }
  895. # verify that all the arguments are present in the build output directory
  896. function assert_built()
  897. {
  898. until [ -z "$1" ]; do
  899. if [ ! -f "${BUILD}/$1" ]; then
  900. failure "File $1 should be in the build output directory"
  901. fi
  902. shift
  903. done
  904. }
  905. # Test if a file has been rebuilt.
  906. function file_was_rebuilt()
  907. {
  908. if [ -z "$IS_DARWIN" ]; then
  909. # can't use [ a -ot b ] here as -ot only gives second resolution
  910. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  911. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  912. return 0
  913. else
  914. return 1
  915. fi
  916. else
  917. # macOS: work around 1-second resolution by adding a sleep in take_build_snapshot
  918. if [ ${SNAPSHOT}/$1 -ot ${BUILD}/$1 ]; then
  919. return 0
  920. else
  921. return 1
  922. fi
  923. fi
  924. }
  925. # verify all the arguments passed in were rebuilt relative to the snapshot
  926. function assert_rebuilt()
  927. {
  928. until [ -z "$1" ]; do
  929. assert_built "$1"
  930. if [ ! -f "${SNAPSHOT}/$1" ]; then
  931. failure "File $1 should be in original build snapshot"
  932. fi
  933. if ! file_was_rebuilt "$1"; then
  934. failure "File $1 should have been rebuilt"
  935. fi
  936. shift
  937. done
  938. }
  939. # verify all the arguments are in the build directory & snapshot,
  940. # but were not rebuilt
  941. function assert_not_rebuilt()
  942. {
  943. until [ -z "$1" ]; do
  944. assert_built "$1"
  945. if [ ! -f "${SNAPSHOT}/$1" ]; then
  946. failure "File $1 should be in snapshot build directory"
  947. fi
  948. if file_was_rebuilt "$1"; then
  949. failure "File $1 should not have been rebuilt"
  950. fi
  951. shift
  952. done
  953. }
  954. # do a "clean" that doesn't depend on idf.py
  955. function clean_build_dir()
  956. {
  957. PRESERVE_ROOT_ARG=
  958. if [ -z "$IS_DARWIN" ]; then
  959. PRESERVE_ROOT_ARG=--preserve-root
  960. fi
  961. rm -rf $PRESERVE_ROOT_ARG ${BUILD}/* ${BUILD}/.*
  962. }
  963. # check the bytes 3-4 of the binary image header. e.g.:
  964. # bin_header_match app.bin 0210
  965. function bin_header_match()
  966. {
  967. expected=$2
  968. filename=$1
  969. actual=$(xxd -s 2 -l 2 -ps $1)
  970. if [ ! "$expected" = "$actual" ]; then
  971. failure "Incorrect binary image header, expected $expected got $actual"
  972. fi
  973. }
  974. cd ${TESTDIR}
  975. run_tests