build_examples.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/bin/bash
  2. #
  3. # Build all examples from the examples directory, out of tree to
  4. # ensure they can run when copied to a new directory.
  5. #
  6. # Runs as part of CI process.
  7. #
  8. # Assumes PWD is an out-of-tree build directory, and will copy examples
  9. # to individual subdirectories, one by one.
  10. #
  11. #
  12. # Without arguments it just builds all examples
  13. #
  14. # With one argument <JOB_NAME> it builds part of the examples. This is a useful for
  15. # parallel execution in CI.
  16. # <JOB_NAME> must look like this:
  17. # <some_text_label>_<num>
  18. # It scans .gitlab-ci.yaml to count number of jobs which have name "<some_text_label>_<num>"
  19. # It scans the filesystem to count all examples
  20. # Based on this, it decides to run qa set of examples.
  21. #
  22. # -----------------------------------------------------------------------------
  23. # Safety settings (see https://gist.github.com/ilg-ul/383869cbb01f61a51c4d).
  24. if [[ ! -z ${DEBUG_SHELL} ]]
  25. then
  26. set -x # Activate the expand mode if DEBUG is anything but empty.
  27. fi
  28. set -o errexit # Exit if command failed.
  29. set -o pipefail # Exit if pipe failed.
  30. set -o nounset # Exit if variable not set.
  31. # Remove the initial space and instead use '\n'.
  32. IFS=$'\n\t'
  33. # -----------------------------------------------------------------------------
  34. die() {
  35. echo "${1:-"Unknown Error"}" 1>&2
  36. exit 1
  37. }
  38. [ -z ${IDF_PATH} ] && die "IDF_PATH is not set"
  39. [ -z ${LOG_PATH} ] && die "LOG_PATH is not set"
  40. [ -d ${LOG_PATH} ] || mkdir -p ${LOG_PATH}
  41. echo "build_examples running in ${PWD}"
  42. export BATCH_BUILD=1
  43. export V=0 # only build verbose if there's an error
  44. shopt -s lastpipe # Workaround for Bash to use variables in loops (http://mywiki.wooledge.org/BashFAQ/024)
  45. RESULT=0
  46. FAILED_EXAMPLES=""
  47. RESULT_ISSUES=22 # magic number result code for issues found
  48. LOG_SUSPECTED=${LOG_PATH}/common_log.txt
  49. touch ${LOG_SUSPECTED}
  50. SDKCONFIG_DEFAULTS_CI=sdkconfig.ci
  51. EXAMPLE_PATHS=$( find ${IDF_PATH}/examples/ -type f -name Makefile | grep -v "/build_system/cmake/" | sort )
  52. if [ -z "${CI_NODE_TOTAL:-}" ]
  53. then
  54. START_NUM=0
  55. if [ "${1:-}" ]; then
  56. START_NUM=$1
  57. fi
  58. END_NUM=999
  59. else
  60. JOB_NUM=${CI_NODE_INDEX}
  61. # count number of the jobs
  62. NUM_OF_JOBS=${CI_NODE_TOTAL}
  63. # count number of examples
  64. NUM_OF_EXAMPLES=$( echo "${EXAMPLE_PATHS}" | wc -l )
  65. [ -z ${NUM_OF_EXAMPLES} ] && die "NUM_OF_EXAMPLES is bad"
  66. # separate intervals
  67. #57 / 5 == 12
  68. NUM_OF_EX_PER_JOB=$(( (${NUM_OF_EXAMPLES} + ${NUM_OF_JOBS} - 1) / ${NUM_OF_JOBS} ))
  69. [ -z ${NUM_OF_EX_PER_JOB} ] && die "NUM_OF_EX_PER_JOB is bad"
  70. # ex.: [0; 12); [12; 24); [24; 36); [36; 48); [48; 60)
  71. START_NUM=$(( (${JOB_NUM} - 1) * ${NUM_OF_EX_PER_JOB} ))
  72. [ -z ${START_NUM} ] && die "START_NUM is bad"
  73. END_NUM=$(( ${JOB_NUM} * ${NUM_OF_EX_PER_JOB} ))
  74. [ -z ${END_NUM} ] && die "END_NUM is bad"
  75. fi
  76. build_example () {
  77. local ID=$1
  78. shift
  79. local MAKE_FILE=$1
  80. shift
  81. local EXAMPLE_DIR=$(dirname "${MAKE_FILE}")
  82. local EXAMPLE_NAME=$(basename "${EXAMPLE_DIR}")
  83. # Check if the example needs a different base directory.
  84. # Path of the Makefile relative to $IDF_PATH
  85. local MAKE_FILE_REL=${MAKE_FILE#"${IDF_PATH}/"}
  86. # Look for it in build_example_dirs.txt:
  87. local COPY_ROOT_REL=$(sed -n -E "s|${MAKE_FILE_REL}[[:space:]]+(.*)|\1|p" < ${IDF_PATH}/tools/ci/build_example_dirs.txt)
  88. if [[ -n "${COPY_ROOT_REL}" && -d "${IDF_PATH}/${COPY_ROOT_REL}/" ]]; then
  89. local COPY_ROOT=${IDF_PATH}/${COPY_ROOT_REL}
  90. else
  91. local COPY_ROOT=${EXAMPLE_DIR}
  92. fi
  93. echo "Building ${EXAMPLE_NAME} as ${ID}..."
  94. mkdir -p "example_builds/${ID}"
  95. cp -r "${COPY_ROOT}" "example_builds/${ID}"
  96. local COPY_ROOT_PARENT=$(dirname ${COPY_ROOT})
  97. local EXAMPLE_DIR_REL=${EXAMPLE_DIR#"${COPY_ROOT_PARENT}"}
  98. pushd "example_builds/${ID}/${EXAMPLE_DIR_REL}"
  99. # be stricter in the CI build than the default IDF settings
  100. export EXTRA_CFLAGS=${PEDANTIC_CFLAGS}
  101. export EXTRA_CXXFLAGS=${PEDANTIC_CXXFLAGS}
  102. # sdkconfig files are normally not checked into git, but may be present when
  103. # a developer runs this script locally
  104. rm -f sdkconfig
  105. # If sdkconfig.ci file is present, append it to sdkconfig.defaults,
  106. # replacing environment variables
  107. if [[ -f "$SDKCONFIG_DEFAULTS_CI" ]]; then
  108. # Make sure that the last line of sdkconfig.defaults is terminated. Otherwise, the first line
  109. # of $SDKCONFIG_DEFAULTS_CI will be joined with the last one of sdkconfig.defaults.
  110. echo >> sdkconfig.defaults
  111. cat $SDKCONFIG_DEFAULTS_CI | $IDF_PATH/tools/ci/envsubst.py >> sdkconfig.defaults
  112. fi
  113. # build non-verbose first
  114. local BUILDLOG=${LOG_PATH}/ex_${ID}_log.txt
  115. touch ${BUILDLOG}
  116. local FLASH_ARGS=build/download.config
  117. make clean >>${BUILDLOG} 2>&1 &&
  118. make defconfig >>${BUILDLOG} 2>&1 &&
  119. make all >>${BUILDLOG} 2>&1 &&
  120. make print_flash_cmd >${FLASH_ARGS}.full 2>>${BUILDLOG} ||
  121. {
  122. RESULT=$?; FAILED_EXAMPLES+=" ${EXAMPLE_NAME}" ;
  123. }
  124. tail -n 1 ${FLASH_ARGS}.full > ${FLASH_ARGS} || :
  125. test -s ${FLASH_ARGS} || die "Error: ${FLASH_ARGS} file is empty"
  126. cat ${BUILDLOG}
  127. popd
  128. grep -i "error\|warning\|command not found" "${BUILDLOG}" 2>&1 >> "${LOG_SUSPECTED}" || :
  129. }
  130. EXAMPLE_NUM=0
  131. echo "Current job will build example ${START_NUM} - ${END_NUM}"
  132. for EXAMPLE_PATH in ${EXAMPLE_PATHS}
  133. do
  134. if [[ $EXAMPLE_NUM -lt $START_NUM || $EXAMPLE_NUM -ge $END_NUM ]]
  135. then
  136. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  137. continue
  138. fi
  139. echo ">>> example [ ${EXAMPLE_NUM} ] - $EXAMPLE_PATH"
  140. build_example "${EXAMPLE_NUM}" "${EXAMPLE_PATH}"
  141. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  142. done
  143. # show warnings
  144. echo -e "\nFound issues:"
  145. # Ignore the next messages:
  146. # "error.o" or "-Werror" in compiler's command line
  147. # "reassigning to symbol" or "changes choice state" in sdkconfig
  148. # 'Compiler and toochain versions is not supported' from make/project.mk
  149. IGNORE_WARNS="\
  150. library/error\.o\
  151. \|\ -Werror\
  152. \|.*error.*\.o\
  153. \|.*error.*\.d\
  154. \|reassigning to symbol\
  155. \|changes choice state\
  156. \|Compiler version is not supported\
  157. \|Toolchain version is not supported\
  158. \|Python 3 versions older than 3\.6 are not supported\.\
  159. \|Support for Python 2 is deprecated and will be removed in future versions\.\
  160. "
  161. sort -u "${LOG_SUSPECTED}" | grep -v "${IGNORE_WARNS}" \
  162. && RESULT=$RESULT_ISSUES \
  163. || echo -e "\tNone"
  164. [ -z ${FAILED_EXAMPLES} ] || echo -e "\nThere are errors in the next examples: $FAILED_EXAMPLES"
  165. [ $RESULT -eq 0 ] || echo -e "\nFix all warnings and errors above to pass the test!"
  166. echo -e "\nReturn code = $RESULT"
  167. exit $RESULT