My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mftest 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/usr/bin/env bash
  2. #
  3. # mftest Select a test to apply and build
  4. # mftest -b [#] Build the auto-detected environment
  5. # mftest -u [#] Upload the auto-detected environment
  6. # mftest [name] [index] [-y] Set config options and optionally build a test
  7. #
  8. MFINFO=$(mfinfo) || exit 1
  9. [[ -d Marlin/src ]] || { echo "Please 'cd' to the Marlin repo root." ; exit 1 ; }
  10. perror() { echo -e "$0: \033[0;31m$1 -- $2\033[0m" ; }
  11. errout() { echo -e "\033[0;31m$1\033[0m" ; }
  12. bugout() { ((DEBUG)) && echo -e "\033[0;32m$1\033[0m" ; }
  13. usage() {
  14. echo "
  15. Usage: mftest [-t|--env=<env>] [-n|--num=<num>] [-m|--make] [-y|--build=<Y|n>]
  16. mftest [-a|--autobuild]
  17. mftest [-r|--rebuild]
  18. mftest [-u|--autoupload] [-n|--num=<num>]
  19. OPTIONS
  20. -t --env The environment of the test to apply / run. (As named in platformio.ini.)
  21. -n --num The index of the test to run. (In *-tests file order.)
  22. -m --make Use the make / Docker method for the build.
  23. -y --build Skip 'Do you want to build this test?' and assume YES.
  24. -h --help Print this help.
  25. -a --autobuild PIO Build using the MOTHERBOARD environment.
  26. -u --autoupload PIO Upload using the MOTHERBOARD environment.
  27. -v --verbose Extra output for debugging.
  28. env shortcuts: tree due esp lin lpc|lpc8 lpc9 m128 m256|mega stm|f1 f4 f7 s6 teensy|t31|t32 t35|t36 t40|t41
  29. "
  30. }
  31. TESTPATH=buildroot/tests
  32. STATE_FILE=$( echo ./.pio/.mftestrc )
  33. SED=$(which gsed || which sed)
  34. shopt -s extglob nocasematch
  35. # Matching patterns
  36. ISNUM='^[0-9]+$'
  37. ISCMD='^(restore|opt|exec|use|pins|env)_'
  38. ISEXEC='^exec_'
  39. ISCONT='\\ *$'
  40. # Get environment, test number, etc. from the command
  41. TESTENV='-'
  42. CHOICE=0
  43. DEBUG=0
  44. while getopts 'abhmruvyn:t:-:' OFLAG; do
  45. case "${OFLAG}" in
  46. a) AUTO_BUILD=1 ; bugout "Auto-Build target..." ;;
  47. h) EXIT_USAGE=1 ;;
  48. m) USE_MAKE=1 ; bugout "Using make with Docker..." ;;
  49. n) case "$OPTARG" in
  50. *[!0-9]*) perror "option requires a number" $OFLAG ; EXIT_USAGE=1 ;;
  51. *) CHOICE="$OPTARG" ; bugout "Got a number: $CHOICE" ;;
  52. esac
  53. ;;
  54. r) REBUILD=1 ; bugout "Rebuilding previous..." ;;
  55. t) TESTENV="$OPTARG" ; bugout "Got a target: $TESTENV" ;;
  56. u) AUTO_BUILD=2 ; bugout "Auto-Upload target..." ;;
  57. v) DEBUG=1 ; bugout "Debug ON" ;;
  58. y) BUILD_YES='Y' ; bugout "Build will initiate..." ;;
  59. -) IFS="=" read -r ONAM OVAL <<< "$OPTARG"
  60. case "$ONAM" in
  61. help) [[ -z "$OVAL" ]] || perror "option can't take value $OVAL" $ONAM ; EXIT_USAGE=1 ;;
  62. autobuild) AUTO_BUILD=1 ; bugout "Auto-Build target..." ;;
  63. autoupload) AUTO_BUILD=2 ; bugout "Auto-Upload target..." ;;
  64. env) case "$OVAL" in
  65. '') perror "option requires a value" $ONAM ; EXIT_USAGE=1 ;;
  66. *) TESTENV="$OVAL" ; bugout "Got a target: $TESTENV" ;;
  67. esac
  68. ;;
  69. num) case "$OVAL" in
  70. [0-9]+) CHOICE="$OVAL" ; bugout "Got a number: $CHOICE" ;;
  71. *) perror "option requires a value" $ONAM ; EXIT_USAGE=1 ;;
  72. esac
  73. ;;
  74. rebuild) REBUILD=1 ; bugout "Rebuilding previous..." ;;
  75. make) USE_MAKE=1 ; bugout "Using make with Docker..." ;;
  76. debug|verbose) DEBUG=1 ; bugout "Debug ON" ;;
  77. build) case "$OVAL" in
  78. ''|y|yes) BUILD_YES='Y' ;;
  79. n|no) BUILD_YES='N' ;;
  80. *) perror "option value must be y, n, yes, or no" $ONAM ; EXIT_USAGE=1 ;;
  81. esac
  82. bugout "Build will initiate? ($BUILD_YES)"
  83. ;;
  84. *) perror "Unknown flag" "$OPTARG" ; EXIT_USAGE=1 ;;
  85. esac
  86. ;;
  87. esac
  88. done
  89. ((EXIT_USAGE)) && { usage ; exit 1 ; }
  90. if ((REBUILD)); then
  91. bugout "Rebuilding previous..."
  92. # Build with the last-built env
  93. [[ -f "$STATE_FILE" ]] || { errout "No previous (-r) build state found." ; exit 1 ; }
  94. read TESTENV <"$STATE_FILE"
  95. pio run -d . -e $TESTENV
  96. exit
  97. fi
  98. case $TESTENV in
  99. tree) pio run -d . -e include_tree ; exit 1 ;;
  100. due) TESTENV='DUE' ;;
  101. esp) TESTENV='esp32' ;;
  102. lin*) TESTENV='linux_native' ;;
  103. lpc?(8)) TESTENV='LPC1768' ;;
  104. lpc9) TESTENV='LPC1769' ;;
  105. m128) TESTENV='mega1280' ;;
  106. m256) TESTENV='mega2560' ;;
  107. mega) TESTENV='mega2560' ;;
  108. stm) TESTENV='STM32F103RE' ;;
  109. f1) TESTENV='STM32F103RE' ;;
  110. f4) TESTENV='STM32F4' ;;
  111. f7) TESTENV='STM32F7' ;;
  112. s6) TESTENV='FYSETC_S6' ;;
  113. teensy) TESTENV='teensy31' ;;
  114. t31) TESTENV='teensy31' ;;
  115. t32) TESTENV='teensy31' ;;
  116. t35) TESTENV='teensy35' ;;
  117. t36) TESTENV='teensy35' ;;
  118. t40) TESTENV='teensy41' ;;
  119. t41) TESTENV='teensy41' ;;
  120. esac
  121. if ((AUTO_BUILD)); then
  122. #
  123. # List environments that apply to the current MOTHERBOARD.
  124. #
  125. echo ; echo -n "Auto " ; ((AUTO_BUILD == 2)) && echo "Upload..." || echo "Build..."
  126. MB=$( grep -E "^\s*#define MOTHERBOARD" Marlin/Configuration.h | awk '{ print $3 }' | $SED 's/BOARD_//' )
  127. [[ -z $MB ]] && { echo "Error - Can't read MOTHERBOARD setting." ; exit 1 ; }
  128. BLINE=$( grep -E "define\s+BOARD_$MB\b" Marlin/src/core/boards.h )
  129. BNUM=$( $SED -E 's/^.+BOARD_[^ ]+ +([0-9]+).+$/\1/' <<<"$BLINE" )
  130. BDESC=$( $SED -E 's/^.+\/\/ *(.+)$/\1/' <<<"$BLINE" )
  131. [[ -z $BNUM ]] && { echo "Error - Can't find $MB in boards list." ; exit 1 ; }
  132. ENVS=( $( grep -EA1 "MB\(.*\b$MB\b.*\)" Marlin/src/pins/pins.h | grep -E '#include.+//.+env:[^ ]+' | grep -oE 'env:[^ ]+' | $SED -E 's/env://' ) )
  133. [[ -z $ENVS ]] && { errout "Error - Can't find target(s) for $MB ($BNUM)." ; exit 1 ; }
  134. ECOUNT=${#ENVS[*]}
  135. if [[ $ECOUNT == 1 ]]; then
  136. TARGET=$ENVS
  137. else
  138. if [[ $CHOICE == 0 ]]; then
  139. # List env names and numbers. Get selection.
  140. echo "Available targets for \"$BDESC\" | $MB ($BNUM):"
  141. IND=0 ; for ENV in "${ENVS[@]}"; do let IND++ ; echo " $IND) $ENV" ; done
  142. if [[ $ECOUNT > 1 ]]; then
  143. for (( ; ; ))
  144. do
  145. read -p "Select a target for '$MB' (1-$ECOUNT) : " CHOICE
  146. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  147. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= ECOUNT)) && break
  148. errout ">>> Invalid environment choice '$CHOICE'."
  149. done
  150. echo
  151. fi
  152. else
  153. echo "Detected \"$BDESC\" | $MB ($BNUM)."
  154. [[ $CHOICE > $ECOUNT ]] && { echo "Environment selection out of range." ; exit 1 ; }
  155. fi
  156. TARGET="${ENVS[$CHOICE-1]}"
  157. echo "Selected $TARGET"
  158. fi
  159. echo "$TARGET" >"$STATE_FILE"
  160. if ((AUTO_BUILD == 2)); then
  161. echo "Uploading environment $TARGET for board $MB ($BNUM)..." ; echo
  162. pio run -t upload -e $TARGET
  163. else
  164. echo "Building environment $TARGET for board $MB ($BNUM)..." ; echo
  165. pio run -e $TARGET
  166. fi
  167. exit
  168. fi
  169. #
  170. # List available tests and ask for selection
  171. #
  172. if [[ $TESTENV == '-' ]]; then
  173. IND=0
  174. NAMES=()
  175. for FILE in $( ls -1 $TESTPATH/*-tests )
  176. do
  177. let IND++
  178. TNAME=${FILE/-tests/}
  179. TNAME=${TNAME/$TESTPATH\//}
  180. NAMES+=($TNAME)
  181. (( IND < 10 )) && echo -n " "
  182. echo " $IND) $TNAME"
  183. done
  184. echo
  185. for (( ; ; ))
  186. do
  187. read -p "Select a test to apply (1-$IND) : " NAMEIND
  188. [[ -z "$NAMEIND" ]] && { errout "(canceled)" ; exit 1 ; }
  189. [[ $NAMEIND =~ $ISNUM ]] && ((NAMEIND >= 1 && NAMEIND <= IND)) && { TESTENV=${NAMES[$NAMEIND-1]} ; echo ; break ; }
  190. errout "Invalid selection."
  191. done
  192. fi
  193. # Get the contents of the test file
  194. OUT=$( cat $TESTPATH/$TESTENV-tests 2>/dev/null ) || { errout "Can't find test '$TESTENV'." ; exit 1 ; }
  195. # Count up the number of tests
  196. TESTCOUNT=$( awk "/$ISEXEC/{a++}END{print a}" <<<"$OUT" )
  197. # User entered a number?
  198. (( CHOICE && CHOICE > TESTCOUNT )) && { errout "Invalid test selection '$CHOICE' (1-$TESTCOUNT)." ; exit 1 ; }
  199. if [[ $CHOICE == 0 ]]; then
  200. #
  201. # List test descriptions with numbers and get selection
  202. #
  203. echo "Available '$TESTENV' tests:" ; echo "$OUT" | {
  204. IND=0
  205. while IFS= read -r LINE
  206. do
  207. if [[ $LINE =~ $ISEXEC ]]; then
  208. DESC=$( "$SED" -E 's/^exec_test \$1 \$2 "([^"]+)".*$/\1/g' <<<"$LINE" )
  209. (( ++IND < 10 )) && echo -n " "
  210. echo " $IND) $DESC"
  211. fi
  212. done
  213. }
  214. CHOICE=1
  215. if [[ $TESTCOUNT > 1 ]]; then
  216. for (( ; ; ))
  217. do
  218. read -p "Select a '$TESTENV' test (1-$TESTCOUNT) : " CHOICE
  219. [[ -z "$CHOICE" ]] && { errout "(canceled)" ; exit 1 ; }
  220. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= TESTCOUNT)) && break
  221. errout ">>> Invalid test selection '$CHOICE'."
  222. done
  223. fi
  224. fi
  225. #
  226. # Run the specified test lines
  227. #
  228. echo -ne "\033[0;33m"
  229. echo "$OUT" | {
  230. IND=0
  231. GOTX=0
  232. CMD=""
  233. while IFS= read -r LINE
  234. do
  235. if [[ $LINE =~ $ISCMD || $GOTX == 1 ]]; then
  236. ((!IND)) && let IND++
  237. if [[ $LINE =~ $ISEXEC ]]; then
  238. ((IND++ > CHOICE)) && break
  239. else
  240. ((!HEADER)) && {
  241. HEADER=1
  242. echo -e "\n#\n# Test $TESTENV ($CHOICE) $DESC\n#"
  243. }
  244. ((IND == CHOICE)) && {
  245. GOTX=1
  246. [[ $CMD == "" ]] && CMD="$LINE" || CMD=$( echo -e "$CMD$LINE" | $SED -e 's/\\//g' | $SED -E 's/ +/ /g' )
  247. [[ $LINE =~ $ISCONT ]] || { echo "$CMD" ; eval "$CMD" ; CMD="" ; }
  248. }
  249. fi
  250. fi
  251. done
  252. }
  253. echo -ne "\033[0m"
  254. # Make clear it's a TEST
  255. opt_set CUSTOM_MACHINE_NAME "\"$TESTENV-tests ($CHOICE)\""
  256. # Build the test too?
  257. if [[ -z "$BUILD_YES" ]]; then
  258. echo
  259. read -p "Build $TESTENV test #$CHOICE (y/N) ? " BUILD_YES
  260. fi
  261. [[ $BUILD_YES == 'Y' || $BUILD_YES == 'Yes' ]] && {
  262. ((USE_MAKE)) && make tests-single-local TEST_TARGET=$TESTENV ONLY_TEST=$CHOICE
  263. ((USE_MAKE)) || pio run -d . -e $TESTENV
  264. echo "$TESTENV" >"$STATE_FILE"
  265. }