install-debian.wheezy.sysroot.py revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 using build/detect_host_arch.py.
84      SRC_DIR = os.path.abspath(
85          os.path.join(SCRIPT_DIR, '..', '..', '..', '..'))
86      sys.path.append(os.path.join(SRC_DIR, 'build'))
87      import detect_host_arch
88
89      detected_host_arch = detect_host_arch.HostArch()
90      if detected_host_arch == 'x64':
91        host_arch = 'amd64'
92      elif detected_host_arch == 'ia32':
93        host_arch = 'i386'
94    if host_arch != options.arch:
95      return 0
96
97  # The sysroot directory should match the one specified in build/common.gypi.
98  # TODO(thestig) Consider putting this else where to avoid having to recreate
99  # it on every build.
100  linux_dir = os.path.dirname(SCRIPT_DIR)
101  if options.arch == 'amd64':
102    sysroot = os.path.join(linux_dir, SYSROOT_DIR_AMD64)
103    tarball_filename = TARBALL_AMD64
104    tarball_sha1sum = TARBALL_AMD64_SHA1SUM
105  else:
106    sysroot = os.path.join(linux_dir, SYSROOT_DIR_I386)
107    tarball_filename = TARBALL_I386
108    tarball_sha1sum = TARBALL_I386_SHA1SUM
109  url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, REVISION, tarball_filename)
110
111  stamp = os.path.join(sysroot, '.stamp')
112  if os.path.exists(stamp):
113    with open(stamp) as s:
114      if s.read() == url:
115        print 'Debian Wheezy %s root image already up-to-date: %s' % \
116            (options.arch, sysroot)
117        return 0
118
119  print 'Installing Debian Wheezy %s root image: %s' % (options.arch, sysroot)
120  if os.path.isdir(sysroot):
121    shutil.rmtree(sysroot)
122  os.mkdir(sysroot)
123  tarball = os.path.join(sysroot, tarball_filename)
124  subprocess.check_call(['curl', '-L', url, '-o', tarball])
125  sha1sum = get_sha1(tarball)
126  if sha1sum != tarball_sha1sum:
127    print 'Tarball sha1sum is wrong.'
128    print 'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum)
129    return 1
130  subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
131  os.remove(tarball)
132
133  with open(stamp, 'w') as s:
134    s.write(url)
135  return 0
136
137
138if __name__ == '__main__':
139  parser = optparse.OptionParser('usage: %prog [OPTIONS]')
140  parser.add_option('', '--linux-only', dest='linux_only', action='store_true',
141                    default=False, help='Only install sysroot for official '
142                                        'Linux builds')
143  parser.add_option('', '--arch', dest='arch',
144                    help='Sysroot architecture, i386 or amd64')
145  options, args = parser.parse_args()
146  sys.exit(main(options))
147