blob: 7dfbd89f28845b3090a4e61323a987d0d12f874d [file] [log] [blame]
John Abd-El-Malek86723092014-11-07 10:58:14 -08001#!/usr/bin/env python
2# Copyright 2014 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
6# This a simple script to make building/testing Mojo components easier.
7
8import argparse
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -08009from copy import deepcopy
John Abd-El-Malek86723092014-11-07 10:58:14 -080010import os
11import platform
John Abd-El-Maleka46d0122014-11-10 13:47:26 -080012import re
John Abd-El-Malek86723092014-11-07 10:58:14 -080013import subprocess
14import sys
15
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -080016from get_test_list import GetTestList
Viet-Trung Luu931979c2014-11-24 16:13:52 -080017from mopy.config import Config
18from mopy.paths import Paths
19
20
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -080021def _args_to_config(args):
Viet-Trung Luu931979c2014-11-24 16:13:52 -080022 # Default to host OS.
23 target_os = None
24 if args.android:
25 target_os = Config.OS_ANDROID
26 elif args.chromeos:
27 target_os = Config.OS_CHROMEOS
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -080028
Nick Brayf93c0fa2015-01-13 14:02:57 -080029 if args.cpu_arch is None and args.android:
30 args.cpu_arch = 'arm'
31 target_arch = args.cpu_arch
32
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -080033 additional_args = {}
34
35 if 'clang' in args:
Viet-Trung Luuc3462782014-11-25 12:47:08 -080036 additional_args['is_clang'] = args.clang
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -080037
38 if 'asan' in args and args.asan:
39 additional_args['sanitizer'] = Config.SANITIZER_ASAN
40
41 # Additional non-standard config entries:
42
43 if 'goma' in args:
44 goma_dir = os.environ.get('GOMA_DIR')
45 goma_home_dir = os.path.join(os.getenv('HOME', ''), 'goma')
46 if args.goma and goma_dir:
47 additional_args['use_goma'] = True
48 additional_args['goma_dir'] = goma_dir
49 elif args.goma and os.path.exists(goma_home_dir):
50 additional_args['use_goma'] = True
51 additional_args['goma_dir'] = goma_home_dir
52 else:
53 additional_args['use_goma'] = False
54 additional_args['goma_dir'] = None
55
Nick Brayf93c0fa2015-01-13 14:02:57 -080056 additional_args['use_nacl'] = args.nacl
Nick Braye7561b42015-01-09 10:54:19 -080057
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -080058 if 'dry_run' in args:
59 additional_args['dry_run'] = args.dry_run
60
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -080061 if 'builder_name' in args:
62 additional_args['builder_name'] = args.builder_name
63 if 'build_number' in args:
64 additional_args['build_number'] = args.build_number
65 if 'master_name' in args:
66 additional_args['master_name'] = args.master_name
67 if 'test_results_server' in args:
68 additional_args['test_results_server'] = args.test_results_server
69
Nick Brayf93c0fa2015-01-13 14:02:57 -080070 return Config(target_os=target_os, target_arch=target_arch,
71 is_debug=args.debug, **additional_args)
John Abd-El-Malekf8a33b82014-11-07 16:01:25 -080072
John Abd-El-Malek86723092014-11-07 10:58:14 -080073
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -080074def _get_out_dir(config):
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -080075 paths = Paths(config)
Viet-Trung Luu931979c2014-11-24 16:13:52 -080076 return paths.SrcRelPath(paths.build_dir)
John Abd-El-Malek86723092014-11-07 10:58:14 -080077
78
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -080079def sync(config):
John Abd-El-Malek86723092014-11-07 10:58:14 -080080 # pylint: disable=W0613
81 return subprocess.call(['gclient', 'sync'])
82
83
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -080084def gn(config):
James Robinson5f36d372015-01-08 13:03:56 -080085 command = ['gn', 'gen', '--check']
John Abd-El-Malek86723092014-11-07 10:58:14 -080086
87 gn_args = []
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -080088 gn_args.append('is_debug=' + ('true' if config.is_debug else 'false'))
89 gn_args.append('is_asan=' + ('true' if config.sanitizer ==
90 Config.SANITIZER_ASAN else 'false'))
Viet-Trung Luuc3462782014-11-25 12:47:08 -080091 if config.is_clang is not None:
92 gn_args.append('is_clang=' + ('true' if config.is_clang else 'false'))
93 else:
94 gn_args.append('is_clang=' + ('true' if config.target_os not in
95 (Config.OS_ANDROID, Config.OS_WINDOWS)
96 else 'false'))
John Abd-El-Malek86723092014-11-07 10:58:14 -080097
Viet-Trung Luuc3462782014-11-25 12:47:08 -080098 if config.values['use_goma']:
John Abd-El-Malek86723092014-11-07 10:58:14 -080099 gn_args.append('use_goma=true')
Viet-Trung Luuc3462782014-11-25 12:47:08 -0800100 gn_args.append(r'''goma_dir=\"%s\"''' % config.values['goma_dir'])
John Abd-El-Malek86723092014-11-07 10:58:14 -0800101 else:
102 gn_args.append('use_goma=false')
103
Nick Braye7561b42015-01-09 10:54:19 -0800104 if config.values['use_nacl']:
105 gn_args.append('mojo_use_nacl=true')
106
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800107 if config.target_os == Config.OS_ANDROID:
Nick Brayf93c0fa2015-01-13 14:02:57 -0800108 gn_args.append(r'''os=\"android\"''')
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800109 elif config.target_os == Config.OS_CHROMEOS:
Elliot Glayshere1fd4942015-01-14 15:49:41 -0800110 gn_args.append(r'''os=\"chromeos\" use_system_harfbuzz=false''')
John Abd-El-Malek86723092014-11-07 10:58:14 -0800111
Nick Brayf93c0fa2015-01-13 14:02:57 -0800112 gn_args.append(r'''cpu_arch=\"%s\"''' % config.target_arch)
113
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800114 out_dir = _get_out_dir(config)
John Abd-El-Malek86723092014-11-07 10:58:14 -0800115 command.append(out_dir)
116 command.append('--args="%s"' % ' '.join(gn_args))
117
118 print 'Running %s ...' % ' '.join(command)
119 return subprocess.call(' '.join(command), shell=True)
120
121
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800122def _get_gn_arg_value(out_dir, arg):
Viet-Trung Luuc3462782014-11-25 12:47:08 -0800123 args_file_path = os.path.join(out_dir, 'args.gn')
Michael Wasserman89f35902014-11-12 16:01:42 -0800124 if os.path.isfile(args_file_path):
125 key_value_regex = re.compile(r'^%s = (.+)$' % arg)
Viet-Trung Luuc3462782014-11-25 12:47:08 -0800126 with open(args_file_path, 'r') as args_file:
Michael Wasserman89f35902014-11-12 16:01:42 -0800127 for line in args_file.readlines():
128 m = key_value_regex.search(line)
129 if m:
130 return m.group(1).strip('"')
John Abd-El-Maleka46d0122014-11-10 13:47:26 -0800131 return ''
John Abd-El-Malek86723092014-11-07 10:58:14 -0800132
133
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800134def build(config):
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800135 out_dir = _get_out_dir(config)
Colin Blundellae10f6f2014-12-08 12:53:01 +0100136 print 'Building in %s ...' % out_dir
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800137 if _get_gn_arg_value(out_dir, 'use_goma') == 'true':
Colin Blundellae10f6f2014-12-08 12:53:01 +0100138 # Use the configured goma directory.
139 local_goma_dir = _get_gn_arg_value(out_dir, 'goma_dir')
140 print 'Ensuring goma (in %s) started ...' % local_goma_dir
141 command = ['python',
142 os.path.join(local_goma_dir, 'goma_ctl.py'),
143 'ensure_start']
144 exit_code = subprocess.call(command)
145 if exit_code:
146 return exit_code
147
Nick Braye5ac3422014-12-16 13:27:24 -0800148 return subprocess.call(['ninja', '-j', '1000', '-l', '100', '-C', out_dir])
John Abd-El-Malek86723092014-11-07 10:58:14 -0800149 else:
Nick Braye5ac3422014-12-16 13:27:24 -0800150 return subprocess.call(['ninja', '-C', out_dir])
John Abd-El-Malek86723092014-11-07 10:58:14 -0800151
152
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800153def _run_tests(config, test_types):
154 """Runs the tests of the given type(s) for the given config."""
John Abd-El-Malek86723092014-11-07 10:58:14 -0800155
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800156 assert isinstance(test_types, list)
157 config = deepcopy(config)
158 config.values['test_types'] = test_types
John Abd-El-Malek86723092014-11-07 10:58:14 -0800159
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800160 test_list = GetTestList(config)
161 dry_run = config.values.get('dry_run')
Ojan Vafai338355d2014-11-20 15:49:20 -0800162 final_exit_code = 0
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800163 for entry in test_list:
164 print 'Running: %s' % entry['name']
165 print 'Command: %s' % entry['command']
166 if dry_run:
167 continue
John Abd-El-Malek86723092014-11-07 10:58:14 -0800168
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800169 exit_code = subprocess.call(entry['command'])
Ojan Vafai338355d2014-11-20 15:49:20 -0800170 if not final_exit_code:
171 final_exit_code = exit_code
Ojan Vafai338355d2014-11-20 15:49:20 -0800172 return final_exit_code
John Abd-El-Malek86723092014-11-07 10:58:14 -0800173
Zachary Andersona83affc2014-11-25 10:59:27 -0800174
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800175def test(config):
176 return _run_tests(config, [Config.TEST_TYPE_DEFAULT])
177
178
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800179def perftest(config):
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800180 return _run_tests(config, [Config.TEST_TYPE_PERF])
John Abd-El-Malek86723092014-11-07 10:58:14 -0800181
182
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800183def pytest(config):
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800184 return _run_tests(config, ['python'])
John Abd-El-Malek86723092014-11-07 10:58:14 -0800185
186
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800187def darttest(config):
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800188 return _run_tests(config, ['dart'])
John Abd-El-Malek86723092014-11-07 10:58:14 -0800189
190
Nick Brayad87e372015-01-13 14:46:59 -0800191def nacltest(config):
192 return _run_tests(config, ['nacl'])
193
194
John Abd-El-Malek86723092014-11-07 10:58:14 -0800195def main():
Viet-Trung Luu931979c2014-11-24 16:13:52 -0800196 os.chdir(Paths().src_root)
John Abd-El-Malekf8a33b82014-11-07 16:01:25 -0800197
John Abd-El-Malek86723092014-11-07 10:58:14 -0800198 parser = argparse.ArgumentParser(description='A script to make building'
199 '/testing Mojo components easier.')
200
201 parent_parser = argparse.ArgumentParser(add_help=False)
Scott Violet665d8452014-12-05 08:57:09 -0800202 parent_parser.add_argument('--asan', help='Use Address Sanitizer',
203 action='store_true')
204
John Abd-El-Malek86723092014-11-07 10:58:14 -0800205 debug_group = parent_parser.add_mutually_exclusive_group()
206 debug_group.add_argument('--debug', help='Debug build (default)',
207 default=True, action='store_true')
208 debug_group.add_argument('--release', help='Release build', default=False,
209 dest='debug', action='store_false')
210
211 os_group = parent_parser.add_mutually_exclusive_group()
212 os_group.add_argument('--android', help='Build for Android',
213 action='store_true')
214 os_group.add_argument('--chromeos', help='Build for ChromeOS',
215 action='store_true')
216
Nick Brayf93c0fa2015-01-13 14:02:57 -0800217 parent_parser.add_argument('--cpu-arch',
218 help='CPU architecture to build for.',
219 choices=['x64', 'x86', 'arm'])
220
221 parent_parser.add_argument('--nacl', help='Add in NaCl', default=False,
222 action='store_true')
223
John Abd-El-Malek86723092014-11-07 10:58:14 -0800224 subparsers = parser.add_subparsers()
225
226 sync_parser = subparsers.add_parser('sync', parents=[parent_parser],
227 help='Sync using gclient (does not run gn).')
228 sync_parser.set_defaults(func=sync)
229
230 gn_parser = subparsers.add_parser('gn', parents=[parent_parser],
231 help='Run gn for mojo (does not sync).')
232 gn_parser.set_defaults(func=gn)
John Abd-El-Malek86723092014-11-07 10:58:14 -0800233 clang_group = gn_parser.add_mutually_exclusive_group()
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800234 clang_group.add_argument('--clang', help='Use Clang (default)', default=None,
John Abd-El-Malek86723092014-11-07 10:58:14 -0800235 action='store_true')
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800236 clang_group.add_argument('--gcc', help='Use GCC',
John Abd-El-Malek86723092014-11-07 10:58:14 -0800237 dest='clang', action='store_false')
238 goma_group = gn_parser.add_mutually_exclusive_group()
239 goma_group.add_argument('--goma',
240 help='Use Goma (if $GOMA_DIR is set or $HOME/goma '
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800241 'exists; default)',
John Abd-El-Malek86723092014-11-07 10:58:14 -0800242 default=True,
243 action='store_true')
244 goma_group.add_argument('--no-goma', help='Don\'t use Goma', default=False,
245 dest='goma', action='store_false')
246
247 build_parser = subparsers.add_parser('build', parents=[parent_parser],
248 help='Build')
249 build_parser.set_defaults(func=build)
250
251 test_parser = subparsers.add_parser('test', parents=[parent_parser],
252 help='Run unit tests (does not build).')
253 test_parser.set_defaults(func=test)
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800254 test_parser.add_argument('--dry-run',
255 help='Print instead of executing commands',
256 default=False, action='store_true')
John Abd-El-Malek86723092014-11-07 10:58:14 -0800257
John Abd-El-Malek86723092014-11-07 10:58:14 -0800258 perftest_parser = subparsers.add_parser('perftest', parents=[parent_parser],
259 help='Run perf tests (does not build).')
260 perftest_parser.set_defaults(func=perftest)
261
262 pytest_parser = subparsers.add_parser('pytest', parents=[parent_parser],
263 help='Run Python unit tests (does not build).')
264 pytest_parser.set_defaults(func=pytest)
265
266 darttest_parser = subparsers.add_parser('darttest', parents=[parent_parser],
267 help='Run Dart unit tests (does not build).')
268 darttest_parser.set_defaults(func=darttest)
269
Nick Brayad87e372015-01-13 14:46:59 -0800270 nacltest_parser = subparsers.add_parser('nacltest', parents=[parent_parser],
271 help='Run NaCl unit tests (does not build).')
272 nacltest_parser.set_defaults(func=nacltest)
273
John Abd-El-Malek86723092014-11-07 10:58:14 -0800274 args = parser.parse_args()
Viet-Trung Luu0f09fc72014-12-01 15:39:51 -0800275 config = _args_to_config(args)
Viet-Trung Luu24e7e7a2014-11-25 11:06:53 -0800276 return args.func(config)
John Abd-El-Malek86723092014-11-07 10:58:14 -0800277
278
279if __name__ == '__main__':
280 sys.exit(main())