install-sysroot.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. """Install Debian sysroots for building chromium.
  6. """
  7. # The sysroot is needed to ensure that binaries that get built will run on
  8. # the oldest stable version of Debian that we currently support.
  9. # This script can be run manually but is more often run as part of gclient
  10. # hooks. When run from hooks this script is a no-op on non-linux platforms.
  11. # The sysroot image could be constructed from scratch based on the current state
  12. # of the Debian archive but for consistency we use a pre-built root image (we
  13. # don't want upstream changes to Debian to effect the chromium build until we
  14. # choose to pull them in). The images will normally need to be rebuilt every
  15. # time chrome's build dependencies are changed but should also be updated
  16. # periodically to include upstream security fixes from Debian.
  17. import hashlib
  18. import json
  19. import platform
  20. import optparse
  21. import os
  22. import re
  23. import shutil
  24. import subprocess
  25. import sys
  26. import urllib2
  27. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  28. URL_PREFIX = 'http://s3.amazonaws.com'
  29. URL_PATH = 'gh-contractor-zcbenz/toolchain'
  30. VALID_ARCHS = ('arm', 'arm64', 'i386', 'amd64')
  31. class Error(Exception):
  32. pass
  33. def GetSha1(filename):
  34. sha1 = hashlib.sha1()
  35. with open(filename, 'rb') as f:
  36. while True:
  37. # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
  38. chunk = f.read(1024*1024)
  39. if not chunk:
  40. break
  41. sha1.update(chunk)
  42. return sha1.hexdigest()
  43. def main(args):
  44. parser = optparse.OptionParser('usage: %prog [OPTIONS]', description=__doc__)
  45. parser.add_option('--running-as-hook', action='store_true',
  46. default=False, help='Used when running from gclient hooks.'
  47. ' Installs default sysroot images.')
  48. parser.add_option('--arch', type='choice', choices=VALID_ARCHS,
  49. help='Sysroot architecture: %s' % ', '.join(VALID_ARCHS))
  50. parser.add_option('--all', action='store_true',
  51. help='Install all sysroot images (useful when updating the'
  52. ' images)')
  53. options, _ = parser.parse_args(args)
  54. if options.running_as_hook and not sys.platform.startswith('linux'):
  55. return 0
  56. if options.running_as_hook:
  57. return 0
  58. elif options.arch:
  59. InstallDefaultSysrootForArch(options.arch)
  60. elif options.all:
  61. for arch in VALID_ARCHS:
  62. InstallDefaultSysrootForArch(arch)
  63. else:
  64. print 'You much specify either --arch, --all or --running-as-hook'
  65. return 1
  66. return 0
  67. def InstallDefaultSysrootForArch(target_arch):
  68. if target_arch not in VALID_ARCHS:
  69. raise Error('Unknown architecture: %s' % target_arch)
  70. InstallSysroot('Jessie', target_arch)
  71. def InstallSysroot(target_platform, target_arch):
  72. # The sysroot directory should match the one specified in build/common.gypi.
  73. # TODO(thestig) Consider putting this elsewhere to avoid having to recreate
  74. # it on every build.
  75. linux_dir = os.path.dirname(SCRIPT_DIR)
  76. sysroots_file = os.path.join(SCRIPT_DIR, 'sysroots.json')
  77. sysroots = json.load(open(sysroots_file))
  78. sysroot_key = '%s_%s' % (target_platform.lower(), target_arch)
  79. if sysroot_key not in sysroots:
  80. raise Error('No sysroot for: %s %s' % (target_platform, target_arch))
  81. sysroot_dict = sysroots[sysroot_key]
  82. revision = sysroot_dict['Revision']
  83. tarball_filename = sysroot_dict['Tarball']
  84. tarball_sha1sum = sysroot_dict['Sha1Sum']
  85. sysroot = os.path.join(linux_dir, 'vendor', sysroot_dict['SysrootDir'])
  86. url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
  87. stamp = os.path.join(sysroot, '.stamp')
  88. if os.path.exists(stamp):
  89. with open(stamp) as s:
  90. if s.read() == url:
  91. print '%s %s sysroot image already up to date: %s' % \
  92. (target_platform, target_arch, sysroot)
  93. return
  94. print 'Installing Debian %s %s root image: %s' % \
  95. (target_platform, target_arch, sysroot)
  96. if os.path.isdir(sysroot):
  97. shutil.rmtree(sysroot)
  98. os.mkdir(sysroot)
  99. tarball = os.path.join(sysroot, tarball_filename)
  100. print 'Downloading %s' % url
  101. sys.stdout.flush()
  102. sys.stderr.flush()
  103. for _ in range(3):
  104. try:
  105. response = urllib2.urlopen(url)
  106. with open(tarball, "wb") as f:
  107. f.write(response.read())
  108. break
  109. except Exception:
  110. pass
  111. else:
  112. raise Error('Failed to download %s' % url)
  113. sha1sum = GetSha1(tarball)
  114. if sha1sum != tarball_sha1sum:
  115. raise Error('Tarball sha1sum is wrong.'
  116. 'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum))
  117. subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
  118. os.remove(tarball)
  119. with open(stamp, 'w') as s:
  120. s.write(url)
  121. if __name__ == '__main__':
  122. try:
  123. sys.exit(main(sys.argv[1:]))
  124. except Error as e:
  125. sys.stderr.write(str(e) + '\n')
  126. sys.exit(1)