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
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
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.
27 from lib.util import get_host_arch
30 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
31 URL_PREFIX = 'https://github.com'
32 URL_PATH = 'atom/debian-sysroot-image-creator/releases/download'
33 REVISION_AMD64 = 'v0.5.0'
34 REVISION_I386 = 'v0.5.0'
35 REVISION_ARM = 'v0.5.0'
36 TARBALL_AMD64 = 'debian_wheezy_amd64_sysroot.tgz'
37 TARBALL_I386 = 'debian_wheezy_i386_sysroot.tgz'
38 TARBALL_ARM = 'debian_wheezy_arm_sysroot.tgz'
39 TARBALL_AMD64_SHA1SUM = '981b2440d446156801c6fdecffb5edcadf27593c'
40 TARBALL_I386_SHA1SUM = '2e4e43c1e8718595e37c6b6ab89256dae53adf23'
41 TARBALL_ARM_SHA1SUM = '448e635f38e99d6d860db538a9db85ac74d36e41'
42 SYSROOT_DIR_AMD64 = 'debian_wheezy_amd64-sysroot'
43 SYSROOT_DIR_I386 = 'debian_wheezy_i386-sysroot'
44 SYSROOT_DIR_ARM = 'debian_wheezy_arm-sysroot'
46 valid_archs = ('arm', 'i386', 'amd64')
49 def GetSha1(filename):
51 with open(filename, 'rb') as f:
53 # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
54 chunk = f.read(1024*1024)
58 return sha1.hexdigest()
61 def DetectArch(gyp_defines):
62 # Check for optional target_arch and only install for that architecture.
63 # If target_arch is not specified, then only install for the host
65 if 'target_arch=x64' in gyp_defines:
67 elif 'target_arch=ia32' in gyp_defines:
69 elif 'target_arch=arm' in gyp_defines:
72 detected_host_arch = get_host_arch()
73 if detected_host_arch == 'x64':
75 elif detected_host_arch == 'ia32':
77 elif detected_host_arch == 'arm':
80 print "Unknown host arch: %s" % detected_host_arch
86 if options.linux_only:
87 # This argument is passed when run from the gclient hooks.
88 # In this case we return early on non-linux platforms.
89 if not sys.platform.startswith('linux'):
92 gyp_defines = os.environ.get('GYP_DEFINES', '')
95 target_arch = options.arch
97 target_arch = DetectArch(gyp_defines)
99 print 'Unable to detect host architecture'
102 if options.linux_only and target_arch != 'arm':
103 # When run from runhooks, only install the sysroot for an Official Chrome
104 # Linux build, except on ARM where we always use a sysroot.
105 defined = ['branding=Chrome', 'buildtype=Official']
106 undefined = ['chromeos=1']
107 for option in defined:
108 if option not in gyp_defines:
110 for option in undefined:
111 if option in gyp_defines:
114 # The sysroot directory should match the one specified in build/common.gypi.
115 # TODO(thestig) Consider putting this else where to avoid having to recreate
117 linux_dir = os.path.join(SCRIPT_DIR, '..', 'vendor')
118 if target_arch == 'amd64':
119 sysroot = os.path.join(linux_dir, SYSROOT_DIR_AMD64)
120 tarball_filename = TARBALL_AMD64
121 tarball_sha1sum = TARBALL_AMD64_SHA1SUM
122 revision = REVISION_AMD64
123 elif target_arch == 'arm':
124 sysroot = os.path.join(linux_dir, SYSROOT_DIR_ARM)
125 tarball_filename = TARBALL_ARM
126 tarball_sha1sum = TARBALL_ARM_SHA1SUM
127 revision = REVISION_ARM
128 elif target_arch == 'i386':
129 sysroot = os.path.join(linux_dir, SYSROOT_DIR_I386)
130 tarball_filename = TARBALL_I386
131 tarball_sha1sum = TARBALL_I386_SHA1SUM
132 revision = REVISION_I386
134 print 'Unknown architecture: %s' % target_arch
137 url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
139 stamp = os.path.join(sysroot, '.stamp')
140 if os.path.exists(stamp):
141 with open(stamp) as s:
143 print 'Debian Wheezy %s root image already up-to-date: %s' % \
144 (target_arch, sysroot)
147 print 'Installing Debian Wheezy %s root image: %s' % (target_arch, sysroot)
148 if os.path.isdir(sysroot):
149 shutil.rmtree(sysroot)
151 tarball = os.path.join(sysroot, tarball_filename)
152 print 'Downloading %s' % url
155 subprocess.check_call(['curl', '--fail', '-L', url, '-o', tarball])
156 sha1sum = GetSha1(tarball)
157 if sha1sum != tarball_sha1sum:
158 print 'Tarball sha1sum is wrong.'
159 print 'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum)
161 subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
164 with open(stamp, 'w') as s:
169 if __name__ == '__main__':
170 parser = optparse.OptionParser('usage: %prog [OPTIONS]')
171 parser.add_option('--linux-only', action='store_true',
172 default=False, help='Only install sysroot for official '
174 parser.add_option('--arch', type='choice', choices=valid_archs,
175 help='Sysroot architecture: %s' % ', '.join(valid_archs))
176 options, _ = parser.parse_args()