mftest 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. MFINFO=$(mfinfo) || exit 1
  9. [[ -d Marlin/src ]] || { echo "Please 'cd' up to repo root." ; exit 1 ; }
  10. TESTPATH=buildroot/tests
  11. STATE_FILE=$( echo ./.pio/.mftestrc )
  12. SED=$(which gsed || which sed)
  13. shopt -s extglob nocasematch
  14. # Matching patterns
  15. ISNUM='^[0-9]+$'
  16. ISCMD='^(restore|opt|exec|use|pins|env)_'
  17. ISEXEC='^exec_'
  18. ISCONT='\\ *$'
  19. # Get the environment and test number from the command
  20. TESTENV=${1:-'-'}
  21. CHOICE=${2:-0}
  22. AUTOENV=0
  23. # Allow shorthand for test name
  24. case $TESTENV in
  25. tree) pio run -d . -e include_tree ; exit 1 ;;
  26. due) TESTENV='DUE' ;;
  27. esp) TESTENV='esp32' ;;
  28. lin*) TESTENV='linux_native' ;;
  29. lpc?(8)) TESTENV='LPC1768' ;;
  30. lpc9) TESTENV='LPC1769' ;;
  31. m128) TESTENV='mega1280' ;;
  32. m256) TESTENV='mega2560' ;;
  33. mega) TESTENV='mega2560' ;;
  34. stm) TESTENV='STM32F103RE' ;;
  35. f1) TESTENV='STM32F103RE' ;;
  36. f4) TESTENV='STM32F4' ;;
  37. f7) TESTENV='STM32F7' ;;
  38. s6) TESTENV='FYSETC_S6' ;;
  39. teensy) TESTENV='teensy31' ;;
  40. t31) TESTENV='teensy31' ;;
  41. t32) TESTENV='teensy31' ;;
  42. t35) TESTENV='teensy35' ;;
  43. t36) TESTENV='teensy35' ;;
  44. t40) TESTENV='teensy41' ;;
  45. t41) TESTENV='teensy41' ;;
  46. -h|--help) echo -e "$(basename $0) : Marlin Firmware test, build, and upload\n"
  47. echo "Usage: $(basename $0) ................. Select env and test to apply / run"
  48. echo " $(basename $0) [-y] env ........ Select a test for env to apply / run"
  49. echo " $(basename $0) [-y] env test ... Apply / run the specified env test"
  50. echo " $(basename $0) -b [variant] .... Auto-build the specified variant"
  51. echo " $(basename $0) -u [variant] .... Auto-build and upload the specified variant"
  52. echo
  53. echo "env shortcuts: tree due esp lin lpc|lpc8 lpc9 m128 m256|mega stm|f1 f4 f7 s6 teensy|t31|t32 t35|t36 t40|t41"
  54. exit
  55. ;;
  56. # Build with the last-built env
  57. -r) [[ -f "$STATE_FILE" ]] || { echo "No previous (-r) build state found." ; exit 1 ; }
  58. read TESTENV <"$STATE_FILE"
  59. pio run -d . -e $TESTENV
  60. exit
  61. ;;
  62. -[bu]) MB=$( grep -E "^\s*#define MOTHERBOARD" Marlin/Configuration.h | awk '{ print $3 }' | $SED 's/BOARD_//' )
  63. [[ -z $MB ]] && { echo "Error - Can't read MOTHERBOARD setting." ; exit 1 ; }
  64. BLINE=$( grep -E "define\s+BOARD_$MB\b" Marlin/src/core/boards.h )
  65. BNUM=$( $SED -E 's/^.+BOARD_[^ ]+ +([0-9]+).+$/\1/' <<<"$BLINE" )
  66. BDESC=$( $SED -E 's/^.+\/\/ *(.+)$/\1/' <<<"$BLINE" )
  67. [[ -z $BNUM ]] && { echo "Error - Can't find $MB in boards list." ; exit 1 ; }
  68. readarray -t ENVS <<< $( grep -EA1 "MB\(.*\b$MB\b.*\)" Marlin/src/pins/pins.h | grep -E '#include.+//.+env:.+' | grep -oE 'env:[^ ]+' | $SED -E 's/env://' )
  69. [[ -z $ENVS ]] && { echo "Error - Can't find target(s) for $MB ($BNUM)." ; exit 1 ; }
  70. ECOUNT=${#ENVS[*]}
  71. if [[ $ECOUNT == 1 ]]; then
  72. TARGET=$ENVS
  73. else
  74. if [[ $CHOICE == 0 ]]; then
  75. #
  76. # List env names and numbers. Get selection.
  77. #
  78. echo "Available targets for \"$BDESC\" | $MB ($BNUM):"
  79. IND=0 ; for ENV in "${ENVS[@]}"; do let IND++ ; echo " $IND) $ENV" ; done
  80. if [[ $ECOUNT > 1 ]]; then
  81. for (( ; ; ))
  82. do
  83. read -p "Select a target for '$MB' (1-$ECOUNT) : " CHOICE
  84. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  85. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= ECOUNT)) && break
  86. echo ">>> Invalid environment choice '$CHOICE'."
  87. done
  88. echo
  89. fi
  90. else
  91. echo "Detected \"$BDESC\" | $MB ($BNUM)."
  92. [[ $CHOICE > $ECOUNT ]] && { echo "Environment selection out of range." ; exit 1 ; }
  93. fi
  94. TARGET="${ENVS[$CHOICE-1]}"
  95. echo "Selected $TARGET"
  96. fi
  97. echo "$TARGET" >"$STATE_FILE"
  98. if [[ $TESTENV == "-u" ]]; then
  99. echo "Build/Uploading environment $TARGET for board $MB ($BNUM)..." ; echo
  100. pio run -t upload -e $TARGET
  101. else
  102. echo "Building environment $TARGET for board $MB ($BNUM)..." ; echo
  103. pio run -e $TARGET
  104. fi
  105. exit
  106. ;;
  107. # The -y flag may come first
  108. -y) TESTENV=${2:-'-'} ; CHOICE=${3:-0} ;;
  109. -[a-z]) echo "Unknown flag $TESTENV" ; exit 1 ;;
  110. -) ;;
  111. esac
  112. #
  113. # List available tests and ask for selection
  114. #
  115. if [[ $TESTENV == '-' ]]; then
  116. IND=0
  117. NAMES=()
  118. for FILE in $( ls -1 $TESTPATH/*-tests )
  119. do
  120. let IND++
  121. TNAME=${FILE/-tests/}
  122. TNAME=${TNAME/$TESTPATH\//}
  123. NAMES+=($TNAME)
  124. (( IND < 10 )) && echo -n " "
  125. echo " $IND) $TNAME"
  126. done
  127. echo
  128. for (( ; ; ))
  129. do
  130. read -p "Select a test to apply (1-$IND) : " NAMEIND
  131. [[ -z "$NAMEIND" ]] && { echo '(canceled)' ; exit 1 ; }
  132. [[ $NAMEIND =~ $ISNUM ]] && ((NAMEIND >= 1 && NAMEIND <= IND)) && { TESTENV=${NAMES[$NAMEIND-1]} ; echo ; break ; }
  133. echo "Invalid selection."
  134. done
  135. fi
  136. # Get the contents of the test file
  137. OUT=$( cat $TESTPATH/$TESTENV-tests 2>/dev/null ) || { echo "Can't find test '$TESTENV'." ; exit 1 ; }
  138. # Count up the number of tests
  139. TESTCOUNT=$( awk "/$ISEXEC/{a++}END{print a}" <<<"$OUT" )
  140. # User entered a number?
  141. (( CHOICE && CHOICE > TESTCOUNT )) && { echo "Invalid test selection '$CHOICE' (1-$TESTCOUNT)." ; exit 1 ; }
  142. if [[ $CHOICE == 0 ]]; then
  143. #
  144. # List test descriptions with numbers and get selection
  145. #
  146. echo "Available '$TESTENV' tests:" ; echo "$OUT" | {
  147. IND=0
  148. while IFS= read -r LINE
  149. do
  150. if [[ $LINE =~ $ISEXEC ]]; then
  151. DESC=$( "$SED" -E 's/^.+"(.*)".*$/\1/g' <<<"$LINE" )
  152. (( ++IND < 10 )) && echo -n " "
  153. echo " $IND) $DESC"
  154. fi
  155. done
  156. }
  157. CHOICE=1
  158. if [[ $TESTCOUNT > 1 ]]; then
  159. for (( ; ; ))
  160. do
  161. read -p "Select a '$TESTENV' test (1-$TESTCOUNT) : " CHOICE
  162. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  163. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= TESTCOUNT)) && break
  164. echo ">>> Invalid test selection '$CHOICE'."
  165. done
  166. fi
  167. fi
  168. #
  169. # Run the specified test lines
  170. #
  171. echo "$OUT" | {
  172. IND=0
  173. GOTX=0
  174. CMD=""
  175. while IFS= read -r LINE
  176. do
  177. if [[ $LINE =~ $ISCMD || $GOTX == 1 ]]; then
  178. ((!IND)) && let IND++
  179. if [[ $LINE =~ $ISEXEC ]]; then
  180. ((IND++ > CHOICE)) && break
  181. else
  182. ((!HEADER)) && {
  183. HEADER=1
  184. echo -e "\n#\n# Test $TESTENV ($CHOICE) $DESC\n#"
  185. }
  186. ((IND == CHOICE)) && {
  187. GOTX=1
  188. [[ $CMD == "" ]] && CMD="$LINE" || CMD=$( echo -e "$CMD$LINE" | $SED -e 's/\\//g' )
  189. [[ $LINE =~ $ISCONT ]] || { echo $CMD ; eval "$CMD" ; CMD="" ; }
  190. }
  191. fi
  192. fi
  193. done
  194. }
  195. # Make clear it's a TEST
  196. opt_set CUSTOM_MACHINE_NAME "\"$TESTENV-tests ($CHOICE)\""
  197. # Get a -y parameter the lazy way
  198. [[ "$2" == "-y" || "$3" == "-y" ]] && BUILD_YES='Y'
  199. # Build the test too?
  200. if [[ $BUILD_YES != 'Y' ]]; then
  201. echo
  202. read -p "Build $TESTENV test #$CHOICE (y/N) ? " BUILD_YES
  203. fi
  204. [[ $BUILD_YES == 'Y' || $BUILD_YES == 'Yes' ]] && {
  205. pio run -d . -e $TESTENV
  206. echo "$TESTENV" >"$STATE_FILE"
  207. }