Add mips support to install-debian.wheezy.sysroot.py
[chromium-blink-merge.git] / chrome / installer / linux / sysroot_scripts / install-debian.wheezy.sysroot.py
blobdfca9a60cc2daaffb2d1e7616904de06d00e449f
1 #!/usr/bin/env python
2 # Copyright (c) 2013 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.
6 # Script to install a Debian Wheezy sysroot for making official Google Chrome
7 # Linux builds.
8 # The sysroot is needed to make Chrome work for Debian Wheezy.
9 # This script can be run manually but is more often run as part of gclient
10 # hooks. When run from hooks this script should be a no-op on non-linux
11 # platforms.
13 # The sysroot image could be constructed from scratch based on the current
14 # state or Debian Wheezy but for consistency we currently use a pre-built root
15 # image. The image will normally need to be rebuilt every time chrome's build
16 # dependancies are changed.
18 import hashlib
19 import platform
20 import optparse
21 import os
22 import re
23 import shutil
24 import subprocess
25 import sys
28 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
29 URL_PREFIX = 'http://storage.googleapis.com'
30 URL_PATH = 'chrome-linux-sysroot/toolchain'
31 REVISION_AMD64 = 'a2d45701cb21244b9514e420950ba6ba687fb655'
32 REVISION_ARM = 'a2d45701cb21244b9514e420950ba6ba687fb655'
33 REVISION_I386 = 'a2d45701cb21244b9514e420950ba6ba687fb655'
34 REVISION_MIPS = '7749d2957387abf225b6d45154c3ddad142148dc'
35 TARBALL_AMD64 = 'debian_wheezy_amd64_sysroot.tgz'
36 TARBALL_ARM = 'debian_wheezy_arm_sysroot.tgz'
37 TARBALL_I386 = 'debian_wheezy_i386_sysroot.tgz'
38 TARBALL_MIPS = 'debian_wheezy_mips_sysroot.tgz'
39 TARBALL_AMD64_SHA1SUM = '601216c0f980e798e7131635f3dd8171b3dcbcde'
40 TARBALL_ARM_SHA1SUM = '6289593b36616526562a4d85ae9c92b694b8ce7e'
41 TARBALL_I386_SHA1SUM = '0090e5a4b56ab9ffb5d557da6a520195ab59b446'
42 TARBALL_MIPS_SHA1SUM = '3b4d782a237db4aac185a638572a7747c1a21825'
43 SYSROOT_DIR_AMD64 = 'debian_wheezy_amd64-sysroot'
44 SYSROOT_DIR_ARM = 'debian_wheezy_arm-sysroot'
45 SYSROOT_DIR_I386 = 'debian_wheezy_i386-sysroot'
46 SYSROOT_DIR_MIPS = 'debian_wheezy_mips-sysroot'
48 valid_archs = ('arm', 'i386', 'amd64', 'mips')
51 def GetSha1(filename):
52 sha1 = hashlib.sha1()
53 with open(filename, 'rb') as f:
54 while True:
55 # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
56 chunk = f.read(1024*1024)
57 if not chunk:
58 break
59 sha1.update(chunk)
60 return sha1.hexdigest()
63 def DetectArch(gyp_defines):
64 # Check for optional target_arch and only install for that architecture.
65 # If target_arch is not specified, then only install for the host
66 # architecture.
67 if 'target_arch=x64' in gyp_defines:
68 return 'amd64'
69 elif 'target_arch=ia32' in gyp_defines:
70 return 'i386'
71 elif 'target_arch=arm' in gyp_defines:
72 return 'arm'
73 elif 'target_arch=mipsel' in gyp_defines:
74 return 'mips'
76 # Figure out host arch using build/detect_host_arch.py and
77 # set target_arch to host arch
78 SRC_DIR = os.path.abspath(
79 os.path.join(SCRIPT_DIR, '..', '..', '..', '..'))
80 sys.path.append(os.path.join(SRC_DIR, 'build'))
81 import detect_host_arch
83 detected_host_arch = detect_host_arch.HostArch()
84 if detected_host_arch == 'x64':
85 return 'amd64'
86 elif detected_host_arch == 'ia32':
87 return 'i386'
88 elif detected_host_arch == 'arm':
89 return 'arm'
90 elif detected_host_arch == 'mips':
91 return 'mips'
92 else:
93 print "Unknown host arch: %s" % detected_host_arch
95 return None
98 def main():
99 if options.running_as_hook and not sys.platform.startswith('linux'):
100 return 0
102 gyp_defines = os.environ.get('GYP_DEFINES', '')
104 if options.arch:
105 target_arch = options.arch
106 else:
107 target_arch = DetectArch(gyp_defines)
108 if not target_arch:
109 print 'Unable to detect host architecture'
110 return 1
112 if options.running_as_hook and target_arch != 'arm' and target_arch != 'mips':
113 # When run from runhooks, only install the sysroot for an Official Chrome
114 # Linux build, except on ARM where we always use a sysroot.
115 skip_if_defined = ['branding=Chrome', 'buildtype=Official']
116 skip_if_undefined = ['chromeos=1']
117 for option in skip_if_defined:
118 if option not in gyp_defines:
119 return 0
120 for option in skip_if_undefined:
121 if option in gyp_defines:
122 return 0
124 # The sysroot directory should match the one specified in build/common.gypi.
125 # TODO(thestig) Consider putting this else where to avoid having to recreate
126 # it on every build.
127 linux_dir = os.path.dirname(SCRIPT_DIR)
128 if target_arch == 'amd64':
129 sysroot = os.path.join(linux_dir, SYSROOT_DIR_AMD64)
130 tarball_filename = TARBALL_AMD64
131 tarball_sha1sum = TARBALL_AMD64_SHA1SUM
132 revision = REVISION_AMD64
133 elif target_arch == 'arm':
134 sysroot = os.path.join(linux_dir, SYSROOT_DIR_ARM)
135 tarball_filename = TARBALL_ARM
136 tarball_sha1sum = TARBALL_ARM_SHA1SUM
137 revision = REVISION_ARM
138 elif target_arch == 'i386':
139 sysroot = os.path.join(linux_dir, SYSROOT_DIR_I386)
140 tarball_filename = TARBALL_I386
141 tarball_sha1sum = TARBALL_I386_SHA1SUM
142 revision = REVISION_I386
143 elif target_arch == 'mips':
144 sysroot = os.path.join(linux_dir, SYSROOT_DIR_MIPS)
145 tarball_filename = TARBALL_MIPS
146 tarball_sha1sum = TARBALL_MIPS_SHA1SUM
147 revision = REVISION_MIPS
148 else:
149 print 'Unknown architecture: %s' % target_arch
150 assert(False)
152 url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
154 stamp = os.path.join(sysroot, '.stamp')
155 if os.path.exists(stamp):
156 with open(stamp) as s:
157 if s.read() == url:
158 print 'Debian Wheezy %s root image already up-to-date: %s' % \
159 (target_arch, sysroot)
160 return 0
162 print 'Installing Debian Wheezy %s root image: %s' % (target_arch, sysroot)
163 if os.path.isdir(sysroot):
164 shutil.rmtree(sysroot)
165 os.mkdir(sysroot)
166 tarball = os.path.join(sysroot, tarball_filename)
167 print 'Downloading %s' % url
168 sys.stdout.flush()
169 sys.stderr.flush()
170 subprocess.check_call(['curl', '--fail', '-L', url, '-o', tarball])
171 sha1sum = GetSha1(tarball)
172 if sha1sum != tarball_sha1sum:
173 print 'Tarball sha1sum is wrong.'
174 print 'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum)
175 return 1
176 subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
177 os.remove(tarball)
179 with open(stamp, 'w') as s:
180 s.write(url)
181 return 0
184 if __name__ == '__main__':
185 parser = optparse.OptionParser('usage: %prog [OPTIONS]')
186 parser.add_option('--running-as-hook', action='store_true',
187 default=False, help='Used when running from gclient hooks.'
188 ' In this mode the sysroot will only '
189 'be installed for official Linux '
190 'builds or ARM Linux builds')
191 parser.add_option('--arch', type='choice', choices=valid_archs,
192 help='Sysroot architecture: %s' % ', '.join(valid_archs))
193 options, _ = parser.parse_args()
194 sys.exit(main())