toolchain.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright (c) 2012 The Chromium OS Authors.
  2. #
  3. # See file CREDITS for list of people who contributed to this
  4. # project.
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. # MA 02111-1307 USA
  20. #
  21. import glob
  22. import os
  23. import bsettings
  24. import command
  25. class Toolchain:
  26. """A single toolchain
  27. Public members:
  28. gcc: Full path to C compiler
  29. path: Directory path containing C compiler
  30. cross: Cross compile string, e.g. 'arm-linux-'
  31. arch: Architecture of toolchain as determined from the first
  32. component of the filename. E.g. arm-linux-gcc becomes arm
  33. """
  34. def __init__(self, fname, test, verbose=False):
  35. """Create a new toolchain object.
  36. Args:
  37. fname: Filename of the gcc component
  38. test: True to run the toolchain to test it
  39. """
  40. self.gcc = fname
  41. self.path = os.path.dirname(fname)
  42. self.cross = os.path.basename(fname)[:-3]
  43. pos = self.cross.find('-')
  44. self.arch = self.cross[:pos] if pos != -1 else 'sandbox'
  45. env = self.MakeEnvironment()
  46. # As a basic sanity check, run the C compiler with --version
  47. cmd = [fname, '--version']
  48. if test:
  49. result = command.RunPipe([cmd], capture=True, env=env)
  50. self.ok = result.return_code == 0
  51. if verbose:
  52. print 'Tool chain test: ',
  53. if self.ok:
  54. print 'OK'
  55. else:
  56. print 'BAD'
  57. print 'Command: ', cmd
  58. print result.stdout
  59. print result.stderr
  60. else:
  61. self.ok = True
  62. self.priority = self.GetPriority(fname)
  63. def GetPriority(self, fname):
  64. """Return the priority of the toolchain.
  65. Toolchains are ranked according to their suitability by their
  66. filename prefix.
  67. Args:
  68. fname: Filename of toolchain
  69. Returns:
  70. Priority of toolchain, 0=highest, 20=lowest.
  71. """
  72. priority_list = ['-elf', '-unknown-linux-gnu', '-linux', '-elf',
  73. '-none-linux-gnueabi', '-uclinux', '-none-eabi',
  74. '-gentoo-linux-gnu', '-linux-gnueabi', '-le-linux', '-uclinux']
  75. for prio in range(len(priority_list)):
  76. if priority_list[prio] in fname:
  77. return prio
  78. return prio
  79. def MakeEnvironment(self):
  80. """Returns an environment for using the toolchain.
  81. Thie takes the current environment, adds CROSS_COMPILE and
  82. augments PATH so that the toolchain will operate correctly.
  83. """
  84. env = dict(os.environ)
  85. env['CROSS_COMPILE'] = self.cross
  86. env['PATH'] += (':' + self.path)
  87. return env
  88. class Toolchains:
  89. """Manage a list of toolchains for building U-Boot
  90. We select one toolchain for each architecture type
  91. Public members:
  92. toolchains: Dict of Toolchain objects, keyed by architecture name
  93. paths: List of paths to check for toolchains (may contain wildcards)
  94. """
  95. def __init__(self):
  96. self.toolchains = {}
  97. self.paths = []
  98. for name, value in bsettings.GetItems('toolchain'):
  99. if '*' in value:
  100. self.paths += glob.glob(value)
  101. else:
  102. self.paths.append(value)
  103. def Add(self, fname, test=True, verbose=False):
  104. """Add a toolchain to our list
  105. We select the given toolchain as our preferred one for its
  106. architecture if it is a higher priority than the others.
  107. Args:
  108. fname: Filename of toolchain's gcc driver
  109. test: True to run the toolchain to test it
  110. """
  111. toolchain = Toolchain(fname, test, verbose)
  112. add_it = toolchain.ok
  113. if toolchain.arch in self.toolchains:
  114. add_it = (toolchain.priority <
  115. self.toolchains[toolchain.arch].priority)
  116. if add_it:
  117. self.toolchains[toolchain.arch] = toolchain
  118. def Scan(self, verbose):
  119. """Scan for available toolchains and select the best for each arch.
  120. We look for all the toolchains we can file, figure out the
  121. architecture for each, and whether it works. Then we select the
  122. highest priority toolchain for each arch.
  123. Args:
  124. verbose: True to print out progress information
  125. """
  126. if verbose: print 'Scanning for tool chains'
  127. for path in self.paths:
  128. if verbose: print " - scanning path '%s'" % path
  129. for subdir in ['.', 'bin', 'usr/bin']:
  130. dirname = os.path.join(path, subdir)
  131. if verbose: print " - looking in '%s'" % dirname
  132. for fname in glob.glob(dirname + '/*gcc'):
  133. if verbose: print " - found '%s'" % fname
  134. self.Add(fname, True, verbose)
  135. def List(self):
  136. """List out the selected toolchains for each architecture"""
  137. print 'List of available toolchains (%d):' % len(self.toolchains)
  138. if len(self.toolchains):
  139. for key, value in sorted(self.toolchains.iteritems()):
  140. print '%-10s: %s' % (key, value.gcc)
  141. else:
  142. print 'None'
  143. def Select(self, arch):
  144. """Returns the toolchain for a given architecture
  145. Args:
  146. args: Name of architecture (e.g. 'arm', 'ppc_8xx')
  147. returns:
  148. toolchain object, or None if none found
  149. """
  150. for name, value in bsettings.GetItems('toolchain-alias'):
  151. if arch == name:
  152. arch = value
  153. if not arch in self.toolchains:
  154. raise ValueError, ("No tool chain found for arch '%s'" % arch)
  155. return self.toolchains[arch]