build_examples.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. # only 0 or 1 arguments
  43. [ $# -le 1 ] || die "Have to run as $(basename $0) [<JOB_NAME>]"
  44. export BATCH_BUILD=1
  45. export V=0 # only build verbose if there's an error
  46. shopt -s lastpipe # Workaround for Bash to use variables in loops (http://mywiki.wooledge.org/BashFAQ/024)
  47. RESULT=0
  48. FAILED_EXAMPLES=""
  49. RESULT_ISSUES=22 # magic number result code for issues found
  50. LOG_SUSPECTED=${LOG_PATH}/common_log.txt
  51. touch ${LOG_SUSPECTED}
  52. SDKCONFIG_DEFAULTS_CI=sdkconfig.ci
  53. EXAMPLE_PATHS=$( find ${IDF_PATH}/examples/ -type f -name Makefile | grep -v "/build_system/cmake/" | sort )
  54. if [ $# -eq 0 ]
  55. then
  56. START_NUM=0
  57. END_NUM=999
  58. else
  59. JOB_NAME=$1
  60. # parse text prefix at the beginning of string 'some_your_text_NUM'
  61. # (will be 'some_your_text' without last '_')
  62. JOB_PATTERN=$( echo ${JOB_NAME} | sed -n -r 's/^(.*)_[0-9]+$/\1/p' )
  63. [ -z ${JOB_PATTERN} ] && die "JOB_PATTERN is bad"
  64. # parse number 'NUM' at the end of string 'some_your_text_NUM'
  65. # NOTE: Getting rid of the leading zero to get the decimal
  66. JOB_NUM=$( echo ${JOB_NAME} | sed -n -r 's/^.*_0*([0-9]+)$/\1/p' )
  67. [ -z ${JOB_NUM} ] && die "JOB_NUM is bad"
  68. # count number of the jobs
  69. NUM_OF_JOBS=$( grep -c -E "^${JOB_PATTERN}_[0-9]+:$" "${IDF_PATH}/.gitlab-ci.yml" )
  70. [ -z ${NUM_OF_JOBS} ] && die "NUM_OF_JOBS is bad"
  71. # count number of examples
  72. NUM_OF_EXAMPLES=$( echo "${EXAMPLE_PATHS}" | wc -l )
  73. [ -z ${NUM_OF_EXAMPLES} ] && die "NUM_OF_EXAMPLES is bad"
  74. # separate intervals
  75. #57 / 5 == 12
  76. NUM_OF_EX_PER_JOB=$(( (${NUM_OF_EXAMPLES} + ${NUM_OF_JOBS} - 1) / ${NUM_OF_JOBS} ))
  77. [ -z ${NUM_OF_EX_PER_JOB} ] && die "NUM_OF_EX_PER_JOB is bad"
  78. # ex.: [0; 12); [12; 24); [24; 36); [36; 48); [48; 60)
  79. START_NUM=$(( ${JOB_NUM} * ${NUM_OF_EX_PER_JOB} ))
  80. [ -z ${START_NUM} ] && die "START_NUM is bad"
  81. END_NUM=$(( (${JOB_NUM} + 1) * ${NUM_OF_EX_PER_JOB} ))
  82. [ -z ${END_NUM} ] && die "END_NUM is bad"
  83. fi
  84. build_example () {
  85. local ID=$1
  86. shift
  87. local MAKE_FILE=$1
  88. shift
  89. local EXAMPLE_DIR=$(dirname "${MAKE_FILE}")
  90. local EXAMPLE_NAME=$(basename "${EXAMPLE_DIR}")
  91. # Check if the example needs a different base directory.
  92. # Path of the Makefile relative to $IDF_PATH
  93. local MAKE_FILE_REL=${MAKE_FILE#"${IDF_PATH}/"}
  94. # Look for it in build_example_dirs.txt:
  95. local COPY_ROOT_REL=$(sed -n -E "s|${MAKE_FILE_REL}[[:space:]]+(.*)|\1|p" < ${IDF_PATH}/tools/ci/build_example_dirs.txt)
  96. if [[ -n "${COPY_ROOT_REL}" && -d "${IDF_PATH}/${COPY_ROOT_REL}/" ]]; then
  97. local COPY_ROOT=${IDF_PATH}/${COPY_ROOT_REL}
  98. else
  99. local COPY_ROOT=${EXAMPLE_DIR}
  100. fi
  101. echo "Building ${EXAMPLE_NAME} as ${ID}..."
  102. mkdir -p "example_builds/${ID}"
  103. cp -r "${COPY_ROOT}" "example_builds/${ID}"
  104. local COPY_ROOT_PARENT=$(dirname ${COPY_ROOT})
  105. local EXAMPLE_DIR_REL=${EXAMPLE_DIR#"${COPY_ROOT_PARENT}"}
  106. pushd "example_builds/${ID}/${EXAMPLE_DIR_REL}"
  107. # be stricter in the CI build than the default IDF settings
  108. export EXTRA_CFLAGS=${PEDANTIC_CFLAGS}
  109. export EXTRA_CXXFLAGS=${EXTRA_CFLAGS}
  110. # sdkconfig files are normally not checked into git, but may be present when
  111. # a developer runs this script locally
  112. rm -f sdkconfig
  113. # If sdkconfig.ci file is present, append it to sdkconfig.defaults,
  114. # replacing environment variables
  115. if [[ -f "$SDKCONFIG_DEFAULTS_CI" ]]; then
  116. cat $SDKCONFIG_DEFAULTS_CI | $IDF_PATH/tools/ci/envsubst.py >> sdkconfig.defaults
  117. fi
  118. # build non-verbose first
  119. local BUILDLOG=${LOG_PATH}/ex_${ID}_log.txt
  120. touch ${BUILDLOG}
  121. local FLASH_ARGS=build/download.config
  122. make clean >>${BUILDLOG} 2>&1 &&
  123. make defconfig >>${BUILDLOG} 2>&1 &&
  124. make all >>${BUILDLOG} 2>&1 &&
  125. make print_flash_cmd >${FLASH_ARGS}.full 2>>${BUILDLOG} ||
  126. {
  127. RESULT=$?; FAILED_EXAMPLES+=" ${EXAMPLE_NAME}" ;
  128. }
  129. tail -n 1 ${FLASH_ARGS}.full > ${FLASH_ARGS} || :
  130. test -s ${FLASH_ARGS} || die "Error: ${FLASH_ARGS} file is empty"
  131. cat ${BUILDLOG}
  132. popd
  133. grep -i "error\|warning\|command not found" "${BUILDLOG}" 2>&1 >> "${LOG_SUSPECTED}" || :
  134. }
  135. EXAMPLE_NUM=0
  136. for EXAMPLE_PATH in ${EXAMPLE_PATHS}
  137. do
  138. if [[ $EXAMPLE_NUM -lt $START_NUM || $EXAMPLE_NUM -ge $END_NUM ]]
  139. then
  140. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  141. continue
  142. fi
  143. echo ">>> example [ ${EXAMPLE_NUM} ] - $EXAMPLE_PATH"
  144. build_example "${EXAMPLE_NUM}" "${EXAMPLE_PATH}"
  145. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  146. done
  147. # show warnings
  148. echo -e "\nFound issues:"
  149. # Ignore the next messages:
  150. # "error.o" or "-Werror" in compiler's command line
  151. # "reassigning to symbol" or "changes choice state" in sdkconfig
  152. # 'Compiler and toochain versions is not supported' from make/project.mk
  153. IGNORE_WARNS="\
  154. library/error\.o\
  155. \|\ -Werror\
  156. \|error\.d\
  157. \|reassigning to symbol\
  158. \|changes choice state\
  159. \|Compiler version is not supported\
  160. \|Toolchain version is not supported\
  161. "
  162. sort -u "${LOG_SUSPECTED}" | grep -v "${IGNORE_WARNS}" \
  163. && RESULT=$RESULT_ISSUES \
  164. || echo -e "\tNone"
  165. [ -z ${FAILED_EXAMPLES} ] || echo -e "\nThere are errors in the next examples: $FAILED_EXAMPLES"
  166. [ $RESULT -eq 0 ] || echo -e "\nFix all warnings and errors above to pass the test!"
  167. echo -e "\nReturn code = $RESULT"
  168. exit $RESULT