build_all_examples 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env bash
  2. #
  3. # Usage:
  4. #
  5. # build_all_examples [-b|--branch=<branch>] - Branch to fetch from Configurations repo
  6. # [-c|--continue] - Continue the paused build
  7. # [-d|--debug] - Print extra debug output
  8. # [-i|--ini] - Archive ini/json/yml files in the temp config folder
  9. # [-l|--limit=#] - Limit the number of builds in this run
  10. # [-n|--nobuild] - Don't actually build anything.
  11. # [-r|--resume=<path>] - Start at some config in the filesystem order
  12. # [-s|--skip] - Do the thing
  13. #
  14. # build_all_examples [...] branch [resume-from]
  15. #
  16. . mfutil
  17. GITREPO=https://github.com/MarlinFirmware/Configurations.git
  18. STAT_FILE=./.pio/.buildall
  19. usage() { echo "
  20. Usage: $SELF [-b|--branch=<branch>] [-d|--debug] [-i|--ini] [-r|--resume=<path>]
  21. $SELF [-b|--branch=<branch>] [-d|--debug] [-i|--ini] [-c|--continue]
  22. $SELF [-b|--branch=<branch>] [-d|--debug] [-i|--ini] [-s|--skip]
  23. $SELF [-b|--branch=<branch>] [-d|--debug] [-n|--nobuild]
  24. $SELF [...] branch [resume-point]
  25. "
  26. }
  27. # Assume the most recent configs
  28. BRANCH=import-2.1.x
  29. unset FIRST_CONF
  30. EXIT_USAGE=
  31. LIMIT=1000
  32. while getopts 'b:cdhil:nqr:sv-:' OFLAG; do
  33. case "${OFLAG}" in
  34. b) BRANCH=$OPTARG ; bugout "Branch: $BRANCH" ;;
  35. r) FIRST_CONF="$OPTARG" ; bugout "Resume: $FIRST_CONF" ;;
  36. c) CONTINUE=1 ; bugout "Continue" ;;
  37. s) CONTSKIP=1 ; bugout "Continue, skipping" ;;
  38. i) COPY_INI=1 ; bugout "Archive INI/JSON/YML files" ;;
  39. h) EXIT_USAGE=1 ; break ;;
  40. l) LIMIT=$OPTARG ; bugout "Limit to $LIMIT build(s)" ;;
  41. d|v) DEBUG=1 ; bugout "Debug ON" ;;
  42. n) DRYRUN=1 ; bugout "Dry Run" ;;
  43. -) IFS="=" read -r ONAM OVAL <<< "$OPTARG"
  44. case "$ONAM" in
  45. branch) BRANCH=$OVAL ; bugout "Branch: $BRANCH" ;;
  46. resume) FIRST_CONF="$OVAL" ; bugout "Resume: $FIRST_CONF" ;;
  47. continue) CONTINUE=1 ; bugout "Continue" ;;
  48. skip) CONTSKIP=2 ; bugout "Continue, skipping" ;;
  49. limit) LIMIT=$OVAL ; bugout "Limit to $LIMIT build(s)" ;;
  50. ini) COPY_INI=1 ; bugout "Archive INI/JSON/YML files" ;;
  51. help) [[ -z "$OVAL" ]] || perror "option can't take value $OVAL" $ONAM ; EXIT_USAGE=1 ;;
  52. debug) DEBUG=1 ; bugout "Debug ON" ;;
  53. nobuild) DRYRUN=1 ; bugout "Dry Run" ;;
  54. *) EXIT_USAGE=2 ; echo "$SELF: unrecognized option \`--$ONAM'" ; break ;;
  55. esac
  56. ;;
  57. *) EXIT_USAGE=2 ; break ;;
  58. esac
  59. done
  60. # Extra arguments count as BRANCH, FIRST_CONF
  61. shift $((OPTIND - 1))
  62. [[ $# > 0 ]] && { BRANCH=$1 ; shift 1 ; bugout "BRANCH=$BRANCH" ; }
  63. [[ $# > 0 ]] && { FIRST_CONF=$1 ; shift 1 ; bugout "FIRST_CONF=$FIRST_CONF" ; }
  64. [[ $# > 0 ]] && { EXIT_USAGE=2 ; echo "too many arguments" ; }
  65. ((EXIT_USAGE)) && { usage ; let EXIT_USAGE-- ; exit $EXIT_USAGE ; }
  66. echo "This script downloads each Configuration and attempts to build it."
  67. echo "On failure the last-built configs will be left in your working copy."
  68. echo "Restore your configs with 'git checkout -f' or 'git reset --hard HEAD'."
  69. if [[ -f "$STAT_FILE" ]]; then
  70. IFS='*' read BRANCH FIRST_CONF <"$STAT_FILE"
  71. fi
  72. # If -c is given start from the last attempted build
  73. if ((CONTINUE)); then
  74. if [[ -z $BRANCH || -z $FIRST_CONF ]]; then
  75. echo "Nothing to continue"
  76. exit
  77. fi
  78. elif ((CONTSKIP)); then
  79. if [[ -n $BRANCH && -n $FIRST_CONF ]]; then
  80. SKIP_CONF=1
  81. else
  82. echo "Nothing to skip"
  83. exit
  84. fi
  85. fi
  86. # Check if the current repository has unmerged changes
  87. if [[ $SKIP_CONF ]]; then
  88. echo "Skipping $FIRST_CONF"
  89. elif [[ $FIRST_CONF ]]; then
  90. echo "Resuming from $FIRST_CONF"
  91. else
  92. git diff --quiet || { echo "The working copy is modified. Commit or stash changes before proceeding."; exit ; }
  93. fi
  94. # Create a temporary folder inside .pio
  95. TMP=./.pio/build-$BRANCH
  96. [[ -d "$TMP" ]] || mkdir -p $TMP
  97. # Download Configurations into the temporary folder
  98. if [[ ! -e "$TMP/README.md" ]]; then
  99. echo "Fetching Configurations from GitHub to $TMP"
  100. git clone --depth=1 --single-branch --branch "$BRANCH" $GITREPO "$TMP" || { echo "Failed to clone the configuration repository"; exit ; }
  101. else
  102. echo "Using cached Configurations at $TMP"
  103. fi
  104. echo -e "Start build...\n====================="
  105. shopt -s nullglob
  106. IFS='
  107. '
  108. CONF_TREE=$( ls -d "$TMP"/config/examples/*/ "$TMP"/config/examples/*/*/ "$TMP"/config/examples/*/*/*/ "$TMP"/config/examples/*/*/*/*/ | grep -vE ".+\.(\w+)$" )
  109. for CONF in $CONF_TREE ; do
  110. # Get a config's directory name
  111. DIR=$( echo $CONF | sed "s|$TMP/config/examples/||" )
  112. # If looking for a config, skip others
  113. [[ $FIRST_CONF ]] && [[ $FIRST_CONF != $DIR && "$FIRST_CONF/" != $DIR ]] && continue
  114. # Once found, stop looking
  115. unset FIRST_CONF
  116. # If skipping, don't build the found one
  117. [[ $SKIP_CONF ]] && { unset SKIP_CONF ; continue ; }
  118. # ...if skipping, don't build this one
  119. compgen -G "${CONF}Con*.h" > /dev/null || continue
  120. # Build or print build command for --nobuild
  121. if [[ $DRYRUN ]]; then
  122. echo -e "\033[0;32m[DRYRUN] build_example internal \"$TMP\" \"$DIR\"\033[0m"
  123. else
  124. # Remember where we are in case of failure
  125. echo "${BRANCH}*${DIR}" >"$STAT_FILE"
  126. # Build folder is unknown so delete all report files
  127. if [[ $COPY_INI ]]; then
  128. IFIND='find ./.pio/build/ -name "config.ini" -o -name "schema.json" -o -name "schema.yml"'
  129. $IFIND -exec rm "{}" \;
  130. fi
  131. ((DEBUG)) && echo "\"$HERE/build_example\" internal \"$TMP\" \"$DIR\""
  132. "$HERE/build_example" internal "$TMP" "$DIR" || { echo "Failed to build $DIR"; exit ; }
  133. # Build folder is unknown so copy all report files
  134. [[ $COPY_INI ]] && $IFIND -exec cp "{}" "$CONF" \;
  135. fi
  136. ((--LIMIT)) || { echo "Limit reached" ; PAUSE=1 ; break ; }
  137. done
  138. # Delete the build state if not paused early
  139. [[ $PAUSE ]] || rm "$STAT_FILE"
  140. # Delete the temp folder if not preserving generated INI files
  141. if [[ -e "$TMP/config/examples" ]]; then
  142. if [[ $COPY_INI ]]; then
  143. OPEN=$( which gnome-open xdg-open open | head -n1 )
  144. $OPEN "$TMP"
  145. elif [[ ! $PAUSE ]]; then
  146. rm -rf "$TMP"
  147. fi
  148. fi