+
+ def ResolveReferences(self, var_dict, args):
+ """Resolve variable references in a string
+
+ This converts ${blah} within the string to the value of blah.
+ This function works recursively.
+
+ Args:
+ var_dict: Dictionary containing variables and their values
+ args: String containing make arguments
+ Returns:
+ Resolved string
+
+ >>> bsettings.Setup()
+ >>> tcs = Toolchains()
+ >>> tcs.Add('fred', False)
+ >>> var_dict = {'oblique' : 'OBLIQUE', 'first' : 'fi${second}rst', \
+ 'second' : '2nd'}
+ >>> tcs.ResolveReferences(var_dict, 'this=${oblique}_set')
+ 'this=OBLIQUE_set'
+ >>> tcs.ResolveReferences(var_dict, 'this=${oblique}_set${first}nd')
+ 'this=OBLIQUE_setfi2ndrstnd'
+ """
+ re_var = re.compile('(\$\{[-_a-z0-9A-Z]{1,}\})')
+
+ while True:
+ m = re_var.search(args)
+ if not m:
+ break
+ lookup = m.group(0)[2:-1]
+ value = var_dict.get(lookup, '')
+ args = args[:m.start(0)] + value + args[m.end(0):]
+ return args
+
+ def GetMakeArguments(self, board):
+ """Returns 'make' arguments for a given board
+
+ The flags are in a section called 'make-flags'. Flags are named
+ after the target they represent, for example snapper9260=TESTING=1
+ will pass TESTING=1 to make when building the snapper9260 board.
+
+ References to other boards can be added in the string also. For
+ example:
+
+ [make-flags]
+ at91-boards=ENABLE_AT91_TEST=1
+ snapper9260=${at91-boards} BUILD_TAG=442
+ snapper9g45=${at91-boards} BUILD_TAG=443
+
+ This will return 'ENABLE_AT91_TEST=1 BUILD_TAG=442' for snapper9260
+ and 'ENABLE_AT91_TEST=1 BUILD_TAG=443' for snapper9g45.
+
+ A special 'target' variable is set to the board target.
+
+ Args:
+ board: Board object for the board to check.
+ Returns:
+ 'make' flags for that board, or '' if none
+ """
+ self._make_flags['target'] = board.target
+ arg_str = self.ResolveReferences(self._make_flags,
+ self._make_flags.get(board.target, ''))
+ args = arg_str.split(' ')
+ i = 0
+ while i < len(args):
+ if not args[i]:
+ del args[i]
+ else:
+ i += 1
+ return args
+
+ def LocateArchUrl(self, fetch_arch):
+ """Find a toolchain available online
+
+ Look in standard places for available toolchains. At present the
+ only standard place is at kernel.org.
+
+ Args:
+ arch: Architecture to look for, or 'list' for all
+ Returns:
+ If fetch_arch is 'list', a tuple:
+ Machine architecture (e.g. x86_64)
+ List of toolchains
+ else
+ URL containing this toolchain, if avaialble, else None
+ """
+ arch = command.OutputOneLine('uname', '-m')
+ base = 'https://www.kernel.org/pub/tools/crosstool/files/bin'
+ versions = ['4.9.0', '4.6.3', '4.6.2', '4.5.1', '4.2.4']
+ links = []
+ for version in versions:
+ url = '%s/%s/%s/' % (base, arch, version)
+ print 'Checking: %s' % url
+ response = urllib2.urlopen(url)
+ html = response.read()
+ parser = MyHTMLParser(fetch_arch)
+ parser.feed(html)
+ if fetch_arch == 'list':
+ links += parser.links
+ elif parser.arch_link:
+ return url + parser.arch_link
+ if fetch_arch == 'list':
+ return arch, links
+ return None
+
+ def Download(self, url):
+ """Download a file to a temporary directory
+
+ Args:
+ url: URL to download
+ Returns:
+ Tuple:
+ Temporary directory name
+ Full path to the downloaded archive file in that directory,
+ or None if there was an error while downloading
+ """
+ print 'Downloading: %s' % url
+ leaf = url.split('/')[-1]
+ tmpdir = tempfile.mkdtemp('.buildman')
+ response = urllib2.urlopen(url)
+ fname = os.path.join(tmpdir, leaf)
+ fd = open(fname, 'wb')
+ meta = response.info()
+ size = int(meta.getheaders('Content-Length')[0])
+ done = 0
+ block_size = 1 << 16
+ status = ''
+
+ # Read the file in chunks and show progress as we go
+ while True:
+ buffer = response.read(block_size)
+ if not buffer:
+ print chr(8) * (len(status) + 1), '\r',
+ break
+
+ done += len(buffer)
+ fd.write(buffer)
+ status = r'%10d MiB [%3d%%]' % (done / 1024 / 1024,
+ done * 100 / size)
+ status = status + chr(8) * (len(status) + 1)
+ print status,
+ sys.stdout.flush()
+ fd.close()
+ if done != size:
+ print 'Error, failed to download'
+ os.remove(fname)
+ fname = None
+ return tmpdir, fname
+
+ def Unpack(self, fname, dest):
+ """Unpack a tar file
+
+ Args:
+ fname: Filename to unpack
+ dest: Destination directory
+ Returns:
+ Directory name of the first entry in the archive, without the
+ trailing /
+ """
+ stdout = command.Output('tar', 'xvfJ', fname, '-C', dest)
+ return stdout.splitlines()[0][:-1]
+
+ def TestSettingsHasPath(self, path):
+ """Check if buildman will find this toolchain
+
+ Returns:
+ True if the path is in settings, False if not
+ """
+ paths = self.GetPathList(False)
+ return path in paths
+
+ def ListArchs(self):
+ """List architectures with available toolchains to download"""
+ host_arch, archives = self.LocateArchUrl('list')
+ re_arch = re.compile('[-a-z0-9.]*_([^-]*)-.*')
+ arch_set = set()
+ for archive in archives:
+ # Remove the host architecture from the start
+ arch = re_arch.match(archive[len(host_arch):])
+ if arch:
+ arch_set.add(arch.group(1))
+ return sorted(arch_set)
+
+ def FetchAndInstall(self, arch):
+ """Fetch and install a new toolchain
+
+ arch:
+ Architecture to fetch, or 'list' to list
+ """
+ # Fist get the URL for this architecture
+ col = terminal.Color()
+ print col.Color(col.BLUE, "Downloading toolchain for arch '%s'" % arch)
+ url = self.LocateArchUrl(arch)
+ if not url:
+ print ("Cannot find toolchain for arch '%s' - use 'list' to list" %
+ arch)
+ return 2
+ home = os.environ['HOME']
+ dest = os.path.join(home, '.buildman-toolchains')
+ if not os.path.exists(dest):
+ os.mkdir(dest)
+
+ # Download the tar file for this toolchain and unpack it
+ tmpdir, tarfile = self.Download(url)
+ if not tarfile:
+ return 1
+ print col.Color(col.GREEN, 'Unpacking to: %s' % dest),
+ sys.stdout.flush()
+ path = self.Unpack(tarfile, dest)
+ os.remove(tarfile)
+ os.rmdir(tmpdir)
+ print
+
+ # Check that the toolchain works
+ print col.Color(col.GREEN, 'Testing')
+ dirpath = os.path.join(dest, path)
+ compiler_fname_list = self.ScanPath(dirpath, True)
+ if not compiler_fname_list:
+ print 'Could not locate C compiler - fetch failed.'
+ return 1
+ if len(compiler_fname_list) != 1:
+ print col.Color(col.RED, 'Warning, ambiguous toolchains: %s' %
+ ', '.join(compiler_fname_list))
+ toolchain = Toolchain(compiler_fname_list[0], True, True)
+
+ # Make sure that it will be found by buildman
+ if not self.TestSettingsHasPath(dirpath):
+ print ("Adding 'download' to config file '%s'" %
+ bsettings.config_fname)
+ bsettings.SetItem('toolchain', 'download', '%s/*/*' % dest)
+ return 0