build_examples.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env 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. cat $SDKCONFIG_DEFAULTS_CI | $IDF_PATH/tools/ci/envsubst.py >> sdkconfig.defaults
  109. fi
  110. # build non-verbose first
  111. local BUILDLOG=${LOG_PATH}/ex_${ID}_log.txt
  112. touch ${BUILDLOG}
  113. local FLASH_ARGS=build/download.config
  114. make clean >>${BUILDLOG} 2>&1 &&
  115. make defconfig >>${BUILDLOG} 2>&1 &&
  116. make all >>${BUILDLOG} 2>&1 &&
  117. make print_flash_cmd >${FLASH_ARGS}.full 2>>${BUILDLOG} ||
  118. {
  119. RESULT=$?; FAILED_EXAMPLES+=" ${EXAMPLE_NAME}" ;
  120. }
  121. tail -n 1 ${FLASH_ARGS}.full > ${FLASH_ARGS} || :
  122. test -s ${FLASH_ARGS} || die "Error: ${FLASH_ARGS} file is empty"
  123. cat ${BUILDLOG}
  124. popd
  125. grep -i "error\|warning\|command not found" "${BUILDLOG}" 2>&1 >> "${LOG_SUSPECTED}" || :
  126. }
  127. EXAMPLE_NUM=0
  128. echo "Current job will build example ${START_NUM} - ${END_NUM}"
  129. for EXAMPLE_PATH in ${EXAMPLE_PATHS}
  130. do
  131. if [[ $EXAMPLE_NUM -lt $START_NUM || $EXAMPLE_NUM -ge $END_NUM ]]
  132. then
  133. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  134. continue
  135. fi
  136. echo ">>> example [ ${EXAMPLE_NUM} ] - $EXAMPLE_PATH"
  137. build_example "${EXAMPLE_NUM}" "${EXAMPLE_PATH}"
  138. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  139. done
  140. # show warnings
  141. echo -e "\nFound issues:"
  142. # Ignore the next messages:
  143. # "error.o" or "-Werror" in compiler's command line
  144. # "reassigning to symbol" or "changes choice state" in sdkconfig
  145. # 'Compiler and toochain versions is not supported' from make/project.mk
  146. IGNORE_WARNS="\
  147. library/error\.o\
  148. \|\ -Werror\
  149. \|.*error.*\.o\
  150. \|.*error.*\.d\
  151. \|reassigning to symbol\
  152. \|changes choice state\
  153. \|Compiler version is not supported\
  154. \|Toolchain version is not supported\
  155. "
  156. sort -u "${LOG_SUSPECTED}" | grep -v "${IGNORE_WARNS}" \
  157. && RESULT=$RESULT_ISSUES \
  158. || echo -e "\tNone"
  159. [ -z ${FAILED_EXAMPLES} ] || echo -e "\nThere are errors in the next examples: $FAILED_EXAMPLES"
  160. [ $RESULT -eq 0 ] || echo -e "\nFix all warnings and errors above to pass the test!"
  161. echo -e "\nReturn code = $RESULT"
  162. exit $RESULT