1#!/usr/bin/env python
2# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5# vim: set ts=2 sw=2 et sts=2 ai:
6
7"""Minimal tool to download binutils from Google storage.
8
9TODO(mithro): Replace with generic download_and_extract tool.
10"""
11
12import os
13import platform
14import re
15import shutil
16import subprocess
17import sys
18
19
20BINUTILS_DIR = os.path.abspath(os.path.dirname(__file__))
21BINUTILS_FILE = 'binutils.tar.bz2'
22BINUTILS_TOOLS = ['bin/ld.gold', 'bin/objcopy', 'bin/objdump']
23BINUTILS_OUT = 'Release'
24
25DETECT_HOST_ARCH = os.path.abspath(os.path.join(
26    BINUTILS_DIR, '../../build/detect_v8_host_arch.py'))
27
28
29def ReadFile(filename):
30  with file(filename, 'r') as f:
31    return f.read().strip()
32
33
34def WriteFile(filename, content):
35  assert not os.path.exists(filename)
36  with file(filename, 'w') as f:
37    f.write(content)
38    f.write('\n')
39
40
41def GetArch():
42  gyp_host_arch = re.search(
43      'host_arch=(\S*)', os.environ.get('GYP_DEFINES', ''))
44  if gyp_host_arch:
45    arch = gyp_host_arch.group(1)
46    # This matches detect_host_arch.py.
47    if arch == 'x86_64':
48      return 'x64'
49    return arch
50
51  return subprocess.check_output(['python', DETECT_HOST_ARCH]).strip()
52
53
54def FetchAndExtract(arch):
55  archdir = os.path.join(BINUTILS_DIR, 'Linux_' + arch)
56  tarball = os.path.join(archdir, BINUTILS_FILE)
57  outdir = os.path.join(archdir, BINUTILS_OUT)
58
59  sha1file = tarball + '.sha1'
60  if not os.path.exists(sha1file):
61    print "WARNING: No binutils found for your architecture (%s)!" % arch
62    return 0
63
64  checksum = ReadFile(sha1file)
65
66  stampfile = tarball + '.stamp'
67  if os.path.exists(stampfile):
68    if (os.path.exists(tarball) and
69        os.path.exists(outdir) and
70        checksum == ReadFile(stampfile)):
71      return 0
72    else:
73      os.unlink(stampfile)
74
75  print "Downloading", tarball
76  subprocess.check_call([
77      'download_from_google_storage',
78      '--no_resume',
79      '--no_auth',
80      '--bucket', 'chromium-binutils',
81      '-s', sha1file])
82  assert os.path.exists(tarball)
83
84  if os.path.exists(outdir):
85    shutil.rmtree(outdir)
86  assert not os.path.exists(outdir)
87  os.makedirs(outdir)
88  assert os.path.exists(outdir)
89
90  print "Extracting", tarball
91  subprocess.check_call(['tar', 'axf', tarball], cwd=outdir)
92
93  for tool in BINUTILS_TOOLS:
94    assert os.path.exists(os.path.join(outdir, tool))
95
96  WriteFile(stampfile, checksum)
97  return 0
98
99
100def main(args):
101  if not sys.platform.startswith('linux'):
102    return 0
103
104  arch = GetArch()
105  if arch == 'x64':
106    return FetchAndExtract(arch)
107  if arch == 'ia32':
108    ret = FetchAndExtract(arch)
109    if ret != 0:
110      return ret
111    # Fetch the x64 toolchain as well for official bots with 64-bit kernels.
112    return FetchAndExtract('x64')
113  return 0
114
115
116if __name__ == '__main__':
117  sys.exit(main(sys.argv))
118