My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

mftest 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 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=2 ;;
  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=2 ;;
  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=2 ;;
  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=2 ;;
  80. esac
  81. bugout "Build will initiate? ($BUILD_YES)"
  82. ;;
  83. *) perror "Unknown flag" "$OPTARG" ; EXIT_USAGE=2 ;;
  84. esac
  85. ;;
  86. *) EXIT_USAGE=2 ;;
  87. esac
  88. done
  89. ((EXIT_USAGE)) && { usage ; let EXIT_USAGE-- ; exit $EXIT_USAGE ; }
  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 -s -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. lp8|lpc8) TESTENV='LPC1768' ;;
  104. lp9|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. [1-9][1-9]|[1-9]) TESTNUM=$TESTENV ; TESTENV=- ;;
  121. esac
  122. if ((AUTO_BUILD)); then
  123. #
  124. # List environments that apply to the current MOTHERBOARD.
  125. #
  126. case $(uname | tr '[:upper:]' '[:lower:]') in
  127. darwin) SYS='mac' ;;
  128. *linux) SYS='lin' ;;
  129. win*) SYS='win' ;;
  130. msys*) SYS='win' ;;
  131. cygwin*) SYS='win' ;;
  132. mingw*) SYS='win' ;;
  133. *) SYS='uni' ;;
  134. esac
  135. echo ; echo -n "Auto " ; ((AUTO_BUILD == 2)) && echo "Upload..." || echo "Build..."
  136. MB=$( grep -E "^\s*#define MOTHERBOARD" Marlin/Configuration.h | awk '{ print $3 }' | $SED 's/BOARD_//' )
  137. [[ -z $MB ]] && { echo "Error - Can't read MOTHERBOARD setting." ; exit 1 ; }
  138. BLINE=$( grep -E "define\s+BOARD_$MB\b" Marlin/src/core/boards.h )
  139. BNUM=$( $SED -E 's/^.+BOARD_[^ ]+ +([0-9]+).+$/\1/' <<<"$BLINE" )
  140. BDESC=$( $SED -E 's/^.+\/\/ *(.+)$/\1/' <<<"$BLINE" )
  141. [[ -z $BNUM ]] && { echo "Error - Can't find $MB in boards list." ; exit 1 ; }
  142. 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)://" ) )
  143. [[ -z $ENVS ]] && { errout "Error - Can't find target(s) for $MB ($BNUM)." ; exit 1 ; }
  144. ECOUNT=${#ENVS[*]}
  145. if [[ $ECOUNT == 1 ]]; then
  146. TARGET=$ENVS
  147. else
  148. if [[ $CHOICE == 0 ]]; then
  149. # List env names and numbers. Get selection.
  150. echo "Available targets for \"$BDESC\" | $MB ($BNUM):"
  151. IND=0 ; for ENV in "${ENVS[@]}"; do let IND++ ; echo " $IND) $ENV" ; done
  152. if [[ $ECOUNT > 1 ]]; then
  153. for (( ; ; ))
  154. do
  155. read -p "Select a target for '$MB' (1-$ECOUNT) : " CHOICE
  156. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  157. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= ECOUNT)) && break
  158. errout ">>> Invalid environment choice '$CHOICE'."
  159. done
  160. echo
  161. fi
  162. else
  163. echo "Detected \"$BDESC\" | $MB ($BNUM)."
  164. [[ $CHOICE > $ECOUNT ]] && { echo "Environment selection out of range." ; exit 1 ; }
  165. fi
  166. TARGET="${ENVS[$CHOICE-1]}"
  167. echo "Selected $TARGET"
  168. fi
  169. echo "$TARGET" >"$STATE_FILE"
  170. if ((AUTO_BUILD == 2)); then
  171. echo "Uploading environment $TARGET for board $MB ($BNUM)..." ; echo
  172. pio run -t upload -e $TARGET
  173. else
  174. echo "Building environment $TARGET for board $MB ($BNUM)..." ; echo
  175. pio run -s -e $TARGET
  176. fi
  177. exit
  178. fi
  179. #
  180. # List available tests and ask for selection
  181. #
  182. if [[ $TESTENV == '-' ]]; then
  183. IND=0
  184. NAMES=()
  185. for FILE in $( ls -1 $TESTPATH/* )
  186. do
  187. let IND++
  188. TNAME=${FILE/$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 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 "\"Test $TESTENV ($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 -s -d . -e $TESTENV
  279. echo "$TESTENV" >"$STATE_FILE"
  280. }