My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mftest 11KB

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