mftest 11 KB

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