test_build_system_cmake.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. IDF_COMPONENT_PREFIX="idf_component"
  55. print_status "Initial clean build"
  56. # if build fails here, everything fails
  57. idf.py build || exit $?
  58. # check all the expected build artifacts from the clean build
  59. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  60. print_status "Updating component source file rebuilds component"
  61. # touch a file & do a build
  62. take_build_snapshot
  63. touch ${IDF_PATH}/components/esp32/cpu_start.c
  64. idf.py build || failure "Failed to partial build"
  65. assert_rebuilt ${APP_BINS} esp-idf/esp32/libesp32.a esp-idf/esp32/CMakeFiles/${IDF_COMPONENT_PREFIX}_esp32.dir/cpu_start.c.obj
  66. assert_not_rebuilt esp-idf/lwip/liblwip.a esp-idf/freertos/libfreertos.a ${BOOTLOADER_BINS} ${PARTITION_BIN}
  67. print_status "Bootloader source file rebuilds bootloader"
  68. take_build_snapshot
  69. touch ${IDF_PATH}/components/bootloader/subproject/main/bootloader_start.c
  70. idf.py build || failure "Failed to partial build bootloader"
  71. assert_rebuilt ${BOOTLOADER_BINS} bootloader/esp-idf/main/CMakeFiles/${IDF_COMPONENT_PREFIX}_main.dir/bootloader_start.c.obj
  72. assert_not_rebuilt ${APP_BINS} ${PARTITION_BIN}
  73. print_status "Partition CSV file rebuilds partitions"
  74. take_build_snapshot
  75. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  76. idf.py build || failure "Failed to build partition table"
  77. assert_rebuilt ${PARTITION_BIN}
  78. assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
  79. print_status "Partial build doesn't compile anything by default"
  80. take_build_snapshot
  81. # verify no build files are refreshed by a partial make
  82. ALL_BUILD_FILES=$(find ${BUILD} -type f | sed "s@${BUILD}/@@" | grep -v '^.')
  83. idf.py build || failure "Partial build failed"
  84. assert_not_rebuilt ${ALL_BUILD_FILES}
  85. print_status "Rebuild when app version was changed"
  86. clean_build_dir
  87. # App version
  88. echo "IDF_VER_0123456789_0123456789_0123456789" > ${IDF_PATH}/version.txt
  89. echo "project-version-1.0" > ${TESTDIR}/template/version.txt
  90. idf.py build || failure "Failed to build with app version"
  91. print_status "Change app version"
  92. take_build_snapshot
  93. echo "project-version-2.0(012345678901234567890123456789)" > ${TESTDIR}/template/version.txt
  94. idf.py build || failure "Failed to rebuild with changed app version"
  95. assert_rebuilt ${APP_BINS}
  96. assert_not_rebuilt ${BOOTLOADER_BINS} esp-idf/esp32/libesp32.a
  97. print_status "Re-building does not change app.bin"
  98. take_build_snapshot
  99. idf.py build
  100. assert_not_rebuilt ${APP_BINS} ${BOOTLOADER_BINS} esp-idf/esp32/libesp32.a
  101. rm -f ${IDF_PATH}/version.txt
  102. rm -f ${TESTDIR}/template/version.txt
  103. 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."
  104. idf.py build >> log.log || failure "Failed to build"
  105. version="Project version: "
  106. version+=$(git describe --always --tags --dirty)
  107. grep "${version}" log.log || failure "Project version should have a hash commit"
  108. print_status "Can set COMPONENT_SRCS with spaces"
  109. clean_build_dir
  110. touch main/main2.c
  111. sed -i 's/^set(COMPONENT_SRCS.*/set(COMPONENT_SRCS "main.c main2.c")/' main/CMakeLists.txt
  112. idf.py build || failure "Set COMPONENT_SRCS with spaces build failed"
  113. git checkout -- main/CMakeLists.txt
  114. rm main/main2.c
  115. print_status "Use IDF version variables in component CMakeLists.txt file"
  116. clean_build_dir
  117. (echo -e "if (NOT IDF_VERSION_MAJOR)\n message(FATAL_ERROR \"IDF version not set\")\n endif()" \
  118. && cat main/CMakeLists.txt) > main/CMakeLists.new && mv main/CMakeLists.new main/CMakeLists.txt
  119. idf.py reconfigure || failure "Failed to use IDF_VERSION_MAJOR in component CMakeLists.txt"
  120. git checkout -- main/CMakeLists.txt
  121. print_status "Moving BUILD_DIR_BASE out of tree"
  122. clean_build_dir
  123. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  124. idf.py -B "${OUTOFTREE_BUILD}" build || failure "Failed to build with out-of-tree build dir"
  125. NEW_BUILD_FILES=$(find ${OUTOFREE_BUILD} -type f)
  126. if [ -z "${NEW_BUILD_FILES}" ]; then
  127. failure "No files found in new build directory!"
  128. fi
  129. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  130. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  131. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  132. fi
  133. print_status "BUILD_DIR_BASE inside default build directory"
  134. clean_build_dir
  135. idf.py -B "build/subdirectory" build || failure "Failed to build with build dir as subdir"
  136. NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
  137. if [ -z "${NEW_BUILD_FILES}" ]; then
  138. failure "No files found in new build directory!"
  139. fi
  140. print_status "Can still clean build if all text files are CRLFs"
  141. clean_build_dir
  142. find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
  143. # make a copy of esp-idf and CRLFify it
  144. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  145. mkdir -p ${CRLF_ESPIDF}
  146. cp -r ${IDF_PATH}/* ${CRLF_ESPIDF}
  147. # don't CRLFify executable files, as Linux will fail to execute them
  148. find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
  149. IDF_PATH=${CRLF_ESPIDF} idf.py build || failure "Failed to build with CRLFs in source"
  150. # do the same checks we do for the clean build
  151. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  152. print_status "Updating rom ld file should re-link app and bootloader"
  153. clean_build_dir
  154. idf.py build
  155. take_build_snapshot
  156. sleep 1 # ninja may ignore if the timestamp delta is too low
  157. cp ${IDF_PATH}/components/esp32/ld/esp32.rom.ld .
  158. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp32/ld/esp32.rom.ld
  159. tail ${IDF_PATH}/components/esp32/ld/esp32.rom.ld
  160. idf.py build || failure "Failed to rebuild with modified linker script"
  161. assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
  162. mv esp32.rom.ld ${IDF_PATH}/components/esp32/ld/
  163. print_status "Updating app-only ld file should only re-link app"
  164. take_build_snapshot
  165. cp ${IDF_PATH}/components/esp32/ld/esp32.project.ld.in .
  166. sleep 1 # ninja may ignore if the timestamp delta is too low
  167. echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp32/ld/esp32.project.ld.in
  168. idf.py build || failure "Failed to rebuild with modified linker script"
  169. assert_rebuilt ${APP_BINS}
  170. assert_not_rebuilt ${BOOTLOADER_BINS}
  171. mv esp32.project.ld.in ${IDF_PATH}/components/esp32/ld/
  172. print_status "Updating fragment file should only re-link app" # only app linker script is generated by tool for now
  173. take_build_snapshot
  174. cp ${IDF_PATH}/components/esp32/ld/esp32_fragments.lf .
  175. sleep 1 # ninja may ignore if the timestamp delta is too low
  176. echo "# (Build test comment)" >> ${IDF_PATH}/components/esp32/ld/esp32_fragments.lf
  177. idf.py build || failure "Failed to rebuild with modified linker fragment file"
  178. assert_rebuilt ${APP_BINS}
  179. assert_not_rebuilt ${BOOTLOADER_BINS}
  180. mv esp32_fragments.lf ${IDF_PATH}/components/esp32/ld/
  181. print_status "sdkconfig update triggers full recompile"
  182. clean_build_dir
  183. idf.py build
  184. take_build_snapshot
  185. # need to actually change config, or cmake is too smart to rebuild
  186. sed -i.bak s/^\#\ CONFIG_FREERTOS_UNICORE\ is\ not\ set/CONFIG_FREERTOS_UNICORE=y/ sdkconfig
  187. idf.py build
  188. # check the sdkconfig.h file was rebuilt
  189. assert_rebuilt config/sdkconfig.h
  190. # pick one each of .c, .cpp, .S that #includes sdkconfig.h
  191. # and therefore should rebuild
  192. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/syscall_table.c.obj
  193. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  194. assert_rebuilt esp-idf/freertos/CMakeFiles/${IDF_COMPONENT_PREFIX}_freertos.dir/xtensa_vectors.S.obj
  195. mv sdkconfig.bak sdkconfig
  196. print_status "Updating project CMakeLists.txt triggers full recompile"
  197. clean_build_dir
  198. idf.py build
  199. take_build_snapshot
  200. # Need to actually change the build config, or CMake won't do anything
  201. cp CMakeLists.txt CMakeLists.bak
  202. sed -i.bak 's/^project(/add_compile_options("-DUSELESS_MACRO_DOES_NOTHING=1")\nproject\(/' CMakeLists.txt
  203. idf.py build || failure "Build failed"
  204. mv CMakeLists.bak CMakeLists.txt
  205. # similar to previous test
  206. assert_rebuilt esp-idf/newlib/CMakeFiles/${IDF_COMPONENT_PREFIX}_newlib.dir/syscall_table.c.obj
  207. assert_rebuilt esp-idf/nvs_flash/CMakeFiles/${IDF_COMPONENT_PREFIX}_nvs_flash.dir/src/nvs_api.cpp.obj
  208. assert_rebuilt esp-idf/freertos/CMakeFiles/${IDF_COMPONENT_PREFIX}_freertos.dir/xtensa_vectors.S.obj
  209. mv sdkconfig.bak sdkconfig
  210. print_status "Can build with Ninja (no idf.py)"
  211. clean_build_dir
  212. (cd build && cmake -G Ninja .. && ninja) || failure "Ninja build failed"
  213. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  214. print_status "Can build with GNU Make (no idf.py)"
  215. clean_build_dir
  216. mkdir build
  217. (cd build && cmake -G "Unix Makefiles" .. && make) || failure "Make build failed"
  218. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  219. print_status "Can build with IDF_PATH set via cmake cache not environment"
  220. clean_build_dir
  221. sed -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  222. export IDF_PATH_BACKUP="$IDF_PATH"
  223. (unset IDF_PATH &&
  224. cd build &&
  225. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  226. ninja) || failure "Ninja build failed"
  227. mv CMakeLists.txt.bak CMakeLists.txt
  228. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  229. print_status "Can build with IDF_PATH unset and inferred by build system"
  230. clean_build_dir
  231. sed -i.bak "s%\$ENV{IDF_PATH}%\${ci_idf_path}%" CMakeLists.txt # expand to a hardcoded path
  232. (ci_idf_path=${IDF_PATH} && unset IDF_PATH && cd build &&
  233. cmake -G Ninja -D ci_idf_path=${ci_idf_path} .. && ninja) || failure "Ninja build failed"
  234. mv CMakeLists.txt.bak CMakeLists.txt
  235. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  236. print_status "Can build with IDF_PATH unset and inferred by cmake when Kconfig needs it to be set"
  237. clean_build_dir
  238. sed -i.bak 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
  239. export IDF_PATH_BACKUP="$IDF_PATH"
  240. mv main/Kconfig.projbuild main/Kconfig.projbuild_bak
  241. echo "source \"\$IDF_PATH/examples/wifi/getting_started/station/main/Kconfig.projbuild\"" > main/Kconfig.projbuild
  242. (unset IDF_PATH &&
  243. cd build &&
  244. cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
  245. ninja) || failure "Ninja build failed"
  246. mv CMakeLists.txt.bak CMakeLists.txt
  247. mv main/Kconfig.projbuild_bak main/Kconfig.projbuild
  248. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  249. # Next two tests will use this fake 'esp31b' target
  250. export fake_target=esp31b
  251. mkdir -p components/$fake_target
  252. touch components/$fake_target/CMakeLists.txt
  253. cp ${IDF_PATH}/tools/cmake/toolchain-esp32.cmake components/$fake_target/toolchain-$fake_target.cmake
  254. sed -i.bak '/cmake_minimum_required/ a\
  255. set(COMPONENTS esptool_py)' CMakeLists.txt
  256. print_status "Can override IDF_TARGET from environment"
  257. clean_build_dir
  258. rm sdkconfig
  259. export IDF_TARGET=$fake_target
  260. (cd build && cmake -G Ninja .. ) || failure "Failed to configure with IDF_TARGET set in environment"
  261. grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured for IDF_TARGET correctly"
  262. grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt"
  263. unset IDF_TARGET
  264. print_status "Can set target using idf.py -D"
  265. clean_build_dir
  266. rm sdkconfig
  267. idf.py -DIDF_TARGET=$fake_target reconfigure || failure "Failed to set target via idf.py"
  268. grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured correctly using idf.py -D"
  269. grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py -D"
  270. # Clean up modifications for the fake target
  271. mv CMakeLists.txt.bak CMakeLists.txt
  272. rm -rf components
  273. print_status "Can find toolchain file in component directory"
  274. clean_build_dir
  275. mv ${IDF_PATH}/tools/cmake/toolchain-esp32.cmake ${IDF_PATH}/components/esp32/
  276. idf.py build || failure "Failed to build with toolchain file in component directory"
  277. mv ${IDF_PATH}/components/esp32/toolchain-esp32.cmake ${IDF_PATH}/tools/cmake/
  278. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  279. print_status "Can build with auto generated CMakeLists.txt"
  280. clean_build_dir
  281. mv CMakeLists.txt CMakeLists.bak
  282. ${IDF_PATH}/tools/cmake/convert_to_cmake.py .
  283. idf.py build || failure "Auto generated CMakeLists.txt build failed"
  284. mv CMakeLists.bak CMakeLists.txt
  285. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  286. print_status "Setting EXTRA_COMPONENT_DIRS works"
  287. clean_build_dir
  288. mkdir -p main/main/main # move main component contents to another directory
  289. mv main/* main/main/main
  290. cp CMakeLists.txt CMakeLists.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  291. 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
  292. idf.py build || failure "Build with EXTRA_COMPONENT_DIRS set failed"
  293. mv CMakeLists.bak CMakeLists.txt # revert previous modifications
  294. mv main/main/main/* main
  295. rm -rf main/main
  296. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
  297. print_status "sdkconfig should have contents both files: sdkconfig and sdkconfig.defaults"
  298. idf.py clean > /dev/null;
  299. idf.py fullclean > /dev/null;
  300. rm -f sdkconfig.defaults;
  301. rm -f sdkconfig;
  302. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults;
  303. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig;
  304. idf.py reconfigure > /dev/null;
  305. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults should be into sdkconfig"
  306. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig should be into sdkconfig"
  307. rm sdkconfig;
  308. rm sdkconfig.defaults;
  309. print_status "Building a project with CMake library imported and PSRAM workaround, all files compile with workaround"
  310. rm -rf build
  311. echo "CONFIG_SPIRAM_SUPPORT=y" >> sdkconfig.defaults
  312. echo "CONFIG_SPIRAM_CACHE_WORKAROUND=y" >> sdkconfig.defaults
  313. # note: we do 'reconfigure' here, as we just need to run cmake
  314. idf.py -C $IDF_PATH/examples/build_system/cmake/import_lib -B `pwd`/build reconfigure -D SDKCONFIG_DEFAULTS="`pwd`/sdkconfig.defaults"
  315. grep -q '"command"' build/compile_commands.json || failure "compile_commands.json missing or has no no 'commands' in it"
  316. (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"
  317. rm sdkconfig.defaults
  318. print_status "Displays partition table when executing target partition_table"
  319. idf.py partition_table | grep -E "# Espressif .+ Partition Table"
  320. rm -r build
  321. print_status "Make sure a full build never runs '/usr/bin/env python' or similar"
  322. OLDPATH="$PATH"
  323. PYTHON="$(which python)"
  324. rm -rf build
  325. cat > ./python << EOF
  326. #!/bin/sh
  327. echo "The build system has executed '/usr/bin/env python' or similar"
  328. exit 1
  329. EOF
  330. chmod +x ./python
  331. export PATH="$(pwd):$PATH"
  332. ${PYTHON} $IDF_PATH/tools/idf.py build || failure "build failed"
  333. export PATH="$OLDPATH"
  334. rm ./python
  335. print_status "Custom bootloader overrides original"
  336. clean_build_dir
  337. (mkdir components && cd components && cp -r $IDF_PATH/components/bootloader .)
  338. idf.py build
  339. grep "$PWD/components/bootloader/subproject/main/bootloader_start.c" build/bootloader/compile_commands.json \
  340. || failure "Custom bootloader source files should be built instead of the original's"
  341. rm -rf components
  342. print_status "Check ccache is used to build when present"
  343. touch ccache && chmod +x ccache # make sure that ccache is present for this test
  344. (export PATH=$PWD:$PATH && idf.py reconfigure | grep "ccache will be used for faster builds") || failure "ccache should be used when present"
  345. (export PATH=$PWD:$PATH && idf.py reconfigure --no-ccache | grep -c "ccache will be used for faster builds" | grep -wq 0) \
  346. || failure "ccache should not be used even when present if --no-ccache is specified"
  347. rm -f ccache
  348. print_status "Empty directory not treated as a component"
  349. clean_build_dir
  350. mkdir -p components/esp32 && idf.py reconfigure
  351. ! grep "$PWD/components/esp32" $PWD/build/project_description.json || failure "Failed to build with empty esp32 directory in components"
  352. rm -rf components
  353. print_status "If a component directory is added to COMPONENT_DIRS, its subdirectories are not added"
  354. clean_build_dir
  355. mkdir -p main/test
  356. echo "register_component()" > main/test/CMakeLists.txt
  357. idf.py reconfigure
  358. ! grep "$PWD/main/test" $PWD/build/project_description.json || failure "COMPONENT_DIRS has added component subdirectory to the build"
  359. grep "$PWD/main" $PWD/build/project_description.json || failure "COMPONENT_DIRS parent component directory should be included in the build"
  360. rm -rf main/test
  361. print_status "If a component directory is added to COMPONENT_DIRS, its sibling directories are not added"
  362. clean_build_dir
  363. mkdir -p mycomponents/mycomponent
  364. echo "register_component()" > mycomponents/mycomponent/CMakeLists.txt
  365. # first test by adding single component directory to EXTRA_COMPONENT_DIRS
  366. mkdir -p mycomponents/esp32
  367. echo "register_component()" > mycomponents/esp32/CMakeLists.txt
  368. idf.py reconfigure -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents/mycomponent
  369. ! grep "$PWD/mycomponents/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  370. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  371. rm -rf mycomponents/esp32
  372. # now the same thing, but add a components directory
  373. mkdir -p esp32
  374. echo "register_component()" > esp32/CMakeLists.txt
  375. idf.py reconfigure -DEXTRA_COMPONENT_DIRS=$PWD/mycomponents
  376. ! grep "$PWD/esp32" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  377. grep "$PWD/mycomponents/mycomponent" $PWD/build/project_description.json || failure "EXTRA_COMPONENT_DIRS valid sibling directory should be in the build"
  378. rm -rf esp32
  379. rm -rf mycomponents
  380. print_status "All tests completed"
  381. if [ -n "${FAILURES}" ]; then
  382. echo "Some failures were detected:"
  383. echo -e "${FAILURES}"
  384. exit 1
  385. else
  386. echo "Build tests passed."
  387. fi
  388. }
  389. function print_status()
  390. {
  391. echo "******** $1"
  392. STATUS="$1"
  393. }
  394. function failure()
  395. {
  396. echo "!!!!!!!!!!!!!!!!!!!"
  397. echo "FAILURE: $1"
  398. echo "!!!!!!!!!!!!!!!!!!!"
  399. FAILURES="${FAILURES}${STATUS} :: $1\n"
  400. }
  401. TESTDIR=${PWD}/build_system_tests_$$
  402. mkdir -p ${TESTDIR}
  403. # set NOCLEANUP=1 if you want to keep the test directory around
  404. # for post-mortem debugging
  405. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  406. SNAPSHOT=${TESTDIR}/snapshot
  407. BUILD=${TESTDIR}/template/build
  408. # copy all the build output to a snapshot directory
  409. function take_build_snapshot()
  410. {
  411. rm -rf ${SNAPSHOT}
  412. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  413. }
  414. # verify that all the arguments are present in the build output directory
  415. function assert_built()
  416. {
  417. until [ -z "$1" ]; do
  418. if [ ! -f "${BUILD}/$1" ]; then
  419. failure "File $1 should be in the build output directory"
  420. fi
  421. shift
  422. done
  423. }
  424. # Test if a file has been rebuilt.
  425. function file_was_rebuilt()
  426. {
  427. # can't use [ a -ot b ] here as -ot only gives second resolution
  428. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  429. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  430. return 0
  431. else
  432. return 1
  433. fi
  434. }
  435. # verify all the arguments passed in were rebuilt relative to the snapshot
  436. function assert_rebuilt()
  437. {
  438. until [ -z "$1" ]; do
  439. assert_built "$1"
  440. if [ ! -f "${SNAPSHOT}/$1" ]; then
  441. failure "File $1 should be in original build snapshot"
  442. fi
  443. if ! file_was_rebuilt "$1"; then
  444. failure "File $1 should have been rebuilt"
  445. fi
  446. shift
  447. done
  448. }
  449. # verify all the arguments are in the build directory & snapshot,
  450. # but were not rebuilt
  451. function assert_not_rebuilt()
  452. {
  453. until [ -z "$1" ]; do
  454. assert_built "$1"
  455. if [ ! -f "${SNAPSHOT}/$1" ]; then
  456. failure "File $1 should be in snapshot build directory"
  457. fi
  458. if file_was_rebuilt "$1"; then
  459. failure "File $1 should not have been rebuilt"
  460. fi
  461. shift
  462. done
  463. }
  464. # do a "clean" that doesn't depend on idf.py
  465. function clean_build_dir()
  466. {
  467. rm -rf --preserve-root ${BUILD}/* ${BUILD}/.*
  468. }
  469. cd ${TESTDIR}
  470. run_tests