- add sources.
[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 platform
19 import optparse
20 import os
21 import re
22 import shutil
23 import subprocess
24 import sys
25
26
27 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
28 URL_PREFIX = 'https://commondatastorage.googleapis.com'
29 URL_PATH = 'chrome-linux-sysroot/toolchain'
30 REVISION = 232685
31 TARBALL_AMD64 = 'debian_wheezy_amd64_sysroot.tgz'
32 TARBALL_I386 = 'debian_wheezy_i386_sysroot.tgz'
33 SYSROOT_DIR_AMD64 = 'debian_wheezy_amd64-sysroot'
34 SYSROOT_DIR_I386 = 'debian_wheezy_i386-sysroot'
35
36
37 def main(args):
38   if options.arch not in ['amd64', 'i386']:
39     print 'Unknown architecture: %s' % options.arch
40     return 1
41
42   if options.linux_only:
43     # This argument is passed when run from the gclient hooks.
44     # In this case we return early on non-linux platforms.
45     if not sys.platform.startswith('linux'):
46       return 0
47
48     # Only install the sysroot for an Official Chrome Linux build.
49     defined = ['branding=Chrome', 'buildtype=Official']
50     undefined = ['chromeos=1']
51     gyp_defines = os.environ.get('GYP_DEFINES', '')
52     for option in defined:
53       if option not in gyp_defines:
54         return 0
55     for option in undefined:
56       if option in gyp_defines:
57         return 0
58
59     # Check for optional target_arch and only install for that architecture.
60     # If target_arch is not specified, then only install for the host
61     # architecture.
62     host_arch = ''
63     if 'target_arch=x64' in gyp_defines:
64       host_arch = 'amd64'
65     elif 'target_arch=ia32' in gyp_defines:
66       host_arch = 'i386'
67     else:
68       # Figure out host arch, like the host_arch variable in build/common.gypi.
69       machine_type = platform.machine()
70       if machine_type in ['amd64', 'x86_64']:
71         host_arch = 'amd64'
72       elif re.match('(i[3-6]86|i86pc)$', machine_type):
73         host_arch = 'i386'
74     if host_arch != options.arch:
75       return 0
76
77   # The sysroot directory should match the one specified in build/common.gypi.
78   # TODO(thestig) Consider putting this else where to avoid having to recreate
79   # it on every build.
80   linux_dir = os.path.dirname(SCRIPT_DIR)
81   if options.arch == 'amd64':
82     sysroot = os.path.join(linux_dir, SYSROOT_DIR_AMD64)
83     tarball_filename = TARBALL_AMD64
84   else:
85     sysroot = os.path.join(linux_dir, SYSROOT_DIR_I386)
86     tarball_filename = TARBALL_I386
87   url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, REVISION, tarball_filename)
88
89   stamp = os.path.join(sysroot, '.stamp')
90   if os.path.exists(stamp):
91     with open(stamp) as s:
92       if s.read() == url:
93         print 'Debian Wheezy %s root image already up-to-date: %s' % \
94             (options.arch, sysroot)
95         return 0
96
97   print 'Installing Debian Wheezy %s root image: %s' % (options.arch, sysroot)
98   if os.path.isdir(sysroot):
99     shutil.rmtree(sysroot)
100   os.mkdir(sysroot)
101   tarball = os.path.join(sysroot, tarball_filename)
102   subprocess.check_call(['curl', '-L', url, '-o', tarball])
103   subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
104   os.remove(tarball)
105
106   with open(stamp, 'w') as s:
107     s.write(url)
108   return 0
109
110
111 if __name__ == '__main__':
112   parser = optparse.OptionParser('usage: %prog [OPTIONS]')
113   parser.add_option('', '--linux-only', dest='linux_only', action='store_true',
114                     default=False, help='Only install sysroot for official '
115                                         'Linux builds')
116   parser.add_option('', '--arch', dest='arch',
117                     help='Sysroot architecture, i386 or amd64')
118   options, args = parser.parse_args()
119   sys.exit(main(options))