Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / build / linux / sysroot_scripts / install-sysroot.py
blobc1112891e282c0b255925f97cf6dfbbe63c1d562
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 build_dir = os.path.dirname(os.path.dirname(os.path.join(SCRIPT_DIR)))
79 sys.path.append(build_dir)
80 import detect_host_arch
82 detected_host_arch = detect_host_arch.HostArch()
83 if detected_host_arch == 'x64':
84 return 'amd64'
85 elif detected_host_arch == 'ia32':
86 return 'i386'
87 elif detected_host_arch == 'arm':
88 return 'arm'
89 elif detected_host_arch == 'mips':
90 return 'mips'
91 else:
92 print "Unknown host arch: %s" % detected_host_arch
94 return None
97 def UsingSysroot(target_arch, gyp_defines):
98 # ChromeOS uses a chroot, so doesn't need a sysroot
99 if 'chromeos=1' in gyp_defines:
100 return False
102 # When cross-compiling we always use a sysroot
103 if target_arch in ('arm', 'mips', 'i386'):
104 return True
106 # Setting use_sysroot=1 GYP_DEFINES forces the use of the sysroot even
107 # when not cross compiling
108 if 'use_sysroot=1' in gyp_defines:
109 return True
111 # Official builds always use the sysroot.
112 if 'branding=Chrome' in gyp_defines and 'buildtype=Official' in gyp_defines:
113 return True
115 return False
118 def main():
119 if options.running_as_hook and not sys.platform.startswith('linux'):
120 return 0
122 gyp_defines = os.environ.get('GYP_DEFINES', '')
124 if options.arch:
125 target_arch = options.arch
126 else:
127 target_arch = DetectArch(gyp_defines)
128 if not target_arch:
129 print 'Unable to detect host architecture'
130 return 1
132 if options.running_as_hook and not UsingSysroot(target_arch, gyp_defines):
133 return 0
135 # The sysroot directory should match the one specified in build/common.gypi.
136 # TODO(thestig) Consider putting this else where to avoid having to recreate
137 # it on every build.
138 linux_dir = os.path.dirname(SCRIPT_DIR)
139 if target_arch == 'amd64':
140 sysroot = os.path.join(linux_dir, SYSROOT_DIR_AMD64)
141 tarball_filename = TARBALL_AMD64
142 tarball_sha1sum = TARBALL_AMD64_SHA1SUM
143 revision = REVISION_AMD64
144 elif target_arch == 'arm':
145 sysroot = os.path.join(linux_dir, SYSROOT_DIR_ARM)
146 tarball_filename = TARBALL_ARM
147 tarball_sha1sum = TARBALL_ARM_SHA1SUM
148 revision = REVISION_ARM
149 elif target_arch == 'i386':
150 sysroot = os.path.join(linux_dir, SYSROOT_DIR_I386)
151 tarball_filename = TARBALL_I386
152 tarball_sha1sum = TARBALL_I386_SHA1SUM
153 revision = REVISION_I386
154 elif target_arch == 'mips':
155 sysroot = os.path.join(linux_dir, SYSROOT_DIR_MIPS)
156 tarball_filename = TARBALL_MIPS
157 tarball_sha1sum = TARBALL_MIPS_SHA1SUM
158 revision = REVISION_MIPS
159 else:
160 print 'Unknown architecture: %s' % target_arch
161 assert(False)
163 url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
165 stamp = os.path.join(sysroot, '.stamp')
166 if os.path.exists(stamp):
167 with open(stamp) as s:
168 if s.read() == url:
169 print 'Debian Wheezy %s root image already up-to-date: %s' % \
170 (target_arch, sysroot)
171 return 0
173 print 'Installing Debian Wheezy %s root image: %s' % (target_arch, sysroot)
174 if os.path.isdir(sysroot):
175 shutil.rmtree(sysroot)
176 os.mkdir(sysroot)
177 tarball = os.path.join(sysroot, tarball_filename)
178 print 'Downloading %s' % url
179 sys.stdout.flush()
180 sys.stderr.flush()
181 subprocess.check_call(['curl', '--fail', '-L', url, '-o', tarball])
182 sha1sum = GetSha1(tarball)
183 if sha1sum != tarball_sha1sum:
184 print 'Tarball sha1sum is wrong.'
185 print 'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum)
186 return 1
187 subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
188 os.remove(tarball)
190 with open(stamp, 'w') as s:
191 s.write(url)
192 return 0
195 if __name__ == '__main__':
196 parser = optparse.OptionParser('usage: %prog [OPTIONS]')
197 parser.add_option('--running-as-hook', action='store_true',
198 default=False, help='Used when running from gclient hooks.'
199 ' In this mode the sysroot will only '
200 'be installed for official Linux '
201 'builds or ARM Linux builds')
202 parser.add_option('--arch', type='choice', choices=valid_archs,
203 help='Sysroot architecture: %s' % ', '.join(valid_archs))
204 options, _ = parser.parse_args()
205 sys.exit(main())