3 # Test the build system for basic consistency (Cmake/idf.py version)
5 # A bash script that tests some likely build failure scenarios in a row
7 # Assumes PWD is an out-of-tree build directory, and will create a
8 # subdirectory inside it to run build tests in.
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.
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.
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.
28 # Set up some variables
30 # override ESP_IDF_TEMPLATE_GIT to point to a local dir if you're testing and want fast iterations
31 [ -z ${ESP_IDF_TEMPLATE_GIT} ] && ESP_IDF_TEMPLATE_GIT=https://github.com/espressif/esp-idf-template.git
33 # uncomment next line to produce a lot more debug output
36 export PATH="$IDF_PATH/tools:$PATH" # for idf.py
42 print_status "Checking prerequisites"
43 [ -z ${IDF_PATH} ] && echo "IDF_PATH is not set. Need path to esp-idf installation." && exit 2
45 print_status "Cloning template from ${ESP_IDF_TEMPLATE_GIT}..."
46 git clone ${ESP_IDF_TEMPLATE_GIT} template
48 if [ -z $CHECKOUT_REF_SCRIPT ]; then
49 git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
51 $CHECKOUT_REF_SCRIPT esp-idf-template
54 print_status "Try to clean fresh directory..."
55 idf.py fullclean || exit $?
57 # all relative to the build directory
58 BOOTLOADER_BINS="bootloader/bootloader.elf bootloader/bootloader.bin"
59 APP_BINS="app-template.elf app-template.bin"
60 PARTITION_BIN="partition_table/partition-table.bin"
62 print_status "Initial clean build"
63 # if build fails here, everything fails
64 idf.py build || exit $?
66 # check all the expected build artifacts from the clean build
67 assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
69 print_status "Updating component source file rebuilds component"
70 # touch a file & do a build
72 touch ${IDF_PATH}/components/esp32/cpu_start.c
73 idf.py build || failure "Failed to partial build"
74 assert_rebuilt ${APP_BINS} esp32/libesp32.a esp32/CMakeFiles/esp32.dir/cpu_start.c.obj
75 assert_not_rebuilt lwip/liblwip.a freertos/libfreertos.a ${BOOTLOADER_BINS} ${PARTITION_BIN}
77 print_status "Bootloader source file rebuilds bootloader"
79 touch ${IDF_PATH}/components/bootloader/subproject/main/bootloader_start.c
80 idf.py build || failure "Failed to partial build bootloader"
81 assert_rebuilt ${BOOTLOADER_BINS} bootloader/CMakeFiles/bootloader.elf.dir/main/bootloader_start.c.obj
82 assert_not_rebuilt ${APP_BINS} ${PARTITION_BIN}
84 print_status "Partition CSV file rebuilds partitions"
86 touch ${IDF_PATH}/components/partition_table/partitions_singleapp.csv
87 idf.py build || failure "Failed to build partition table"
88 assert_rebuilt ${PARTITION_BIN}
89 assert_not_rebuilt app-template.bin app-template.elf ${BOOTLOADER_BINS}
91 print_status "Partial build doesn't compile anything by default"
93 # verify no build files are refreshed by a partial make
94 ALL_BUILD_FILES=$(find ${BUILD} -type f | sed "s@${BUILD}/@@" | grep -v '^.')
95 idf.py build || failure "Partial build failed"
96 assert_not_rebuilt ${ALL_BUILD_FILES}
98 print_status "Moving BUILD_DIR_BASE out of tree"
100 OUTOFTREE_BUILD=${TESTDIR}/alt_build
101 idf.py -B "${OUTOFTREE_BUILD}" build || failure "Failed to build with out-of-tree build dir"
102 NEW_BUILD_FILES=$(find ${OUTOFREE_BUILD} -type f)
103 if [ -z "${NEW_BUILD_FILES}" ]; then
104 failure "No files found in new build directory!"
106 DEFAULT_BUILD_FILES=$(find ${BUILD} -mindepth 1)
107 if [ -n "${DEFAULT_BUILD_FILES}" ]; then
108 failure "Some files were incorrectly put into the default build directory: ${DEFAULT_BUILD_FILES}"
111 print_status "BUILD_DIR_BASE inside default build directory"
113 idf.py -B "build/subdirectory" build || failure "Failed to build with build dir as subdir"
114 NEW_BUILD_FILES=$(find ${BUILD}/subdirectory -type f)
115 if [ -z "${NEW_BUILD_FILES}" ]; then
116 failure "No files found in new build directory!"
119 print_status "Can still clean build if all text files are CRLFs"
121 find . -path .git -prune -exec unix2dos {} \; # CRLFify template dir
122 # make a copy of esp-idf and CRLFify it
123 CRLF_ESPIDF=${TESTDIR}/esp-idf-crlf
124 mkdir -p ${CRLF_ESPIDF}
125 cp -r ${IDF_PATH}/* ${CRLF_ESPIDF}
126 # don't CRLFify executable files, as Linux will fail to execute them
127 find ${CRLF_ESPIDF} -name .git -prune -name build -prune -type f ! -perm 755 -exec unix2dos {} \;
128 IDF_PATH=${CRLF_ESPIDF} idf.py build || failure "Failed to build with CRLFs in source"
129 # do the same checks we do for the clean build
130 assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
132 print_status "Updating rom ld file should re-link app and bootloader"
136 sleep 1 # ninja may ignore if the timestamp delta is too low
137 cp ${IDF_PATH}/components/esp32/ld/esp32.rom.ld .
138 echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp32/ld/esp32.rom.ld
139 tail ${IDF_PATH}/components/esp32/ld/esp32.rom.ld
140 idf.py build || failure "Failed to rebuild with modified linker script"
141 assert_rebuilt ${APP_BINS} ${BOOTLOADER_BINS}
142 mv esp32.rom.ld ${IDF_PATH}/components/esp32/ld/
144 print_status "Updating app-only ld file should only re-link app"
146 cp ${IDF_PATH}/components/esp32/ld/esp32.common.ld .
147 sleep 1 # ninja may ignore if the timestamp delta is too low
148 echo "/* (Build test comment) */" >> ${IDF_PATH}/components/esp32/ld/esp32.common.ld
149 idf.py build || failure "Failed to rebuild with modified linker script"
150 assert_rebuilt ${APP_BINS}
151 assert_not_rebuilt ${BOOTLOADER_BINS}
152 mv esp32.common.ld ${IDF_PATH}/components/esp32/ld/
154 print_status "sdkconfig update triggers full recompile"
158 # need to actually change config, or cmake is too smart to rebuild
159 sed -i s/CONFIG_FREERTOS_UNICORE=/CONFIG_FREERTOS_UNICORE=y/ sdkconfig
161 # check the sdkconfig.h file was rebuilt
162 assert_rebuilt config/sdkconfig.h
163 # pick one each of .c, .cpp, .S that #includes sdkconfig.h
164 # and therefore should rebuild
165 assert_rebuilt newlib/CMakeFiles/newlib.dir/syscall_table.c.obj
166 assert_rebuilt nvs_flash/CMakeFiles/nvs_flash.dir/src/nvs_api.cpp.obj
167 assert_rebuilt freertos/CMakeFiles/freertos.dir/xtensa_vectors.S.obj
169 print_status "Updating project CMakeLists.txt triggers full recompile"
173 # Need to actually change the build config, or CMake won't do anything
174 cp CMakeLists.txt CMakeLists.bak
175 sed -i 's/^project(/add_compile_options("-DUSELESS_MACRO_DOES_NOTHING=1")\nproject\(/' CMakeLists.txt
176 idf.py build || failure "Build failed"
177 mv CMakeLists.bak CMakeLists.txt
178 # similar to previous test
179 assert_rebuilt newlib/CMakeFiles/newlib.dir/syscall_table.c.obj
180 assert_rebuilt nvs_flash/CMakeFiles/nvs_flash.dir/src/nvs_api.cpp.obj
181 assert_rebuilt freertos/CMakeFiles/freertos.dir/xtensa_vectors.S.obj
183 print_status "Can build with Ninja (no idf.py)"
185 (cd build && cmake -G Ninja .. && ninja) || failure "Ninja build failed"
186 assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
188 print_status "Can build with GNU Make (no idf.py)"
191 (cd build && cmake -G "Unix Makefiles" .. && make) || failure "Make build failed"
192 assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
194 print_status "Can build with IDF_PATH set via cmake cache not environment"
196 cp CMakeLists.txt CMakeLists.bak
197 sed -i 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
198 export IDF_PATH_BACKUP="$IDF_PATH"
201 cmake -G Ninja .. -DIDF_PATH=${IDF_PATH_BACKUP} &&
202 ninja) || failure "Ninja build failed"
203 mv CMakeLists.bak CMakeLists.txt
204 assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
206 print_status "Can build with IDF_PATH unset and inferred by build system"
208 cp CMakeLists.txt CMakeLists.bak
209 sed -i "s%\$ENV{IDF_PATH}%${IDF_PATH}%" CMakeLists.txt # expand to a hardcoded path
210 (unset IDF_PATH && cd build &&
211 cmake -G Ninja .. && ninja) || failure "Ninja build failed"
212 mv CMakeLists.bak CMakeLists.txt
213 assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
215 print_status "All tests completed"
216 if [ -n "${FAILURES}" ]; then
217 echo "Some failures were detected:"
218 echo -e "${FAILURES}"
221 echo "Build tests passed."
225 function print_status()
233 echo "!!!!!!!!!!!!!!!!!!!"
235 echo "!!!!!!!!!!!!!!!!!!!"
236 FAILURES="${FAILURES}${STATUS} :: $1\n"
239 TESTDIR=${PWD}/build_system_tests_$$
241 # set NOCLEANUP=1 if you want to keep the test directory around
242 # for post-mortem debugging
243 [ -z ${NOCLEANUP} ] && trap "rm -rf ${TESTDIR}" EXIT KILL
245 SNAPSHOT=${TESTDIR}/snapshot
246 BUILD=${TESTDIR}/template/build
249 # copy all the build output to a snapshot directory
250 function take_build_snapshot()
253 cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
256 # verify that all the arguments are present in the build output directory
257 function assert_built()
259 until [ -z "$1" ]; do
260 if [ ! -f "${BUILD}/$1" ]; then
261 failure "File $1 should be in the build output directory"
267 # Test if a file has been rebuilt.
268 function file_was_rebuilt()
270 # can't use [ a -ot b ] here as -ot only gives second resolution
271 # but stat -c %y seems to be microsecond at least for tmpfs, ext4..
272 if [ "$(stat -c %y ${SNAPSHOT}/$1)" != "$(stat -c %y ${BUILD}/$1)" ]; then
279 # verify all the arguments passed in were rebuilt relative to the snapshot
280 function assert_rebuilt()
282 until [ -z "$1" ]; do
284 if [ ! -f "${SNAPSHOT}/$1" ]; then
285 failure "File $1 should be in original build snapshot"
287 if ! file_was_rebuilt "$1"; then
288 failure "File $1 should have been rebuilt"
294 # verify all the arguments are in the build directory & snapshot,
295 # but were not rebuilt
296 function assert_not_rebuilt()
298 until [ -z "$1" ]; do
300 if [ ! -f "${SNAPSHOT}/$1" ]; then
301 failure "File $1 should be in snapshot build directory"
303 if file_was_rebuilt "$1"; then
304 failure "File $1 should not have been rebuilt"
310 # do a "clean" that doesn't depend on idf.py
311 function clean_build_dir()
313 rm -rf --preserve-root ${BUILD}/* ${BUILD}/.*