mftest 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. which pio >/dev/null || { echo "Make sure 'pio' is in your execution PATH." ; exit 1 ; }
  10. perror() { echo -e "$0: \033[0;31m$1 -- $2\033[0m" ; }
  11. errout() { echo -e "\033[0;31m$1\033[0m" ; }
  12. bugout() { ((DEBUG)) && echo -e "\033[0;32m$1\033[0m" ; }
  13. usage() {
  14. echo "
  15. Usage: mftest [-t|--env=<env|index>] [-n|--num=<num>] [-m|--make] [-y|--build=<Y|n>]
  16. mftest [-a|--autobuild]
  17. mftest [-r|--rebuild]
  18. mftest [-s|--silent]
  19. mftest [-u|--autoupload] [-n|--num=<num>]
  20. OPTIONS
  21. -t --env The environment to apply / run, or the menu index number.
  22. -n --num The index of the test to run. (In file order.)
  23. -m --make Use the make / Docker method for the build.
  24. -y --build Skip 'Do you want to build this test?' and assume YES.
  25. -h --help Print this help.
  26. -a --autobuild PIO Build using the MOTHERBOARD environment.
  27. -u --autoupload PIO Upload using the MOTHERBOARD environment.
  28. -v --verbose Extra output for debugging.
  29. -s --silent Silence build output from PlatformIO.
  30. -d --default Restore to defaults before applying configs.
  31. 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
  32. "
  33. }
  34. TESTPATH=buildroot/tests
  35. STATE_FILE="./.pio/.mftestrc"
  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. -) ONAM="${OPTARG%%=*}" ; 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. shift $((OPTIND - 1))
  98. ((EXIT_USAGE)) && { usage ; let EXIT_USAGE-- ; exit $EXIT_USAGE ; }
  99. if ((REBUILD)); then
  100. bugout "Rebuilding previous..."
  101. # Build with the last-built env
  102. [[ -f "$STATE_FILE" ]] || { errout "No previous (-r) build state found." ; exit 1 ; }
  103. read TESTENV <"$STATE_FILE"
  104. pio run $SILENT_FLAG -d . -e $TESTENV
  105. exit 0
  106. fi
  107. case $TESTENV in
  108. tree) pio run -d . -e include_tree ; exit 1 ;;
  109. due) TESTENV='DUE' ;;
  110. esp) TESTENV='esp32' ;;
  111. lin*) TESTENV='linux_native' ;;
  112. lp8|lpc8) TESTENV='LPC1768' ;;
  113. lp9|lpc9) TESTENV='LPC1769' ;;
  114. m128) TESTENV='mega1280' ;;
  115. m256) TESTENV='mega2560' ;;
  116. mega) TESTENV='mega2560' ;;
  117. stm) TESTENV='STM32F103RE' ;;
  118. f1) TESTENV='STM32F103RE' ;;
  119. f4) TESTENV='STM32F4' ;;
  120. f7) TESTENV='STM32F7' ;;
  121. s6) TESTENV='FYSETC_S6' ;;
  122. teensy) TESTENV='teensy31' ;;
  123. t31) TESTENV='teensy31' ;;
  124. t32) TESTENV='teensy31' ;;
  125. t35) TESTENV='teensy35' ;;
  126. t36) TESTENV='teensy35' ;;
  127. t40) TESTENV='teensy41' ;;
  128. t41) TESTENV='teensy41' ;;
  129. [1-9]|[1-9][0-9]) TESTNUM=$TESTENV ; TESTENV=- ;;
  130. esac
  131. if ((AUTO_BUILD)); then
  132. #
  133. # List environments that apply to the current MOTHERBOARD.
  134. #
  135. case $(uname | tr '[:upper:]' '[:lower:]') in
  136. darwin) SYS='mac' ;;
  137. *linux) SYS='lin' ;;
  138. win*) SYS='win' ;;
  139. msys*) SYS='win' ;;
  140. cygwin*) SYS='win' ;;
  141. mingw*) SYS='win' ;;
  142. *) SYS='uni' ;;
  143. esac
  144. echo ; echo -n "Auto " ; ((AUTO_BUILD == 2)) && echo "Upload..." || echo "Build..."
  145. #
  146. # Get the MOTHERBOARD define value from the .h file and strip off the "BOARD_" prefix
  147. #
  148. ACODE='/^[[:space:]]*#define[[:space:]]MOTHERBOARD[[:space:]]/ { sub(/^BOARD_/, "", $3); print $3 }'
  149. MB=$(awk "$ACODE" Marlin/Configuration.h 2>/dev/null)
  150. [[ -z $MB ]] && MB=$(awk "$ACODE" Marlin/Config.h 2>/dev/null)
  151. [[ -z $MB ]] && { echo "Error - Can't read MOTHERBOARD setting." ; exit 1 ; }
  152. BLINE=$( grep -E "define\s+BOARD_$MB\b" Marlin/src/core/boards.h )
  153. BNUM=$( sed -E 's/^.+BOARD_[^ ]+ +([0-9]+).+$/\1/' <<<"$BLINE" )
  154. BDESC=$( sed -E 's/^.+\/\/ *(.+)$/\1/' <<<"$BLINE" )
  155. [[ -z $BNUM ]] && { echo "Error - Can't find BOARD_$MB in core/boards.h." ; exit 1 ; }
  156. 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)://" ) )
  157. [[ -z $ENVS ]] && { errout "Error - Can't find target(s) for $MB ($BNUM)." ; exit 1 ; }
  158. ECOUNT=${#ENVS[*]}
  159. if [[ $ECOUNT == 1 ]]; then
  160. TARGET=$ENVS
  161. else
  162. if [[ $CHOICE == 0 ]]; then
  163. # List env names and numbers. Get selection.
  164. echo "Available targets for \"$BDESC\" | $MB ($BNUM):"
  165. IND=0 ; for ENV in "${ENVS[@]}"; do let IND++ ; echo " $IND) $ENV" ; done
  166. if [[ $ECOUNT > 1 ]]; then
  167. for (( ; ; ))
  168. do
  169. read -p "Select a target for '$MB' (1-$ECOUNT) : " CHOICE
  170. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  171. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= ECOUNT)) && break
  172. errout ">>> Invalid environment choice '$CHOICE'."
  173. done
  174. echo
  175. fi
  176. else
  177. echo "Detected \"$BDESC\" | $MB ($BNUM)."
  178. [[ $CHOICE > $ECOUNT ]] && { echo "Environment selection out of range." ; exit 1 ; }
  179. fi
  180. TARGET="${ENVS[$CHOICE-1]}"
  181. if [[ $MB == 'SIMULATED' && $TARGET == 'linux_native' ]]; then
  182. TARGET="simulator_linux_release" # Skip the linux_native environment
  183. fi
  184. echo "Selected $TARGET"
  185. fi
  186. echo "$TARGET" >"$STATE_FILE"
  187. if ((AUTO_BUILD == 2)); then
  188. echo "Uploading environment $TARGET for board $MB ($BNUM)..." ; echo
  189. pio run $SILENT_FLAG -t upload -e $TARGET
  190. else
  191. echo "Building environment $TARGET for board $MB ($BNUM)..." ; echo
  192. pio run $SILENT_FLAG -e $TARGET
  193. fi
  194. exit $?
  195. fi
  196. #
  197. # List available tests and ask for selection
  198. #
  199. if [[ $TESTENV == '-' ]]; then
  200. IND=0
  201. NAMES=()
  202. MENU=()
  203. BIGLEN=0
  204. for FILE in $( ls -1 $TESTPATH/* | sort -f )
  205. do
  206. let IND++
  207. TNAME=${FILE/$TESTPATH\//}
  208. NAMES+=($TNAME)
  209. IFS=""
  210. ITEM=$( printf "%2i) %s" $IND $TNAME )
  211. MENU+=($ITEM)
  212. [[ ${#ITEM} -gt $BIGLEN ]] && BIGLEN=${#ITEM}
  213. done
  214. (( BIGLEN += 2 ))
  215. THIRD=$(( (${#MENU[@]} + 2) / 3 ))
  216. for ((i = 0; i < $THIRD; i++))
  217. do
  218. COL1=$i ; COL2=$(( $i + $THIRD )) ; COL3=$(( $i + 2 * $THIRD ))
  219. FMT="%-${BIGLEN}s"
  220. printf "${FMT}${FMT}${FMT}\n" ${MENU[$COL1]} ${MENU[$COL2]} ${MENU[$COL3]}
  221. done
  222. echo
  223. for (( ; ; ))
  224. do
  225. if [[ $TESTNUM -gt 0 ]]; then
  226. NAMEIND=$TESTNUM
  227. else
  228. read -p "Select a test to apply (1-$IND) : " NAMEIND
  229. fi
  230. [[ -z $NAMEIND ]] && { errout "(canceled)" ; exit 1 ; }
  231. TESTENV=${NAMES[$NAMEIND-1]}
  232. [[ $TESTNUM -gt 0 ]] && { echo "Preselected test $TESTNUM ... ($TESTENV)" ; TESTNUM='' ; }
  233. [[ $NAMEIND =~ $ISNUM ]] && ((NAMEIND >= 1 && NAMEIND <= IND)) && { TESTENV=${NAMES[$NAMEIND-1]} ; echo ; break ; }
  234. errout "Invalid selection."
  235. done
  236. fi
  237. # Get the contents of the test file
  238. OUT=$( cat $TESTPATH/$TESTENV 2>/dev/null ) || { errout "Can't find test '$TESTENV'." ; exit 1 ; }
  239. # Count up the number of tests
  240. TESTCOUNT=$( awk "/$ISEXEC/{a++}END{print a}" <<<"$OUT" )
  241. # User entered a number?
  242. (( CHOICE && CHOICE > TESTCOUNT )) && { errout "Invalid test selection '$CHOICE' (1-$TESTCOUNT)." ; exit 1 ; }
  243. if [[ $CHOICE == 0 ]]; then
  244. #
  245. # List test descriptions with numbers and get selection
  246. #
  247. echo "Available '$TESTENV' tests:" ; echo "$OUT" | {
  248. IND=0
  249. while IFS= read -r LINE
  250. do
  251. if [[ $LINE =~ $ISEXEC ]]; then
  252. DESC=$( sed -E 's/^exec_test \$1 \$2 "([^"]+)".*$/\1/g' <<<"$LINE" )
  253. (( ++IND < 10 )) && echo -n " "
  254. echo " $IND) $DESC"
  255. fi
  256. done
  257. }
  258. CHOICE=1
  259. if [[ $TESTCOUNT > 1 ]]; then
  260. for (( ; ; ))
  261. do
  262. read -p "Select a '$TESTENV' test (1-$TESTCOUNT) : " CHOICE
  263. [[ -z "$CHOICE" ]] && { errout "(canceled)" ; exit 1 ; }
  264. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= TESTCOUNT)) && break
  265. errout ">>> Invalid test selection '$CHOICE'."
  266. done
  267. fi
  268. fi
  269. #
  270. # Restore to defaults if requested
  271. #
  272. ((DL_DEFAULTS)) && use_example_configs
  273. #
  274. # Run the specified test lines
  275. #
  276. echo -ne "\033[0;33m"
  277. echo "$OUT" | {
  278. IND=0
  279. GOTX=0
  280. CMD=""
  281. while IFS= read -r LINE
  282. do
  283. if [[ $LINE =~ $ISCMD || $GOTX == 1 ]]; then
  284. ((!IND)) && let IND++
  285. if [[ $LINE =~ $ISEXEC ]]; then
  286. ((IND++ > CHOICE)) && break
  287. else
  288. ((!HEADER)) && {
  289. HEADER=1
  290. echo -e "\n#\n# Test $TESTENV ($CHOICE) $DESC\n#"
  291. }
  292. ((IND == CHOICE)) && {
  293. GOTX=1
  294. [[ -n $DL_DEFAULTS && $LINE =~ $ISRST ]] && LINE="use_example_configs"
  295. [[ $CMD == "" ]] && CMD="$LINE" || CMD=$( echo -e "$CMD$LINE" | sed -e 's/\\//g' | sed -E 's/ +/ /g' )
  296. [[ $LINE =~ $ISCONT ]] || { echo "$CMD" ; eval "$CMD" ; CMD="" ; }
  297. }
  298. fi
  299. fi
  300. done
  301. }
  302. echo -ne "\033[0m"
  303. # Make clear it's a TEST
  304. opt_set CUSTOM_MACHINE_NAME "\"Test $TESTENV ($CHOICE)\""
  305. # Build the test too?
  306. if [[ -z "$BUILD_YES" ]]; then
  307. echo
  308. read -p "Build $TESTENV test #$CHOICE (y/N) ? " BUILD_YES
  309. fi
  310. [[ $BUILD_YES == 'Y' || $BUILD_YES == 'Yes' ]] && {
  311. ((USE_MAKE)) && make tests-single-local TEST_TARGET=$TESTENV ONLY_TEST=$CHOICE
  312. ((USE_MAKE)) || pio run $SILENT_FLAG -d . -e $TESTENV
  313. echo "$TESTENV" >"$STATE_FILE"
  314. }