test_build_system_cmake.sh 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #!/bin/bash
  2. #
  3. # Test the build system for basic consistency (Cmake/idf.py version)
  4. #
  5. # A bash script that tests some likely build failure scenarios in a row
  6. #
  7. # Assumes PWD is an out-of-tree build directory, and will create a
  8. # subdirectory inside it to run build tests in.
  9. #
  10. # Environment variables:
  11. # IDF_PATH - must be set
  12. # ESP_IDF_TEMPLATE_GIT - Can override git clone source for template app. Otherwise github.
  13. # NOCLEANUP - Set to '1' if you want the script to leave its temporary directory when done, for post-mortem.
  14. #
  15. #
  16. # Internals:
  17. # * The tests run in sequence & the system keeps track of all failures to print at the end.
  18. # * BUILD directory is set to default BUILD_DIR_BASE
  19. # * The "print_status" function both prints a status line to the log and keeps track of which test is running.
  20. # * Calling the "failure" function prints a failure message to the log and also adds to the list of failures to print at the end.
  21. # * The function "assert_built" tests for a file relative to the BUILD directory.
  22. # * The function "take_build_snapshot" can be paired with the functions "assert_rebuilt" and "assert_not_rebuilt" to compare file timestamps and verify if they were rebuilt or not since the snapshot was taken.
  23. #
  24. # To add a new test case, add it to the end of the run_tests function. Note that not all test cases do comprehensive cleanup
  25. # (although very invasive ones like appending CRLFs to all files take a copy of the esp-idf tree), however the clean_build_dir
  26. # function can be used to force-delete all files from the build output directory.
  27. # Set up some variables
  28. #
  29. # override ESP_IDF_TEMPLATE_GIT to point to a local dir if you're testing and want fast iterations
  30. [ -z ${ESP_IDF_TEMPLATE_GIT} ] && ESP_IDF_TEMPLATE_GIT=https://github.com/espressif/esp-idf-template.git
  31. # uncomment next line to produce a lot more debug output
  32. #export V=1
  33. export PATH="$IDF_PATH/tools:$PATH" # for idf.py
  34. function run_tests()
  35. {
  36. FAILURES=
  37. STATUS="Starting"
  38. print_status "Checking prerequisites"
  39. [ -z ${IDF_PATH} ] && echo "IDF_PATH is not set. Need path to esp-idf installation." && exit 2
  40. print_status "Cloning template from ${ESP_IDF_TEMPLATE_GIT}..."
  41. git clone ${ESP_IDF_TEMPLATE_GIT} template
  42. cd template
  43. if [ -z $CHECKOUT_REF_SCRIPT ]; then
  44. git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
  45. else
  46. $CHECKOUT_REF_SCRIPT esp-idf-template .
  47. fi
  48. print_status "Try to clean fresh directory..."
  49. idf.py fullclean || exit $?
  50. # all relative to the build directory
  51. BOOTLOADER_BINS="bootloader/bootloader.elf bootloader/bootloader.bin"
  52. APP_BINS="app-template.elf app-template.bin"
  53. PARTITION_BIN="partition_table/partition-table.bin"
  54. PHY_INIT_BIN="phy_init_data.bin"
  55. BUILD_ARTIFACTS="project_description.json flasher_args.json config/kconfig_menus.json config/sdkconfig.json"
  56. IDF_COMPONENT_PREFIX="__idf"
  57. print_status "Initial clean build"
  58. # if build fails here, everything fails
  59. idf.py build || exit $?
  60. # check all the expected build artifacts from the clean build
  61. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} ${BUILD_ARTIFACTS}
  62. print_status "Updating component source file rebuilds component"
  63. # touch a file & do a build
  64. take_build_snapshot
  65. touch ${IDF_PATH}/components/esp32/cpu_start.c
  66. idf.py build || failure "Failed to partial build"
  67. assert_rebuilt ${APP_BINS} esp-idf/esp32/libesp32.a esp-idf/esp32/CMakeFiles/${IDF_COMPONENT_PREFIX}_esp32.dir/cpu_start.c.obj
  68. assert_not_rebuilt esp-idf/lwip/liblwip.a esp-idf/freertos/libfreertos.a ${BOOTLOADER_BINS} ${PARTITION_BIN}
  69. print_status "Bootloader source file rebuilds bootloader"
  70. take_build_snapshot
  71. touch ${IDF_PATH}/components/bootloader/subproject/main/bootloader_start.c
  72. idf.py build || failure "Failed to partial build bootloader"
  73. assert_rebuilt ${BOOTLOADER_BINS} bootloader/esp-idf/main/CMakeFiles/${IDF_COMPONENT_PREFIX}_main.dir/bootloader_start.c.obj
  74. assert_not_rebuilt ${APP_BINS} ${PARTITION_BIN}
  75. print_status "Partition CSV file rebuilds partitions"
  76. take_build_snapshot
  77. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  78. idf.py build || failure "Failed to build partition table"
  79. assert_rebuilt ${PARTITION_BIN}
  80. assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
  81. print_status "Partial build doesn't compile anything by default"
  82. take_build_snapshot
  83. # verify no build files are refreshed by a partial make
  84. ALL_BUILD_FILES=$(find ${BUILD} -type f | sed "s@${BUILD}/@@" | grep -v '^.')
  85. idf.py build || failure "Partial build failed"
  86. assert_not_rebuilt ${ALL_BUILD_FILES}
  87. print_status "Rebuild when app version was changed"
  88. clean_build_dir
  89. # App version
  90. echo "IDF_VER_0123456789_0123456789_0123456789" > ${IDF_PATH}/version.txt
  91. echo "project-version-1.0" > ${TESTDIR}/template/version.txt
  92. idf.py build || failure "Failed to build with app version"
  93. print_status "Change app version"
  94. take_build_snapshot
  95. echo "project-version-2.0(012345678901234567890123456789)" > ${TESTDIR}/template/version.txt
  96. idf.py build || failure "Failed to rebuild with changed app version"
  97. assert_rebuilt ${APP_BINS}
  98. assert_not_rebuilt ${BOOTLOADER_BINS} esp-idf/esp32/libesp32.a
  99. print_status "Re-building does not change app.bin"
  100. take_build_snapshot
  101. idf.py build
  102. assert_not_rebuilt ${APP_BINS} ${BOOTLOADER_BINS} esp-idf/esp32/libesp32.a
  103. rm -f ${IDF_PATH}/version.txt
  104. rm -f ${TESTDIR}/template/version.txt
  105. print_status "Get the version of app from git describe. Project is not inside IDF and do not have a tag only a hash commit."
  106. idf.py build >> log.log || failure "Failed to build"
  107. version="Project version: "
  108. version+=$(git describe --always --tags --dirty)
  109. grep "${version}" log.log || failure "Project version should have a hash commit"
  110. print_status "Can set COMPONENT_SRCS with spaces"
  111. clean_build_dir
  112. touch main/main2.c
  113. sed -i 's/^set(COMPONENT_SRCS.*/set(COMPONENT_SRCS "main.c main2.c")/' main/CMakeLists.txt
  114. idf.py build || failure "Set COMPONENT_SRCS with spaces build failed"
  115. git checkout -- main/CMakeLists.txt
  116. rm main/main2.c
  117. print_status "Use IDF version variables in component CMakeLists.txt file"
  118. clean_build_dir
  119. (echo -e "if (NOT IDF_VERSION_MAJOR)\n message(FATAL_ERROR \"IDF version not set\")\n endif()" \
  120. && cat main/CMakeLists.txt) > main/CMakeLists.new && mv main/CMakeLists.new main/CMakeLists.txt
  121. idf.py reconfigure || failure "Failed to use IDF_VERSION_MAJOR in component CMakeLists.txt"
  122. git checkout -- main/CMakeLists.txt
  123. print_status "Moving BUILD_DIR_BASE out of tree"
  124. clean_build_dir
  125. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  126. idf.py -B "${OUTOFTREE_BUILD}" build || failure "Failed to build with out-of-tree build dir"
  127. NEW_BUILD_FILES=$(find ${OUTOFREE_BUILD} -type f)
  128. if [ -z "${NEW_BUILD_FILES}" ]; then
  129. failure "No files found in new build directory!"
  130. fi
  131. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  132. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  133. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  134. fi
  135. print_status "BUILD_DIR_BASE inside default build directory"
  136. clean_build_dir
  137. idf.py -B "build/subdirectory" build || failure "Failed to build with build dir as subdir"
  138. NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
  139. if [ -z "${NEW_BUILD_FILES}" ]; then
  140. failure "No files found in new build directory!"
  141. fi
  142. print_status "Can still clean build if all text files are CRLFs"
  143. clean_build_dir
  144. find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
  145. # make a copy of esp-idf and CRLFify it
  146. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  147. mkdir -p ${CRLF_ESPIDF}
  148. cp -r ${IDF_PATH}/* ${CRLF_ESPIDF}
  149. # don't CRLFify executable files, as Linux will fail to execute them
  150. find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
  151. IDF_PATH=${CRLF_ESPIDF} idf.py build || failure "Failed to build with CRLFs in source"
  152. # do the same checks we do for the clean build
  153. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  154. print_status "Updating rom ld file should re-link app and bootloader"
  155. clean_build_dir
  156. idf.py build
  157. take_build_snapshot
  158. sleep 1 # ninja may ignore if the timestamp delta is too low
  159. cp ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld .
  160. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld
  161. tail ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld
  162. idf.py build || failure "Failed to rebuild with modified linker script"
  163. assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
  164. mv esp32.rom.ld ${IDF_PATH}/components/esp_rom/esp32/ld/
  165. print_status "Updating app-only ld file should only re-link app"
  166. take_build_snapshot
  167. cp ${IDF_PATH}/components/esp32/ld/esp32.project.ld.in .
  168. sleep 1 # ninja may ignore if the timestamp delta is too low
  169. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp32/ld/esp32.project.ld.in
  170. idf.py build || failure "Failed to rebuild with modified linker script"
  171. assert_rebuilt ${APP_BINS}
  172. assert_not_rebuilt ${BOOTLOADER_BINS}
  173. mv esp32.project.ld.in ${IDF_PATH}/components/esp32/ld/
  174. print_status "Updating fragment file should only re-link app" # only app linker script is generated by tool for now
  175. take_build_snapshot
  176. cp ${IDF_PATH}/components/esp32/ld/esp32_fragments.lf .
  177. sleep 1 # ninja may ignore if the timestamp delta is too low
  178. echo "# (Build test comment)" >> ${IDF_PATH}/components/esp32/ld/esp32_fragments.lf
  179. idf.py build || failure "Failed to rebuild with modified linker fragment file"
  180. assert_rebuilt ${APP_BINS}
  181. assert_not_rebuilt ${BOOTLOADER_BINS}
  182. mv esp32_fragments.lf ${IDF_PATH}/components/esp32/ld/
  183. print_status "sdkconfig update triggers full recompile"
  184. clean_build_dir
  185. idf.py build
  186. take_build_snapshot
  187. # need to actually change config, or cmake is too smart to rebuild
  188. sed -i.bak s/^\#\ CONFIG_FREERTOS_UNICORE\ is\ not\ set/CONFIG_FREERTOS_UNICORE=y/ sdkconfig
  189. idf.py build
  190. # check the sdkconfig.h file was rebuilt
  191. assert_rebuilt config/sdkconfig.h
  192. # pick one each of .c, .cpp, .S that #includes sdkconfig.h
  193. # and therefore should rebuild
  194. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/syscall_table.c.obj
  195. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  196. assert_rebuilt esp-idf/freertos/CMakeFiles/${IDF_COMPONENT_PREFIX}_freertos.dir/xtensa_vectors.S.obj
  197. mv sdkconfig.bak sdkconfig
  198. print_status "Updating project CMakeLists.txt triggers full recompile"
  199. clean_build_dir
  200. idf.py build
  201. take_build_snapshot
  202. # Need to actually change the build config, or CMake won't do anything
  203. cp CMakeLists.txt CMakeLists.bak
  204. sed -i.bak 's/^project(/add_compile_options("-DUSELESS_MACRO_DOES_NOTHING=1")\nproject\(/' CMakeLists.txt
  205. idf.py build || failure "Build failed"
  206. mv CMakeLists.bak CMakeLists.txt
  207. # similar to previous test
  208. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/syscall_table.c.obj
  209. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  210. assert_rebuilt esp-idf/freertos/CMakeFiles/${IDF_COMPONENT_PREFIX}_freertos.dir/xtensa_vectors.S.obj
  211. mv sdkconfig.bak sdkconfig
  212. print_status "Can build with Ninja (no idf.py)"
  213. clean_build_dir
  214. (cd build && cmake -G Ninja .. && ninja) || failure "Ninja build failed"
  215. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  216. print_status "Can build with GNU Make (no idf.py)"
  217. clean_build_dir
  218. mkdir build
  219. (cd build && cmake -G "Unix Makefiles" .. && make) || failure "Make build failed"
  220. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  221. print_status "Can build with IDF_PATH set via cmake cache not environment"
  222. clean_build_dir
  223. sed -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  224. export IDF_PATH_BACKUP="$IDF_PATH"
  225. (unset IDF_PATH &&
  226. cd build &&
  227. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  228. ninja) || failure "Ninja build failed"
  229. mv CMakeLists.txt.bak CMakeLists.txt
  230. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  231. print_status "Can build with IDF_PATH unset and inferred by build system"
  232. clean_build_dir
  233. sed -i.bak "s%\$ENV{IDF_PATH}%\${ci_idf_path}%" CMakeLists.txt # expand to a hardcoded path
  234. (ci_idf_path=${IDF_PATH} && unset IDF_PATH && cd build &&
  235. cmake -G Ninja -D ci_idf_path=${ci_idf_path} .. && ninja) || failure "Ninja build failed"
  236. mv CMakeLists.txt.bak CMakeLists.txt
  237. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  238. print_status "Can build with IDF_PATH unset and inferred by cmake when Kconfig needs it to be set"
  239. clean_build_dir
  240. sed -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  241. export IDF_PATH_BACKUP="$IDF_PATH"
  242. mv main/Kconfig.projbuild main/Kconfig.projbuild_bak
  243. echo "source \"\$IDF_PATH/examples/wifi/getting_started/station/main/Kconfig.projbuild\"" > main/Kconfig.projbuild
  244. (unset IDF_PATH &&
  245. cd build &&
  246. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  247. ninja) || failure "Ninja build failed"
  248. mv CMakeLists.txt.bak CMakeLists.txt
  249. mv main/Kconfig.projbuild_bak main/Kconfig.projbuild
  250. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  251. # Next two tests will use this fake 'esp31b' target
  252. export fake_target=esp31b
  253. mkdir -p components/$fake_target
  254. mkdir -p ${IDF_PATH}/components/xtensa/$fake_target/include
  255. touch components/$fake_target/CMakeLists.txt
  256. cp ${IDF_PATH}/tools/cmake/toolchain-esp32.cmake components/$fake_target/toolchain-$fake_target.cmake
  257. sed -i.bak '/cmake_minimum_required/ a\
  258. set(COMPONENTS esptool_py)' CMakeLists.txt
  259. print_status "Can override IDF_TARGET from environment"
  260. clean_build_dir
  261. rm sdkconfig
  262. export IDF_TARGET=$fake_target
  263. (cd build && cmake -G Ninja .. ) || failure "Failed to configure with IDF_TARGET set in environment"
  264. grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured for IDF_TARGET correctly"
  265. grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt"
  266. unset IDF_TARGET
  267. print_status "Can set target using idf.py -D"
  268. clean_build_dir
  269. rm sdkconfig
  270. idf.py -DIDF_TARGET=$fake_target reconfigure || failure "Failed to set target via idf.py"
  271. grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured correctly using idf.py -D"
  272. grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py -D"
  273. print_status "Can set target using -D as subcommand parameter for idf.py"
  274. clean_build_dir
  275. rm sdkconfig
  276. idf.py reconfigure -DIDF_TARGET=$fake_target || failure "Failed to set target via idf.py subcommand -D parameter"
  277. grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured correctly using idf.py reconfigure -D"
  278. grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py reconfigure -D"
  279. # Clean up modifications for the fake target
  280. mv CMakeLists.txt.bak CMakeLists.txt
  281. rm -rf components
  282. print_status "Can find toolchain file in component directory"
  283. clean_build_dir
  284. mv ${IDF_PATH}/tools/cmake/toolchain-esp32.cmake ${IDF_PATH}/components/esp32/
  285. idf.py build || failure "Failed to build with toolchain file in component directory"
  286. mv ${IDF_PATH}/components/esp32/toolchain-esp32.cmake ${IDF_PATH}/tools/cmake/
  287. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  288. print_status "Can build with auto generated CMakeLists.txt"
  289. clean_build_dir
  290. mv CMakeLists.txt CMakeLists.bak
  291. ${IDF_PATH}/tools/cmake/convert_to_cmake.py .
  292. idf.py build || failure "Auto generated CMakeLists.txt build failed"
  293. mv CMakeLists.bak CMakeLists.txt
  294. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  295. print_status "Setting EXTRA_COMPONENT_DIRS works"
  296. clean_build_dir
  297. mkdir -p main/main/main # move main component contents to another directory
  298. mv main/* main/main/main
  299. cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  300. 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
  301. idf.py build || failure "Build with EXTRA_COMPONENT_DIRS set failed"
  302. mv CMakeLists.bak CMakeLists.txt # revert previous modifications
  303. mv main/main/main/* main
  304. rm -rf main/main
  305. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  306. print_status "sdkconfig should have contents both files: sdkconfig and sdkconfig.defaults"
  307. idf.py clean > /dev/null;
  308. idf.py fullclean > /dev/null;
  309. rm -f sdkconfig.defaults;
  310. rm -f sdkconfig;
  311. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults;
  312. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig;
  313. idf.py reconfigure > /dev/null;
  314. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults should be into sdkconfig"
  315. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig should be into sdkconfig"
  316. rm sdkconfig;
  317. rm sdkconfig.defaults;
  318. print_status "can build with phy_init_data"
  319. idf.py clean > /dev/null;
  320. idf.py fullclean > /dev/null;
  321. rm -f sdkconfig.defaults;
  322. rm -f sdkconfig;
  323. echo "CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION=y" >> sdkconfig.defaults;
  324. idf.py reconfigure > /dev/null;
  325. idf.py build || failure "Failed to build with PHY_INIT_DATA"
  326. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN} ${PHY_INIT_BIN}
  327. rm sdkconfig;
  328. rm sdkconfig.defaults;
  329. print_status "can build with ethernet component disabled"
  330. idf.py clean > /dev/null;
  331. idf.py fullclean > /dev/null;
  332. rm -f sdkconfig.defaults;
  333. rm -f sdkconfig;
  334. echo "CONFIG_ETH_USE_SPI_ETHERNET=" >> sdkconfig.defaults;
  335. echo "CONFIG_ETH_USE_ESP32_EMAC=" >> sdkconfig.defaults;
  336. idf.py reconfigure > /dev/null;
  337. idf.py build || failure "Failed to build with ethernet component disabled"
  338. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  339. rm sdkconfig;
  340. rm sdkconfig.defaults;
  341. print_status "Building a project with CMake library imported and PSRAM workaround, all files compile with workaround"
  342. # Test for libraries compiled within ESP-IDF
  343. rm -r build sdkconfig
  344. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" >> sdkconfig.defaults
  345. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  346. # note: we do 'reconfigure' here, as we just need to run cmake
  347. idf.py -C $IDF_PATH/examples/build_system/cmake/import_lib -B `pwd`/build -D SDKCONFIG_DEFAULTS="`pwd`/sdkconfig.defaults" reconfigure
  348. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  349. (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"
  350. rm -r build sdkconfig sdkconfig.defaults
  351. print_status "Test for external libraries in custom CMake projects with ESP-IDF components linked"
  352. mkdir build
  353. IDF_AS_LIB=$IDF_PATH/examples/build_system/cmake/idf_as_lib
  354. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" > $IDF_AS_LIB/sdkconfig
  355. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> $IDF_AS_LIB/sdkconfig
  356. # note: we just need to run cmake
  357. (cd build && cmake $IDF_AS_LIB -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32)
  358. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  359. (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"
  360. for strat in MEMW NOPS DUPLDST; do
  361. print_status "Test for external libraries in custom CMake projects with PSRAM strategy $strat"
  362. rm -r build sdkconfig sdkconfig.defaults sdkconfig.defaults.esp32
  363. stratlc=`echo $strat | tr A-Z a-z`
  364. echo "CONFIG_ESP32_SPIRAM_SUPPORT=y" > sdkconfig.defaults
  365. echo "CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_$strat=y" >> sdkconfig.defaults
  366. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  367. # note: we do 'reconfigure' here, as we just need to run cmake
  368. idf.py reconfigure
  369. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  370. (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"
  371. echo ${PWD}
  372. rm -r sdkconfig.defaults build
  373. done
  374. print_status "Cleaning Python bytecode"
  375. idf.py clean > /dev/null
  376. idf.py fullclean > /dev/null
  377. if [ "$(find $IDF_PATH -name "*.py[co]" | wc -l)" -eq 0 ]; then
  378. failure "No Python bytecode in IDF!"
  379. fi
  380. idf.py python-clean
  381. if [ "$(find $IDF_PATH -name "*.py[co]" | wc -l)" -gt 0 ]; then
  382. failure "Python bytecode isn't working!"
  383. fi
  384. print_status "Displays partition table when executing target partition_table"
  385. idf.py partition_table | grep -E "# Espressif .+ Partition Table"
  386. rm -r build
  387. print_status "Make sure a full build never runs '/usr/bin/env python' or similar"
  388. OLDPATH="$PATH"
  389. PYTHON="$(which python)"
  390. rm -rf build
  391. cat > ./python << EOF
  392. #!/bin/sh
  393. echo "The build system has executed '/usr/bin/env python' or similar"
  394. exit 1
  395. EOF
  396. chmod +x ./python
  397. export PATH="$(pwd):$PATH"
  398. ${PYTHON} $IDF_PATH/tools/idf.py build || failure "build failed"
  399. export PATH="$OLDPATH"
  400. rm ./python
  401. print_status "Handling deprecated Kconfig options"
  402. idf.py clean > /dev/null;
  403. rm -f sdkconfig.defaults;
  404. rm -f sdkconfig;
  405. echo "" > ${IDF_PATH}/sdkconfig.rename;
  406. idf.py build > /dev/null;
  407. echo "CONFIG_TEST_OLD_OPTION=y" >> sdkconfig;
  408. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" >> ${IDF_PATH}/sdkconfig.rename;
  409. echo -e "\n\
  410. menu \"test\"\n\
  411. config TEST_NEW_OPTION\n\
  412. bool \"test\"\n\
  413. default \"n\"\n\
  414. help\n\
  415. TEST_NEW_OPTION description\n\
  416. endmenu\n" >> ${IDF_PATH}/Kconfig;
  417. idf.py build > /dev/null;
  418. grep "CONFIG_TEST_OLD_OPTION=y" sdkconfig || failure "CONFIG_TEST_OLD_OPTION should be in sdkconfig for backward compatibility"
  419. grep "CONFIG_TEST_NEW_OPTION=y" sdkconfig || failure "CONFIG_TEST_NEW_OPTION should be now in sdkconfig"
  420. grep "#define CONFIG_TEST_NEW_OPTION 1" build/config/sdkconfig.h || failure "sdkconfig.h should contain the new macro"
  421. grep "#define CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" build/config/sdkconfig.h || failure "sdkconfig.h should contain the compatibility macro"
  422. grep "set(CONFIG_TEST_OLD_OPTION \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_OLD_OPTION should be in auto.conf for backward compatibility"
  423. grep "set(CONFIG_TEST_NEW_OPTION \"y\")" build/config/sdkconfig.cmake || failure "CONFIG_TEST_NEW_OPTION should be now in auto.conf"
  424. rm -f sdkconfig sdkconfig.defaults
  425. pushd ${IDF_PATH}
  426. git checkout -- sdkconfig.rename Kconfig
  427. popd
  428. print_status "Handling deprecated Kconfig options in sdkconfig.defaults"
  429. idf.py clean;
  430. rm -f sdkconfig;
  431. echo "CONFIG_TEST_OLD_OPTION=7" > sdkconfig.defaults;
  432. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" > ${IDF_PATH}/sdkconfig.rename;
  433. echo -e "\n\
  434. menu \"test\"\n\
  435. config TEST_NEW_OPTION\n\
  436. int \"TEST_NEW_OPTION\"\n\
  437. range 0 10\n\
  438. default 5\n\
  439. help\n\
  440. TEST_NEW_OPTION description\n\
  441. endmenu\n" >> ${IDF_PATH}/Kconfig;
  442. idf.py build > /dev/null;
  443. grep "CONFIG_TEST_OLD_OPTION=7" sdkconfig || failure "CONFIG_TEST_OLD_OPTION=7 should be in sdkconfig for backward compatibility"
  444. grep "CONFIG_TEST_NEW_OPTION=7" sdkconfig || failure "CONFIG_TEST_NEW_OPTION=7 should be in sdkconfig"
  445. rm -f sdkconfig.defaults;
  446. pushd ${IDF_PATH}
  447. git checkout -- sdkconfig.rename Kconfig
  448. popd
  449. print_status "Confserver can be invoked by idf.py"
  450. echo '{"version": 1}' | idf.py confserver || failure "Couldn't load confserver"
  451. print_status "Check ccache is used to build"
  452. touch ccache && chmod +x ccache # make sure that ccache is present for this test
  453. (export PATH=$PWD:$PATH && idf.py --ccache reconfigure | grep "ccache will be used") || failure "ccache should be used when --cache is specified"
  454. idf.py fullclean
  455. (export PATH=$PWD:$PATH && idf.py reconfigure| grep -c "ccache will be used" | grep -wq 0) \
  456. || failure "ccache should not be used even when present if --ccache is not specified"
  457. (export PATH=$PWD:$PATH && idf.py --no-ccache reconfigure| grep -c "ccache will be used" | grep -wq 0) \
  458. || failure "--no-ccache causes no issue for backward compatibility"
  459. rm -f ccache
  460. print_status "Custom bootloader overrides original"
  461. clean_build_dir
  462. (mkdir components && cd components && cp -r $IDF_PATH/components/bootloader .)
  463. idf.py build
  464. grep "$PWD/components/bootloader/subproject/main/bootloader_start.c" build/bootloader/compile_commands.json \
  465. || failure "Custom bootloader source files should be built instead of the original's"
  466. rm -rf components
  467. print_status "Empty directory not treated as a component"
  468. clean_build_dir
  469. mkdir -p components/esp32 && idf.py reconfigure
  470. ! grep "$PWD/components/esp32" $PWD/build/project_description.json || failure "Failed to build with empty esp32 directory in components"
  471. rm -rf components
  472. print_status "If a component directory is added to COMPONENT_DIRS, its subdirectories are not added"
  473. clean_build_dir
  474. mkdir -p main/test
  475. echo "idf_component_register()" > main/test/CMakeLists.txt
  476. idf.py reconfigure
  477. ! grep "$PWD/main/test" $PWD/build/project_description.json || failure "COMPONENT_DIRS has added component subdirectory to the build"
  478. grep "$PWD/main" $PWD/build/project_description.json || failure "COMPONENT_DIRS parent component directory should be included in the build"
  479. rm -rf main/test
  480. print_status "If a component directory is added to COMPONENT_DIRS, its sibling directories are not added"
  481. clean_build_dir
  482. mkdir -p mycomponents/mycomponent
  483. echo "idf_component_register()" > mycomponents/mycomponent/CMakeLists.txt
  484. # first test by adding single component directory to EXTRA_COMPONENT_DIRS
  485. mkdir -p mycomponents/esp32
  486. echo "idf_component_register()" > mycomponents/esp32/CMakeLists.txt
  487. idf.py -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents/mycomponent reconfigure
  488. ! grep "$PWD/mycomponents/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  489. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  490. rm -rf mycomponents/esp32
  491. # now the same thing, but add a components directory
  492. mkdir -p esp32
  493. echo "idf_component_register()" > esp32/CMakeLists.txt
  494. idf.py -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents reconfigure
  495. ! grep "$PWD/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  496. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  497. rm -rf esp32
  498. rm -rf mycomponents
  499. # idf.py global and subcommand parameters
  500. print_status "Cannot set -D twice: for command and subcommand of idf.py (with different values)"
  501. idf.py -DAAA=BBB build -DAAA=BBB -DCCC=EEE
  502. if [ $? -eq 0 ]; then
  503. failure "It shouldn't be allowed to set -D twice (globally and for subcommand) with different set of options"
  504. fi
  505. print_status "Can set -D twice: globally and for subcommand, only if values are the same"
  506. idf.py -DAAA=BBB -DCCC=EEE build -DAAA=BBB -DCCC=EEE || failure "It should be allowed to set -D twice (globally and for subcommand) if values are the same"
  507. # idf.py subcommand options, (using monitor with as example)
  508. print_status "Can set options to subcommands: print_filter for monitor"
  509. mv ${IDF_PATH}/tools/idf_monitor.py ${IDF_PATH}/tools/idf_monitor.py.tmp
  510. echo "import sys;print(sys.argv[1:])" > ${IDF_PATH}/tools/idf_monitor.py
  511. idf.py build || "Failed to build project"
  512. 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)"
  513. mv ${IDF_PATH}/tools/idf_monitor.py.tmp ${IDF_PATH}/tools/idf_monitor.py
  514. print_status "Fail on build time works"
  515. clean_build_dir
  516. cp CMakeLists.txt CMakeLists.txt.bak
  517. printf "\nif(NOT EXISTS \"\${CMAKE_CURRENT_LIST_DIR}/hello.txt\") \nfail_at_build_time(test_file \"hello.txt does not exists\") \nendif()" >> CMakeLists.txt
  518. ! idf.py build || failure "Build should fail if requirements are not satisfied"
  519. touch hello.txt
  520. idf.py build || failure "Build succeeds once requirements are satisfied"
  521. rm -rf hello.txt CMakeLists.txt
  522. mv CMakeLists.txt.bak CMakeLists.txt
  523. rm -rf CMakeLists.txt.bak
  524. print_status "Component properties are set"
  525. clean_build_dir
  526. cp CMakeLists.txt CMakeLists.txt.bak
  527. printf "\nidf_component_get_property(srcs main SRCS)\nmessage(STATUS SRCS:\${srcs})" >> CMakeLists.txt
  528. (idf.py reconfigure | grep "SRCS:$(realpath main/main.c)") || failure "Component properties should be set"
  529. rm -rf CMakeLists.txt
  530. mv CMakeLists.txt.bak CMakeLists.txt
  531. rm -rf CMakeLists.txt.bak
  532. print_status "Supports git worktree"
  533. clean_build_dir
  534. git branch test_build_system
  535. git worktree add ../esp-idf-template-test test_build_system
  536. diff <(idf.py reconfigure | grep "Project version") <(cd ../esp-idf-template-test && idf.py reconfigure | grep "Project version") \
  537. || failure "Version on worktree should have been properly resolved"
  538. git worktree remove ../esp-idf-template-test
  539. print_status "Defaults set properly for unspecified idf_build_process args"
  540. pushd $IDF_PATH/examples/build_system/cmake/idf_as_lib
  541. cp CMakeLists.txt CMakeLists.txt.bak
  542. echo -e "\nidf_build_get_property(project_dir PROJECT_DIR)" >> CMakeLists.txt
  543. echo -e "\nmessage(\"Project directory: \${project_dir}\")" >> CMakeLists.txt
  544. mkdir build && cd build
  545. cmake .. -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32 &> log.txt
  546. grep "Project directory: $IDF_PATH/examples/build_system/cmake/idf_as_lib" log.txt || failure "PROJECT_DIR default was not set"
  547. cd ..
  548. mv CMakeLists.txt.bak CMakeLists.txt
  549. rm -rf build
  550. popd
  551. print_status "All tests completed"
  552. if [ -n "${FAILURES}" ]; then
  553. echo "Some failures were detected:"
  554. echo -e "${FAILURES}"
  555. exit 1
  556. else
  557. echo "Build tests passed."
  558. fi
  559. }
  560. function print_status()
  561. {
  562. echo "******** $1"
  563. STATUS="$1"
  564. }
  565. function failure()
  566. {
  567. echo "!!!!!!!!!!!!!!!!!!!"
  568. echo "FAILURE: $1"
  569. echo "!!!!!!!!!!!!!!!!!!!"
  570. FAILURES="${FAILURES}${STATUS} :: $1\n"
  571. }
  572. TESTDIR=${PWD}/build_system_tests_$$
  573. mkdir -p ${TESTDIR}
  574. # set NOCLEANUP=1 if you want to keep the test directory around
  575. # for post-mortem debugging
  576. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  577. SNAPSHOT=${TESTDIR}/snapshot
  578. BUILD=${TESTDIR}/template/build
  579. # copy all the build output to a snapshot directory
  580. function take_build_snapshot()
  581. {
  582. rm -rf ${SNAPSHOT}
  583. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  584. }
  585. # verify that all the arguments are present in the build output directory
  586. function assert_built()
  587. {
  588. until [ -z "$1" ]; do
  589. if [ ! -f "${BUILD}/$1" ]; then
  590. failure "File $1 should be in the build output directory"
  591. fi
  592. shift
  593. done
  594. }
  595. # Test if a file has been rebuilt.
  596. function file_was_rebuilt()
  597. {
  598. # can't use [ a -ot b ] here as -ot only gives second resolution
  599. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  600. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  601. return 0
  602. else
  603. return 1
  604. fi
  605. }
  606. # verify all the arguments passed in were rebuilt relative to the snapshot
  607. function assert_rebuilt()
  608. {
  609. until [ -z "$1" ]; do
  610. assert_built "$1"
  611. if [ ! -f "${SNAPSHOT}/$1" ]; then
  612. failure "File $1 should be in original build snapshot"
  613. fi
  614. if ! file_was_rebuilt "$1"; then
  615. failure "File $1 should have been rebuilt"
  616. fi
  617. shift
  618. done
  619. }
  620. # verify all the arguments are in the build directory & snapshot,
  621. # but were not rebuilt
  622. function assert_not_rebuilt()
  623. {
  624. until [ -z "$1" ]; do
  625. assert_built "$1"
  626. if [ ! -f "${SNAPSHOT}/$1" ]; then
  627. failure "File $1 should be in snapshot build directory"
  628. fi
  629. if file_was_rebuilt "$1"; then
  630. failure "File $1 should not have been rebuilt"
  631. fi
  632. shift
  633. done
  634. }
  635. # do a "clean" that doesn't depend on idf.py
  636. function clean_build_dir()
  637. {
  638. rm -rf --preserve-root ${BUILD}/* ${BUILD}/.*
  639. }
  640. cd ${TESTDIR}
  641. run_tests