electron/script/install-sysroot.py

153 lines
4.8 KiB
Python
Raw Normal View History

2015-07-01 09:22:40 +00:00
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
2017-05-22 07:31:54 +00:00
"""Install Debian sysroots for building chromium.
"""
# The sysroot is needed to ensure that binaries that get built will run on
# the oldest stable version of Debian that we currently support.
2015-07-01 09:22:40 +00:00
# This script can be run manually but is more often run as part of gclient
2017-05-22 07:31:54 +00:00
# hooks. When run from hooks this script is a no-op on non-linux platforms.
2015-07-01 09:22:40 +00:00
2017-05-22 07:31:54 +00:00
# The sysroot image could be constructed from scratch based on the current state
# of the Debian archive but for consistency we use a pre-built root image (we
# don't want upstream changes to Debian to effect the chromium build until we
# choose to pull them in). The images will normally need to be rebuilt every
# time chrome's build dependencies are changed but should also be updated
# periodically to include upstream security fixes from Debian.
2015-07-01 09:22:40 +00:00
import hashlib
2017-05-22 07:31:54 +00:00
import json
2015-07-01 09:22:40 +00:00
import platform
import optparse
import os
import re
import shutil
import subprocess
import sys
2017-05-22 07:31:54 +00:00
import urllib2
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
2015-07-01 09:22:40 +00:00
2017-05-22 07:31:54 +00:00
URL_PREFIX = 'http://s3.amazonaws.com'
URL_PATH = 'gh-contractor-zcbenz/toolchain'
2015-07-01 09:22:40 +00:00
2017-05-22 07:31:54 +00:00
VALID_ARCHS = ('arm', 'arm64', 'i386', 'amd64')
class Error(Exception):
pass
2015-07-01 09:22:40 +00:00
def GetSha1(filename):
sha1 = hashlib.sha1()
with open(filename, 'rb') as f:
while True:
# Read in 1mb chunks, so it doesn't all have to be loaded into memory.
chunk = f.read(1024*1024)
if not chunk:
break
sha1.update(chunk)
return sha1.hexdigest()
2017-05-22 07:31:54 +00:00
def main(args):
parser = optparse.OptionParser('usage: %prog [OPTIONS]', description=__doc__)
parser.add_option('--running-as-hook', action='store_true',
default=False, help='Used when running from gclient hooks.'
' Installs default sysroot images.')
parser.add_option('--arch', type='choice', choices=VALID_ARCHS,
help='Sysroot architecture: %s' % ', '.join(VALID_ARCHS))
parser.add_option('--all', action='store_true',
help='Install all sysroot images (useful when updating the'
' images)')
options, _ = parser.parse_args(args)
if options.running_as_hook and not sys.platform.startswith('linux'):
return 0
if options.running_as_hook:
return 0
elif options.arch:
InstallDefaultSysrootForArch(options.arch)
elif options.all:
for arch in VALID_ARCHS:
InstallDefaultSysrootForArch(arch)
2015-07-01 09:22:40 +00:00
else:
2017-05-22 07:31:54 +00:00
print 'You much specify either --arch, --all or --running-as-hook'
return 1
2015-07-01 09:22:40 +00:00
2017-05-22 07:31:54 +00:00
return 0
2015-07-01 09:22:40 +00:00
2017-05-22 07:31:54 +00:00
def InstallDefaultSysrootForArch(target_arch):
if target_arch not in VALID_ARCHS:
raise Error('Unknown architecture: %s' % target_arch)
2017-12-08 07:12:47 +00:00
InstallSysroot('Stretch', target_arch)
2015-07-01 09:22:40 +00:00
2017-05-22 07:31:54 +00:00
def InstallSysroot(target_platform, target_arch):
2017-12-08 07:12:47 +00:00
# The sysroot directory should match the one specified in
# build/config/sysroot.gni.
2017-05-22 07:31:54 +00:00
# TODO(thestig) Consider putting this elsewhere to avoid having to recreate
2015-07-01 09:22:40 +00:00
# it on every build.
2017-05-22 07:31:54 +00:00
linux_dir = os.path.dirname(SCRIPT_DIR)
sysroots_file = os.path.join(SCRIPT_DIR, 'sysroots.json')
sysroots = json.load(open(sysroots_file))
sysroot_key = '%s_%s' % (target_platform.lower(), target_arch)
if sysroot_key not in sysroots:
raise Error('No sysroot for: %s %s' % (target_platform, target_arch))
sysroot_dict = sysroots[sysroot_key]
revision = sysroot_dict['Revision']
tarball_filename = sysroot_dict['Tarball']
tarball_sha1sum = sysroot_dict['Sha1Sum']
2017-05-22 07:52:40 +00:00
sysroot = os.path.join(linux_dir, 'vendor', sysroot_dict['SysrootDir'])
2015-07-01 09:22:40 +00:00
2016-03-08 15:05:32 +00:00
url = '%s/%s/%s/%s' % (URL_PREFIX, URL_PATH, revision, tarball_filename)
2015-07-01 09:22:40 +00:00
stamp = os.path.join(sysroot, '.stamp')
if os.path.exists(stamp):
with open(stamp) as s:
if s.read() == url:
2017-05-22 07:31:54 +00:00
return
2015-07-01 09:22:40 +00:00
2017-05-22 07:31:54 +00:00
print 'Installing Debian %s %s root image: %s' % \
(target_platform, target_arch, sysroot)
2015-07-01 09:22:40 +00:00
if os.path.isdir(sysroot):
shutil.rmtree(sysroot)
os.mkdir(sysroot)
tarball = os.path.join(sysroot, tarball_filename)
print 'Downloading %s' % url
sys.stdout.flush()
sys.stderr.flush()
2017-05-22 07:31:54 +00:00
for _ in range(3):
try:
response = urllib2.urlopen(url)
with open(tarball, "wb") as f:
f.write(response.read())
break
2017-05-22 08:18:57 +00:00
except Exception:
2017-05-22 07:31:54 +00:00
pass
else:
raise Error('Failed to download %s' % url)
2016-03-08 15:05:32 +00:00
sha1sum = GetSha1(tarball)
if sha1sum != tarball_sha1sum:
2017-05-22 07:31:54 +00:00
raise Error('Tarball sha1sum is wrong.'
'Expected %s, actual: %s' % (tarball_sha1sum, sha1sum))
2015-07-01 09:22:40 +00:00
subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
os.remove(tarball)
with open(stamp, 'w') as s:
s.write(url)
if __name__ == '__main__':
2017-05-22 07:31:54 +00:00
try:
sys.exit(main(sys.argv[1:]))
except Error as e:
sys.stderr.write(str(e) + '\n')
sys.exit(1)