build_examples_cmake.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 [ -z "${CI_NODE_TOTAL:-}" ]
  56. then
  57. START_NUM=0
  58. END_NUM=999
  59. else
  60. JOB_NUM=${CI_NODE_INDEX}
  61. # count number of the jobs
  62. NUM_OF_JOBS=${CI_NODE_TOTAL}
  63. # count number of examples
  64. NUM_OF_EXAMPLES=$( echo "${EXAMPLE_PATHS}" | wc -l )
  65. [ ${NUM_OF_EXAMPLES} -lt 100 ] && die "NUM_OF_EXAMPLES is bad"
  66. # separate intervals
  67. #57 / 5 == 12
  68. NUM_OF_EX_PER_JOB=$(( (${NUM_OF_EXAMPLES} + ${NUM_OF_JOBS} - 1) / ${NUM_OF_JOBS} ))
  69. [ -z ${NUM_OF_EX_PER_JOB} ] && die "NUM_OF_EX_PER_JOB is bad"
  70. # ex.: [0; 12); [12; 24); [24; 36); [36; 48); [48; 60)
  71. START_NUM=$(( (${JOB_NUM} - 1) * ${NUM_OF_EX_PER_JOB} ))
  72. [ -z ${START_NUM} ] && die "START_NUM is bad"
  73. END_NUM=$(( ${JOB_NUM} * ${NUM_OF_EX_PER_JOB} ))
  74. [ -z ${END_NUM} ] && die "END_NUM is bad"
  75. fi
  76. build_example () {
  77. local ID=$1
  78. shift
  79. local CMAKELISTS=$1
  80. shift
  81. local EXAMPLE_DIR=$(dirname "${CMAKELISTS}")
  82. local EXAMPLE_NAME=$(basename "${EXAMPLE_DIR}")
  83. echo "Building ${EXAMPLE_NAME} as ${ID}..."
  84. mkdir -p "example_builds/${ID}"
  85. cp -r "${EXAMPLE_DIR}" "example_builds/${ID}"
  86. pushd "example_builds/${ID}/${EXAMPLE_NAME}"
  87. # be stricter in the CI build than the default IDF settings
  88. export EXTRA_CFLAGS=${PEDANTIC_CFLAGS}
  89. export EXTRA_CXXFLAGS=${EXTRA_CFLAGS}
  90. # sdkconfig files are normally not checked into git, but may be present when
  91. # a developer runs this script locally
  92. rm -f sdkconfig
  93. # If sdkconfig.ci file is present, append it to sdkconfig.defaults,
  94. # replacing environment variables
  95. if [[ -f "$SDKCONFIG_DEFAULTS_CI" ]]; then
  96. cat $SDKCONFIG_DEFAULTS_CI | $IDF_PATH/tools/ci/envsubst.py >> sdkconfig.defaults
  97. fi
  98. # build non-verbose first
  99. local BUILDLOG=${LOG_PATH}/ex_${ID}_log.txt
  100. touch ${BUILDLOG}
  101. if [ "$EXAMPLE_NAME" != "idf_as_lib" ]; then
  102. idf.py fullclean >>${BUILDLOG} 2>&1 &&
  103. idf.py build >>${BUILDLOG} 2>&1
  104. else
  105. rm -rf build &&
  106. ./build.sh >>${BUILDLOG} 2>&1
  107. fi ||
  108. {
  109. RESULT=$?; FAILED_EXAMPLES+=" ${EXAMPLE_NAME}" ;
  110. }
  111. cat ${BUILDLOG}
  112. popd
  113. grep -i "error\|warning" "${BUILDLOG}" 2>&1 | grep -v "error.c.obj" >> "${LOG_SUSPECTED}" || :
  114. }
  115. EXAMPLE_NUM=0
  116. echo "Current job will build example ${START_NUM} - ${END_NUM}"
  117. for EXAMPLE_PATH in ${EXAMPLE_PATHS}
  118. do
  119. if [[ $EXAMPLE_NUM -lt $START_NUM || $EXAMPLE_NUM -ge $END_NUM ]]
  120. then
  121. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  122. continue
  123. fi
  124. echo ">>> example [ ${EXAMPLE_NUM} ] - $EXAMPLE_PATH"
  125. build_example "${EXAMPLE_NUM}" "${EXAMPLE_PATH}"
  126. EXAMPLE_NUM=$(( $EXAMPLE_NUM + 1 ))
  127. done
  128. # show warnings
  129. echo -e "\nFound issues:"
  130. # Ignore the next messages:
  131. # "error.o" or "-Werror" in compiler's command line
  132. # "reassigning to symbol" or "changes choice state" in sdkconfig
  133. # 'Compiler and toochain versions is not supported' from crosstool_version_check.cmake
  134. IGNORE_WARNS="\
  135. library/error\.o\
  136. \|\ -Werror\
  137. \|error\.d\
  138. \|reassigning to symbol\
  139. \|changes choice state\
  140. \|crosstool_version_check\.cmake\
  141. \|Python 3 versions older than 3.6 are not supported\
  142. \|Python 2 is deprecated and will be removed in future versions\
  143. "
  144. sort -u "${LOG_SUSPECTED}" | grep -v "${IGNORE_WARNS}" \
  145. && RESULT=$RESULT_ISSUES \
  146. || echo -e "\tNone"
  147. [ -z ${FAILED_EXAMPLES} ] || echo -e "\nThere are errors in the next examples: $FAILED_EXAMPLES"
  148. [ $RESULT -eq 0 ] || echo -e "\nFix all warnings and errors above to pass the test!"
  149. echo -e "\nReturn code = $RESULT"
  150. exit $RESULT