]> granicus.if.org Git - esp-idf/blob - tools/ci/test_build_system_cmake.sh
cmake: Fix issues when IDF_PATH is not set in environment
[esp-idf] / tools / ci / test_build_system_cmake.sh
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
28 # Set up some variables
29 #
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
32
33 # uncomment next line to produce a lot more debug output
34 #export V=1
35
36 export PATH="$IDF_PATH/tools:$PATH"  # for idf.py
37
38 function run_tests()
39 {
40     FAILURES=
41     STATUS="Starting"
42     print_status "Checking prerequisites"
43     [ -z ${IDF_PATH} ] && echo "IDF_PATH is not set. Need path to esp-idf installation." && exit 2
44
45     print_status "Cloning template from ${ESP_IDF_TEMPLATE_GIT}..."
46     git clone ${ESP_IDF_TEMPLATE_GIT} template
47     cd template
48     if [ -z $CHECKOUT_REF_SCRIPT ]; then
49         git checkout ${CI_BUILD_REF_NAME} || echo "Using esp-idf-template default branch..."
50     else
51         $CHECKOUT_REF_SCRIPT esp-idf-template
52     fi
53
54     print_status "Try to clean fresh directory..."
55     idf.py fullclean || exit $?
56
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"
61
62     print_status "Initial clean build"
63     # if build fails here, everything fails
64     idf.py build || exit $?
65
66     # check all the expected build artifacts from the clean build
67     assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
68
69     print_status "Updating component source file rebuilds component"
70     # touch a file & do a build
71     take_build_snapshot
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}
76
77     print_status "Bootloader source file rebuilds bootloader"
78     take_build_snapshot
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}
83
84     print_status "Partition CSV file rebuilds partitions"
85     take_build_snapshot
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}
90
91     print_status "Partial build doesn't compile anything by default"
92     take_build_snapshot
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}
97
98     print_status "Moving BUILD_DIR_BASE out of tree"
99     clean_build_dir
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!"
105     fi
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}"
109     fi
110
111     print_status "BUILD_DIR_BASE inside default build directory"
112     clean_build_dir
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!"
117     fi
118
119     print_status "Can still clean build if all text files are CRLFs"
120     clean_build_dir
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}
131
132     print_status "Updating rom ld file should re-link app and bootloader"
133     clean_build_dir
134     idf.py build
135     take_build_snapshot
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/
143
144     print_status "Updating app-only ld file should only re-link app"
145     take_build_snapshot
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/
153
154     print_status "sdkconfig update triggers full recompile"
155     clean_build_dir
156     idf.py build
157     take_build_snapshot
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
160     idf.py build
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
168
169     print_status "Updating project CMakeLists.txt triggers full recompile"
170     clean_build_dir
171     idf.py build
172     take_build_snapshot
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
182
183     print_status "Can build with Ninja (no idf.py)"
184     clean_build_dir
185     (cd build && cmake -G Ninja .. && ninja)  || failure "Ninja build failed"
186     assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
187
188     print_status "Can build with GNU Make (no idf.py)"
189     clean_build_dir
190     mkdir build
191     (cd build && cmake -G "Unix Makefiles" .. && make) || failure "Make build failed"
192     assert_built ${APP_BINS} ${BOOTLOADER_BINS} ${PARTITION_BIN}
193
194     print_status "Can build with IDF_PATH set via cmake cache not environment"
195     clean_build_dir
196     cp CMakeLists.txt CMakeLists.bak
197     sed -i 's/ENV{IDF_PATH}/{IDF_PATH}/' CMakeLists.txt
198     export IDF_PATH_BACKUP="$IDF_PATH"
199     (unset IDF_PATH &&
200          cd build &&
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}
205
206     print_status "Can build with IDF_PATH unset and inferred by build system"
207     clean_build_dir
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}
214
215     print_status "All tests completed"
216     if [ -n "${FAILURES}" ]; then
217         echo "Some failures were detected:"
218         echo -e "${FAILURES}"
219         exit 1
220     else
221         echo "Build tests passed."
222     fi
223 }
224
225 function print_status()
226 {
227     echo "******** $1"
228     STATUS="$1"
229 }
230
231 function failure()
232 {
233     echo "!!!!!!!!!!!!!!!!!!!"
234     echo "FAILURE: $1"
235     echo "!!!!!!!!!!!!!!!!!!!"
236     FAILURES="${FAILURES}${STATUS} :: $1\n"
237 }
238
239 TESTDIR=${PWD}/build_system_tests_$$
240 mkdir -p ${TESTDIR}
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
244
245 SNAPSHOT=${TESTDIR}/snapshot
246 BUILD=${TESTDIR}/template/build
247
248
249 # copy all the build output to a snapshot directory
250 function take_build_snapshot()
251 {
252     rm -rf ${SNAPSHOT}
253     cp -ap ${TESTDIR}/template/build ${SNAPSHOT}
254 }
255
256 # verify that all the arguments are present in the build output directory
257 function assert_built()
258 {
259     until [ -z "$1" ]; do
260         if [ ! -f "${BUILD}/$1" ]; then
261             failure "File $1 should be in the build output directory"
262         fi
263         shift
264     done
265 }
266
267 # Test if a file has been rebuilt.
268 function file_was_rebuilt()
269 {
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
273         return 0
274     else
275         return 1
276     fi
277 }
278
279 # verify all the arguments passed in were rebuilt relative to the snapshot
280 function assert_rebuilt()
281 {
282     until [ -z "$1" ]; do
283         assert_built "$1"
284         if [ ! -f "${SNAPSHOT}/$1" ]; then
285             failure "File $1 should be in original build snapshot"
286         fi
287         if ! file_was_rebuilt "$1"; then
288             failure "File $1 should have been rebuilt"
289         fi
290         shift
291     done
292 }
293
294 # verify all the arguments are in the build directory & snapshot,
295 # but were not rebuilt
296 function assert_not_rebuilt()
297 {
298     until [ -z "$1" ]; do
299         assert_built "$1"
300         if [ ! -f "${SNAPSHOT}/$1" ]; then
301             failure "File $1 should be in snapshot build directory"
302         fi
303         if file_was_rebuilt "$1"; then
304             failure "File $1 should not have been rebuilt"
305         fi
306         shift
307     done
308 }
309
310 # do a "clean" that doesn't depend on idf.py
311 function clean_build_dir()
312 {
313     rm -rf --preserve-root ${BUILD}/* ${BUILD}/.*
314 }
315
316 cd ${TESTDIR}
317 run_tests