1#!/usr/bin/env bash
2#
3# Usage: build_template_app.sh [-p]
4# -p: optional, if specified, do a fast build for every chip target with cmake and only the default
5#     config. Otherwise permutations of (chip target, build system, configs) which were not built in
6#     the fast build will be built.
7#
8# Needs to be called under IDF root folder
9#
10# This script will call find_apps.py with the following arguments:
11# - CMake build arguments: --work-dir {BUILD_PATH}/cmake --build-dir ${BUILD_DIR} --build-log ${BUILD_LOG_CMAKE}
12# - Make build arguments: --work-dir {BUILD_PATH}/make/${BUILD_DIR} --build-dir build --build-log ${BUILD_LOG_MAKE}
13
14set -euo pipefail
15
16gen_configs() {
17    # CONFIG_COMPILER_OPTIMIZATION_NONE with flag -O0
18    echo "CONFIG_COMPILER_OPTIMIZATION_NONE=y" > esp-idf-template/sdkconfig.ci.O0
19    echo "CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE=y" >> esp-idf-template/sdkconfig.ci.O0
20    # -O0 makes the bootloader too large to fit in the default space, otherwise(!)
21    echo "CONFIG_PARTITION_TABLE_OFFSET=0x10000" >> esp-idf-template/sdkconfig.ci.O0
22
23    # CONFIG_COMPILER_OPTIMIZATION_SIZE with flag -Os
24    echo "CONFIG_COMPILER_OPTIMIZATION_SIZE=y" > esp-idf-template/sdkconfig.ci.Os
25    echo "CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y" >> esp-idf-template/sdkconfig.ci.Os
26
27    # CONFIG_COMPILER_OPTIMIZATION_PERF with flag -O2
28    echo "CONFIG_COMPILER_OPTIMIZATION_PERF=y" > esp-idf-template/sdkconfig.ci.O2
29    echo "CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF=y" >> esp-idf-template/sdkconfig.ci.O2
30
31    # This part will be built in earlier stage (pre_build job) with only cmake. Built with make in later stage
32    # CONFIG_COMPILER_OPTIMIZATION_DEFAULT with flag -Og
33    echo "CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y" > esp-idf-template/sdkconfig.ci2.Og
34    echo "CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG=y" >> esp-idf-template/sdkconfig.ci2.Og
35
36    # Needs to be built with specific extra flags
37    # Same as O2, but also disable assertions.
38    cp esp-idf-template/sdkconfig.ci.O2 esp-idf-template/sdkconfig.ci3.no_assert
39    echo "CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y" >> esp-idf-template/sdkconfig.ci3.no_assert
40}
41
42get_config_str() {
43    CONFIG_STR=
44    until [ $# -eq 0 ]
45    do
46        CONFIG_STR+=" --config $1"
47        shift
48    done
49    echo ${CONFIG_STR}
50}
51
52search_cmake() {
53    TARGET=$1
54    shift
55    CONFIG_STR=$*
56    tools/find_apps.py -vv --format json --work-dir ${BUILD_PATH}/cmake --build-dir ${BUILD_DIR} --build-log ${BUILD_LOG_CMAKE} -p esp-idf-template --build-system cmake ${CONFIG_STR} --target ${TARGET} --output scan_temp.json
57    cat scan_temp.json >> scan.json
58    rm scan_temp.json
59}
60
61search_make() {
62    TARGET=$1
63    shift
64    CONFIG_STR=$*
65    tools/find_apps.py -vv --format json --work-dir ${BUILD_PATH}/make/${BUILD_DIR} --build-dir build --build-log ${BUILD_LOG_MAKE} -p esp-idf-template --build-system make ${CONFIG_STR} --target ${TARGET} --output scan_temp.json
66    cat scan_temp.json >> scan.json
67    rm scan_temp.json
68}
69
70build() {
71    tools/build_apps.py -vv --format json --keep-going --parallel-count 1 --parallel-index 1 --size-info ${SIZE_INFO_LOCATION} scan.json
72    rm scan.json
73}
74
75build_stage2() {
76    CONFIG_STR=$(get_config_str sdkconfig.ci.*=)
77    search_cmake esp32 ${CONFIG_STR}
78    search_cmake esp32s2 ${CONFIG_STR}
79    search_cmake esp32s3 ${CONFIG_STR}
80    search_cmake esp32c3 ${CONFIG_STR}
81    search_cmake esp32h2 ${CONFIG_STR}
82
83    CONFIG_STR=$(get_config_str sdkconfig.ci.*= sdkconfig.ci2.*=)
84    search_make esp32 ${CONFIG_STR}
85
86    build build_list_1.json
87
88    CONFIG_STR=$(get_config_str sdkconfig.ci3.*=)
89    search_make esp32 ${CONFIG_STR}
90    search_cmake esp32 ${CONFIG_STR}
91    search_cmake esp32s2 ${CONFIG_STR}
92    search_cmake esp32s3 ${CONFIG_STR}
93    search_cmake esp32c3 ${CONFIG_STR}
94    search_cmake esp32h2 ${CONFIG_STR}
95
96    # Override EXTRA_CFLAGS and EXTRA_CXXFLAGS in the environment
97    export EXTRA_CFLAGS=${PEDANTIC_CFLAGS/-Werror=unused-variable -Werror=unused-but-set-variable -Werror=unused-function/}
98    export EXTRA_CXXFLAGS=${PEDANTIC_CXXFLAGS/-Werror=unused-variable -Werror=unused-but-set-variable -Werror=unused-function/}
99    build
100}
101
102build_stage1() {
103    CONFIG_STR=$(get_config_str sdkconfig.ci2.*=)
104    search_cmake esp32 ${CONFIG_STR}
105    search_cmake esp32s2 ${CONFIG_STR}
106    search_cmake esp32s3 ${CONFIG_STR}
107    search_cmake esp32c3 ${CONFIG_STR}
108    search_cmake esp32h2 ${CONFIG_STR}
109
110    build
111}
112
113# Default arguments
114# STAGE:
115# 1 (-p): fast build, 2 (default): regular build
116STAGE=2
117
118# Parse arguments: -p for STAGE
119for arg in $*
120do
121    if [ ${arg} = "-p" ]; then
122        STAGE=1
123    fi
124done
125
126mkdir -p ${BUILD_PATH}/make
127mkdir -p ${BUILD_PATH}/cmake
128mkdir -p ${LOG_PATH}
129rm -f scan.json
130
131gen_configs
132
133if [ ${STAGE} = 1 ]
134then
135    build_stage1
136else
137    build_stage2
138fi
139