build_examples_cmake.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. # Remove the initial space and instead use '\n'.
  31. IFS=$'\n\t'
  32. export PATH="$IDF_PATH/tools:$PATH" # for idf.py
  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. set -o nounset # Exit if variable not set.
  42. echo "build_examples running in ${PWD}"
  43. # only 0 or 1 arguments
  44. [ $# -le 1 ] || die "Have to run as $(basename $0) [<JOB_NAME>]"
  45. export BATCH_BUILD=1
  46. export V=0 # only build verbose if there's an error
  47. shopt -s lastpipe # Workaround for Bash to use variables in loops (http://mywiki.wooledge.org/BashFAQ/024)
  48. RESULT=0
  49. FAILED_EXAMPLES=""
  50. RESULT_ISSUES=22 # magic number result code for issues found
  51. LOG_SUSPECTED=${LOG_PATH}/common_log.txt
  52. touch ${LOG_SUSPECTED}
  53. SDKCONFIG_DEFAULTS_CI=sdkconfig.ci
  54. EXAMPLE_PATHS=$( find ${IDF_PATH}/examples/ -type f -name CMakeLists.txt | grep -v "/components/" | grep -v "/main/" | sort )
  55. if [ $# -eq 0 ]
  56. then
  57. START_NUM=0
  58. END_NUM=999
  59. else
  60. JOB_NAME=$1
  61. # parse text prefix at the beginning of string 'some_your_text_NUM'
  62. # (will be 'some_your_text' without last '_')
  63. JOB_PATTERN=$( echo ${JOB_NAME} | sed -n -r 's/^(.*)_[0-9]+$/\1/p' )
  64. [ -z ${JOB_PATTERN} ] && die "JOB_PATTERN is bad"
  65. # parse number 'NUM' at the end of string 'some_your_text_NUM'
  66. # NOTE: Getting rid of the leading zero to get the decimal
  67. JOB_NUM=$( echo ${JOB_NAME} | sed -n -r 's/^.*_0*([0-9]+)$/\1/p' )
  68. [ -z ${JOB_NUM} ] && die "JOB_NUM is bad"
  69. # count number of the jobs
  70. NUM_OF_JOBS=$( grep -c -E "^${JOB_PATTERN}_[0-9]+:$" "${IDF_PATH}/.gitlab-ci.yml" )
  71. [ -z ${NUM_OF_JOBS} ] && die "NUM_OF_JOBS is bad"
  72. # count number of examples
  73. NUM_OF_EXAMPLES=$( echo "${EXAMPLE_PATHS}" | wc -l )
  74. [ ${NUM_OF_EXAMPLES} -lt 100 ] && die "NUM_OF_EXAMPLES is bad"
  75. # separate intervals
  76. #57 / 5 == 12
  77. NUM_OF_EX_PER_JOB=$(( (${NUM_OF_EXAMPLES} + ${NUM_OF_JOBS} - 1) / ${NUM_OF_JOBS} ))
  78. [ -z ${NUM_OF_EX_PER_JOB} ] && die "NUM_OF_EX_PER_JOB is bad"
  79. # ex.: [0; 12); [12; 24); [24; 36); [36; 48); [48; 60)
  80. START_NUM=$(( ${JOB_NUM} * ${NUM_OF_EX_PER_JOB} ))
  81. [ -z ${START_NUM} ] && die "START_NUM is bad"
  82. END_NUM=$(( (${JOB_NUM} + 1) * ${NUM_OF_EX_PER_JOB} ))
  83. [ -z ${END_NUM} ] && die "END_NUM is bad"
  84. fi
  85. build_example () {
  86. local ID=$1
  87. shift
  88. local CMAKELISTS=$1
  89. shift
  90. local EXAMPLE_DIR=$(dirname "${CMAKELISTS}")
  91. local EXAMPLE_NAME=$(basename "${EXAMPLE_DIR}")
  92. echo "Building ${EXAMPLE_NAME} as ${ID}..."
  93. mkdir -p "example_builds/${ID}"
  94. cp -r "${EXAMPLE_DIR}" "example_builds/${ID}"
  95. pushd "example_builds/${ID}/${EXAMPLE_NAME}"
  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. cat $SDKCONFIG_DEFAULTS_CI | $IDF_PATH/tools/ci/envsubst.py >> sdkconfig.defaults
  106. fi
  107. # build non-verbose first
  108. local BUILDLOG=${LOG_PATH}/ex_${ID}_log.txt
  109. touch ${BUILDLOG}
  110. if [ "$EXAMPLE_NAME" != "idf_as_lib" ]; then
  111. idf.py fullclean >>${BUILDLOG} 2>&1 &&
  112. idf.py build >>${BUILDLOG} 2>&1
  113. else
  114. rm -rf build &&
  115. ./build.sh >>${BUILDLOG} 2>&1
  116. fi &&
  117. cp build/flash_project_args build/download.config || # backwards compatible download.config filename
  118. {
  119. RESULT=$?; FAILED_EXAMPLES+=" ${EXAMPLE_NAME}" ;
  120. }
  121. cat ${BUILDLOG}
  122. popd
  123. grep -i "error\|warning" "${BUILDLOG}" 2>&1 | grep -v "error.c.obj" >> "${LOG_SUSPECTED}" || :
  124. }
  125. EXAMPLE_NUM=0
  126. for EXAMPLE_PATH in ${EXAMPLE_PATHS}
  127. do
  128. if [[ $EXAMPLE_NUM -lt $START_NUM || $EXAMPLE_NUM -ge $END_NUM ]]
  129. then
  130. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  131. continue
  132. fi
  133. echo ">>> example [ ${EXAMPLE_NUM} ] - $EXAMPLE_PATH"
  134. build_example "${EXAMPLE_NUM}" "${EXAMPLE_PATH}"
  135. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  136. done
  137. # show warnings
  138. echo -e "\nFound issues:"
  139. # Ignore the next messages:
  140. # "error.o" or "-Werror" in compiler's command line
  141. # "reassigning to symbol" or "changes choice state" in sdkconfig
  142. # 'Compiler and toochain versions is not supported' from crosstool_version_check.cmake
  143. IGNORE_WARNS="\
  144. library/error\.o\
  145. \|\ -Werror\
  146. \|error\.d\
  147. \|reassigning to symbol\
  148. \|changes choice state\
  149. \|crosstool_version_check\.cmake\
  150. "
  151. sort -u "${LOG_SUSPECTED}" | grep -v "${IGNORE_WARNS}" \
  152. && RESULT=$RESULT_ISSUES \
  153. || echo -e "\tNone"
  154. [ -z ${FAILED_EXAMPLES} ] || echo -e "\nThere are errors in the next examples: $FAILED_EXAMPLES"
  155. [ $RESULT -eq 0 ] || echo -e "\nFix all warnings and errors above to pass the test!"
  156. echo -e "\nReturn code = $RESULT"
  157. exit $RESULT