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

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