build_all_examples 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env bash
  2. #
  3. # Usage:
  4. #
  5. # build_all_examples [-a|--archive] - Copy the binary to the export location
  6. # [-B|--base] - Base path of configurations, overriding -b
  7. # [-b|--branch=<branch>] - Branch to fetch from Configurations repo (import-2.1.x)
  8. # [-c|--continue] - Continue the paused build
  9. # [-d|-v|--debug] - Print extra debug output (after)
  10. # [-e|--export=N] - Set CONFIG_EXPORT and export to the export location
  11. # [-f|--nofail] - Don't stop on a failed build
  12. # [-h|--help] - Print usage and exit
  13. # [-l|--limit=#] - Limit the number of builds in this run
  14. # [-m|--many] - Build all the environments for each example
  15. # [-n|--nobuild] - Don't actually build anything
  16. # [-o|--output] - Redirect export / archiving to another location
  17. # (By default export to origin config folders)
  18. # [-p|--purge] - Purge the status file and start over
  19. # [-r|--resume=<path>] - Start at some config in the filesystem order
  20. # [-s|--skip] - Continue the paused build, skipping one
  21. #
  22. HERE=`dirname $0`
  23. PATH="$HERE:$PATH"
  24. . mfutil
  25. GITREPO=https://github.com/MarlinFirmware/Configurations.git
  26. STAT_FILE=./.pio/.buildall
  27. usage() { echo "Usage:
  28. build_all_examples [-a|--archive] - Copy the binary to the export location
  29. [-B|--base] - Base path of configurations, overriding -b
  30. [-b|--branch=<branch>] - Branch to fetch from Configurations repo (import-2.1.x)
  31. [-c|--continue] - Continue the paused build
  32. [-d|-v|--debug] - Print extra debug output (after)
  33. [-e|--export=N] - Set CONFIG_EXPORT and export to the export location
  34. [-f|--nofail] - Don't stop on a failed build
  35. [-h|--help] - Print usage and exit
  36. [-l|--limit=#] - Limit the number of builds in this run
  37. [-m|--many] - Build all the environments for each example
  38. [-n|--nobuild] - Don't actually build anything
  39. [-o|--output] - Redirect export / archiving to another location
  40. (By default export to origin config folders)
  41. [-p|--purge] - Purge the status file and start over
  42. [-r|--resume=<path>] - Start at some config in the filesystem order
  43. [-s|--skip] - Continue the paused build, skipping one
  44. "
  45. }
  46. # Assume the most recent configs
  47. BRANCH=import-2.1.x
  48. unset FIRST_CONF
  49. EXIT_USAGE=
  50. LIMIT=1000
  51. while getopts 'aB:b:cde:fhl:mno:pr:sv-:' OFLAG; do
  52. case "${OFLAG}" in
  53. a) ARCHIVE=1 ; bugout "Archiving" ;;
  54. B) CBASE=${OPTARG%/} ; bugout "Base: $CBASE" ;;
  55. b) BRANCH=$OPTARG ; bugout "Branch: $BRANCH" ;;
  56. c) CONTINUE=1 ; bugout "Continue" ;;
  57. d|v) DEBUG=1 ; bugout "Debug ON" ;;
  58. e) CEXPORT=$OPTARG ; bugout "Export $CEXPORT" ;;
  59. f) NOFAIL=1 ; bugout "Continue on Fail" ;;
  60. h) EXIT_USAGE=1 ; break ;;
  61. l) LIMIT=$OPTARG ; bugout "Limit to $LIMIT build(s)" ;;
  62. m) MANY=1 ; bugout "Many Envs" ;;
  63. n) DRYRUN=1 ; bugout "Dry Run" ;;
  64. o) OUTBASE="${OPTARG%/}" ; bugout "Archive to $OUTBASE" ;;
  65. p) PURGE=1 ; bugout "Purge stat file" ;;
  66. r) ISRES=1 ; FIRST_CONF=$OPTARG ; bugout "Resume: $FIRST_CONF" ;;
  67. s) CONTSKIP=1 ; bugout "Continue, skipping" ;;
  68. -) ONAM="${OPTARG%%=*}" ; OVAL="${OPTARG#*=}"
  69. case "$ONAM" in
  70. archive) ARCHIVE=1 ; bugout "Archiving" ;;
  71. base) CBASE=${OVAL%/} ; bugout "Base: $CBASE" ;;
  72. branch) BRANCH=$OVAL ; bugout "Branch: $BRANCH" ;;
  73. many) MANY=1 ; bugout "Many Envs" ;;
  74. nofail) NOFAIL=1 ; bugout "Continue on Fail" ;;
  75. resume) ISRES=1 ; FIRST_CONF=$OVAL ; bugout "Resume: $FIRST_CONF" ;;
  76. continue) CONTINUE=1 ; bugout "Continue" ;;
  77. skip) CONTSKIP=1 ; bugout "Continue, skipping" ;;
  78. export) CEXPORT=$OVAL ; bugout "Export $EXPORT" ;;
  79. output) OUTBASE="${OVAL%/}" ; bugout "Archive to $OUTBASE" ;;
  80. limit) LIMIT=$OVAL ; bugout "Limit to $LIMIT build(s)" ;;
  81. help) [[ -z "$OVAL" ]] || perror "option can't take value $OVAL" $ONAM ; EXIT_USAGE=1 ;;
  82. debug) DEBUG=1 ; bugout "Debug ON" ;;
  83. nobuild) DRYRUN=1 ; bugout "Dry Run" ;;
  84. purge) PURGE=1 ; bugout "Purge stat file" ;;
  85. *) EXIT_USAGE=2 ; echo "$SELF: unrecognized option \`--$ONAM'" ; break ;;
  86. esac
  87. ;;
  88. *) EXIT_USAGE=2 ; break ;;
  89. esac
  90. done
  91. shift $((OPTIND - 1))
  92. # Check for mixed continue, skip, resume arguments. Only one should be used.
  93. ((CONTINUE + CONTSKIP + ISRES + PURGE > 1)) && { echo "Don't mix -c, -p, -s, and -r options" ; echo ; EXIT_USAGE=2 ; }
  94. # Exit with helpful usage information
  95. ((EXIT_USAGE)) && { usage ; let EXIT_USAGE-- ; exit $EXIT_USAGE ; }
  96. echo
  97. echo "This script downloads all example configs and attempts to build them."
  98. echo "On failure the last-built configs are left in your working copy."
  99. echo "Restore your configs with 'git checkout -f' or 'git reset --hard HEAD'."
  100. echo
  101. [[ -n $PURGE ]] && rm -f "$STAT_FILE"
  102. [[ -z $FIRST_CONF && -f "$STAT_FILE" ]] && IFS='*' read BRANCH FIRST_CONF <"$STAT_FILE"
  103. # If -c is given start from the last attempted build
  104. if ((CONTINUE)); then
  105. if [[ -z $BRANCH || -z $FIRST_CONF ]]; then
  106. echo "Nothing to continue"
  107. exit
  108. fi
  109. elif ((CONTSKIP)); then
  110. if [[ -n $BRANCH && -n $FIRST_CONF ]]; then
  111. SKIP_CONF=1
  112. else
  113. echo "Nothing to skip"
  114. exit
  115. fi
  116. fi
  117. # Check if the current repository has unmerged changes
  118. if ((SKIP_CONF)); then
  119. echo "Skipping $FIRST_CONF"
  120. elif [[ -n $FIRST_CONF ]]; then
  121. echo "Resuming from $FIRST_CONF"
  122. else
  123. git diff --quiet || { echo "The working copy is modified. Commit or stash changes before proceeding."; exit ; }
  124. fi
  125. # Check for the given base path
  126. if [[ -n $CBASE ]]; then
  127. CBASE="${CBASE/#\~/$HOME}"
  128. [[ -d "$CBASE" ]] || { echo "Given base -B $CBASE not found." ; exit ; }
  129. else
  130. # Make a Configurations temporary folder if needed
  131. CBASE=./.pio/build-$BRANCH
  132. [[ -d "$CBASE" ]] || mkdir -p "$CBASE"
  133. # Download the specified Configurations branch if needed
  134. if [[ ! -e "$CBASE/README.md" ]]; then
  135. echo "Fetching Configurations from GitHub to $CBASE"
  136. git clone --depth=1 --single-branch --branch "$BRANCH" $GITREPO "$CBASE" || { echo "Failed to clone the configuration repository"; exit ; }
  137. fi
  138. fi
  139. # Build
  140. echo -e "=====================\nProceed with builds...\n====================="
  141. shopt -s nullglob
  142. export PAUSE=1
  143. # Get a list of all folders that contain a file matching "Configuration*.h"
  144. find -ds "$CBASE"/config/examples -type d -name 'Configuration.h' -o -name 'Configuration_adv.h' -print0 | while IFS= read -r -d $'\0' CONF; do
  145. # Remove the file name and slash from the end of the path
  146. CONF=${CONF%/*}
  147. # Get a config's directory name
  148. DIR=${CONF#$CBASE/config/examples/}
  149. # If looking for a config, skip others
  150. [[ $FIRST_CONF ]] && [[ $FIRST_CONF != $DIR && "$FIRST_CONF/" != $DIR ]] && { ((DEBUG)) && echo "[SKIP] $DIR" ; continue ; }
  151. # Once found, stop looking
  152. unset FIRST_CONF
  153. # If skipping, don't build the found one
  154. [[ $SKIP_CONF ]] && { unset SKIP_CONF ; continue ; }
  155. # Either Configuration.h or Configuration_adv.h must exist
  156. [[ -f "$CONF"/Configuration.h || -f "$CONF"/Configuration_adv.h ]] || { echo "[NONE] $DIR" ; continue ; }
  157. # Command arguments for 'build_example'
  158. CARGS=("-b" "$CBASE" "-c" "$DIR")
  159. # Exporting? Add -e argument
  160. ((CEXPORT)) && CARGS+=("-e" "$CEXPORT")
  161. # Build many environments? Add -m argument
  162. ((MANY)) && CARGS+=("-m")
  163. # Continue on fail? Add -f argument
  164. ((NOFAIL)) && CARGS+=("-f")
  165. # Archive the build? Add -a argument
  166. ((ARCHIVE)) && CARGS+=("-a")
  167. # Redirecting the export/archive output? Add -o argument
  168. [[ -n $OUTBASE ]] && CARGS+=("-o" "$OUTBASE")
  169. # Build or print build command for --nobuild
  170. if ((DRYRUN)); then
  171. echo -e "\033[0;32m[DRYRUN] build_example ${CARGS[@]}\033[0m"
  172. else
  173. # Remember where we are in case of failure
  174. echo "${BRANCH}*${DIR}" >"$STAT_FILE"
  175. ((DEBUG)) && echo "build_example ${CARGS[@]}"
  176. # Invoke build_example
  177. build_example "${CARGS[@]}" || { echo "Failed to build $DIR" ; exit ; }
  178. fi
  179. echo
  180. ((--LIMIT)) || { echo "Specified limit reached" ; break ; }
  181. echo
  182. export PAUSE=0
  183. done
  184. echo "Exiting"
  185. # Delete the build state if not paused early
  186. ((PAUSE)) || rm -f "$STAT_FILE"