install-sysroot.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. # Script to install a Debian Wheezy sysroot for making official Google Chrome
  6. # Linux builds.
  7. # The sysroot is needed to make Chrome work for Debian Wheezy.
  8. # This script can be run manually but is more often run as part of gclient
  9. # hooks. When run from hooks this script should be a no-op on non-linux
  10. # platforms.
  11. # The sysroot image could be constructed from scratch based on the current
  12. # state or Debian Wheezy but for consistency we currently use a pre-built root
  13. # image. The image will normally need to be rebuilt every time chrome's build
  14. # dependancies are changed.
  15. import hashlib
  16. import platform
  17. import optparse
  18. import os
  19. import re
  20. import shutil
  21. import subprocess
  22. import sys
  23. from lib.util import get_host_arch
  24. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  25. URL_PREFIX = 'https://github.com'
  26. URL_PATH = 'atom/debian-sysroot-image-creator/releases/download'
  27. REVISION_AMD64 = 'v0.5.0'
  28. REVISION_I386 = 'v0.5.0'
  29. REVISION_ARM = 'v0.5.0'
  30. TARBALL_AMD64 = 'debian_wheezy_amd64_sysroot.tgz'
  31. TARBALL_I386 = 'debian_wheezy_i386_sysroot.tgz'
  32. TARBALL_ARM = 'debian_wheezy_arm_sysroot.tgz'
  33. TARBALL_AMD64_SHA1SUM = '981b2440d446156801c6fdecffb5edcadf27593c'
  34. TARBALL_I386_SHA1SUM = '2e4e43c1e8718595e37c6b6ab89256dae53adf23'
  35. TARBALL_ARM_SHA1SUM = '448e635f38e99d6d860db538a9db85ac74d36e41'
  36. SYSROOT_DIR_AMD64 = 'debian_wheezy_amd64-sysroot'
  37. SYSROOT_DIR_I386 = 'debian_wheezy_i386-sysroot'
  38. SYSROOT_DIR_ARM = 'debian_wheezy_arm-sysroot'
  39. valid_archs = ('arm', 'i386', 'amd64')
  40. def GetSha1(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. def DetectArch(gyp_defines):
  51. # Check for optional target_arch and only install for that architecture.
  52. # If target_arch is not specified, then only install for the host
  53. # architecture.
  54. if 'target_arch=x64' in gyp_defines:
  55. return 'amd64'
  56. elif 'target_arch=ia32' in gyp_defines:
  57. return 'i386'
  58. elif 'target_arch=arm' in gyp_defines:
  59. return 'arm'
  60. detected_host_arch = get_host_arch()
  61. if detected_host_arch == 'x64':
  62. return 'amd64'
  63. elif detected_host_arch == 'ia32':
  64. return 'i386'
  65. elif detected_host_arch == 'arm':
  66. return 'arm'
  67. else:
  68. print "Unknown host arch: %s" % detected_host_arch
  69. return None
  70. def main():
  71. if options.linux_only:
  72. # This argument is passed when run from the gclient hooks.
  73. # In this case we return early on non-linux platforms.
  74. if not sys.platform.startswith('linux'):
  75. return 0
  76. gyp_defines = os.environ.get('GYP_DEFINES', '')
  77. if options.arch:
  78. target_arch = options.arch
  79. else:
  80. target_arch = DetectArch(gyp_defines)
  81. if not target_arch:
  82. print 'Unable to detect host architecture'
  83. return 1
  84. if options.linux_only and target_arch != 'arm':
  85. # When run from runhooks, only install the sysroot for an Official Chrome
  86. # Linux build, except on ARM where we always use a sysroot.
  87. defined = ['branding=Chrome', 'buildtype=Official']
  88. undefined = ['chromeos=1']
  89. for option in defined:
  90. if option not in gyp_defines:
  91. return 0
  92. for option in undefined:
  93. if option in gyp_defines:
  94. return 0
  95. # The sysroot directory should match the one specified in build/common.gypi.
  96. # TODO(thestig) Consider putting this else where to avoid having to recreate
  97. # it on every build.
  98. linux_dir = os.path.join(SCRIPT_DIR, '..', 'vendor')
  99. if target_arch == 'amd64':
  100. sysroot = os.path.join(linux_dir, SYSROOT_DIR_AMD64)
  101. tarball_filename = TARBALL_AMD64
  102. tarball_sha1sum = TARBALL_AMD64_SHA1SUM
  103. revision = REVISION_AMD64
  104. elif target_arch == 'arm':
  105. sysroot = os.path.join(linux_dir, SYSROOT_DIR_ARM)
  106. tarball_filename = TARBALL_ARM
  107. tarball_sha1sum = TARBALL_ARM_SHA1SUM
  108. revision = REVISION_ARM
  109. elif target_arch == 'i386':
  110. sysroot = os.path.join(linux_dir, SYSROOT_DIR_I386)
  111. tarball_filename = TARBALL_I386
  112. tarball_sha1sum = TARBALL_I386_SHA1SUM
  113. revision = REVISION_I386
  114. else:
  115. print 'Unknown architecture: %s' % target_arch
  116. assert(False)
  117. url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
  118. stamp = os.path.join(sysroot, '.stamp')
  119. if os.path.exists(stamp):
  120. with open(stamp) as s:
  121. if s.read() == url:
  122. print 'Debian Wheezy %s root image already up-to-date: %s' % \
  123. (target_arch, sysroot)
  124. return 0
  125. print 'Installing Debian Wheezy %s root image: %s' % (target_arch, sysroot)
  126. if os.path.isdir(sysroot):
  127. shutil.rmtree(sysroot)
  128. os.mkdir(sysroot)
  129. tarball = os.path.join(sysroot, tarball_filename)
  130. print 'Downloading %s' % url
  131. sys.stdout.flush()
  132. sys.stderr.flush()
  133. subprocess.check_call(['curl', '--fail', '-L', url, '-o', tarball])
  134. sha1sum = GetSha1(tarball)
  135. if sha1sum != tarball_sha1sum:
  136. print 'Tarball sha1sum is wrong.'
  137. print 'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum)
  138. return 1
  139. subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
  140. os.remove(tarball)
  141. with open(stamp, 'w') as s:
  142. s.write(url)
  143. return 0
  144. if __name__ == '__main__':
  145. parser = optparse.OptionParser('usage: %prog [OPTIONS]')
  146. parser.add_option('--linux-only', action='store_true',
  147. default=False, help='Only install sysroot for official '
  148. 'Linux builds')
  149. parser.add_option('--arch', type='choice', choices=valid_archs,
  150. help='Sysroot architecture: %s' % ', '.join(valid_archs))
  151. options, _ = parser.parse_args()
  152. sys.exit(main())