MK404-build.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #!/bin/bash
  2. # This bash script is used to compile automatically and run the MK404 simulator
  3. #
  4. # Supported OS: Linux64 bit
  5. #
  6. # Linux:
  7. # Linux Ubuntu
  8. # 1. Follow these instructions
  9. # 2. Open Ubuntu bash and get latest updates with 'sudo apt-get update'
  10. # 3. Install latest updates with 'sudo apt-get upgrade'
  11. #
  12. #
  13. # Version: 1.0.0-Build_6
  14. # Change log:
  15. # 11 Feb 2021, 3d-gussner, Inital
  16. # 11 Feb 2021, 3d-gussner, Optional flags to check for updates
  17. # 12 Feb 2021, 3d-gussner, Update cmake
  18. # 13 Feb 2021, 3d-gussner, Auto build SD cards
  19. # 18 Jun 2021, 3d-gussner, Documentation and version number
  20. # 18 Jun 2021, 3d-gussner, Added some arguments and checks
  21. while getopts c:f:g:m:n:p:u:v:x:?h flag
  22. do
  23. case "${flag}" in
  24. c) check_flag=${OPTARG};;
  25. f) board_flash_flag=${OPTARG};;
  26. g) graphics_flag=${OPTARG};;
  27. h) help_flag=1;;
  28. m) mk404_flag=${OPTARG};;
  29. p) mk404_printer_flag=${OPTARG};;
  30. n) new_build_flag=${OPTARG};;
  31. u) update_flag=${OPTARG};;
  32. v) firmware_version_flag=${OPTARG};;
  33. x) board_mem_flag=${OPTARG};;
  34. ?) help_flag=1;;
  35. esac
  36. done
  37. #Debug echos
  38. #echo "$check_flag"
  39. #echo "$update_flag"
  40. #echo "$new_build_flag"
  41. #echo "$mk404_flag"
  42. #echo "$graphics_flag"
  43. #echo "$mk404_printer_flag"
  44. # '?' 'h' argument usage and help
  45. if [ "$help_flag" == "1" ] ; then
  46. echo "***************************************"
  47. echo "* MK404-build.sh Version: 1.0.0-Build_6 *"
  48. echo "***************************************"
  49. echo "Arguments:"
  50. echo "$(tput setaf 2)-c$(tput sgr0) Check for update '$(tput setaf 2)0$(tput sgr0)' no '$(tput setaf 2)1$(tput sgr0)' yes"
  51. echo "$(tput setaf 2)-f$(tput sgr0) Board flash size '$(tput setaf 2)256$(tput sgr0)','$(tput setaf 2)384$(tput sgr0)','$(tput setaf 2)512$(tput sgr0)','$(tput setaf 2)1024$(tput sgr0)''$(tput setaf 2)32M$(tput sgr0)'"
  52. echo "$(tput setaf 2)-g$(tput sgr0) Start MK404 grafics '$(tput setaf 2)0$(tput sgr0)' no '$(tput setaf 2)1$(tput sgr0)' lite '$(tput setaf 2)2$(tput sgr0)' fancy"
  53. echo "$(tput setaf 2)-h$(tput sgr0) Help"
  54. echo "$(tput setaf 2)-g$(tput sgr0) Start MK404 grafics '$(tput setaf 2)0$(tput sgr0)' no '$(tput setaf 2)1$(tput sgr0)' lite '$(tput setaf 2)2$(tput sgr0)' fancy"
  55. echo "$(tput setaf 2)-m$(tput sgr0) Start MK404 sim '$(tput setaf 2)0$(tput sgr0)' no '$(tput setaf 2)1$(tput sgr0)' yes '$(tput setaf 2)2$(tput sgr0)' with MMU2"
  56. echo "$(tput setaf 2)-n$(tput sgr0) Force new build '$(tput setaf 2)0$(tput sgr0)' no '$(tput setaf 2)1$(tput sgr0)' yes"
  57. echo "$(tput setaf 2)-p$(tput sgr0) MK404 Printer '$(tput setaf 2)MK25$(tput sgr0)', '$(tput setaf 2)MK25S$(tput sgr0)', '$(tput setaf 2)MK3$(tput sgr0)' or '$(tput setaf 2)MK3S$(tput sgr0)'"
  58. echo "$(tput setaf 2)-u$(tput sgr0) Start MK404 grafics '$(tput setaf 2)0$(tput sgr0)' no '$(tput setaf 2)1$(tput sgr0)' lite '$(tput setaf 2)2$(tput sgr0)' fancy"
  59. echo "$(tput setaf 2)-v$(tput sgr0) Prusa-Firmware version '$(tput setaf 2)path+file name$(tput sgr0)'"
  60. echo "$(tput setaf 2)-x$(tput sgr0) Board memory size '$(tput setaf 2)8$(tput sgr0)' or '$(tput setaf 2)64$(tput sgr0)' Kb."
  61. echo "$(tput setaf 2)-?$(tput sgr0) Help"
  62. echo
  63. echo "Brief USAGE:"
  64. echo " $(tput setaf 2)./MK404-build.sh$(tput sgr0) [-c] [-f] [-g] [-m] [-n] [-p] [-u] [-h] [-?]"
  65. echo
  66. echo "Example:"
  67. echo " $(tput setaf 2)./MK404-build.sh -f 1$(tput sgr0)"
  68. echo " Will force an update and rebuild the MK404 SIM"
  69. echo
  70. echo " $(tput setaf 2)./MK404-build.sh -m 1 -g 1 -v ../../../../Prusa-Firmware/PF-build-hex/FW3100-Build4481/BOAD_EINSY_1_0a/FW3100-Build4481-1_75mm_MK3S-EINSy10a-E3Dv6full.hex$(tput sgr0)"
  71. echo " Will start MK404 with Prusa_MK3S and Prusa-Firmware 3.10.0-Build4481"
  72. exit 1
  73. fi
  74. #### Start check if OSTYPE is supported
  75. OS_FOUND=$( command -v uname)
  76. case $( "${OS_FOUND}" | tr '[:upper:]' '[:lower:]') in
  77. linux*)
  78. TARGET_OS="linux"
  79. ;;
  80. *)
  81. TARGET_OS='unknown'
  82. ;;
  83. esac
  84. # Linux
  85. if [ $TARGET_OS == "linux" ]; then
  86. if [ $(uname -m) == "x86_64" ]; then
  87. echo "$(tput setaf 2)Linux 64-bit found$(tput sgr0)"
  88. Processor="64"
  89. #elif [[ $(uname -m) == "i386" || $(uname -m) == "i686" ]]; then
  90. # echo "$(tput setaf 2)Linux 32-bit found$(tput sgr0)"
  91. # Processor="32"
  92. else
  93. echo "$(tput setaf 1)Unsupported OS: Linux $(uname -m)"
  94. echo "Please refer to the notes of build.sh$(tput sgr0)"
  95. exit 2
  96. fi
  97. else
  98. echo "$(tput setaf 1)This script doesn't support your Operating system!"
  99. echo "Please use Linux 64-bit"
  100. echo "Read the notes of build.sh$(tput sgr0)"
  101. exit 2
  102. fi
  103. sleep 2
  104. #### End check if OSTYPE is supported
  105. #### Check MK404 dependencies
  106. packages=(
  107. "libelf-dev"
  108. "gcc-7"
  109. "gcc-avr"
  110. "libglew-dev"
  111. "freeglut3-dev"
  112. "libsdl-sound1.2-dev"
  113. "libpng-dev"
  114. "cmake"
  115. "zip"
  116. "wget"
  117. "git"
  118. "build-essential"
  119. "lcov"
  120. "mtools"
  121. )
  122. for check_package in ${packages[@]}; do
  123. if dpkg-query -W -f'${db:Status-Abbrev}\n' $check_package 2>/dev/null \
  124. | grep -q '^.i $'; then
  125. echo "$(tput setaf 2)$check_package: Installed$(tput sgr0)"
  126. else
  127. echo "$(tput setaf 1)$check_package: Not installed use $(tput setaf 3)'sudo apt install $check_package'$(tput setaf 1) to install missing package$(tput sgr0)"
  128. not_installed=1;
  129. fi
  130. done
  131. if [ "$not_installed" = "1" ]; then
  132. exit 3
  133. fi
  134. #### End Check MK404 dependencies
  135. #### Set build environment
  136. MK404_SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
  137. MK404_URL="https://github.com/vintagepc/MK404.git"
  138. MK404_owner="vintagepc"
  139. MK404_project="MK404"
  140. MK404_PATH="$MK404_SCRIPT_PATH/../MK404/master"
  141. MK404_BUILD_PATH="$MK404_PATH/build"
  142. # List few useful data
  143. echo
  144. echo "Script path :" $MK404_SCRIPT_PATH
  145. echo "OS :" $TARGET_OS
  146. echo ""
  147. echo "MK404 path :" $MK404_PATH
  148. # Clone MK404 if needed
  149. if [ ! -d $MK404_PATH ]; then
  150. #release_url=$(curl -Ls -o /dev/null -w %{url_effective} https://github.com/$MK404_owner/$MK404_project/releases/latest)
  151. #release_tag=$(basename $release_url)
  152. #git clone -b $release_tag -- https://github.com/$MK404_owner/$MK404_project.git $MK404_PATH
  153. git clone $MK404_URL $MK404_PATH
  154. fi
  155. #
  156. cd $MK404_PATH
  157. #Check MK404 agruments
  158. #Check mk404_printer_flag
  159. if [ ! -z $mk404_printer_flag ]; then
  160. if [[ "$mk404_printer_flag" == "MK3" || "$mk404_printer_flag" == "MK3S" || "$mk404_printer_flag" == "MK25" || "$mk404_printer_flag" == "MK25S" ]]; then
  161. MK404_PRINTER_TEMP=$mk404_printer_flag
  162. else
  163. echo "Following Printers are supported: MK25, MK25S, MK3 and MK3S"
  164. exit 4
  165. fi
  166. fi
  167. #Check if Build is selected with argument '-f'
  168. if [ ! -z "$board_flash_flag" ] ; then
  169. if [ "$board_flash_flag" == "256" ] ; then
  170. BOARD_FLASH="0x3FFFF"
  171. BOARD_maximum_size="253952"
  172. echo "Board flash size : $board_flash_flag Kb, $BOARD_maximum_size bytes, $BOARD_FLASH (hex)"
  173. elif [ "$board_flash_flag" == "384" ] ; then
  174. BOARD_FLASH="0x5FFFF"
  175. BOARD_maximum_size="385024"
  176. echo "Board flash size : $board_flash_flag Kb, $BOARD_maximum_size bytes, $BOARD_FLASH (hex)"
  177. elif [ "$board_flash_flag" == "512" ] ; then
  178. BOARD_FLASH="0x7FFFF"
  179. BOARD_maximum_size="516096"
  180. echo "Board flash size : $board_flash_flag Kb, $BOARD_maximum_size bytes, $BOARD_FLASH (hex)"
  181. elif [ "$board_flash_flag" == "1024" ] ; then
  182. BOARD_FLASH="0xFFFFF"firmware_version_flag
  183. BOARD_maximum_size="1040384"
  184. echo "Board flash size : $board_flash_flag Kb, $BOARD_maximum_size bytes, $BOARD_FLASH (hex)"
  185. elif [[ "$board_flash_flag" == "32M" || "$board_flash_flag" == "32768" ]] ; then
  186. BOARD_FLASH="0x1FFFFFF"
  187. BOARD_maximum_size="33546240"
  188. echo "Board flash size : 32 Mb, $BOARD_maximum_size bytes, $BOARD_FLASH (hex)"
  189. else
  190. echo "Unsupported board flash size chosen. Only '256', '384', '512', '1024' and '32M' are allowed."
  191. exit 5
  192. fi
  193. fi
  194. #Check if Build is selected with argument '-x'
  195. if [ ! -z "$board_mem_flag" ] ; then
  196. if [ "$board_mem_flag" == "8" ] ; then
  197. BOARD_MEM="0x21FF"
  198. echo "Board mem size : $board_mem_flag Kb, $BOARD_MEM (hex)"
  199. elif [ "$board_mem_flag" == "64" ] ; then
  200. BOARD_MEM="0xFFFF"
  201. echo "Board mem size : $board_mem_flag Kb, $BOARD_MEM (hex)"
  202. else
  203. echo "Unsupported board mem size chosen. Only '8', '64' are allowed."
  204. exit 6
  205. fi
  206. fi
  207. if [ "$new_build_flag" == "1" ]; then
  208. check_flag=1
  209. update_flag=1
  210. fi
  211. if [ "$update_flag" == "1" ]; then
  212. check_flag=1
  213. fi
  214. #End Check MK404 agruments
  215. #Check for updates
  216. if [ "$check_flag" == "1" ]; then
  217. if [ -d $MK404_BUILD_PATH ]; then
  218. cd $MK404_BUILD_PATH
  219. MK404_current_version=$( command ./MK404 --version | grep "MK404" | cut -f 4 -d " ")
  220. cd $MK404_PATH
  221. else
  222. echo "Cannot check current version as it has not been build."
  223. fi
  224. # Get local Commit_Hash
  225. MK404_local_GIT_COMMIT_HASH=$(git log --pretty=format:"%H" -1)
  226. # Get local Commit_Number
  227. MK404_local_GIT_COMMIT_NUMBER=$(git rev-list HEAD --count)
  228. # Get remote Commit_Hash
  229. MK404_remote_GIT_COMMIT_HASH=$(git ls-remote --heads $(git config --get remote.origin.url) | grep "refs/heads/master" | cut -f 1)
  230. # Get remote Commit_Number
  231. MK404_remote_GIT_COMMIT_NUMBER=$(git rev-list origin/master --count)
  232. # Output
  233. echo ""
  234. echo "Current version : $MK404_current_version"
  235. echo ""
  236. echo "Current local hash : $MK404_local_GIT_COMMIT_HASH"
  237. echo "Current local commit nr : $MK404_local_GIT_COMMIT_NUMBER"
  238. if [ "$MK404_local_GIT_COMMIT_HASH" != "$MK404_remote_GIT_COMMIT_HASH" ]; then
  239. echo "$(tput setaf 1)"
  240. else
  241. echo "$(tput setaf 2)"
  242. fi
  243. echo "Current remote hash : $MK404_remote_GIT_COMMIT_HASH"
  244. echo "Current remote commit nr: $MK404_remote_GIT_COMMIT_NUMBER"
  245. echo "$(tput sgr 0)"
  246. # Check for updates
  247. if [[ "$MK404_local_GIT_COMMIT_HASH" != "$MK404_remote_GIT_COMMIT_HASH" && -z "$update_flag" ]]; then
  248. echo "$(tput setaf 2)Update is availible.$(tput sgr 0)"
  249. read -t 10 -n 1 -p "$(tput setaf 3)Update now Y/n$(tput sgr 0)" update_answer
  250. if [ "$update_answer" == "Y" ]; then
  251. update_flag=1
  252. fi
  253. echo ""
  254. fi
  255. fi
  256. # Fetch updates and force new build
  257. if [ "$update_flag" == "1" ]; then
  258. if [ "$MK404_local_GIT_COMMIT_HASH" != "$MK404_remote_GIT_COMMIT_HASH" ]; then
  259. echo ""
  260. git fetch --all
  261. read -t 10 -p "$(tput setaf 2)Updating MK404 !$(tput sgr 0)"
  262. echo ""
  263. git reset --hard origin/master
  264. read -t 10 -p "$(tput setaf 2)Compiling MK404 !$(tput sgr 0)"
  265. echo ""
  266. new_build_flag=1
  267. fi
  268. fi
  269. # Prepare MK404
  270. mkdir -p $MK404_BUILD_PATH
  271. if [[ ! -f "$MK404_BUILD_PATH/Makefile" || "$new_build_flag" == "1" ]]; then
  272. # Init and update submodules
  273. git submodule init
  274. git submodule update
  275. cmake -B$MK404_BUILD_PATH -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles"
  276. fi
  277. # Make MK404
  278. cd $MK404_BUILD_PATH
  279. if [[ ! -f "$MK404_BUILD_PATH/MK404" || "$new_build_flag" == "1" ]]; then
  280. make
  281. fi
  282. # Make SDcards
  283. if [[ ! -f "$MK404_BUILD_PATH/Prusa_MK3S_SDcard.bin" || "$new_build_flag" == "1" ]]; then
  284. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK3S_SDcard.bin
  285. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK3_SDcard.bin
  286. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK25_13_SDcard.bin
  287. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK25S_13_SDcard.bin
  288. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK3SMMU2_SDcard.bin
  289. cmake --build $MK404_BUILD_PATH --config Release --target Prusa_MK3MMU2_SDcard.bin
  290. fi
  291. # Prepare run MK404
  292. #Check MK404_Printer
  293. MK404_PRINTER_TEMP=$(echo $firmware_version_flag | sed 's/\(.*\)\///' | grep 'MK3')
  294. if [ ! -z $MK404_PRINTER_TEMP ]; then
  295. MK404_PRINTER=MK3
  296. fi
  297. MK404_PRINTER_TEMP=$(echo $firmware_version_flag | sed 's/\(.*\)\///' | grep 'MK3S')
  298. if [ ! -z $MK404_PRINTER_TEMP ]; then
  299. MK404_PRINTER=MK3S
  300. fi
  301. MK404_PRINTER_TEMP=$(echo $firmware_version_flag | sed 's/\(.*\)\///' | grep 'MK25')
  302. if [ ! -z $MK404_PRINTER_TEMP ]; then
  303. MK404_PRINTER=MK25
  304. fi
  305. MK404_PRINTER_TEMP=$(echo $firmware_version_flag | sed 's/\(.*\)\///' | grep 'MK25S')
  306. if [ ! -z $MK404_PRINTER_TEMP ]; then
  307. MK404_PRINTER=MK25S
  308. fi
  309. if [ -z "$MK404_PRINTER" ]; then
  310. echo "Tried to determine MK404 printer from hex file, but failed!"
  311. echo "Add argument -p with 'MK25', 'MK25S', 'MK3' or 'MK3S' to start MK404"
  312. fi
  313. if [ ! -z $mk404_printer_flag ]; then
  314. if [ "$mk404_printer_flag" != "$MK404_PRINTER" ]; then
  315. echo "$(tput setaf 3)You defined a different printer type than the firmware!"
  316. echo "This can cause unexpected issues.$(tput sgr 0)"
  317. echo
  318. PS3="Select $(tput setaf 2)printer$(tput sgr 0) you want to use."
  319. select which in "$(tput setaf 2)$MK404_PRINTER$(tput sgr 0)" "$mk404_printer_flag"; do
  320. case $which in
  321. $MK404_PRINTER)
  322. echo "Set $MK404_PRINTER as printer"
  323. break
  324. ;;
  325. $mk404_printer_flag)
  326. echo "Set $(tput setaf 3)$mk404_printer_flag$(tput sgr 0) as printer"
  327. echo "$(tput setaf 3)This firmware file isn't correct for this printer!!!$(tput sgr 0)"
  328. echo
  329. MK404_PRINTER=$mk404_printer_flag
  330. read -p "Press Enter to continue."
  331. break
  332. ;;
  333. *)
  334. break
  335. ;;
  336. esac
  337. done
  338. fi
  339. fi
  340. if [ -z $MK404_PRINTER ]; then
  341. exit 7
  342. fi
  343. if [[ "$MK404_PRINTER" == "MK25" || "$MK404_PRINTER" == "MK25S" ]]; then
  344. MK404_PRINTER="${MK404_PRINTER}_mR13"
  345. else
  346. if [[ "$mk404_flag" == "2" || "$mk404_flag" == "MMU2" || "$mk404_flag" == "MMU2S" ]]; then # Check if MMU2 is selected only for MK3/S
  347. MK404_PRINTER="${MK404_PRINTER}MMU2"
  348. fi
  349. fi
  350. # Run MK404 with 'debugcore' and/or 'bootloader-file'
  351. if [[ ! -z $MK404_DEBUG && "$MK404_DEBUG" == "atmega404" || ! -z $BOARD_MEM && "$BOARD_MEM" == "0xFFFF" ]]; then
  352. MK404_options="--debugcore"
  353. fi
  354. if [[ ! -z $MK404_DEBUG && "$MK404_DEBUG" == "atmega404_no_bootloader" || ! -z $BOARD_FLASH && "$BOARD_FLASH" != "0x3FFFF" ]]; then
  355. MK404_options='--debugcore --bootloader-file ""'
  356. fi
  357. # Run MK404 with grafics
  358. if [ ! -z "$graphics_flag" ]; then
  359. if [ ! -z "$MK404_options" ]; then
  360. MK404_options="${MK404_options} --colour-extrusion --extrusion Quad_HR -g "
  361. else
  362. MK404_options="--colour-extrusion --extrusion Quad_HR -g "
  363. fi
  364. if [[ "$graphics_flag" == "1" || "$graphics_flag" == "lite" ]]; then
  365. MK404_options="${MK404_options}lite"
  366. elif [[ "$graphics_flag" == "2" || "$graphics_flag" == "fancy" ]]; then
  367. MK404_options="${MK404_options}fancy"
  368. else
  369. echo "$(tput setaf 1)Unsupported MK404 graphics option $graphics_flag$(tput sgr 0)"
  370. fi
  371. fi
  372. if [ ! -z $firmware_version_flag ]; then
  373. MK404_firmware_file=$firmware_version_flag
  374. fi
  375. #Run MK404 SIM
  376. if [ ! -z $mk404_flag ]; then
  377. # Output some useful data
  378. echo "Printer : $MK404_PRINTER"
  379. echo "Options : $MK404_options"
  380. echo ""
  381. read -t 5 -p "Press $(tput setaf 2)Enter$(tput sgr 0) to start MK404"
  382. echo ""
  383. # Change to MK404 build folder
  384. cd $MK404_BUILD_PATH
  385. # Start MK404
  386. # default with serial output and terminal to manipulate it via terminal
  387. echo ""
  388. echo "./MK404 Prusa_$MK404_PRINTER -s --terminal $MK404_options -f $MK404_firmware_file"
  389. sleep 5
  390. ./MK404 Prusa_$MK404_PRINTER -s --terminal $MK404_options -f $MK404_firmware_file || exit 8
  391. fi
  392. #### End of MK404 Simulator