mftest 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. # If AUTO_BUILD is not 2 (upload), strip envs ending in '_xfer' from $ENVS
  159. if [[ $AUTO_BUILD != 2 ]]; then
  160. OENV=()
  161. for env in "${ENVS[@]}"; do
  162. [[ ! $env =~ _xfer$ ]] && OENV+=( "$env" )
  163. done
  164. ENVS=( "${OENV[@]}" )
  165. fi
  166. [[ -z $ENVS ]] && { errout "Error - Can't find target(s) for $MB ($BNUM)." ; exit 1 ; }
  167. ECOUNT=${#ENVS[*]}
  168. if [[ $ECOUNT == 1 ]]; then
  169. TARGET=$ENVS
  170. else
  171. if [[ $CHOICE == 0 ]]; then
  172. # List env names and numbers. Get selection.
  173. echo "Available targets for \"$BDESC\" | $MB ($BNUM):"
  174. IND=0 ; for ENV in "${ENVS[@]}"; do let IND++ ; echo " $IND) $ENV" ; done
  175. if [[ $ECOUNT > 1 ]]; then
  176. for (( ; ; ))
  177. do
  178. read -p "Select a target for '$MB' (1-$ECOUNT) : " CHOICE
  179. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  180. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= ECOUNT)) && break
  181. errout ">>> Invalid environment choice '$CHOICE'."
  182. done
  183. echo
  184. fi
  185. else
  186. echo "Detected \"$BDESC\" | $MB ($BNUM)."
  187. [[ $CHOICE > $ECOUNT ]] && { echo "Environment selection out of range." ; exit 66 ; }
  188. fi
  189. TARGET="${ENVS[$CHOICE-1]}"
  190. if [[ $MB == 'SIMULATED' && $TARGET == 'linux_native' ]]; then
  191. TARGET="simulator_linux_release" # Skip the linux_native environment
  192. fi
  193. echo "Selected $TARGET"
  194. fi
  195. echo "$TARGET" >"$STATE_FILE"
  196. if ((AUTO_BUILD == 2)); then
  197. echo "Uploading environment $TARGET for board $MB ($BNUM)..." ; echo
  198. pio run $SILENT_FLAG -t upload -e $TARGET
  199. else
  200. echo "Building environment $TARGET for board $MB ($BNUM)..." ; echo
  201. pio run $SILENT_FLAG -e $TARGET
  202. fi
  203. exit $?
  204. fi
  205. #
  206. # List available tests and ask for selection
  207. #
  208. if [[ $TESTENV == '-' ]]; then
  209. IND=0
  210. NAMES=()
  211. MENU=()
  212. BIGLEN=0
  213. for FILE in $( ls -1 $TESTPATH/* | sort -f )
  214. do
  215. let IND++
  216. TNAME=${FILE/$TESTPATH\//}
  217. NAMES+=($TNAME)
  218. IFS=""
  219. ITEM=$( printf "%2i) %s" $IND $TNAME )
  220. MENU+=($ITEM)
  221. [[ ${#ITEM} -gt $BIGLEN ]] && BIGLEN=${#ITEM}
  222. done
  223. (( BIGLEN += 2 ))
  224. THIRD=$(( (${#MENU[@]} + 2) / 3 ))
  225. for ((i = 0; i < $THIRD; i++))
  226. do
  227. COL1=$i ; COL2=$(( $i + $THIRD )) ; COL3=$(( $i + 2 * $THIRD ))
  228. FMT="%-${BIGLEN}s"
  229. printf "${FMT}${FMT}${FMT}\n" ${MENU[$COL1]} ${MENU[$COL2]} ${MENU[$COL3]}
  230. done
  231. echo
  232. for (( ; ; ))
  233. do
  234. if [[ $TESTNUM -gt 0 ]]; then
  235. NAMEIND=$TESTNUM
  236. else
  237. read -p "Select a test to apply (1-$IND) : " NAMEIND
  238. fi
  239. [[ -z $NAMEIND ]] && { errout "(canceled)" ; exit 1 ; }
  240. TESTENV=${NAMES[$NAMEIND-1]}
  241. [[ $TESTNUM -gt 0 ]] && { echo "Preselected test $TESTNUM ... ($TESTENV)" ; TESTNUM='' ; }
  242. [[ $NAMEIND =~ $ISNUM ]] && ((NAMEIND >= 1 && NAMEIND <= IND)) && { TESTENV=${NAMES[$NAMEIND-1]} ; echo ; break ; }
  243. errout "Invalid selection."
  244. done
  245. fi
  246. # Get the contents of the test file
  247. OUT=$( cat $TESTPATH/$TESTENV 2>/dev/null ) || { errout "Can't find test '$TESTENV'." ; exit 1 ; }
  248. # Count up the number of tests
  249. TESTCOUNT=$( awk "/$ISEXEC/{a++}END{print a}" <<<"$OUT" )
  250. # User entered a number?
  251. (( CHOICE && CHOICE > TESTCOUNT )) && { errout "Invalid test selection '$CHOICE' (1-$TESTCOUNT)." ; exit 1 ; }
  252. if [[ $CHOICE == 0 ]]; then
  253. #
  254. # List test descriptions with numbers and get selection
  255. #
  256. echo "Available '$TESTENV' tests:" ; echo "$OUT" | {
  257. IND=0
  258. while IFS= read -r LINE
  259. do
  260. if [[ $LINE =~ $ISEXEC ]]; then
  261. DESC=$( sed -E 's/^exec_test \$1 \$2 "([^"]+)".*$/\1/g' <<<"$LINE" )
  262. (( ++IND < 10 )) && echo -n " "
  263. echo " $IND) $DESC"
  264. fi
  265. done
  266. }
  267. CHOICE=1
  268. if [[ $TESTCOUNT > 1 ]]; then
  269. for (( ; ; ))
  270. do
  271. read -p "Select a '$TESTENV' test (1-$TESTCOUNT) : " CHOICE
  272. [[ -z "$CHOICE" ]] && { errout "(canceled)" ; exit 1 ; }
  273. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= TESTCOUNT)) && break
  274. errout ">>> Invalid test selection '$CHOICE'."
  275. done
  276. fi
  277. fi
  278. #
  279. # Restore to defaults if requested
  280. #
  281. ((DL_DEFAULTS)) && use_example_configs
  282. #
  283. # Run the specified test lines
  284. #
  285. echo -ne "\033[0;33m"
  286. echo "$OUT" | {
  287. IND=0
  288. GOTX=0
  289. CMD=""
  290. while IFS= read -r LINE
  291. do
  292. if [[ $LINE =~ $ISCMD || $GOTX == 1 ]]; then
  293. ((!IND)) && let IND++
  294. if [[ $LINE =~ $ISEXEC ]]; then
  295. ((IND++ > CHOICE)) && break
  296. else
  297. ((!HEADER)) && {
  298. HEADER=1
  299. echo -e "\n#\n# Test $TESTENV ($CHOICE) $DESC\n#"
  300. }
  301. ((IND == CHOICE)) && {
  302. GOTX=1
  303. [[ -n $DL_DEFAULTS && $LINE =~ $ISRST ]] && LINE="use_example_configs"
  304. [[ $CMD == "" ]] && CMD="$LINE" || CMD=$( echo -e "$CMD$LINE" | sed -e 's/\\//g' | sed -E 's/ +/ /g' )
  305. [[ $LINE =~ $ISCONT ]] || { echo "$CMD" ; eval "$CMD" ; CMD="" ; }
  306. }
  307. fi
  308. fi
  309. done
  310. }
  311. echo -ne "\033[0m"
  312. # Make clear it's a TEST
  313. opt_set CUSTOM_MACHINE_NAME "\"Test $TESTENV ($CHOICE)\""
  314. # Build the test too?
  315. if [[ -z "$BUILD_YES" ]]; then
  316. echo
  317. read -p "Build $TESTENV test #$CHOICE (y/N) ? " BUILD_YES
  318. fi
  319. [[ $BUILD_YES == 'Y' || $BUILD_YES == 'Yes' ]] && {
  320. ((USE_MAKE)) && make tests-single-local TEST_TARGET=$TESTENV ONLY_TEST=$CHOICE
  321. ((USE_MAKE)) || pio run $SILENT_FLAG -d . -e $TESTENV
  322. echo "$TESTENV" >"$STATE_FILE"
  323. }