download_and_install_gccarm.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #! /usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import os, urllib, subprocess, sys
  4. import archive_directory
  5. import tool_directory
  6. import dev_platform
  7. # FOR PRINTING IN COLOR
  8. class bcolors:
  9. HEADER = '\033[95m'
  10. BLUE = '\033[94m'
  11. GREEN = '\033[92m'
  12. WARNING = '\033[93m'
  13. FAIL = '\033[91m'
  14. ENDC = '\033[0m'
  15. BOLD = '\033[1m'
  16. UNDERLINE = '\033[4m'
  17. BOLD_BLUE = '\033[1m' + '\033[94m'
  18. BOLD_GREEN = '\033[1m' + '\033[92m'
  19. BOLD_RED = '\033[1m' + '\033[91m'
  20. # DISTRIBUTION GCC
  21. def distributionGCC () :
  22. gcc = "gcc-arm-none-eabi-7-2017-q4-major"
  23. if dev_platform.getPlatform () == "linux32" :
  24. gcc = "gcc-arm-none-eabi-5_4-2016q3"
  25. return gcc
  26. # ARCHIVE URL (from https://developer.arm.com/open-source/gnu-toolchain/gnu-rm)
  27. def compilerArchiveURL () :
  28. baseURL = "https://developer.arm.com/-/media/Files/downloads/gnu-rm/"
  29. PLATFORM = dev_platform.getPlatform ()
  30. distribution = "7-2017q4/gcc-arm-none-eabi-7-2017-q4-major-" + PLATFORM + ".tar.bz2"
  31. if dev_platform.getPlatform () == "linux32" :
  32. distribution = "5_4-2016q3/gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2"
  33. return baseURL + distribution
  34. # runCommand
  35. def runCommand (cmd) :
  36. str = "+"
  37. for s in cmd:
  38. str += " " + s
  39. print bcolors.BOLD_BLUE + str + bcolors.ENDC
  40. returncode = subprocess.call (cmd)
  41. if returncode != 0 :
  42. sys.exit (returncode)
  43. # ARCHIVE DOWNLOAD
  44. def downloadReportHook (a,b,fileSize):
  45. # "," at the end of the line is important!
  46. if fileSize < (1 << 10):
  47. sizeString = str (fileSize)
  48. else:
  49. if fileSize < (1 << 20):
  50. sizeString = str (fileSize >> 10) + "Ki"
  51. else:
  52. sizeString = str (fileSize >> 20) + "Mi"
  53. print "% 3.1f%% of %sB\r" % (min(100.0, float(a * b) / fileSize * 100.0), sizeString),
  54. sys.stdout.flush()
  55. def downloadArchive (archiveURL, archivePath):
  56. print ("URL: "+ archiveURL)
  57. print "Downloading..."
  58. urllib.urlretrieve (archiveURL, archivePath, downloadReportHook)
  59. print ""
  60. # Install GCC
  61. def install_gcc (INSTALL_DIR) :
  62. print bcolors.BOLD_GREEN + "Install GCC tools..." + bcolors.ENDC
  63. DISTRIBUTION = distributionGCC ()
  64. # Archive dir
  65. COMPILER_ARCHIVE_DIR = archive_directory.createAndGetArchiveDirectory ()
  66. # Download
  67. if not os.path.exists (COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2"):
  68. print "Nothing found in" + COMPILER_ARCHIVE_DIR
  69. url = compilerArchiveURL ()
  70. runCommand (["rm", "-f", COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2.downloading"])
  71. downloadArchive (url, COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2.downloading")
  72. runCommand (["mv",
  73. COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2.downloading",
  74. COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2"
  75. ])
  76. # Install
  77. if not os.path.exists (INSTALL_DIR):
  78. os.mkdir (INSTALL_DIR)
  79. if not os.path.exists (INSTALL_DIR + "/" + DISTRIBUTION):
  80. runCommand (["cp", COMPILER_ARCHIVE_DIR + "/" + DISTRIBUTION + ".tar.bz2", INSTALL_DIR + "/" + DISTRIBUTION + ".tar.bz2"])
  81. savedCurrentDir = os.getcwd ()
  82. os.chdir (INSTALL_DIR)
  83. print bcolors.BOLD_BLUE + "+ cd " + INSTALL_DIR + bcolors.ENDC
  84. runCommand (["bunzip2", DISTRIBUTION + ".tar.bz2"])
  85. runCommand (["tar", "xf", DISTRIBUTION + ".tar"])
  86. runCommand (["rm", DISTRIBUTION + ".tar"])
  87. os.chdir (savedCurrentDir)
  88. # GET GCC TOOL DIRECTORY
  89. def installGCCARMandGetToolDirectory () :
  90. # Absolute path of tool directory
  91. TOOL_DIRECTORY = tool_directory.toolDirectory () + "/" + distributionGCC ()
  92. # Install tool ?
  93. if not os.path.exists (TOOL_DIRECTORY) :
  94. install_gcc (tool_directory.toolDirectory ())
  95. # Return tool directory
  96. return TOOL_DIRECTORY