blob: cc850f426529a66b627ce58d926521cee2befaa8 [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
27import subprocess
28import sys
kjellander@webrtc.org74304332015-03-05 14:38:09 +000029import textwrap
kjellander@webrtc.org89256622014-08-20 12:10:11 +000030
iannucci@chromium.org16136382014-08-21 15:48:23 +000031# Bump this whenever the algorithm changes and you need bots/devs to re-sync,
32# ignoring the .last_sync_chromium file
kjellander@webrtc.orgbfdee692015-02-03 15:23:34 +000033SCRIPT_VERSION = 4
kjellander@webrtc.org89256622014-08-20 12:10:11 +000034
35ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +000036CHROMIUM_NO_HISTORY = 'CHROMIUM_NO_HISTORY'
37
38def _parse_gclient_dict():
39 gclient_dict = {}
40 try:
41 main_gclient = os.path.join(os.path.dirname(ROOT_DIR), '.gclient')
42 with open(main_gclient, 'rb') as deps_content:
43 exec(deps_content, gclient_dict)
44 except Exception as e:
45 print >> sys.stderr, 'error while parsing .gclient:', e
46 return gclient_dict
47
48
49def get_cache_dir():
50 return _parse_gclient_dict().get('cache_dir')
kjellander@webrtc.org89256622014-08-20 12:10:11 +000051
52
53def get_target_os_list():
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +000054 return ','.join(_parse_gclient_dict().get('target_os', []))
kjellander@webrtc.org89256622014-08-20 12:10:11 +000055
56
57def main():
58 CR_DIR = os.path.join(ROOT_DIR, 'chromium')
59
60 p = argparse.ArgumentParser()
61 p.add_argument('--target-revision', required=True,
62 help='The target chromium git revision [REQUIRED]')
63 p.add_argument('--chromium-dir', default=CR_DIR,
64 help=('The path to the chromium directory to sync '
65 '(default: %(default)r)'))
66 opts = p.parse_args()
67 opts.chromium_dir = os.path.abspath(opts.chromium_dir)
68
iannucci@chromium.org16136382014-08-21 15:48:23 +000069 target_os_list = get_target_os_list()
70
kjellander@webrtc.org89256622014-08-20 12:10:11 +000071 # Do a quick check to see if we were successful last time to make runhooks
72 # sooper fast.
73 flag_file = os.path.join(opts.chromium_dir, '.last_sync_chromium')
iannucci@chromium.org16136382014-08-21 15:48:23 +000074 flag_file_content = '\n'.join([
75 str(SCRIPT_VERSION),
76 opts.target_revision,
77 repr(target_os_list),
78 ])
kjellander@webrtc.orgdde19a62014-11-24 10:08:03 +000079 if (os.path.exists(os.path.join(opts.chromium_dir, 'src')) and
80 os.path.exists(flag_file)):
kjellander@webrtc.org89256622014-08-20 12:10:11 +000081 with open(flag_file, 'r') as f:
iannucci@chromium.org16136382014-08-21 15:48:23 +000082 if f.read() == flag_file_content:
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +000083 print 'Chromium already up to date: ', opts.target_revision
kjellander@webrtc.org89256622014-08-20 12:10:11 +000084 return 0
85 os.unlink(flag_file)
86
87 env = os.environ.copy()
kjellander@webrtc.org7d4e6d02014-11-27 10:41:04 +000088
89 # Avoid downloading NaCl toolchain as part of the Chromium hooks.
90 env.setdefault('GYP_DEFINES', '')
91 env['GYP_DEFINES'] += ' disable_nacl=1'
kjellander@webrtc.org89256622014-08-20 12:10:11 +000092 env['GYP_CHROMIUM_NO_ACTION'] = '1'
93 gclient_cmd = 'gclient.bat' if sys.platform.startswith('win') else 'gclient'
94 args = [
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000095 gclient_cmd, 'sync', '--force', '--revision', 'src@'+opts.target_revision
96 ]
97
kjellander@webrtc.org3aa837c2014-08-20 14:25:43 +000098 if os.environ.get('CHROME_HEADLESS') == '1':
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +000099 # Running on a buildbot.
kjellander@webrtc.org3aa837c2014-08-20 14:25:43 +0000100 args.append('-vvv')
101
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000102 if sys.platform.startswith('win'):
iannucci@chromium.org286210d2014-08-21 02:14:11 +0000103 cache_path = os.path.join(os.path.splitdrive(ROOT_DIR)[0] + os.path.sep,
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000104 'b', 'git-cache')
105 else:
106 cache_path = '/b/git-cache'
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000107 else:
kjellander@webrtc.org74304332015-03-05 14:38:09 +0000108 # Verbose, but not as verbose as on the buildbots.
109 args.append('-v')
110
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000111 # Support developers setting the cache_dir in .gclient.
112 cache_path = get_cache_dir()
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000113
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000114 # Allow for users with poor internet connections to download a Git clone
115 # without history (saves several gigs but is generally slower and doesn't work
116 # with the Git cache).
117 if os.environ.get(CHROMIUM_NO_HISTORY) == '1':
118 if cache_path:
119 print >> sys.stderr, (
120 'You cannot use "no-history" mode for syncing Chrome (i.e. set the '
121 '%s environment variable to 1) when you have cache_dir configured in '
122 'your .gclient.' % CHROMIUM_NO_HISTORY)
123 return 1
124 args.append('--no-history')
125 gclient_entries_file = os.path.join(opts.chromium_dir, '.gclient_entries')
126 else:
127 # Write a temporary .gclient file that has the cache_dir variable added.
iannucci@chromium.org286210d2014-08-21 02:14:11 +0000128 gclientfile = os.path.join(opts.chromium_dir, '.gclient')
129 with open(gclientfile, 'rb') as spec:
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000130 spec = spec.read().splitlines()
131 spec[-1] = 'cache_dir = %r' % (cache_path,)
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000132 with open(gclientfile + '.tmp', 'wb') as f:
iannucci@chromium.org286210d2014-08-21 02:14:11 +0000133 f.write('\n'.join(spec))
134
135 args += [
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000136 '--gclientfile', '.gclient.tmp',
iannucci@chromium.org286210d2014-08-21 02:14:11 +0000137 '--delete_unversioned_trees', '--reset', '--upstream'
138 ]
kjellander@webrtc.org6c6680a2014-09-27 18:41:03 +0000139 gclient_entries_file = os.path.join(opts.chromium_dir,
kjellander@webrtc.org8539bd02014-10-23 12:17:58 +0000140 '.gclient.tmp_entries')
kjellander@webrtc.org6c6680a2014-09-27 18:41:03 +0000141
142 # To avoid gclient sync problems when DEPS entries have been removed we must
143 # wipe the gclient's entries file that contains cached URLs for all DEPS.
144 if os.path.exists(gclient_entries_file):
145 os.unlink(gclient_entries_file)
iannucci@chromium.org98d92d62014-08-20 23:53:59 +0000146
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000147 if target_os_list:
148 args += ['--deps=' + target_os_list]
149
kjellander@webrtc.org74304332015-03-05 14:38:09 +0000150 print textwrap.dedent("""\
151 +--------------------------------------------------------------------+
152 | NOTICE: This sync of Chromium will take a very long time the first |
153 | time you checkout WebRTC as several gigabytes of data has |
154 | to be downloaded. Make sure you don't abort this download |
155 | or you will have to issue a 'gclient sync' followed by a |
156 | 'git auto-svn' to complete the initial setup. If that |
157 | fails, you have to wipe everything clean and start over. |
158 +--------------------------------------------------------------------+""")
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000159 print 'Running "%s" in %s' % (' '.join(args), opts.chromium_dir)
160 ret = subprocess.call(args, cwd=opts.chromium_dir, env=env)
161 if ret == 0:
162 with open(flag_file, 'wb') as f:
iannucci@chromium.org16136382014-08-21 15:48:23 +0000163 f.write(flag_file_content)
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000164
165 return ret
166
167
168if __name__ == '__main__':
169 sys.exit(main())