build_examples.sh 5.1 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} ]]
  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. echo "build_examples running in ${PWD}"
  40. # only 0 or 1 arguments
  41. [ $# -le 1 ] || die "Have to run as $(basename $0) [<JOB_NAME>]"
  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_WARNINGS=22 # magic number result code for "warnings found"
  48. LOG_WARNINGS=${PWD}/build_warnings.log
  49. if [ $# -eq 0 ]
  50. then
  51. START_NUM=0
  52. END_NUM=999
  53. else
  54. JOB_NAME=$1
  55. # parse text prefix at the beginning of string 'some_your_text_NUM'
  56. # (will be 'some_your_text' without last '_')
  57. JOB_PATTERN=$( echo ${JOB_NAME} | sed -n -r 's/^(.*)_[0-9]+$/\1/p' )
  58. [ -z ${JOB_PATTERN} ] && die "JOB_PATTERN is bad"
  59. # parse number 'NUM' at the end of string 'some_your_text_NUM'
  60. JOB_NUM=$( echo ${JOB_NAME} | sed -n -r 's/^.*_([0-9]+)$/\1/p' )
  61. [ -z ${JOB_NUM} ] && die "JOB_NUM is bad"
  62. # count number of the jobs
  63. NUM_OF_JOBS=$( grep -c -E "^${JOB_PATTERN}_[0-9]+:$" "${IDF_PATH}/.gitlab-ci.yml" )
  64. [ -z ${NUM_OF_JOBS} ] && die "NUM_OF_JOBS is bad"
  65. # count number of examples
  66. NUM_OF_EXAMPLES=$( find ${IDF_PATH}/examples/ -type f -name Makefile | wc -l )
  67. [ -z ${NUM_OF_EXAMPLES} ] && die "NUM_OF_EXAMPLES is bad"
  68. # separate intervals
  69. #57 / 5 == 12
  70. NUM_OF_EX_PER_JOB=$(( (${NUM_OF_EXAMPLES} + ${NUM_OF_JOBS} - 1) / ${NUM_OF_JOBS} ))
  71. [ -z ${NUM_OF_EX_PER_JOB} ] && die "NUM_OF_EX_PER_JOB is bad"
  72. # ex.: [0; 12); [12; 24); [24; 36); [36; 48); [48; 60)
  73. START_NUM=$(( ${JOB_NUM} * ${NUM_OF_EX_PER_JOB} ))
  74. [ -z ${START_NUM} ] && die "START_NUM is bad"
  75. END_NUM=$(( (${JOB_NUM} + 1) * ${NUM_OF_EX_PER_JOB} ))
  76. [ -z ${END_NUM} ] && die "END_NUM is bad"
  77. fi
  78. build_example () {
  79. local ID=$1
  80. shift
  81. local MAKE_FILE=$1
  82. shift
  83. local EXAMPLE_DIR=$(dirname "${MAKE_FILE}")
  84. local EXAMPLE_NAME=$(basename "${EXAMPLE_DIR}")
  85. echo "Building ${EXAMPLE_NAME} as ${ID}..."
  86. mkdir -p "example_builds/${ID}"
  87. cp -r "${EXAMPLE_DIR}" "example_builds/${ID}"
  88. pushd "example_builds/${ID}/${EXAMPLE_NAME}"
  89. # be stricter in the CI build than the default IDF settings
  90. export EXTRA_CFLAGS="-Werror -Werror=deprecated-declarations"
  91. export EXTRA_CXXFLAGS=${EXTRA_CFLAGS}
  92. # build non-verbose first
  93. local BUILDLOG=${PWD}/examplebuild.${ID}.log
  94. (
  95. make MAKEFLAGS= clean &&
  96. make MAKEFLAGS= defconfig &&
  97. make all &&
  98. make print_flash_cmd | tail -n 1 > build/download.config
  99. ) &> >(tee -a "${BUILDLOG}") || {
  100. RESULT=$?; FAILED_EXAMPLES+=" ${EXAMPLE_NAME}"
  101. make MAKEFLAGS= V=1 clean defconfig && make V=1 # verbose output for errors
  102. }
  103. popd
  104. if grep ": warning:" "${BUILDLOG}" 2>&1 >> "${LOG_WARNINGS}"; then
  105. [ $RESULT -eq 0 ] && RESULT=$RESULT_WARNINGS
  106. FAILED_EXAMPLES+=" ${EXAMPLE_NAME} (warnings)"
  107. fi
  108. grep -i error "${BUILDLOG}" 2>&1 >> "${LOG_WARNINGS}" || :
  109. rm -f "${BUILDLOG}"
  110. }
  111. EXAMPLE_NUM=0
  112. find ${IDF_PATH}/examples/ -type f -name Makefile | sort | \
  113. while read FN
  114. do
  115. if [[ $EXAMPLE_NUM -lt $START_NUM || $EXAMPLE_NUM -ge $END_NUM ]]
  116. then
  117. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  118. continue
  119. fi
  120. echo ">>> example [ ${EXAMPLE_NUM} ] - $FN"
  121. build_example "${EXAMPLE_NUM}" "${FN}"
  122. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  123. done
  124. # show warnings
  125. echo -e "\nFound issues:"
  126. # pattern is: not 'error.o' and not '-Werror'
  127. grep -v "error.o\|\-Werror" -- "${LOG_WARNINGS}" || echo -e "\tNone"
  128. rm -f "${LOG_WARNINGS}"
  129. if [ $RESULT -eq $RESULT_WARNINGS ]; then
  130. echo "Build would have passed, except for warnings."
  131. fi
  132. [ $RESULT -eq 0 ] || echo "Failed examples: $FAILED_EXAMPLES"
  133. echo -e "\nReturn code = $RESULT"
  134. exit $RESULT