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