Merge pull request #9039 from electron/extract-renderer-base-class
[platform/framework/web/crosswalk-tizen.git] / script / install-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 from lib.util import get_host_arch
28
29
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'
45
46 valid_archs = ('arm', 'i386', 'amd64')
47
48
49 def GetSha1(filename):
50   sha1 = hashlib.sha1()
51   with open(filename, 'rb') as f:
52     while True:
53       # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
54       chunk = f.read(1024*1024)
55       if not chunk:
56         break
57       sha1.update(chunk)
58   return sha1.hexdigest()
59
60
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
64   # architecture.
65   if 'target_arch=x64' in gyp_defines:
66     return 'amd64'
67   elif 'target_arch=ia32' in gyp_defines:
68     return 'i386'
69   elif 'target_arch=arm' in gyp_defines:
70     return 'arm'
71
72   detected_host_arch = get_host_arch()
73   if detected_host_arch == 'x64':
74     return 'amd64'
75   elif detected_host_arch == 'ia32':
76     return 'i386'
77   elif detected_host_arch == 'arm':
78     return 'arm'
79   else:
80     print "Unknown host arch: %s" % detected_host_arch
81
82   return None
83
84
85 def main():
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'):
90       return 0
91
92   gyp_defines = os.environ.get('GYP_DEFINES', '')
93
94   if options.arch:
95     target_arch = options.arch
96   else:
97     target_arch = DetectArch(gyp_defines)
98     if not target_arch:
99       print 'Unable to detect host architecture'
100       return 1
101
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:
109         return 0
110     for option in undefined:
111       if option in gyp_defines:
112         return 0
113
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
116   # it on every build.
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
133   else:
134     print 'Unknown architecture: %s' % target_arch
135     assert(False)
136
137   url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
138
139   stamp = os.path.join(sysroot, '.stamp')
140   if os.path.exists(stamp):
141     with open(stamp) as s:
142       if s.read() == url:
143         print 'Debian Wheezy %s root image already up-to-date: %s' % \
144             (target_arch, sysroot)
145         return 0
146
147   print 'Installing Debian Wheezy %s root image: %s' % (target_arch, sysroot)
148   if os.path.isdir(sysroot):
149     shutil.rmtree(sysroot)
150   os.mkdir(sysroot)
151   tarball = os.path.join(sysroot, tarball_filename)
152   print 'Downloading %s' % url
153   sys.stdout.flush()
154   sys.stderr.flush()
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)
160     return 1
161   subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
162   os.remove(tarball)
163
164   with open(stamp, 'w') as s:
165     s.write(url)
166   return 0
167
168
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 '
173                                         'Linux builds')
174   parser.add_option('--arch', type='choice', choices=valid_archs,
175                     help='Sysroot architecture: %s' % ', '.join(valid_archs))
176   options, _ = parser.parse_args()
177   sys.exit(main())