find_apps_build_apps.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env bash
  2. #
  3. # Find apps and build apps for example_test, custom_test, and unit_test
  4. #
  5. # Runs as part of CI process.
  6. #
  7. # -----------------------------------------------------------------------------
  8. # Safety settings (see https://gist.github.com/ilg-ul/383869cbb01f61a51c4d).
  9. if [[ -n ${DEBUG_SHELL} ]]; then
  10. set -x # Activate the expand mode if DEBUG is anything but empty.
  11. fi
  12. if [ -z ${CI_NODE_TOTAL} ]; then
  13. CI_NODE_TOTAL=1
  14. echo "Assuming CI_NODE_TOTAL=${CI_NODE_TOTAL}"
  15. fi
  16. if [ -z ${CI_NODE_INDEX} ]; then
  17. # Gitlab uses a 1-based index
  18. CI_NODE_INDEX=1
  19. echo "Assuming CI_NODE_INDEX=${CI_NODE_INDEX}"
  20. fi
  21. set -o errexit # Exit if command failed.
  22. set -o pipefail # Exit if pipe failed.
  23. set -o nounset # Exit if variable not set.
  24. export PATH="$IDF_PATH/tools/ci:$IDF_PATH/tools:$PATH"
  25. # -----------------------------------------------------------------------------
  26. die() {
  27. echo "${1:-"Unknown Error"}" 1>&2
  28. exit 1
  29. }
  30. [ -d ${BUILD_PATH} ] || mkdir -p ${BUILD_PATH}
  31. [ -d ${LOG_PATH} ] || mkdir -p ${LOG_PATH}
  32. [ -f ${SIZE_INFO_LOCATION} ] && rm ${SIZE_INFO_LOCATION}
  33. export REALPATH=realpath
  34. if [ "$(uname -s)" = "Darwin" ]; then
  35. export REALPATH=grealpath
  36. fi
  37. # Convert LOG_PATH and BUILD_PATH to relative, to make the json file less verbose.
  38. BUILD_PATH=$(${REALPATH} --relative-to ${IDF_PATH} ${BUILD_PATH})
  39. LOG_PATH=$(${REALPATH} --relative-to ${IDF_PATH} ${LOG_PATH})
  40. ALL_BUILD_LIST_JSON="${BUILD_PATH}/list.json"
  41. JOB_BUILD_LIST_JSON="${BUILD_PATH}/list_job_${CI_NODE_INDEX}.json"
  42. # -----------------------------------------------------------------------------
  43. # common variables, will specify special cases later
  44. WORK_DIR="--work-dir ${BUILD_PATH}/@f/@w/@t"
  45. BUILD_DIR="build"
  46. BUILD_LOG="${LOG_PATH}/@f_@w.txt"
  47. CONFIG="--config sdkconfig.ci=default
  48. --config sdkconfig.ci.*=
  49. --config =default"
  50. export EXTRA_CFLAGS="${PEDANTIC_CFLAGS}"
  51. export EXTRA_CXXFLAGS="${PEDANTIC_CXXFLAGS}"
  52. # --config rules above explained:
  53. # 1. If sdkconfig.ci exists, use it build the example with configuration name "default"
  54. # 2. If sdkconfig.ci.* exists, use it to build the "*" configuration
  55. # 3. If none of the above exist, build the default configuration under the name "default"
  56. # --work-dir and --build-log above uses "placeholders" @x:
  57. # - @f: full path to the test with slashes replaced with underscores
  58. # - @w: wildcard used as config name
  59. # - @t: target name
  60. # so the workdir .../@f/@w/@t would expand to e.g. tools_test_apps_system_startup/default/esp32
  61. # -----------------------------------------------------------------------------
  62. # Example tests specific settings
  63. if [ "${TEST_TYPE}" = "example_test" ]; then
  64. export EXTRA_CFLAGS="${PEDANTIC_CFLAGS:-}"
  65. export EXTRA_CXXFLAGS="${PEDANTIC_CXXFLAGS:-}"
  66. EXTRA_ARGS="--app-list ${SCAN_TEST_JSON}"
  67. # -----------------------------------------------------------------------------
  68. # Custom tests specific settings
  69. elif [ "${TEST_TYPE}" = "custom_test" ]; then
  70. EXTRA_ARGS="--app-list ${SCAN_TEST_JSON}"
  71. # -----------------------------------------------------------------------------
  72. # Unit tests specific settings
  73. elif [ "${TEST_TYPE}" = "unit_test" ]; then
  74. ALL_BUILD_LIST_JSON="${BUILD_PATH}/${IDF_TARGET}/list.json"
  75. JOB_BUILD_LIST_JSON="${BUILD_PATH}/${IDF_TARGET}/list_job_${CI_NODE_INDEX}.json"
  76. mkdir -p ${BUILD_PATH}/${IDF_TARGET}
  77. mkdir -p ${OUTPUT_PATH}/${IDF_TARGET}
  78. WORK_DIR=""
  79. BUILD_DIR="builds/@t/@w"
  80. BUILD_LOG="${LOG_PATH}/@w.txt"
  81. APP_FOLDER="tools/unit-test-app"
  82. EXTRA_ARGS="
  83. -p ${APP_FOLDER}
  84. --build-system ${BUILD_SYSTEM}
  85. --target ${IDF_TARGET}
  86. --recursive
  87. "
  88. CONFIG="--config configs/*="
  89. # -----------------------------------------------------------------------------
  90. else
  91. die "TEST_TYPE should only be one of {example_test, custom_test, unit_test}"
  92. fi
  93. echo "$TEST_TYPE running for target $IDF_TARGET"
  94. cd ${IDF_PATH}
  95. # This part of the script produces the same result for all the build jobs.
  96. #
  97. # It may be moved to a separate stage (pre-build) later, then the build jobs
  98. # will receive ${BUILD_LIST_JSON} file as an artifact.
  99. #
  100. # If changing the work-dir or build-dir, remember to update the "artifacts" in
  101. # gitlab-ci configs, and IDFApp.py.
  102. ${IDF_PATH}/tools/find_apps.py \
  103. -vv \
  104. --format json \
  105. ${WORK_DIR} \
  106. --build-dir ${BUILD_DIR} \
  107. --build-log ${BUILD_LOG} \
  108. --output ${ALL_BUILD_LIST_JSON} \
  109. ${EXTRA_ARGS} \
  110. ${CONFIG}
  111. # The part below is where the actual builds happen
  112. ${IDF_PATH}/tools/build_apps.py \
  113. -vv \
  114. --format json \
  115. --keep-going \
  116. --parallel-count ${CI_NODE_TOTAL} \
  117. --parallel-index ${CI_NODE_INDEX} \
  118. --output-build-list ${JOB_BUILD_LIST_JSON} \
  119. --size-info ${SIZE_INFO_LOCATION} \
  120. ${ALL_BUILD_LIST_JSON}
  121. # Check for build warnings
  122. ${IDF_PATH}/tools/ci/check_build_warnings.py -vv ${JOB_BUILD_LIST_JSON}
  123. if [ "${TEST_TYPE}" = "unit_test" ]; then
  124. # Copy build artifacts to output directory
  125. build_names=$(
  126. cd ${BUILD_PATH}/${IDF_TARGET}
  127. find . -maxdepth 1 \! -name . -prune -type d | cut -c 3-
  128. )
  129. for build_name in $build_names; do
  130. src=${BUILD_PATH}/${IDF_TARGET}/${build_name}
  131. dst=${OUTPUT_PATH}/${IDF_TARGET}/${build_name}
  132. echo "Copying artifacts from ${src} to ${dst}"
  133. rm -rf ${dst}
  134. mkdir -p ${dst}
  135. cp ${src}/{*.bin,*.elf,*.map,sdkconfig,flasher_args.json} ${dst}/
  136. mkdir -p ${dst}/bootloader
  137. cp ${src}/bootloader/*.bin ${dst}/bootloader/
  138. mkdir -p ${dst}/partition_table
  139. cp ${src}/partition_table/*.bin ${dst}/partition_table/
  140. done
  141. # Copy app list json files to build path
  142. mv ${BUILD_PATH}/${IDF_TARGET}/*.json ${BUILD_PATH}
  143. fi