build_all_examples 8.5 KB

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