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

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