blob: 3f6ae91f46d61d1153b19357874492848fc2bb45 [file] [log] [blame]
kjellander@webrtc.org89256622014-08-20 12:10:11 +00001#!/usr/bin/env python
2# Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +000010"""Script to download a Chromium checkout into the workspace.
11
12The script downloads a full Chromium Git clone and its DEPS.
13
14The following environment variable can be used to alter the behavior:
15* CHROMIUM_NO_HISTORY - If set to 1, a Git checkout with no history will be
16 downloaded. This is consumes less bandwidth and disk space but is known to be
17 slower in general if you have a high-speed connection.
18
19After a successful sync has completed, a .last_sync_chromium file is written to
20the chromium directory. While it exists, no more gclient sync operations will be
21performed until the --target-revision changes or the SCRIPT_VERSION constant is
22incremented. The file can be removed manually to force a new sync.
23"""
24
kjellander@webrtc.org89256622014-08-20 12:10:11 +000025import argparse
26import os
Henrik Kjellander05ce5dd2015-06-15 11:10:15 +020027import shutil
kjellander@webrtc.org89256622014-08-20 12:10:11 +000028import subprocess
29import sys
kjellander@webrtc.org74304332015-03-05 14:38:09 +000030import textwrap
kjellander@webrtc.org89256622014-08-20 12:10:11 +000031
iannucci@chromium.org16136382014-08-21 15:48:23 +000032# Bump this whenever the algorithm changes and you need bots/devs to re-sync,
33# ignoring the .last_sync_chromium file
kjellander432950c2016-09-08 10:55:42 -070034SCRIPT_VERSION = 8
kjellander@webrtc.org89256622014-08-20 12:10:11 +000035
36ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +000037CHROMIUM_NO_HISTORY = 'CHROMIUM_NO_HISTORY'
38
Henrik Kjellanderae331342015-04-28 16:08:23 +020039# Duplicated from depot_tools/gclient.py since we cannot depend on that:
40DEPS_OS_CHOICES = {
41 "win32": "win",
42 "win": "win",
43 "cygwin": "win",
44 "darwin": "mac",
45 "mac": "mac",
46 "unix": "unix",
47 "linux": "unix",
48 "linux2": "unix",
49 "linux3": "unix",
50 "android": "android",
51}
52
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +000053def _parse_gclient_dict():
54 gclient_dict = {}
55 try:
56 main_gclient = os.path.join(os.path.dirname(ROOT_DIR), '.gclient')
57 with open(main_gclient, 'rb') as deps_content:
58 exec(deps_content, gclient_dict)
59 except Exception as e:
60 print >> sys.stderr, 'error while parsing .gclient:', e
61 return gclient_dict
62
63
64def get_cache_dir():
65 return _parse_gclient_dict().get('cache_dir')
kjellander@webrtc.org89256622014-08-20 12:10:11 +000066
67
68def get_target_os_list():
Henrik Kjellanderae331342015-04-28 16:08:23 +020069 # Always add the currently running OS since the --deps option will override
70 # that if specified:
71 target_os_list = [DEPS_OS_CHOICES.get(sys.platform, 'unix')]
72 # Add any target_os entries from .gclient.
73 target_os_list += _parse_gclient_dict().get('target_os', [])
74 return ','.join(target_os_list)
kjellander@webrtc.org89256622014-08-20 12:10:11 +000075
76
77def main():
78 CR_DIR = os.path.join(ROOT_DIR, 'chromium')
79
80 p = argparse.ArgumentParser()
81 p.add_argument('--target-revision', required=True,
82 help='The target chromium git revision [REQUIRED]')
83 p.add_argument('--chromium-dir', default=CR_DIR,
84 help=('The path to the chromium directory to sync '
85 '(default: %(default)r)'))
86 opts = p.parse_args()
87 opts.chromium_dir = os.path.abspath(opts.chromium_dir)
88
iannucci@chromium.org16136382014-08-21 15:48:23 +000089 target_os_list = get_target_os_list()
90
kjellander@webrtc.org89256622014-08-20 12:10:11 +000091 # Do a quick check to see if we were successful last time to make runhooks
92 # sooper fast.
93 flag_file = os.path.join(opts.chromium_dir, '.last_sync_chromium')
iannucci@chromium.org16136382014-08-21 15:48:23 +000094 flag_file_content = '\n'.join([
95 str(SCRIPT_VERSION),
96 opts.target_revision,
97 repr(target_os_list),
98 ])
kjellander@webrtc.orgdde19a62014-11-24 10:08:03 +000099 if (os.path.exists(os.path.join(opts.chromium_dir, 'src')) and
100 os.path.exists(flag_file)):
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000101 with open(flag_file, 'r') as f:
iannucci@chromium.org16136382014-08-21 15:48:23 +0000102 if f.read() == flag_file_content:
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000103 print 'Chromium already up to date: ', opts.target_revision
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000104 return 0
105 os.unlink(flag_file)
106
Henrik Kjellander05ce5dd2015-06-15 11:10:15 +0200107 # Workaround to avoid sync failure due move in
108 # https://codereview.chromium.org/1155743013
109 # TODO(kjellander): Remove this after the summer of 2015.
110 freetype_src = os.path.join(CR_DIR, 'src', 'third_party', 'freetype-android',
111 'src')
112 if os.path.isdir(freetype_src):
113 shutil.rmtree(freetype_src)
114
kjellander@webrtc.org7d4e6d02014-11-27 10:41:04 +0000115 # Avoid downloading NaCl toolchain as part of the Chromium hooks.
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000116 gclient_cmd = 'gclient.bat' if sys.platform.startswith('win') else 'gclient'
117 args = [
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000118 gclient_cmd, 'sync', '--force', '--revision', 'src@'+opts.target_revision
119 ]
120
kjellander@webrtc.org3aa837c2014-08-20 14:25:43 +0000121 if os.environ.get('CHROME_HEADLESS') == '1':
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000122 # Running on a buildbot.
kjellander@webrtc.org3aa837c2014-08-20 14:25:43 +0000123 args.append('-vvv')
124
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000125 if sys.platform.startswith('win'):
iannucci@chromium.org286210d2014-08-21 02:14:11 +0000126 cache_path = os.path.join(os.path.splitdrive(ROOT_DIR)[0] + os.path.sep,
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000127 'b', 'git-cache')
128 else:
129 cache_path = '/b/git-cache'
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000130 else:
kjellander@webrtc.org74304332015-03-05 14:38:09 +0000131 # Verbose, but not as verbose as on the buildbots.
132 args.append('-v')
133
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000134 # Support developers setting the cache_dir in .gclient.
135 cache_path = get_cache_dir()
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000136
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000137 # Allow for users with poor internet connections to download a Git clone
138 # without history (saves several gigs but is generally slower and doesn't work
139 # with the Git cache).
140 if os.environ.get(CHROMIUM_NO_HISTORY) == '1':
141 if cache_path:
142 print >> sys.stderr, (
143 'You cannot use "no-history" mode for syncing Chrome (i.e. set the '
144 '%s environment variable to 1) when you have cache_dir configured in '
145 'your .gclient.' % CHROMIUM_NO_HISTORY)
146 return 1
147 args.append('--no-history')
148 gclient_entries_file = os.path.join(opts.chromium_dir, '.gclient_entries')
149 else:
150 # Write a temporary .gclient file that has the cache_dir variable added.
iannucci@chromium.org286210d2014-08-21 02:14:11 +0000151 gclientfile = os.path.join(opts.chromium_dir, '.gclient')
152 with open(gclientfile, 'rb') as spec:
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000153 spec = spec.read().splitlines()
154 spec[-1] = 'cache_dir = %r' % (cache_path,)
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000155 with open(gclientfile + '.tmp', 'wb') as f:
iannucci@chromium.org286210d2014-08-21 02:14:11 +0000156 f.write('\n'.join(spec))
157
158 args += [
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000159 '--gclientfile', '.gclient.tmp',
iannucci@chromium.org286210d2014-08-21 02:14:11 +0000160 '--delete_unversioned_trees', '--reset', '--upstream'
161 ]
kjellander@webrtc.org6c6680a2014-09-27 18:41:03 +0000162 gclient_entries_file = os.path.join(opts.chromium_dir,
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000163 '.gclient.tmp_entries')
kjellander@webrtc.org6c6680a2014-09-27 18:41:03 +0000164
165 # To avoid gclient sync problems when DEPS entries have been removed we must
166 # wipe the gclient's entries file that contains cached URLs for all DEPS.
167 if os.path.exists(gclient_entries_file):
168 os.unlink(gclient_entries_file)
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000169
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000170 if target_os_list:
171 args += ['--deps=' + target_os_list]
172
kjellander@webrtc.org74304332015-03-05 14:38:09 +0000173 print textwrap.dedent("""\
Henrik Kjellanderb3fc48b2015-03-27 14:25:35 +0100174 +---------------------------------------------------------------------+
175 | NOTICE: This sync of Chromium will take a long time as several |
176 | gigabytes of data are downloaded. If this is your initial |
177 | sync and it's interrupted, try running 'gclient sync' again.|
178 | If that fails, wipe everything clean and start over again. |
179 +---------------------------------------------------------------------+""")
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000180 print 'Running "%s" in %s' % (' '.join(args), opts.chromium_dir)
Henrik Kjellanderb4af3d62016-11-16 20:11:29 +0100181 ret = subprocess.call(args, cwd=opts.chromium_dir)
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000182 if ret == 0:
183 with open(flag_file, 'wb') as f:
iannucci@chromium.org16136382014-08-21 15:48:23 +0000184 f.write(flag_file_content)
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000185
186 return ret
187
188
189if __name__ == '__main__':
190 sys.exit(main())