Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / installer / linux / sysroot_scripts / install-debian.wheezy.sysroot.py
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.
5
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.
12
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.
17
18 import hashlib
19 import platform
20 import optparse
21 import os
22 import re
23 import shutil
24 import subprocess
25 import sys
26
27
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'
43
44
45 def get_sha1(filename):
46   sha1 = hashlib.sha1()
47   with open(filename, 'rb') as f:
48     while True:
49       # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
50       chunk = f.read(1024*1024)
51       if not chunk:
52         break
53       sha1.update(chunk)
54   return sha1.hexdigest()
55
56
57 def main(args):
58   if options.arch not in ['amd64', 'i386', 'arm']:
59     print 'Unknown architecture: %s' % options.arch
60     return 1
61
62   if options.linux_only:
63     # This argument is passed when run from the gclient hooks.
64     # In this case we return early on non-linux platforms.
65     if not sys.platform.startswith('linux'):
66       return 0
67
68     gyp_defines = os.environ.get('GYP_DEFINES', '')
69
70     # Only install the sysroot for an Official Chrome Linux build, except
71     # for ARM where we always use a sysroot.
72     if options.arch != 'arm':
73       defined = ['branding=Chrome', 'buildtype=Official']
74       undefined = ['chromeos=1']
75       for option in defined:
76         if option not in gyp_defines:
77           return 0
78       for option in undefined:
79         if option in gyp_defines:
80           return 0
81
82     # Check for optional target_arch and only install for that architecture.
83     # If target_arch is not specified, then only install for the host
84     # architecture.
85     target_arch = ''
86     if 'target_arch=x64' in gyp_defines:
87       target_arch = 'amd64'
88     elif 'target_arch=ia32' in gyp_defines:
89       target_arch = 'i386'
90     elif 'target_arch=arm' in gyp_defines:
91       target_arch = 'arm'
92     else:
93       # Figure out host arch using build/detect_host_arch.py and
94       # set target_arch to host arch
95       SRC_DIR = os.path.abspath(
96           os.path.join(SCRIPT_DIR, '..', '..', '..', '..'))
97       sys.path.append(os.path.join(SRC_DIR, 'build'))
98       import detect_host_arch
99
100       detected_host_arch = detect_host_arch.HostArch()
101       if detected_host_arch == 'x64':
102         target_arch = 'amd64'
103       elif detected_host_arch == 'ia32':
104         target_arch = 'i386'
105       elif detected_host_arch == 'arm':
106         target_arch = 'arm'
107
108     if target_arch != options.arch:
109       return 0
110
111   # The sysroot directory should match the one specified in build/common.gypi.
112   # TODO(thestig) Consider putting this else where to avoid having to recreate
113   # it on every build.
114   linux_dir = os.path.dirname(SCRIPT_DIR)
115   if options.arch == 'amd64':
116     sysroot = os.path.join(linux_dir, SYSROOT_DIR_AMD64)
117     tarball_filename = TARBALL_AMD64
118     tarball_sha1sum = TARBALL_AMD64_SHA1SUM
119     revision = REVISION_AMD64
120   elif options.arch == 'arm':
121     sysroot = os.path.join(linux_dir, SYSROOT_DIR_ARM)
122     tarball_filename = TARBALL_ARM
123     tarball_sha1sum = TARBALL_ARM_SHA1SUM
124     revision = REVISION_ARM
125   elif options.arch == 'i386':
126     sysroot = os.path.join(linux_dir, SYSROOT_DIR_I386)
127     tarball_filename = TARBALL_I386
128     tarball_sha1sum = TARBALL_I386_SHA1SUM
129     revision = REVISION_I386
130   else:
131     assert(false)
132
133
134   url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
135
136   stamp = os.path.join(sysroot, '.stamp')
137   if os.path.exists(stamp):
138     with open(stamp) as s:
139       if s.read() == url:
140         print 'Debian Wheezy %s root image already up-to-date: %s' % \
141             (options.arch, sysroot)
142         return 0
143
144   print 'Installing Debian Wheezy %s root image: %s' % (options.arch, sysroot)
145   if os.path.isdir(sysroot):
146     shutil.rmtree(sysroot)
147   os.mkdir(sysroot)
148   tarball = os.path.join(sysroot, tarball_filename)
149   print 'Downloading %s' % url
150   sys.stdout.flush()
151   sys.stderr.flush()
152   subprocess.check_call(['curl', '--fail', '-L', url, '-o', tarball])
153   sha1sum = get_sha1(tarball)
154   if sha1sum != tarball_sha1sum:
155     print 'Tarball sha1sum is wrong.'
156     print 'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum)
157     return 1
158   subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
159   os.remove(tarball)
160
161   with open(stamp, 'w') as s:
162     s.write(url)
163   return 0
164
165
166 if __name__ == '__main__':
167   parser = optparse.OptionParser('usage: %prog [OPTIONS]')
168   parser.add_option('--linux-only', action='store_true',
169                     default=False, help='Only install sysroot for official '
170                                         'Linux builds')
171   parser.add_option('--arch', help='Sysroot architecture: i386, amd64 or arm')
172   options, args = parser.parse_args()
173   sys.exit(main(args))