test_build_system.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. #!/usr/bin/env bash
  2. #
  3. # Test the build system for basic consistency
  4. #
  5. # A bash script that tests some likely make 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. function run_tests()
  34. {
  35. FAILURES=
  36. STATUS="Starting"
  37. print_status "Checking prerequisites"
  38. [ -z ${IDF_PATH} ] && echo "IDF_PATH is not set. Need path to esp-idf installation." && exit 2
  39. print_status "Cloning template from ${ESP_IDF_TEMPLATE_GIT}..."
  40. git clone ${ESP_IDF_TEMPLATE_GIT} template
  41. cd template
  42. if [ -z $CHECKOUT_REF_SCRIPT ]; then
  43. git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
  44. else
  45. $CHECKOUT_REF_SCRIPT esp-idf-template .
  46. fi
  47. print_status "Updating template config..."
  48. make defconfig || exit $?
  49. print_status "Try to clean fresh directory..."
  50. MAKEFLAGS= make clean || exit $?
  51. BOOTLOADER_BINS="bootloader/bootloader.elf bootloader/bootloader.bin"
  52. APP_BINS="app-template.elf app-template.bin"
  53. PHY_INIT_BIN="phy_init_data.bin"
  54. print_status "Initial clean build"
  55. # if make fails here, everything fails
  56. make || exit $?
  57. # check all the expected build artifacts from the clean build
  58. assert_built ${APP_BINS} ${BOOTLOADER_BINS} partitions_singleapp.bin
  59. [ -f ${BUILD}/partition*.bin ] || failure "A partition table should have been built"
  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/esp_system/port/cpu_start.c
  64. make || failure "Failed to partial build"
  65. assert_rebuilt ${APP_BINS} esp_system/libesp_system.a esp_system/port/cpu_start.o
  66. assert_not_rebuilt lwip/liblwip.a freertos/libfreertos.a ${BOOTLOADER_BINS} partitions_singleapp.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. make bootloader || failure "Failed to partial build bootloader"
  71. assert_rebuilt ${BOOTLOADER_BINS} bootloader/main/bootloader_start.o
  72. assert_not_rebuilt ${APP_BINS} partitions_singleapp.bin
  73. print_status "Partition CSV file rebuilds partitions"
  74. take_build_snapshot
  75. touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
  76. make partition_table || failure "Failed to build partition table"
  77. assert_rebuilt partitions_singleapp.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}/@@")
  83. make || failure "Partial build failed"
  84. assert_not_rebuilt ${ALL_BUILD_FILES}
  85. print_status "Cleaning should remove all files from build"
  86. make clean || failure "Failed to make clean"
  87. ALL_BUILD_FILES=$(find ${BUILD} -type f)
  88. if [ -n "${ALL_BUILD_FILES}" ]; then
  89. failure "Files weren't cleaned: ${ALL_BUILD_FILES}"
  90. fi
  91. print_status "Bootloader build shouldn't leave build output anywhere else"
  92. clean_build_dir
  93. make bootloader
  94. # find wizardry: find any file not named sdkconfig.h that
  95. # isn't in the "bootloader" or "config" directories
  96. find ${BUILD} -type d \( -name bootloader -o -name config \) -prune , -type f ! -name sdkconfig.h || failure "Bootloader built files outside the bootloader or config directories"
  97. print_status "Moving BUILD_DIR_BASE out of tree"
  98. clean_build_dir
  99. OUTOFTREE_BUILD=${TESTDIR}/alt_build
  100. make BUILD_DIR_BASE=${OUTOFTREE_BUILD} || failure "Failed to build with BUILD_DIR_BASE overriden"
  101. NEW_BUILD_FILES=$(find ${OUTOFREE_BUILD} -type f)
  102. if [ -z "${NEW_BUILD_FILES}" ]; then
  103. failure "No files found in new build directory!"
  104. fi
  105. DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
  106. if [ -n "${DEFAULT_BUILD_FILES}" ]; then
  107. failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
  108. fi
  109. print_status "BUILD_DIR_BASE inside default build directory"
  110. clean_build_dir
  111. make BUILD_DIR_BASE=build/subdirectory || failure "Failed to build with BUILD_DIR_BASE as subdir"
  112. NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
  113. if [ -z "${NEW_BUILD_FILES}" ]; then
  114. failure "No files found in new build directory!"
  115. fi
  116. print_status "Parallel builds should work OK"
  117. clean_build_dir
  118. (make -j5 2>&1 | tee ${TESTDIR}/parallel_build.log) || failure "Failed to build in parallel"
  119. if grep -q "warning: jobserver unavailable" ${TESTDIR}/parallel_build.log; then
  120. failure "Parallel build prints 'warning: jobserver unavailable' errors"
  121. fi
  122. print_status "Can still clean build if all text files are CRLFs"
  123. make clean || failure "Unexpected failure to make clean"
  124. find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
  125. # make a copy of esp-idf and CRLFify it
  126. CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
  127. mkdir -p ${CRLF_ESPIDF}
  128. cp -r ${IDF_PATH}/* ${CRLF_ESPIDF}
  129. # don't CRLFify executable files, as Linux will fail to execute them
  130. find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
  131. make IDF_PATH=${CRLF_ESPIDF} || failure "Failed to build with CRLFs in source"
  132. # do the same checks we do for the clean build
  133. assert_built ${APP_BINS} ${BOOTLOADER_BINS} partitions_singleapp.bin
  134. [ -f ${BUILD}/partition*.bin ] || failure "A partition table should have been built in CRLF mode"
  135. print_status "Touching rom ld file should re-link app and bootloader"
  136. make
  137. take_build_snapshot
  138. touch ${IDF_PATH}/components/esp_rom/esp32/ld/esp32.rom.ld
  139. make
  140. assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
  141. print_status "Touching app-only template ld file should only re-link app"
  142. take_build_snapshot
  143. touch ${IDF_PATH}/components/esp_system/ld/esp32/sections.ld.in
  144. make
  145. assert_rebuilt ${APP_BINS}
  146. assert_not_rebuilt ${BOOTLOADER_BINS}
  147. print_status "Touching a linker fragment file should trigger re-link of app" # only app linker script is generated by tool for now
  148. take_build_snapshot
  149. touch ${IDF_PATH}/components/esp_common/common.lf
  150. make
  151. assert_rebuilt ${APP_BINS}
  152. assert_not_rebuilt ${BOOTLOADER_BINS}
  153. print_status "sdkconfig update triggers full recompile"
  154. make
  155. take_build_snapshot
  156. touch sdkconfig
  157. make
  158. # check the component_project_vars.mk file was rebuilt
  159. assert_rebuilt esp32/component_project_vars.mk
  160. # pick one each of .c, .cpp, .S that #includes sdkconfig.h
  161. # and therefore should rebuild
  162. assert_rebuilt newlib/newlib_init.o
  163. assert_rebuilt nvs_flash/src/nvs_api.o
  164. assert_rebuilt freertos/port/xtensa/xtensa_vectors.o
  165. print_status "Updating project Makefile triggers full recompile"
  166. make
  167. take_build_snapshot
  168. touch Makefile
  169. make
  170. # similar to previous test
  171. assert_rebuilt newlib/newlib_init.o
  172. assert_rebuilt nvs_flash/src/nvs_api.o
  173. assert_rebuilt freertos/port/xtensa/xtensa_vectors.o
  174. print_status "print_flash_cmd target should produce one line of output"
  175. make
  176. test $(make print_flash_cmd V=0 | wc -l | tr -d ' ') -eq 1
  177. print_status "Can include/exclude object files"
  178. echo "#error This file should not compile" > main/excluded_file.c
  179. echo "int required_global;" > main/included_file.c
  180. echo "COMPONENT_OBJEXCLUDE := excluded_file.o" >> main/component.mk
  181. echo "COMPONENT_OBJINCLUDE := included_file.o" >> main/component.mk
  182. echo "COMPONENT_ADD_LDFLAGS := -l\$(COMPONENT_NAME) -u required_global" >> main/component.mk
  183. make
  184. git checkout main/component.mk
  185. rm main/{included,excluded}_file.c
  186. print_status "Can include/exclude object files outside of component tree"
  187. mkdir -p extra_source_dir
  188. echo "#error This file should not compile" > extra_source_dir/excluded_file.c
  189. echo "int required_global;" > extra_source_dir/included_file.c
  190. echo "COMPONENT_SRCDIRS := . ../extra_source_dir" >> main/component.mk
  191. echo "COMPONENT_OBJEXCLUDE := ../extra_source_dir/excluded_file.o" >> main/component.mk
  192. echo "COMPONENT_OBJINCLUDE := ../extra_source_dir/included_file.o" >> main/component.mk
  193. echo "COMPONENT_ADD_LDFLAGS := -l\$(COMPONENT_NAME) -u required_global" >> main/component.mk
  194. make
  195. git checkout main/component.mk
  196. rm -rf extra_source_dir
  197. print_status "Can build without git installed on system"
  198. clean_build_dir
  199. # Make provision for getting IDF version
  200. echo "IDF_VER_0123456789_0123456789_0123456789" > ${IDF_PATH}/version.txt
  201. echo "project-version-w.z" > ${TESTDIR}/template/version.txt
  202. # Hide .gitmodules so that submodule check is avoided
  203. [ -f ${IDF_PATH}/.gitmodules ] && mv ${IDF_PATH}/.gitmodules ${IDF_PATH}/.gitmodules_backup
  204. # Overload `git` command
  205. echo -e '#!/bin/bash\ntouch ${IDF_PATH}/git_invoked' > git
  206. chmod +x git
  207. OLD_PATH=$PATH
  208. export PATH="$PWD:$PATH"
  209. make
  210. [ -f ${IDF_PATH}/git_invoked ] && rm ${IDF_PATH}/git_invoked && failure "git should not have been invoked in this case"
  211. rm -f ${IDF_PATH}/version.txt git
  212. rm -f ${TESTDIR}/template/version.txt
  213. [ -f ${IDF_PATH}/.gitmodules_backup ] && mv ${IDF_PATH}/.gitmodules_backup ${IDF_PATH}/.gitmodules
  214. export PATH=$OLD_PATH
  215. print_status "Rebuild when app version was changed"
  216. take_build_snapshot
  217. # App version
  218. echo "project-version-1.0" > ${TESTDIR}/template/version.txt
  219. make
  220. assert_rebuilt ${APP_BINS}
  221. print_status "Change app version"
  222. take_build_snapshot
  223. echo "project-version-2.0(012345678901234567890123456789)" > ${TESTDIR}/template/version.txt
  224. make
  225. assert_rebuilt ${APP_BINS}
  226. assert_not_rebuilt ${BOOTLOADER_BINS} esp_system/libesp_system.a
  227. print_status "Re-building does not change app.bin"
  228. take_build_snapshot
  229. make
  230. assert_not_rebuilt ${APP_BINS} ${BOOTLOADER_BINS} esp_system/libesp_system.a
  231. rm -f ${TESTDIR}/template/version.txt
  232. 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."
  233. make >> log.log || failure "Failed to build"
  234. version="App \"app-template\" version: "
  235. version+=$(git describe --always --tags --dirty)
  236. grep "${version}" log.log || failure "Project version should have a hash commit"
  237. print_status "Get the version of app from Kconfig option"
  238. make clean > /dev/null
  239. rm -f sdkconfig.defaults
  240. rm -f sdkconfig
  241. echo "project_version_from_txt" > ${TESTDIR}/template/version.txt
  242. echo "CONFIG_APP_PROJECT_VER_FROM_CONFIG=y" >> sdkconfig.defaults
  243. echo 'CONFIG_APP_PROJECT_VER="project_version_from_Kconfig"' >> sdkconfig.defaults
  244. make defconfig > /dev/null
  245. make >> log.log || failure "Failed to build"
  246. version="App \"app-template\" version: "
  247. version+="project_version_from_Kconfig"
  248. grep "${version}" log.log || failure "Project version should be from Kconfig"
  249. rm -f sdkconfig.defaults
  250. rm -f sdkconfig
  251. rm -f ${TESTDIR}/template/version.txt
  252. print_status "Build fails if partitions don't fit in flash"
  253. sed -i.bak "s/CONFIG_ESPTOOLPY_FLASHSIZE.\+//" sdkconfig # remove all flashsize config
  254. echo "CONFIG_ESPTOOLPY_FLASHSIZE_1MB=y" >> sdkconfig # introduce undersize flash
  255. make defconfig || failure "Failed to reconfigure with smaller flash"
  256. ( make 2>&1 | grep "does not fit in configured flash size 1MB" ) || failure "Build didn't fail with expected flash size failure message"
  257. mv sdkconfig.bak sdkconfig
  258. print_status "Flash size is correctly set in the bootloader image header"
  259. # Build with the default 2MB setting
  260. rm sdkconfig
  261. make defconfig && make bootloader || failure "Failed to build bootloader"
  262. bin_header_match build/bootloader/bootloader.bin "0210"
  263. # Change to 4MB
  264. echo "CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y" > sdkconfig
  265. make defconfig && make bootloader || failure "Failed to build bootloader"
  266. bin_header_match build/bootloader/bootloader.bin "0220"
  267. # Change to QIO, bootloader should still be DIO (will change to QIO in 2nd stage bootloader)
  268. echo "CONFIG_FLASHMODE_QIO=y" > sdkconfig
  269. make defconfig && make bootloader || failure "Failed to build bootloader"
  270. bin_header_match build/bootloader/bootloader.bin "0210"
  271. # Change to 80 MHz
  272. echo "CONFIG_ESPTOOLPY_FLASHFREQ_80M=y" > sdkconfig
  273. make defconfig && make bootloader || failure "Failed to build bootloader"
  274. bin_header_match build/bootloader/bootloader.bin "021f"
  275. rm sdkconfig
  276. print_status "sdkconfig should have contents of all files: sdkconfig, sdkconfig.defaults, sdkconfig.defaults.IDF_TARGET"
  277. make clean > /dev/null;
  278. rm -f sdkconfig.defaults;
  279. rm -f sdkconfig;
  280. echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> sdkconfig.defaults;
  281. echo "CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y" >> sdkconfig.defaults.esp32;
  282. echo "CONFIG_PARTITION_TABLE_TWO_OTA=y" >> sdkconfig;
  283. make defconfig > /dev/null;
  284. grep "CONFIG_PARTITION_TABLE_OFFSET=0x10000" sdkconfig || failure "The define from sdkconfig.defaults should be into sdkconfig"
  285. grep "CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y" sdkconfig || failure "The define from sdkconfig.defaults.esp32 should be into sdkconfig"
  286. grep "CONFIG_PARTITION_TABLE_TWO_OTA=y" sdkconfig || failure "The define from sdkconfig should be into sdkconfig"
  287. rm sdkconfig sdkconfig.defaults sdkconfig.defaults.esp32
  288. make defconfig
  289. print_status "can build with phy_init_data"
  290. make clean > /dev/null
  291. rm -f sdkconfig.defaults
  292. rm -f sdkconfig
  293. echo "CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION=y" >> sdkconfig.defaults
  294. make defconfig > /dev/null
  295. make || failure "Failed to build with PHY_INIT_DATA"
  296. assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PHY_INIT_BIN}
  297. rm sdkconfig
  298. rm sdkconfig.defaults
  299. make defconfig
  300. print_status "UF2 build works"
  301. rm -f -r build sdkconfig
  302. make defconfig
  303. make uf2
  304. assert_built ${APP_BINS} "uf2.bin"
  305. make uf2-app
  306. assert_built "uf2-app.bin"
  307. rm -f -r build sdkconfig
  308. print_status "Empty directory not treated as a component"
  309. mkdir -p components/esp32
  310. make || failure "Failed to build with empty esp32 directory in components"
  311. rm -rf components
  312. print_status "If a component directory is added to COMPONENT_DIRS, its subdirectories are not added"
  313. mkdir -p main/test
  314. touch main/test/component.mk
  315. echo "#error This should not be built" > main/test/test.c
  316. make || failure "COMPONENT_DIRS has added component subdirectory to the build"
  317. rm -rf main/test
  318. print_status "If a component directory is added to COMPONENT_DIRS, its sibling directories are not added"
  319. mkdir -p mycomponents/mycomponent
  320. touch mycomponents/mycomponent/component.mk
  321. # first test by adding single component directory to EXTRA_COMPONENT_DIRS
  322. mkdir -p mycomponents/esp32
  323. touch mycomponents/esp32/component.mk
  324. make EXTRA_COMPONENT_DIRS=$PWD/mycomponents/mycomponent || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  325. rm -rf mycomponents/esp32
  326. # now the same thing, but add a components directory
  327. mkdir -p esp32
  328. touch esp32/component.mk
  329. make EXTRA_COMPONENT_DIRS=$PWD/mycomponents || failure "EXTRA_COMPONENT_DIRS has added a sibling directory"
  330. rm -rf esp32
  331. rm -rf mycomponents
  332. print_status "Handling deprecated Kconfig options"
  333. make clean > /dev/null;
  334. rm -f sdkconfig.defaults;
  335. rm -f sdkconfig;
  336. echo "" > ${IDF_PATH}/sdkconfig.rename;
  337. make defconfig > /dev/null;
  338. echo "CONFIG_TEST_OLD_OPTION=y" >> sdkconfig;
  339. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" >> ${IDF_PATH}/sdkconfig.rename;
  340. echo -e "\n\
  341. menu \"test\"\n\
  342. config TEST_NEW_OPTION\n\
  343. bool \"test\"\n\
  344. default \"n\"\n\
  345. help\n\
  346. TEST_NEW_OPTION description\n\
  347. endmenu\n" >> ${IDF_PATH}/Kconfig;
  348. make defconfig > /dev/null;
  349. grep "CONFIG_TEST_OLD_OPTION=y" sdkconfig || failure "CONFIG_TEST_OLD_OPTION should be in sdkconfig for backward compatibility"
  350. grep "CONFIG_TEST_NEW_OPTION=y" sdkconfig || failure "CONFIG_TEST_NEW_OPTION should be now in sdkconfig"
  351. grep "#define CONFIG_TEST_NEW_OPTION 1" build/include/sdkconfig.h || failure "sdkconfig.h should contain the new macro"
  352. grep "#define CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" build/include/sdkconfig.h || failure "sdkconfig.h should contain the compatibility macro"
  353. grep "CONFIG_TEST_OLD_OPTION=y" build/include/config/auto.conf || failure "CONFIG_TEST_OLD_OPTION should be in auto.conf for backward compatibility"
  354. grep "CONFIG_TEST_NEW_OPTION=y" build/include/config/auto.conf || failure "CONFIG_TEST_NEW_OPTION should be now in auto.conf"
  355. rm -f sdkconfig sdkconfig.defaults
  356. pushd ${IDF_PATH}
  357. git checkout -- sdkconfig.rename Kconfig
  358. popd
  359. print_status "Handling deprecated Kconfig options in sdkconfig.defaults"
  360. make clean;
  361. rm -f sdkconfig;
  362. echo "CONFIG_TEST_OLD_OPTION=7" > sdkconfig.defaults;
  363. echo "CONFIG_TEST_OLD_OPTION CONFIG_TEST_NEW_OPTION" > ${IDF_PATH}/sdkconfig.rename;
  364. echo -e "\n\
  365. menu \"test\"\n\
  366. config TEST_NEW_OPTION\n\
  367. int \"TEST_NEW_OPTION\"\n\
  368. range 0 10\n\
  369. default 5\n\
  370. help\n\
  371. TEST_NEW_OPTION description\n\
  372. endmenu\n" >> ${IDF_PATH}/Kconfig;
  373. make defconfig > /dev/null;
  374. grep "CONFIG_TEST_OLD_OPTION=7" sdkconfig || failure "CONFIG_TEST_OLD_OPTION=7 should be in sdkconfig for backward compatibility"
  375. grep "CONFIG_TEST_NEW_OPTION=7" sdkconfig || failure "CONFIG_TEST_NEW_OPTION=7 should be in sdkconfig"
  376. rm -f sdkconfig.defaults;
  377. pushd ${IDF_PATH}
  378. git checkout -- sdkconfig.rename Kconfig
  379. popd
  380. print_status "Project components prioritized over EXTRA_COMPONENT_DIRS"
  381. clean_build_dir
  382. mkdir -p extra_dir/my_component
  383. echo "COMPONENT_CONFIG_ONLY := 1" > extra_dir/my_component/component.mk
  384. cp Makefile Makefile.bak # set EXTRA_COMPONENT_DIRS to point to the other directory
  385. sed -i "s%PROJECT_NAME := app-template%PROJECT_NAME := app-template\nEXTRA_COMPONENT_DIRS := extra_dir%" Makefile
  386. (make list-components | grep "$PWD/extra_dir/my_component") || failure "Unable to find component specified in EXTRA_COMPONENT_DIRS"
  387. mkdir -p components/my_component
  388. echo "COMPONENT_CONFIG_ONLY := 1" > components/my_component/component.mk
  389. (make list-components | grep "$PWD/components/my_component") || failure "Project components should be prioritized over EXTRA_COMPONENT_DIRS"
  390. mv Makefile.bak Makefile # revert previous modifications
  391. rm -rf extra_dir components
  392. print_status "COMPONENT_OWNBUILDTARGET, COMPONENT_OWNCLEANTARGET can work"
  393. take_build_snapshot
  394. mkdir -p components/test_component
  395. cat > components/test_component/component.mk <<EOF
  396. COMPONENT_OWNBUILDTARGET:=custom_build
  397. COMPONENT_OWNCLEANTARGET:=custom_clean
  398. .PHONY: custom_target
  399. custom_build:
  400. echo "Running custom_build!"
  401. echo "" | \$(CC) -x c++ -c -o dummy_obj.o -
  402. \$(AR) cr libtest_component.a dummy_obj.o
  403. custom_clean:
  404. rm -f libtest_component.a dummy_obj.o
  405. EOF
  406. make || failure "Failed to build with custom component build target"
  407. [ -f ${BUILD}/test_component/dummy_obj.o ] || failure "Failed to build dummy_obj.o in custom target"
  408. [ -f ${BUILD}/test_component/libtest_component.a ] || failure "Failed to build custom component library"
  409. grep -q "libtest_component.a" ${BUILD}/*.map || failure "Linker didn't see the custom library"
  410. make clean || failure "Failed to make clean with custom clean target"
  411. [ -f ${BUILD}/test_component/dummy_obj.o ] && failure "Custom clean target didn't clean object file"
  412. [ -f ${BUILD}/test_component/libtest_component.a ] && failure "Custom clean target didn't clean library"
  413. rm -rf components/test_component
  414. print_status "All tests completed"
  415. if [ -n "${FAILURES}" ]; then
  416. echo "Some failures were detected:"
  417. echo -e "${FAILURES}"
  418. exit 1
  419. else
  420. echo "Build tests passed."
  421. fi
  422. }
  423. function print_status()
  424. {
  425. echo "******** $1"
  426. STATUS="$1"
  427. }
  428. function failure()
  429. {
  430. echo "!!!!!!!!!!!!!!!!!!!"
  431. echo "FAILURE: $1"
  432. echo "!!!!!!!!!!!!!!!!!!!"
  433. FAILURES="${FAILURES}${STATUS} :: $1\n"
  434. }
  435. TESTDIR=${PWD}/build_system_tests_$$
  436. mkdir -p ${TESTDIR}
  437. # set NOCLEANUP=1 if you want to keep the test directory around
  438. # for post-mortem debugging
  439. [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
  440. SNAPSHOT=${TESTDIR}/snapshot
  441. BUILD=${TESTDIR}/template/build
  442. # copy all the build output to a snapshot directory
  443. function take_build_snapshot()
  444. {
  445. rm -rf ${SNAPSHOT}
  446. cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
  447. }
  448. # verify that all the arguments are present in the build output directory
  449. function assert_built()
  450. {
  451. until [ -z "$1" ]; do
  452. if [ ! -f "${BUILD}/$1" ]; then
  453. failure "File $1 should be in the build output directory"
  454. fi
  455. shift
  456. done
  457. }
  458. # Test if a file has been rebuilt.
  459. function file_was_rebuilt()
  460. {
  461. # can't use [ a -ot b ] here as -ot only gives second resolution
  462. # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
  463. if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
  464. return 0
  465. else
  466. return 1
  467. fi
  468. }
  469. # verify all the arguments passed in were rebuilt relative to the snapshot
  470. function assert_rebuilt()
  471. {
  472. until [ -z "$1" ]; do
  473. assert_built "$1"
  474. if [ ! -f "${SNAPSHOT}/$1" ]; then
  475. failure "File $1 should have been original build snapshot"
  476. fi
  477. if ! file_was_rebuilt "$1"; then
  478. failure "File $1 should have been rebuilt"
  479. fi
  480. shift
  481. done
  482. }
  483. # verify all the arguments are in the build directory & snapshot,
  484. # but were not rebuilt
  485. function assert_not_rebuilt()
  486. {
  487. until [ -z "$1" ]; do
  488. assert_built "$1"
  489. if [ ! -f "${SNAPSHOT}/$1" ]; then
  490. failure "File $1 should be in snapshot build directory"
  491. fi
  492. if file_was_rebuilt "$1"; then
  493. failure "File $1 should not have been rebuilt"
  494. fi
  495. shift
  496. done
  497. }
  498. # do a "clean" that doesn't depend on 'make clean'
  499. function clean_build_dir()
  500. {
  501. rm -rf --preserve-root ${BUILD}/*
  502. }
  503. # check the bytes 3-4 of the binary image header. e.g.:
  504. # bin_header_match app.bin 0210
  505. function bin_header_match()
  506. {
  507. expected=$2
  508. filename=$1
  509. actual=$(xxd -s 2 -l 2 -ps $1)
  510. if [ ! "$expected" = "$actual" ]; then
  511. failure "Incorrect binary image header, expected $expected got $actual"
  512. fi
  513. }
  514. cd ${TESTDIR}
  515. run_tests