build_examples.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. END_NUM=999
  56. else
  57. JOB_NUM=${CI_NODE_INDEX}
  58. # count number of the jobs
  59. NUM_OF_JOBS=${CI_NODE_TOTAL}
  60. # count number of examples
  61. NUM_OF_EXAMPLES=$( echo "${EXAMPLE_PATHS}" | wc -l )
  62. [ -z ${NUM_OF_EXAMPLES} ] && die "NUM_OF_EXAMPLES is bad"
  63. # separate intervals
  64. #57 / 5 == 12
  65. NUM_OF_EX_PER_JOB=$(( (${NUM_OF_EXAMPLES} + ${NUM_OF_JOBS} - 1) / ${NUM_OF_JOBS} ))
  66. [ -z ${NUM_OF_EX_PER_JOB} ] && die "NUM_OF_EX_PER_JOB is bad"
  67. # ex.: [0; 12); [12; 24); [24; 36); [36; 48); [48; 60)
  68. START_NUM=$(( (${JOB_NUM} - 1) * ${NUM_OF_EX_PER_JOB} ))
  69. [ -z ${START_NUM} ] && die "START_NUM is bad"
  70. END_NUM=$(( ${JOB_NUM} * ${NUM_OF_EX_PER_JOB} ))
  71. [ -z ${END_NUM} ] && die "END_NUM is bad"
  72. fi
  73. build_example () {
  74. local ID=$1
  75. shift
  76. local MAKE_FILE=$1
  77. shift
  78. local EXAMPLE_DIR=$(dirname "${MAKE_FILE}")
  79. local EXAMPLE_NAME=$(basename "${EXAMPLE_DIR}")
  80. # Check if the example needs a different base directory.
  81. # Path of the Makefile relative to $IDF_PATH
  82. local MAKE_FILE_REL=${MAKE_FILE#"${IDF_PATH}/"}
  83. # Look for it in build_example_dirs.txt:
  84. local COPY_ROOT_REL=$(sed -n -E "s|${MAKE_FILE_REL}[[:space:]]+(.*)|\1|p" < ${IDF_PATH}/tools/ci/build_example_dirs.txt)
  85. if [[ -n "${COPY_ROOT_REL}" && -d "${IDF_PATH}/${COPY_ROOT_REL}/" ]]; then
  86. local COPY_ROOT=${IDF_PATH}/${COPY_ROOT_REL}
  87. else
  88. local COPY_ROOT=${EXAMPLE_DIR}
  89. fi
  90. echo "Building ${EXAMPLE_NAME} as ${ID}..."
  91. mkdir -p "example_builds/${ID}"
  92. cp -r "${COPY_ROOT}" "example_builds/${ID}"
  93. local COPY_ROOT_PARENT=$(dirname ${COPY_ROOT})
  94. local EXAMPLE_DIR_REL=${EXAMPLE_DIR#"${COPY_ROOT_PARENT}"}
  95. pushd "example_builds/${ID}/${EXAMPLE_DIR_REL}"
  96. # be stricter in the CI build than the default IDF settings
  97. export EXTRA_CFLAGS=${PEDANTIC_CFLAGS}
  98. export EXTRA_CXXFLAGS=${EXTRA_CFLAGS}
  99. # sdkconfig files are normally not checked into git, but may be present when
  100. # a developer runs this script locally
  101. rm -f sdkconfig
  102. # If sdkconfig.ci file is present, append it to sdkconfig.defaults,
  103. # replacing environment variables
  104. if [[ -f "$SDKCONFIG_DEFAULTS_CI" ]]; then
  105. # Make sure that the last line of sdkconfig.defaults is terminated. Otherwise, the first line
  106. # of $SDKCONFIG_DEFAULTS_CI will be joined with the last one of sdkconfig.defaults.
  107. echo >> sdkconfig.defaults
  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\.d\
  150. \|reassigning to symbol\
  151. \|changes choice state\
  152. \|Compiler version is not supported\
  153. \|Toolchain version is not supported\
  154. \|Python 3 versions older than 3.6 are not supported\
  155. \|Python 2 is deprecated and will be removed in future versions\
  156. "
  157. sort -u "${LOG_SUSPECTED}" | grep -v "${IGNORE_WARNS}" \
  158. && RESULT=$RESULT_ISSUES \
  159. || echo -e "\tNone"
  160. [ -z ${FAILED_EXAMPLES} ] || echo -e "\nThere are errors in the next examples: $FAILED_EXAMPLES"
  161. [ $RESULT -eq 0 ] || echo -e "\nFix all warnings and errors above to pass the test!"
  162. echo -e "\nReturn code = $RESULT"
  163. exit $RESULT