build_example 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env bash
  2. #
  3. # Usage:
  4. #
  5. # build_example -b|--base=<path> - Configurations root folder (e.g., ./.pio/build-BRANCH)
  6. # -c|--config=<rel> - Sub-path of the configs to build (within config/examples)
  7. # [-n|--index=N] - Which environment to build, by index (Based on pins.h comments)
  8. # [-e|--export=N] - Use CONFIG_EXPORT N to export the config to the export location
  9. # [-a|--archive] - Archive the build (to the export location)
  10. # [-o|--output] - Redirect export / archiving to another location
  11. # (By default export to origin config folder)
  12. # [-f|--nofail] - Don't stop on a failed build
  13. # [-w|--nowarn] - Suppress warnings with extra config options
  14. # [-r|--reveal] - Reveal the config/export folder after the build
  15. # [-h|--help] - Print usage and exit
  16. # [--allow] - Allow this script to run standalone
  17. #
  18. usage() { echo "Usage:
  19. build_example -b|--base=<path> - Configurations root folder (e.g., ./.pio/build-BRANCH)
  20. -c|--config=<rel> - Sub-path of the configs to build (within config/examples)
  21. [-n|--index=N] - Which environment to build, by index (Based on pins.h comments)
  22. [-e|--export=N] - Use CONFIG_EXPORT N to export the config to the export location
  23. [-a|--archive] - Archive the build (to the export location)
  24. [-o|--output] - Redirect export / archiving to another location
  25. (By default export to origin config folder)
  26. [-f|--nofail] - Don't stop on a failed build
  27. [-w|--nowarn] - Suppress warnings with extra config options
  28. [-r|--reveal] - Reveal the config/export folder after the build
  29. [-h|--help] - Print usage and exit
  30. [--allow] - Allow this script to run standalone
  31. "
  32. }
  33. HERE=`dirname $0`
  34. PATH="$HERE:$PATH"
  35. . mfutil
  36. annc() { echo -e "\033[0;32m$1\033[0m" ; }
  37. alrt() { echo -e "\033[0;31m$1\033[0m" ; }
  38. # Get arguments
  39. BUILD=./.pio/build
  40. CLEANER=
  41. ALLOW=
  42. ARCHIVE=
  43. BASE=
  44. CONFIG=
  45. REVEAL=
  46. EXPNUM=
  47. NOFAIL=
  48. OUTBASE=
  49. BUILDINDEX=1
  50. while getopts 'ab:c:e:fhin:o:r-:' OFLAG; do
  51. case "${OFLAG}" in
  52. a) ARCHIVE=1 ;;
  53. b) BASE="${OPTARG%/}" ;;
  54. c) CONFIG="${OPTARG%/}" ;;
  55. e) EXPNUM="$OPTARG" ;;
  56. n) BUILDINDEX="$OPTARG" ;;
  57. o) OUTBASE="${OPTARG%/}" ;;
  58. h) EXIT_USAGE=1 ; break ;;
  59. f) NOFAIL=1 ;;
  60. r) REVEAL=1 ;;
  61. -) ONAM="${OPTARG%%=*}" ; OVAL="${OPTARG#*=}"
  62. case "$ONAM" in
  63. archive) ARCHIVE=1 ;;
  64. allow) ALLOW=1 ;;
  65. base) BASE="${OVAL%/}" ;;
  66. config) CONFIG="${OVAL%/}" ;;
  67. index) BUILDINDEX="$OVAL" ;;
  68. export) EXPNUM="$OVAL" ;;
  69. output) OUTBASE="${OVAL%/}" ;;
  70. help) EXIT_USAGE=1 ; break ;;
  71. nofail) NOFAIL=1 ;;
  72. reveal) REVEAL=1 ;;
  73. *) EXIT_USAGE=2 ; echo "$SELF: unrecognized option \`--$ONAM'" ; break ;;
  74. esac
  75. ;;
  76. *) EXIT_USAGE=2 ; break ;;
  77. esac
  78. done
  79. shift $((OPTIND - 1))
  80. # Must be called from another script (or with --allow)
  81. [[ $ALLOW || $SHLVL -gt 2 ]] || { echo "Don't call this script directly, use build_all_examples instead." ; exit 1 ; }
  82. # Exit with helpful usage information
  83. ((EXIT_USAGE)) && { usage ; let EXIT_USAGE-- ; exit $EXIT_USAGE ; }
  84. # -b|--base and -c|--config are required
  85. [[ -z $BASE ]] && { echo "-b|--base is required" ; exit 1 ; }
  86. [[ -z $CONFIG ]] && { echo "-c|--config is required" ; exit 1 ; }
  87. # Expand ~ to $HOME in provided arguments
  88. BASE=${BASE/#\~/$HOME}
  89. CONFIG=${CONFIG/#\~/$HOME}
  90. # Make sure the examples exist
  91. SUB1="$BASE/config/examples"
  92. [[ -d "$SUB1" ]] || { echo "-b|--base $BASE doesn't contain config/examples" ; exit 1 ; }
  93. # Make sure the specific config folder exists
  94. SUB="$SUB1/$CONFIG"
  95. [[ -d "$SUB" ]] || { echo "-c|--config $CONFIG doesn't exist" ; exit 1 ; }
  96. # ...and contains Configuration.h or Configuration_adv.h
  97. [[ -f "$SUB"/Configuration.h || -f "$SUB"/Configuration_adv.h ]] || { echo "No configuration files found in $SUB" ; exit 1 ; }
  98. # Get the location for exports and archives
  99. if [[ -n $OUTBASE ]]; then
  100. ARCSUB="${OUTBASE/#\~/$HOME}/$CONFIG"
  101. mkdir -p "$ARCSUB"
  102. else
  103. ARCSUB="$SUB"
  104. fi
  105. # Delete any config files from previous builds
  106. rm -f Marlin/_Bootscreen.h Marlin/_Statusscreen.h
  107. # Copy configurations into the Marlin folder
  108. echo "Getting configuration files from $SUB"
  109. cp "$BASE"/config/default/*.h Marlin/
  110. cp "$SUB"/*.h Marlin/
  111. rm -f Marlin/Config.h Marlin/Config-export.h
  112. set -e
  113. # Strip #error lines from Configuration.h using
  114. awk 'NR < 20 || NR > 30 || !/#error/' Marlin/Configuration.h > Marlin/Configuration.h~
  115. mv Marlin/Configuration.h~ Marlin/Configuration.h
  116. # Hide several warnings when not exporting
  117. [[ -z $EXPNUM ]] && CLEANER=1
  118. # Suppress fatal warnings
  119. if ((CLEANER)); then
  120. opt_add NO_CONTROLLER_CUSTOM_WIRING_WARNING
  121. opt_add NO_AUTO_ASSIGN_WARNING
  122. opt_add NO_CREALITY_DRIVER_WARNING
  123. opt_add DIAG_JUMPERS_REMOVED
  124. opt_add DIAG_PINS_REMOVED
  125. opt_add NO_MK3_FAN_PINS_WARNING
  126. opt_add NO_USER_FEEDBACK_WARNING
  127. opt_add NO_Z_SAFE_HOMING_WARNING
  128. opt_add NO_LCD_CONTRAST_WARNING
  129. opt_add NO_MICROPROBE_WARNING
  130. opt_add NO_CONFIGURATION_EMBEDDING_WARNING
  131. opt_add NO_HOMING_CURRENT_WARNING
  132. fi
  133. # Possible exported file names (in the build folder)
  134. ENAME=("-name" "marlin_config.json" \
  135. "-o" "-name" "config.ini" \
  136. "-o" "-name" "schema.json" \
  137. "-o" "-name" "schema.yml")
  138. # Possible built firmware names (in the build folder)
  139. BNAME=("-name" "firmware*.hex" \
  140. "-o" "-name" "firmware*.bin" \
  141. "-o" "-name" "project*.bin" \
  142. "-o" "-name" "Robin*.bin" \
  143. "-o" "-name" "main_*.bin" \
  144. "-o" "-name" "MarlinSimulator*")
  145. mkdir -p "$BUILD"
  146. # If EXPNUM is set then apply to the config before build
  147. if [[ $EXPNUM ]]; then
  148. opt_set CONFIG_EXPORT $EXPNUM
  149. # Clean up old exports
  150. find "$BUILD" -type f \( "${ENAME[@]}" \) -exec rm "{}" \;
  151. fi
  152. ((ARCHIVE)) && find "$BUILD" -type f \( "${BNAME[@]}" \) -exec rm "{}" \;
  153. set +e
  154. echo "Building example $CONFIG ..."
  155. mftest -s -a -n$BUILDINDEX ; ERR=$?
  156. ((ERR)) && alrt "Failed ($ERR)" || annc "Success"
  157. set -e
  158. if [[ $ERR -gt 0 ]]; then
  159. # Error? For --nofail simply log. Otherwise return the error.
  160. if [[ -n $NOFAIL ]]; then
  161. date +"%F %T [FAIL] $CONFIG" >>./.pio/error-log.txt
  162. else
  163. exit $ERR
  164. fi
  165. else
  166. # Copy exports back to the configs
  167. if [[ -n $EXPNUM ]]; then
  168. annc "Exporting $EXPNUM"
  169. [[ -f Marlin/Config-export.h ]] && { cp Marlin/Config-export.h "$ARCSUB"/Config.h ; }
  170. find "$BUILD" -type f \( "${ENAME[@]}" \) -exec cp "{}" "$ARCSUB" \;
  171. fi
  172. # Copy potential firmware files into the config folder
  173. # TODO: Consider firmware that needs an STM32F4_UPDATE folder.
  174. # Currently only BOARD_CREALITY_F401RE env:STM32F401RE_creality
  175. if ((ARCHIVE)); then
  176. annc "Archiving"
  177. find "$BUILD" -type f \( "${BNAME[@]}" \) -exec sh -c '
  178. ARCSUB="$1" ; CONFIG="$2" ; FILE="$3" ; shift 3
  179. NAME=${FILE##*/} ; SHRT=${NAME%.*} ; DIR=${FILE%/*}
  180. ZIPX=
  181. if [[ $CONFIG == *Simulator* ]]; then
  182. case $(uname | tr '[:upper:]' '[:lower:]') in
  183. darwin) SUB="macOS" ; ZIPX="-X" ;;
  184. *linux) SUB="Linux" ;;
  185. win*) SUB="Windows" ;;
  186. msys*) SUB="Windows" ;;
  187. cygwin*) SUB="Windows" ;;
  188. mingw*) SUB="Windows" ;;
  189. *) SUB='Unix' ;;
  190. esac
  191. ARCH=$(uname -m | tr '[:lower:]' '[:upper:]')
  192. ARCSUB="$ARCSUB/$SUB-$ARCH"
  193. fi
  194. mkdir -p "$ARCSUB"
  195. rm -f "$ARCSUB"/*.zip "$ARCSUB"/*.sha256.txt
  196. cd "$DIR"
  197. SHASUM=$(sha256sum "$NAME" | cut -d" " -f1)
  198. echo "$CONFIG\n$SHASUM" > "$ARCSUB/$NAME.sha256.txt"
  199. zip $ZIPX "$ARCSUB/$SHRT.zip" "$NAME" && rm "$NAME"
  200. cd - >/dev/null
  201. ' sh "$ARCSUB" "$CONFIG" {} +
  202. fi
  203. # Reveal the configs after the build, if requested
  204. ((REVEAL)) && { annc "Revealing $ARCSUB" ; open "$ARCSUB" ; }
  205. fi
  206. exit 0