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 10.0KB

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