build_examples.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. if [ $# -eq 0 ]
  53. then
  54. START_NUM=0
  55. END_NUM=999
  56. else
  57. JOB_NAME=$1
  58. # parse text prefix at the beginning of string 'some_your_text_NUM'
  59. # (will be 'some_your_text' without last '_')
  60. JOB_PATTERN=$( echo ${JOB_NAME} | sed -n -r 's/^(.*)_[0-9]+$/\1/p' )
  61. [ -z ${JOB_PATTERN} ] && die "JOB_PATTERN is bad"
  62. # parse number 'NUM' at the end of string 'some_your_text_NUM'
  63. # NOTE: Getting rid of the leading zero to get the decimal
  64. JOB_NUM=$( echo ${JOB_NAME} | sed -n -r 's/^.*_0*([0-9]+)$/\1/p' )
  65. [ -z ${JOB_NUM} ] && die "JOB_NUM is bad"
  66. # count number of the jobs
  67. NUM_OF_JOBS=$( grep -c -E "^${JOB_PATTERN}_[0-9]+:$" "${IDF_PATH}/.gitlab-ci.yml" )
  68. [ -z ${NUM_OF_JOBS} ] && die "NUM_OF_JOBS is bad"
  69. # count number of examples
  70. NUM_OF_EXAMPLES=$( find ${IDF_PATH}/examples/ -type f -name Makefile | wc -l )
  71. [ -z ${NUM_OF_EXAMPLES} ] && die "NUM_OF_EXAMPLES is bad"
  72. # separate intervals
  73. #57 / 5 == 12
  74. NUM_OF_EX_PER_JOB=$(( (${NUM_OF_EXAMPLES} + ${NUM_OF_JOBS} - 1) / ${NUM_OF_JOBS} ))
  75. [ -z ${NUM_OF_EX_PER_JOB} ] && die "NUM_OF_EX_PER_JOB is bad"
  76. # ex.: [0; 12); [12; 24); [24; 36); [36; 48); [48; 60)
  77. START_NUM=$(( ${JOB_NUM} * ${NUM_OF_EX_PER_JOB} ))
  78. [ -z ${START_NUM} ] && die "START_NUM is bad"
  79. END_NUM=$(( (${JOB_NUM} + 1) * ${NUM_OF_EX_PER_JOB} ))
  80. [ -z ${END_NUM} ] && die "END_NUM is bad"
  81. fi
  82. build_example () {
  83. local ID=$1
  84. shift
  85. local MAKE_FILE=$1
  86. shift
  87. local EXAMPLE_DIR=$(dirname "${MAKE_FILE}")
  88. local EXAMPLE_NAME=$(basename "${EXAMPLE_DIR}")
  89. echo "Building ${EXAMPLE_NAME} as ${ID}..."
  90. mkdir -p "example_builds/${ID}"
  91. cp -r "${EXAMPLE_DIR}" "example_builds/${ID}"
  92. pushd "example_builds/${ID}/${EXAMPLE_NAME}"
  93. # be stricter in the CI build than the default IDF settings
  94. export EXTRA_CFLAGS="-Werror -Werror=deprecated-declarations"
  95. export EXTRA_CXXFLAGS=${EXTRA_CFLAGS}
  96. # build non-verbose first
  97. local BUILDLOG=${LOG_PATH}/ex_${ID}_log.txt
  98. touch ${BUILDLOG}
  99. local FLASH_ARGS=build/download.config
  100. make clean >>${BUILDLOG} 2>&1 &&
  101. make defconfig >>${BUILDLOG} 2>&1 &&
  102. make all >>${BUILDLOG} 2>&1 &&
  103. make print_flash_cmd >${FLASH_ARGS}.full 2>>${BUILDLOG} ||
  104. {
  105. RESULT=$?; FAILED_EXAMPLES+=" ${EXAMPLE_NAME}" ;
  106. }
  107. tail -n 1 ${FLASH_ARGS}.full > ${FLASH_ARGS} || :
  108. test -s ${FLASH_ARGS} || die "Error: ${FLASH_ARGS} file is empty"
  109. cat ${BUILDLOG}
  110. popd
  111. grep -i "error\|warning" "${BUILDLOG}" 2>&1 >> "${LOG_SUSPECTED}" || :
  112. }
  113. EXAMPLE_NUM=0
  114. find ${IDF_PATH}/examples/ -type f -name Makefile | sort | \
  115. while read FN
  116. do
  117. if [[ $EXAMPLE_NUM -lt $START_NUM || $EXAMPLE_NUM -ge $END_NUM ]]
  118. then
  119. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  120. continue
  121. fi
  122. echo ">>> example [ ${EXAMPLE_NUM} ] - $FN"
  123. build_example "${EXAMPLE_NUM}" "${FN}"
  124. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  125. done
  126. # show warnings
  127. echo -e "\nFound issues:"
  128. # Ignore the next messages:
  129. # "error.o" or "-Werror" in compiler's command line
  130. # "reassigning to symbol" or "changes choice state" in sdkconfig
  131. # 'Compiler and toochain versions is not supported' from make/project.mk
  132. IGNORE_WARNS="\
  133. library/error\.o\
  134. \|\ -Werror\
  135. \|error\.d\
  136. \|reassigning to symbol\
  137. \|changes choice state\
  138. \|Compiler version is not supported\
  139. \|Toolchain version is not supported\
  140. "
  141. sort -u "${LOG_SUSPECTED}" | grep -v "${IGNORE_WARNS}" \
  142. && RESULT=$RESULT_ISSUES \
  143. || echo -e "\tNone"
  144. [ -z ${FAILED_EXAMPLES} ] || echo -e "\nThere are errors in the next examples: $FAILED_EXAMPLES"
  145. [ $RESULT -eq 0 ] || echo -e "\nFix all warnings and errors above to pass the test!"
  146. echo -e "\nReturn code = $RESULT"
  147. exit $RESULT