build_examples.sh 5.2 KB

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