Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / installer / linux / sysroot_scripts / install-debian.wheezy.sysroot.py
bloba45e0a747e6b3909cd8e7892799ee8e6954ce5b1
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 = 264817
32 REVISION_I386 = 264817
33 REVISION_ARM = 285950
34 TARBALL_AMD64 = 'debian_wheezy_amd64_sysroot.tgz'
35 TARBALL_I386 = 'debian_wheezy_i386_sysroot.tgz'
36 TARBALL_ARM = 'debian_wheezy_arm_sysroot.tgz'
37 TARBALL_AMD64_SHA1SUM = '74b7231e12aaf45c5c5489d9aebb56bd6abb3653'
38 TARBALL_I386_SHA1SUM = 'fe3d284926839683b00641bc66c9023f872ea4b4'
39 TARBALL_ARM_SHA1SUM = 'fc2f54db168887c5190c4c6686c869bedf668b4e'
40 SYSROOT_DIR_AMD64 = 'debian_wheezy_amd64-sysroot'
41 SYSROOT_DIR_I386 = 'debian_wheezy_i386-sysroot'
42 SYSROOT_DIR_ARM = 'debian_wheezy_arm-sysroot'
44 valid_archs = ('arm', 'i386', 'amd64')
47 def GetSha1(filename):
48 sha1 = hashlib.sha1()
49 with open(filename, 'rb') as f:
50 while True:
51 # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
52 chunk = f.read(1024*1024)
53 if not chunk:
54 break
55 sha1.update(chunk)
56 return sha1.hexdigest()
59 def DetectArch(gyp_defines):
60 # Check for optional target_arch and only install for that architecture.
61 # If target_arch is not specified, then only install for the host
62 # architecture.
63 if 'target_arch=x64' in gyp_defines:
64 return 'amd64'
65 elif 'target_arch=ia32' in gyp_defines:
66 return 'i386'
67 elif 'target_arch=arm' in gyp_defines:
68 return 'arm'
70 # Figure out host arch using build/detect_host_arch.py and
71 # set target_arch to host arch
72 SRC_DIR = os.path.abspath(
73 os.path.join(SCRIPT_DIR, '..', '..', '..', '..'))
74 sys.path.append(os.path.join(SRC_DIR, 'build'))
75 import detect_host_arch
77 detected_host_arch = detect_host_arch.HostArch()
78 if detected_host_arch == 'x64':
79 return 'amd64'
80 elif detected_host_arch == 'ia32':
81 return 'i386'
82 elif detected_host_arch == 'arm':
83 return 'arm'
84 else:
85 print "Unknown host arch: %s" % detected_host_arch
87 return None
90 def main():
91 if options.linux_only:
92 # This argument is passed when run from the gclient hooks.
93 # In this case we return early on non-linux platforms.
94 if not sys.platform.startswith('linux'):
95 return 0
97 gyp_defines = os.environ.get('GYP_DEFINES', '')
99 if options.arch:
100 target_arch = options.arch
101 else:
102 target_arch = DetectArch(gyp_defines)
103 if not target_arch:
104 print 'Unable to detect host architecture'
105 return 1
107 if options.linux_only and target_arch != 'arm':
108 # When run from runhooks, only install the sysroot for an Official Chrome
109 # Linux build, except on ARM where we always use a sysroot.
110 defined = ['branding=Chrome', 'buildtype=Official']
111 undefined = ['chromeos=1']
112 for option in defined:
113 if option not in gyp_defines:
114 return 0
115 for option in undefined:
116 if option in gyp_defines:
117 return 0
119 # The sysroot directory should match the one specified in build/common.gypi.
120 # TODO(thestig) Consider putting this else where to avoid having to recreate
121 # it on every build.
122 linux_dir = os.path.dirname(SCRIPT_DIR)
123 if target_arch == 'amd64':
124 sysroot = os.path.join(linux_dir, SYSROOT_DIR_AMD64)
125 tarball_filename = TARBALL_AMD64
126 tarball_sha1sum = TARBALL_AMD64_SHA1SUM
127 revision = REVISION_AMD64
128 elif target_arch == 'arm':
129 sysroot = os.path.join(linux_dir, SYSROOT_DIR_ARM)
130 tarball_filename = TARBALL_ARM
131 tarball_sha1sum = TARBALL_ARM_SHA1SUM
132 revision = REVISION_ARM
133 elif target_arch == 'i386':
134 sysroot = os.path.join(linux_dir, SYSROOT_DIR_I386)
135 tarball_filename = TARBALL_I386
136 tarball_sha1sum = TARBALL_I386_SHA1SUM
137 revision = REVISION_I386
138 else:
139 print 'Unknown architecture: %s' % target_arch
140 assert(False)
142 url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
144 stamp = os.path.join(sysroot, '.stamp')
145 if os.path.exists(stamp):
146 with open(stamp) as s:
147 if s.read() == url:
148 print 'Debian Wheezy %s root image already up-to-date: %s' % \
149 (target_arch, sysroot)
150 return 0
152 print 'Installing Debian Wheezy %s root image: %s' % (target_arch, sysroot)
153 if os.path.isdir(sysroot):
154 shutil.rmtree(sysroot)
155 os.mkdir(sysroot)
156 tarball = os.path.join(sysroot, tarball_filename)
157 print 'Downloading %s' % url
158 sys.stdout.flush()
159 sys.stderr.flush()
160 subprocess.check_call(['curl', '--fail', '-L', url, '-o', tarball])
161 sha1sum = GetSha1(tarball)
162 if sha1sum != tarball_sha1sum:
163 print 'Tarball sha1sum is wrong.'
164 print 'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum)
165 return 1
166 subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
167 os.remove(tarball)
169 with open(stamp, 'w') as s:
170 s.write(url)
171 return 0
174 if __name__ == '__main__':
175 parser = optparse.OptionParser('usage: %prog [OPTIONS]')
176 parser.add_option('--linux-only', action='store_true',
177 default=False, help='Only install sysroot for official '
178 'Linux builds')
179 parser.add_option('--arch', type='choice', choices=valid_archs,
180 help='Sysroot architecture: %s' % ', '.join(valid_archs))
181 options, _ = parser.parse_args()
182 sys.exit(main())