blob: 10cff5365d7c1e80c767dde8db36001a1c257937 [file] [log] [blame]
dprankefe4602312015-04-08 16:20:351#!/usr/bin/env python
2# Copyright 2015 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"""MB - the Meta-Build wrapper around GYP and GN
7
8MB is a wrapper script for GYP and GN that can be used to generate build files
9for sets of canned configurations and analyze them.
10"""
11
12from __future__ import print_function
13
14import argparse
15import ast
dprankec3441d12015-06-23 23:01:3516import errno
dprankefe4602312015-04-08 16:20:3517import json
18import os
19import pipes
dpranked8113582015-06-05 20:08:2520import pprint
dprankefe4602312015-04-08 16:20:3521import shlex
22import shutil
23import sys
24import subprocess
dprankef61de2f2015-05-14 04:09:5625import tempfile
dprankefe4602312015-04-08 16:20:3526
dprankefe4602312015-04-08 16:20:3527def main(args):
dprankeee5b51f62015-04-09 00:03:2228 mbw = MetaBuildWrapper()
29 mbw.ParseArgs(args)
30 return mbw.args.func()
dprankefe4602312015-04-08 16:20:3531
32
33class MetaBuildWrapper(object):
34 def __init__(self):
35 p = os.path
36 d = os.path.dirname
37 self.chromium_src_dir = p.normpath(d(d(d(p.abspath(__file__)))))
38 self.default_config = p.join(self.chromium_src_dir, 'tools', 'mb',
39 'mb_config.pyl')
dpranke8c2cfd32015-09-17 20:12:3340 self.executable = sys.executable
dpranked1fba482015-04-14 20:54:5141 self.platform = sys.platform
dpranke8c2cfd32015-09-17 20:12:3342 self.sep = os.sep
dprankefe4602312015-04-08 16:20:3543 self.args = argparse.Namespace()
44 self.configs = {}
45 self.masters = {}
46 self.mixins = {}
47 self.private_configs = []
48 self.common_dev_configs = []
49 self.unsupported_configs = []
50
51 def ParseArgs(self, argv):
52 def AddCommonOptions(subp):
53 subp.add_argument('-b', '--builder',
54 help='builder name to look up config from')
55 subp.add_argument('-m', '--master',
56 help='master name to look up config from')
57 subp.add_argument('-c', '--config',
58 help='configuration to analyze')
59 subp.add_argument('-f', '--config-file', metavar='PATH',
60 default=self.default_config,
61 help='path to config file '
62 '(default is //tools/mb/mb_config.pyl)')
63 subp.add_argument('-g', '--goma-dir', default=self.ExpandUser('~/goma'),
64 help='path to goma directory (default is %(default)s).')
65 subp.add_argument('-n', '--dryrun', action='store_true',
66 help='Do a dry run (i.e., do nothing, just print '
67 'the commands that will run)')
dprankee0547cd2015-09-15 01:27:4068 subp.add_argument('-v', '--verbose', action='store_true',
69 help='verbose logging')
dprankefe4602312015-04-08 16:20:3570
71 parser = argparse.ArgumentParser(prog='mb')
72 subps = parser.add_subparsers()
73
74 subp = subps.add_parser('analyze',
75 help='analyze whether changes to a set of files '
76 'will cause a set of binaries to be rebuilt.')
77 AddCommonOptions(subp)
tsergeantf061c992015-07-16 01:34:0078 subp.add_argument('--swarming-targets-file',
79 help='save runtime dependencies for targets listed '
80 'in file.')
dpranked8113582015-06-05 20:08:2581 subp.add_argument('path', nargs=1,
dprankefe4602312015-04-08 16:20:3582 help='path build was generated into.')
83 subp.add_argument('input_path', nargs=1,
84 help='path to a file containing the input arguments '
85 'as a JSON object.')
86 subp.add_argument('output_path', nargs=1,
87 help='path to a file containing the output arguments '
88 'as a JSON object.')
89 subp.set_defaults(func=self.CmdAnalyze)
90
91 subp = subps.add_parser('gen',
92 help='generate a new set of build files')
93 AddCommonOptions(subp)
dpranke74559b52015-06-10 21:20:3994 subp.add_argument('--swarming-targets-file',
95 help='save runtime dependencies for targets listed '
96 'in file.')
dpranked8113582015-06-05 20:08:2597 subp.add_argument('path', nargs=1,
dprankefe4602312015-04-08 16:20:3598 help='path to generate build into')
99 subp.set_defaults(func=self.CmdGen)
100
101 subp = subps.add_parser('lookup',
102 help='look up the command for a given config or '
103 'builder')
104 AddCommonOptions(subp)
105 subp.set_defaults(func=self.CmdLookup)
106
107 subp = subps.add_parser('validate',
108 help='validate the config file')
dprankea5a77ca2015-07-16 23:24:17109 subp.add_argument('-f', '--config-file', metavar='PATH',
110 default=self.default_config,
111 help='path to config file '
112 '(default is //tools/mb/mb_config.pyl)')
dprankefe4602312015-04-08 16:20:35113 subp.set_defaults(func=self.CmdValidate)
114
115 subp = subps.add_parser('help',
116 help='Get help on a subcommand.')
117 subp.add_argument(nargs='?', action='store', dest='subcommand',
118 help='The command to get help for.')
119 subp.set_defaults(func=self.CmdHelp)
120
121 self.args = parser.parse_args(argv)
122
123 def CmdAnalyze(self):
124 vals = self.GetConfig()
125 if vals['type'] == 'gn':
126 return self.RunGNAnalyze(vals)
127 elif vals['type'] == 'gyp':
128 return self.RunGYPAnalyze(vals)
129 else:
130 raise MBErr('Unknown meta-build type "%s"' % vals['type'])
131
132 def CmdGen(self):
133 vals = self.GetConfig()
dprankec161aa92015-09-14 20:21:13134
135 self.ClobberIfNeeded(vals)
136
dprankefe4602312015-04-08 16:20:35137 if vals['type'] == 'gn':
Dirk Pranke0fd41bcd2015-06-19 00:05:50138 return self.RunGNGen(vals)
dpranke08d2ab12015-04-24 21:54:20139 if vals['type'] == 'gyp':
Dirk Pranke0fd41bcd2015-06-19 00:05:50140 return self.RunGYPGen(vals)
dpranke08d2ab12015-04-24 21:54:20141
142 raise MBErr('Unknown meta-build type "%s"' % vals['type'])
dprankefe4602312015-04-08 16:20:35143
144 def CmdLookup(self):
145 vals = self.GetConfig()
146 if vals['type'] == 'gn':
dpranked1fba482015-04-14 20:54:51147 cmd = self.GNCmd('gen', '<path>', vals['gn_args'])
dprankefe4602312015-04-08 16:20:35148 elif vals['type'] == 'gyp':
dprankeedc49c382015-08-14 02:32:59149 if vals['gyp_crosscompile']:
150 self.Print('GYP_CROSSCOMPILE=1')
dpranke8c2cfd32015-09-17 20:12:33151 cmd = self.GYPCmd('<path>', vals['gyp_defines'])
dprankefe4602312015-04-08 16:20:35152 else:
153 raise MBErr('Unknown meta-build type "%s"' % vals['type'])
154
155 self.PrintCmd(cmd)
156 return 0
157
158 def CmdHelp(self):
159 if self.args.subcommand:
160 self.ParseArgs([self.args.subcommand, '--help'])
161 else:
162 self.ParseArgs(['--help'])
163
164 def CmdValidate(self):
165 errs = []
166
167 # Read the file to make sure it parses.
168 self.ReadConfigFile()
169
170 # Figure out the whole list of configs and ensure that no config is
171 # listed in more than one category.
172 all_configs = {}
173 for config in self.common_dev_configs:
174 all_configs[config] = 'common_dev_configs'
175 for config in self.private_configs:
176 if config in all_configs:
177 errs.append('config "%s" listed in "private_configs" also '
178 'listed in "%s"' % (config, all_configs['config']))
179 else:
180 all_configs[config] = 'private_configs'
181 for config in self.unsupported_configs:
182 if config in all_configs:
183 errs.append('config "%s" listed in "unsupported_configs" also '
184 'listed in "%s"' % (config, all_configs['config']))
185 else:
186 all_configs[config] = 'unsupported_configs'
187
188 for master in self.masters:
189 for builder in self.masters[master]:
190 config = self.masters[master][builder]
191 if config in all_configs and all_configs[config] not in self.masters:
192 errs.append('Config "%s" used by a bot is also listed in "%s".' %
193 (config, all_configs[config]))
194 else:
195 all_configs[config] = master
196
197 # Check that every referenced config actually exists.
198 for config, loc in all_configs.items():
199 if not config in self.configs:
200 errs.append('Unknown config "%s" referenced from "%s".' %
201 (config, loc))
202
203 # Check that every actual config is actually referenced.
204 for config in self.configs:
205 if not config in all_configs:
206 errs.append('Unused config "%s".' % config)
207
208 # Figure out the whole list of mixins, and check that every mixin
209 # listed by a config or another mixin actually exists.
210 referenced_mixins = set()
211 for config, mixins in self.configs.items():
212 for mixin in mixins:
213 if not mixin in self.mixins:
214 errs.append('Unknown mixin "%s" referenced by config "%s".' %
215 (mixin, config))
216 referenced_mixins.add(mixin)
217
218 for mixin in self.mixins:
219 for sub_mixin in self.mixins[mixin].get('mixins', []):
220 if not sub_mixin in self.mixins:
221 errs.append('Unknown mixin "%s" referenced by mixin "%s".' %
222 (sub_mixin, mixin))
223 referenced_mixins.add(sub_mixin)
224
225 # Check that every mixin defined is actually referenced somewhere.
226 for mixin in self.mixins:
227 if not mixin in referenced_mixins:
228 errs.append('Unreferenced mixin "%s".' % mixin)
229
230 if errs:
dpranke4323c80632015-08-10 22:53:54231 raise MBErr(('mb config file %s has problems:' % self.args.config_file) +
dprankea33267872015-08-12 15:45:17232 '\n ' + '\n '.join(errs))
dprankefe4602312015-04-08 16:20:35233
dprankee0547cd2015-09-15 01:27:40234 self.Print('mb config file %s looks ok.' % self.args.config_file)
dprankefe4602312015-04-08 16:20:35235 return 0
236
237 def GetConfig(self):
238 self.ReadConfigFile()
239 config = self.ConfigFromArgs()
240 if not config in self.configs:
241 raise MBErr('Config "%s" not found in %s' %
242 (config, self.args.config_file))
243
244 return self.FlattenConfig(config)
245
246 def ReadConfigFile(self):
247 if not self.Exists(self.args.config_file):
248 raise MBErr('config file not found at %s' % self.args.config_file)
249
250 try:
251 contents = ast.literal_eval(self.ReadFile(self.args.config_file))
252 except SyntaxError as e:
253 raise MBErr('Failed to parse config file "%s": %s' %
254 (self.args.config_file, e))
255
256 self.common_dev_configs = contents['common_dev_configs']
257 self.configs = contents['configs']
258 self.masters = contents['masters']
259 self.mixins = contents['mixins']
260 self.private_configs = contents['private_configs']
261 self.unsupported_configs = contents['unsupported_configs']
262
263 def ConfigFromArgs(self):
264 if self.args.config:
265 if self.args.master or self.args.builder:
266 raise MBErr('Can not specific both -c/--config and -m/--master or '
267 '-b/--builder')
268
269 return self.args.config
270
271 if not self.args.master or not self.args.builder:
272 raise MBErr('Must specify either -c/--config or '
273 '(-m/--master and -b/--builder)')
274
275 if not self.args.master in self.masters:
276 raise MBErr('Master name "%s" not found in "%s"' %
277 (self.args.master, self.args.config_file))
278
279 if not self.args.builder in self.masters[self.args.master]:
280 raise MBErr('Builder name "%s" not found under masters[%s] in "%s"' %
281 (self.args.builder, self.args.master, self.args.config_file))
282
283 return self.masters[self.args.master][self.args.builder]
284
285 def FlattenConfig(self, config):
286 mixins = self.configs[config]
287 vals = {
288 'type': None,
289 'gn_args': [],
dprankec161aa92015-09-14 20:21:13290 'gyp_defines': '',
dprankeedc49c382015-08-14 02:32:59291 'gyp_crosscompile': False,
dprankefe4602312015-04-08 16:20:35292 }
293
294 visited = []
295 self.FlattenMixins(mixins, vals, visited)
296 return vals
297
298 def FlattenMixins(self, mixins, vals, visited):
299 for m in mixins:
300 if m not in self.mixins:
301 raise MBErr('Unknown mixin "%s"' % m)
dprankeee5b51f62015-04-09 00:03:22302
303 # TODO: check for cycles in mixins.
dprankefe4602312015-04-08 16:20:35304
305 visited.append(m)
306
307 mixin_vals = self.mixins[m]
308 if 'type' in mixin_vals:
309 vals['type'] = mixin_vals['type']
310 if 'gn_args' in mixin_vals:
311 if vals['gn_args']:
312 vals['gn_args'] += ' ' + mixin_vals['gn_args']
313 else:
314 vals['gn_args'] = mixin_vals['gn_args']
dprankeedc49c382015-08-14 02:32:59315 if 'gyp_crosscompile' in mixin_vals:
316 vals['gyp_crosscompile'] = mixin_vals['gyp_crosscompile']
dprankefe4602312015-04-08 16:20:35317 if 'gyp_defines' in mixin_vals:
318 if vals['gyp_defines']:
319 vals['gyp_defines'] += ' ' + mixin_vals['gyp_defines']
320 else:
321 vals['gyp_defines'] = mixin_vals['gyp_defines']
322 if 'mixins' in mixin_vals:
323 self.FlattenMixins(mixin_vals['mixins'], vals, visited)
324 return vals
325
dprankec161aa92015-09-14 20:21:13326 def ClobberIfNeeded(self, vals):
327 path = self.args.path[0]
328 build_dir = self.ToAbsPath(path)
dpranke8c2cfd32015-09-17 20:12:33329 mb_type_path = self.PathJoin(build_dir, 'mb_type')
dprankec161aa92015-09-14 20:21:13330 needs_clobber = False
331 new_mb_type = vals['type']
332 if self.Exists(build_dir):
333 if self.Exists(mb_type_path):
334 old_mb_type = self.ReadFile(mb_type_path)
335 if old_mb_type != new_mb_type:
336 self.Print("Build type mismatch: was %s, will be %s, clobbering %s" %
337 (old_mb_type, new_mb_type, path))
338 needs_clobber = True
339 else:
340 # There is no 'mb_type' file in the build directory, so this probably
341 # means that the prior build(s) were not done through mb, and we
342 # have no idea if this was a GYP build or a GN build. Clobber it
343 # to be safe.
344 self.Print("%s/mb_type missing, clobbering to be safe" % path)
345 needs_clobber = True
346
347 if needs_clobber:
348 self.RemoveDirectory(build_dir)
349
350 self.MaybeMakeDirectory(build_dir)
351 self.WriteFile(mb_type_path, new_mb_type)
352
Dirk Pranke0fd41bcd2015-06-19 00:05:50353 def RunGNGen(self, vals):
354 path = self.args.path[0]
355
dpranked1fba482015-04-14 20:54:51356 cmd = self.GNCmd('gen', path, vals['gn_args'])
dpranke74559b52015-06-10 21:20:39357
358 swarming_targets = []
359 if self.args.swarming_targets_file:
360 # We need GN to generate the list of runtime dependencies for
361 # the compile targets listed (one per line) in the file so
362 # we can run them via swarming. We use ninja_to_gn.pyl to convert
363 # the compile targets to the matching GN labels.
364 contents = self.ReadFile(self.args.swarming_targets_file)
365 swarming_targets = contents.splitlines()
dpranke8c2cfd32015-09-17 20:12:33366 gn_isolate_map = ast.literal_eval(self.ReadFile(self.PathJoin(
dprankea55584f12015-07-22 00:52:47367 self.chromium_src_dir, 'testing', 'buildbot', 'gn_isolate_map.pyl')))
dpranke74559b52015-06-10 21:20:39368 gn_labels = []
369 for target in swarming_targets:
dprankea55584f12015-07-22 00:52:47370 if not target in gn_isolate_map:
dpranke74559b52015-06-10 21:20:39371 raise MBErr('test target "%s" not found in %s' %
dprankea55584f12015-07-22 00:52:47372 (target, '//testing/buildbot/gn_isolate_map.pyl'))
373 gn_labels.append(gn_isolate_map[target]['label'])
dpranke74559b52015-06-10 21:20:39374
375 gn_runtime_deps_path = self.ToAbsPath(path, 'runtime_deps')
dprankec3441d12015-06-23 23:01:35376
377 # Since GN hasn't run yet, the build directory may not even exist.
378 self.MaybeMakeDirectory(self.ToAbsPath(path))
379
dpranke74559b52015-06-10 21:20:39380 self.WriteFile(gn_runtime_deps_path, '\n'.join(gn_labels) + '\n')
381 cmd.append('--runtime-deps-list-file=%s' % gn_runtime_deps_path)
382
dprankefe4602312015-04-08 16:20:35383 ret, _, _ = self.Run(cmd)
dprankee0547cd2015-09-15 01:27:40384 if ret:
385 # If `gn gen` failed, we should exit early rather than trying to
386 # generate isolates. Run() will have already logged any error output.
387 self.Print('GN gen failed: %d' % ret)
388 return ret
dpranke74559b52015-06-10 21:20:39389
390 for target in swarming_targets:
dprankedbdd9d82015-08-12 21:18:18391 if gn_isolate_map[target]['type'] == 'gpu_browser_test':
392 runtime_deps_target = 'browser_tests'
dpranke6abd8652015-08-28 03:21:11393 elif gn_isolate_map[target]['type'] == 'script':
394 # For script targets, the build target is usually a group,
395 # for which gn generates the runtime_deps next to the stamp file
396 # for the label, which lives under the obj/ directory.
397 label = gn_isolate_map[target]['label']
398 runtime_deps_target = 'obj/%s.stamp' % label.replace(':', '/')
dpranke34bd39d2015-06-24 02:36:52399 else:
dprankedbdd9d82015-08-12 21:18:18400 runtime_deps_target = target
dpranke8c2cfd32015-09-17 20:12:33401 if self.platform == 'win32':
dprankedbdd9d82015-08-12 21:18:18402 deps_path = self.ToAbsPath(path,
403 runtime_deps_target + '.exe.runtime_deps')
404 else:
405 deps_path = self.ToAbsPath(path,
406 runtime_deps_target + '.runtime_deps')
dpranke74559b52015-06-10 21:20:39407 if not self.Exists(deps_path):
408 raise MBErr('did not generate %s' % deps_path)
409
dprankea55584f12015-07-22 00:52:47410 command, extra_files = self.GetIsolateCommand(target, vals,
411 gn_isolate_map)
dpranked5b2b9432015-06-23 16:55:30412
413 runtime_deps = self.ReadFile(deps_path).splitlines()
414
415 isolate_path = self.ToAbsPath(path, target + '.isolate')
416 self.WriteFile(isolate_path,
417 pprint.pformat({
418 'variables': {
419 'command': command,
420 'files': sorted(runtime_deps + extra_files),
dpranked5b2b9432015-06-23 16:55:30421 }
422 }) + '\n')
423
424 self.WriteJSON(
425 {
426 'args': [
427 '--isolated',
dpranke8c2cfd32015-09-17 20:12:33428 self.ToSrcRelPath('%s%s%s.isolated' % (path, self.sep, target)),
dpranked5b2b9432015-06-23 16:55:30429 '--isolate',
dpranke8c2cfd32015-09-17 20:12:33430 self.ToSrcRelPath('%s%s%s.isolate' % (path, self.sep, target)),
dpranked5b2b9432015-06-23 16:55:30431 ],
432 'dir': self.chromium_src_dir,
433 'version': 1,
434 },
435 isolate_path + 'd.gen.json',
436 )
437
dprankefe4602312015-04-08 16:20:35438 return ret
439
dpranked1fba482015-04-14 20:54:51440 def GNCmd(self, subcommand, path, gn_args=''):
441 if self.platform == 'linux2':
dpranke8c2cfd32015-09-17 20:12:33442 subdir = 'linux64'
dpranked1fba482015-04-14 20:54:51443 elif self.platform == 'darwin':
dpranke8c2cfd32015-09-17 20:12:33444 subdir = 'mac'
dpranked1fba482015-04-14 20:54:51445 else:
dpranke8c2cfd32015-09-17 20:12:33446 subdir = 'win'
447 gn_path = self.PathJoin(self.chromium_src_dir, 'buildtools', subdir, 'gn')
dpranked1fba482015-04-14 20:54:51448
449 cmd = [gn_path, subcommand, path]
dprankeee5b51f62015-04-09 00:03:22450 gn_args = gn_args.replace("$(goma_dir)", self.args.goma_dir)
dprankefe4602312015-04-08 16:20:35451 if gn_args:
452 cmd.append('--args=%s' % gn_args)
453 return cmd
454
Dirk Pranke0fd41bcd2015-06-19 00:05:50455 def RunGYPGen(self, vals):
456 path = self.args.path[0]
457
dpranke8c2cfd32015-09-17 20:12:33458 output_dir = self.ParseGYPConfigPath(path)
459 cmd = self.GYPCmd(output_dir, vals['gyp_defines'])
dprankeedc49c382015-08-14 02:32:59460 env = None
461 if vals['gyp_crosscompile']:
462 if self.args.verbose:
463 self.Print('Setting GYP_CROSSCOMPILE=1 in the environment')
464 env = os.environ.copy()
465 env['GYP_CROSSCOMPILE'] = '1'
466 ret, _, _ = self.Run(cmd, env=env)
dprankefe4602312015-04-08 16:20:35467 return ret
468
469 def RunGYPAnalyze(self, vals):
dpranke8c2cfd32015-09-17 20:12:33470 output_dir = self.ParseGYPConfigPath(self.args.path[0])
dprankecda00332015-04-11 04:18:32471 if self.args.verbose:
Dirk Pranke953b27b2015-08-11 03:59:13472 inp = self.ReadInputJSON(['files', 'targets'])
dprankecda00332015-04-11 04:18:32473 self.Print()
474 self.Print('analyze input:')
475 self.PrintJSON(inp)
476 self.Print()
477
dpranke8c2cfd32015-09-17 20:12:33478 cmd = self.GYPCmd(output_dir, vals['gyp_defines'])
dpranke1d306312015-08-11 21:17:33479 cmd.extend(['-f', 'analyzer',
480 '-G', 'config_path=%s' % self.args.input_path[0],
dprankefe4602312015-04-08 16:20:35481 '-G', 'analyzer_output_path=%s' % self.args.output_path[0]])
482 ret, _, _ = self.Run(cmd)
dprankecda00332015-04-11 04:18:32483 if not ret and self.args.verbose:
484 outp = json.loads(self.ReadFile(self.args.output_path[0]))
485 self.Print()
486 self.Print('analyze output:')
dpranke74559b52015-06-10 21:20:39487 self.PrintJSON(outp)
dprankecda00332015-04-11 04:18:32488 self.Print()
489
dprankefe4602312015-04-08 16:20:35490 return ret
491
dprankea55584f12015-07-22 00:52:47492 def GetIsolateCommand(self, target, vals, gn_isolate_map):
dpranked8113582015-06-05 20:08:25493 # This needs to mirror the settings in //build/config/ui.gni:
494 # use_x11 = is_linux && !use_ozone.
495 # TODO(dpranke): Figure out how to keep this in sync better.
dpranke8c2cfd32015-09-17 20:12:33496 use_x11 = (self.platform == 'linux2' and
dpranked8113582015-06-05 20:08:25497 not 'target_os="android"' in vals['gn_args'] and
498 not 'use_ozone=true' in vals['gn_args'])
499
500 asan = 'is_asan=true' in vals['gn_args']
501 msan = 'is_msan=true' in vals['gn_args']
502 tsan = 'is_tsan=true' in vals['gn_args']
503
dpranke8c2cfd32015-09-17 20:12:33504 executable_suffix = '.exe' if self.platform == 'win32' else ''
dpranked8113582015-06-05 20:08:25505
dprankea55584f12015-07-22 00:52:47506 test_type = gn_isolate_map[target]['type']
507 cmdline = []
508 extra_files = []
dpranked8113582015-06-05 20:08:25509
dprankea55584f12015-07-22 00:52:47510 if use_x11 and test_type == 'windowed_test_launcher':
511 extra_files = [
512 'xdisplaycheck',
dpranked8113582015-06-05 20:08:25513 '../../testing/test_env.py',
dprankea55584f12015-07-22 00:52:47514 '../../testing/xvfb.py',
515 ]
516 cmdline = [
517 '../../testing/xvfb.py',
518 '.',
519 './' + str(target),
520 '--brave-new-test-launcher',
521 '--test-launcher-bot-mode',
522 '--asan=%d' % asan,
523 '--msan=%d' % msan,
524 '--tsan=%d' % tsan,
525 ]
526 elif test_type in ('windowed_test_launcher', 'console_test_launcher'):
527 extra_files = [
528 '../../testing/test_env.py'
529 ]
530 cmdline = [
531 '../../testing/test_env.py',
dpranked8113582015-06-05 20:08:25532 './' + str(target) + executable_suffix,
533 '--brave-new-test-launcher',
534 '--test-launcher-bot-mode',
535 '--asan=%d' % asan,
536 '--msan=%d' % msan,
537 '--tsan=%d' % tsan,
dprankea55584f12015-07-22 00:52:47538 ]
dprankedbdd9d82015-08-12 21:18:18539 elif test_type == 'gpu_browser_test':
540 extra_files = [
541 '../../testing/test_env.py'
542 ]
543 gtest_filter = gn_isolate_map[target]['gtest_filter']
544 cmdline = [
545 '../../testing/test_env.py',
dpranke6abd8652015-08-28 03:21:11546 './browser_tests' + executable_suffix,
dprankedbdd9d82015-08-12 21:18:18547 '--test-launcher-bot-mode',
548 '--enable-gpu',
549 '--test-launcher-jobs=1',
550 '--gtest_filter=%s' % gtest_filter,
551 ]
dpranke6abd8652015-08-28 03:21:11552 elif test_type == 'script':
553 extra_files = [
554 '../../testing/test_env.py'
555 ]
556 cmdline = [
557 '../../testing/test_env.py',
558 ] + ['../../' + self.ToSrcRelPath(gn_isolate_map[target]['script'])]
dprankea55584f12015-07-22 00:52:47559 elif test_type in ('raw'):
560 extra_files = []
561 cmdline = [
562 './' + str(target) + executable_suffix,
563 ] + gn_isolate_map[target].get('args')
dpranked8113582015-06-05 20:08:25564
dprankea55584f12015-07-22 00:52:47565 else:
566 self.WriteFailureAndRaise('No command line for %s found (test type %s).'
567 % (target, test_type), output_path=None)
dpranked8113582015-06-05 20:08:25568
569 return cmdline, extra_files
570
dpranke74559b52015-06-10 21:20:39571 def ToAbsPath(self, build_path, *comps):
dpranke8c2cfd32015-09-17 20:12:33572 return self.PathJoin(self.chromium_src_dir,
573 self.ToSrcRelPath(build_path),
574 *comps)
dpranked8113582015-06-05 20:08:25575
dprankeee5b51f62015-04-09 00:03:22576 def ToSrcRelPath(self, path):
577 """Returns a relative path from the top of the repo."""
578 # TODO: Support normal paths in addition to source-absolute paths.
dprankefe4602312015-04-08 16:20:35579 assert(path.startswith('//'))
dpranke8c2cfd32015-09-17 20:12:33580 return path[2:].replace('/', self.sep)
dprankefe4602312015-04-08 16:20:35581
582 def ParseGYPConfigPath(self, path):
dprankeee5b51f62015-04-09 00:03:22583 rpath = self.ToSrcRelPath(path)
dpranke8c2cfd32015-09-17 20:12:33584 output_dir, _, _ = rpath.rpartition(self.sep)
585 return output_dir
dprankefe4602312015-04-08 16:20:35586
dpranke8c2cfd32015-09-17 20:12:33587 def GYPCmd(self, output_dir, gyp_defines):
dprankefe4602312015-04-08 16:20:35588 gyp_defines = gyp_defines.replace("$(goma_dir)", self.args.goma_dir)
589 cmd = [
dpranke8c2cfd32015-09-17 20:12:33590 self.executable,
591 self.PathJoin('build', 'gyp_chromium'),
dprankefe4602312015-04-08 16:20:35592 '-G',
593 'output_dir=' + output_dir,
dprankefe4602312015-04-08 16:20:35594 ]
595 for d in shlex.split(gyp_defines):
596 cmd += ['-D', d]
597 return cmd
598
Dirk Pranke0fd41bcd2015-06-19 00:05:50599 def RunGNAnalyze(self, vals):
600 # analyze runs before 'gn gen' now, so we need to run gn gen
601 # in order to ensure that we have a build directory.
602 ret = self.RunGNGen(vals)
603 if ret:
604 return ret
605
dpranked8113582015-06-05 20:08:25606 inp = self.ReadInputJSON(['files', 'targets'])
dprankecda00332015-04-11 04:18:32607 if self.args.verbose:
608 self.Print()
609 self.Print('analyze input:')
610 self.PrintJSON(inp)
611 self.Print()
612
613 output_path = self.args.output_path[0]
dprankefe4602312015-04-08 16:20:35614
615 # Bail out early if a GN file was modified, since 'gn refs' won't know
616 # what to do about it.
617 if any(f.endswith('.gn') or f.endswith('.gni') for f in inp['files']):
Dirk Prankec965fa32015-04-14 23:46:29618 self.WriteJSON({'status': 'Found dependency (all)'}, output_path)
dprankefe4602312015-04-08 16:20:35619 return 0
620
dprankef61de2f2015-05-14 04:09:56621 # Bail out early if 'all' was asked for, since 'gn refs' won't recognize it.
622 if 'all' in inp['targets']:
dpranke76734662015-04-16 02:17:50623 self.WriteJSON({'status': 'Found dependency (all)'}, output_path)
624 return 0
625
dpranke7c5f614d2015-07-22 23:43:39626 # This shouldn't normally happen, but could due to unusual race conditions,
627 # like a try job that gets scheduled before a patch lands but runs after
628 # the patch has landed.
629 if not inp['files']:
630 self.Print('Warning: No files modified in patch, bailing out early.')
631 self.WriteJSON({'targets': [],
632 'build_targets': [],
633 'status': 'No dependency'}, output_path)
634 return 0
635
Dirk Pranke12ee2db2015-04-14 23:15:32636 ret = 0
dprankef61de2f2015-05-14 04:09:56637 response_file = self.TempFile()
638 response_file.write('\n'.join(inp['files']) + '\n')
639 response_file.close()
640
641 matching_targets = []
642 try:
dpranked1fba482015-04-14 20:54:51643 cmd = self.GNCmd('refs', self.args.path[0]) + [
dpranke067d0142015-05-14 22:52:45644 '@%s' % response_file.name, '--all', '--as=output']
dprankee0547cd2015-09-15 01:27:40645 ret, out, _ = self.Run(cmd, force_verbose=False)
dpranke0b3b7882015-04-24 03:38:12646 if ret and not 'The input matches no targets' in out:
dprankecda00332015-04-11 04:18:32647 self.WriteFailureAndRaise('gn refs returned %d: %s' % (ret, out),
648 output_path)
dpranke8c2cfd32015-09-17 20:12:33649 build_dir = self.ToSrcRelPath(self.args.path[0]) + self.sep
dprankef61de2f2015-05-14 04:09:56650 for output in out.splitlines():
651 build_output = output.replace(build_dir, '')
652 if build_output in inp['targets']:
653 matching_targets.append(build_output)
dpranke067d0142015-05-14 22:52:45654
655 cmd = self.GNCmd('refs', self.args.path[0]) + [
656 '@%s' % response_file.name, '--all']
dprankee0547cd2015-09-15 01:27:40657 ret, out, _ = self.Run(cmd, force_verbose=False)
dpranke067d0142015-05-14 22:52:45658 if ret and not 'The input matches no targets' in out:
659 self.WriteFailureAndRaise('gn refs returned %d: %s' % (ret, out),
660 output_path)
661 for label in out.splitlines():
662 build_target = label[2:]
newt309af8f2015-08-25 22:10:20663 # We want to accept 'chrome/android:chrome_public_apk' and
664 # just 'chrome_public_apk'. This may result in too many targets
dpranke067d0142015-05-14 22:52:45665 # getting built, but we can adjust that later if need be.
666 for input_target in inp['targets']:
667 if (input_target == build_target or
668 build_target.endswith(':' + input_target)):
669 matching_targets.append(input_target)
dprankef61de2f2015-05-14 04:09:56670 finally:
671 self.RemoveFile(response_file.name)
dprankefe4602312015-04-08 16:20:35672
dprankef61de2f2015-05-14 04:09:56673 if matching_targets:
dprankefe4602312015-04-08 16:20:35674 # TODO: it could be that a target X might depend on a target Y
675 # and both would be listed in the input, but we would only need
676 # to specify target X as a build_target (whereas both X and Y are
677 # targets). I'm not sure if that optimization is generally worth it.
dprankee0547cd2015-09-15 01:27:40678 self.WriteJSON({'targets': sorted(set(matching_targets)),
679 'build_targets': sorted(set(matching_targets)),
dprankecda00332015-04-11 04:18:32680 'status': 'Found dependency'}, output_path)
dprankefe4602312015-04-08 16:20:35681 else:
682 self.WriteJSON({'targets': [],
683 'build_targets': [],
dprankecda00332015-04-11 04:18:32684 'status': 'No dependency'}, output_path)
685
dprankee0547cd2015-09-15 01:27:40686 if self.args.verbose:
dprankecda00332015-04-11 04:18:32687 outp = json.loads(self.ReadFile(output_path))
688 self.Print()
689 self.Print('analyze output:')
690 self.PrintJSON(outp)
691 self.Print()
dprankefe4602312015-04-08 16:20:35692
693 return 0
694
dpranked8113582015-06-05 20:08:25695 def ReadInputJSON(self, required_keys):
dprankefe4602312015-04-08 16:20:35696 path = self.args.input_path[0]
dprankecda00332015-04-11 04:18:32697 output_path = self.args.output_path[0]
dprankefe4602312015-04-08 16:20:35698 if not self.Exists(path):
dprankecda00332015-04-11 04:18:32699 self.WriteFailureAndRaise('"%s" does not exist' % path, output_path)
dprankefe4602312015-04-08 16:20:35700
701 try:
702 inp = json.loads(self.ReadFile(path))
703 except Exception as e:
704 self.WriteFailureAndRaise('Failed to read JSON input from "%s": %s' %
dprankecda00332015-04-11 04:18:32705 (path, e), output_path)
dpranked8113582015-06-05 20:08:25706
707 for k in required_keys:
708 if not k in inp:
709 self.WriteFailureAndRaise('input file is missing a "%s" key' % k,
710 output_path)
dprankefe4602312015-04-08 16:20:35711
712 return inp
713
dpranked5b2b9432015-06-23 16:55:30714 def WriteFailureAndRaise(self, msg, output_path):
715 if output_path:
dprankee0547cd2015-09-15 01:27:40716 self.WriteJSON({'error': msg}, output_path, force_verbose=True)
dprankefe4602312015-04-08 16:20:35717 raise MBErr(msg)
718
dprankee0547cd2015-09-15 01:27:40719 def WriteJSON(self, obj, path, force_verbose=False):
dprankecda00332015-04-11 04:18:32720 try:
dprankee0547cd2015-09-15 01:27:40721 self.WriteFile(path, json.dumps(obj, indent=2, sort_keys=True) + '\n',
722 force_verbose=force_verbose)
dprankecda00332015-04-11 04:18:32723 except Exception as e:
724 raise MBErr('Error %s writing to the output path "%s"' %
725 (e, path))
dprankefe4602312015-04-08 16:20:35726
727 def PrintCmd(self, cmd):
dpranke8c2cfd32015-09-17 20:12:33728 if cmd[0] == self.executable:
dprankefe4602312015-04-08 16:20:35729 cmd = ['python'] + cmd[1:]
730 self.Print(*[pipes.quote(c) for c in cmd])
731
dprankecda00332015-04-11 04:18:32732 def PrintJSON(self, obj):
733 self.Print(json.dumps(obj, indent=2, sort_keys=True))
734
dprankefe4602312015-04-08 16:20:35735 def Print(self, *args, **kwargs):
736 # This function largely exists so it can be overridden for testing.
737 print(*args, **kwargs)
738
dprankee0547cd2015-09-15 01:27:40739 def Run(self, cmd, env=None, force_verbose=True):
dprankefe4602312015-04-08 16:20:35740 # This function largely exists so it can be overridden for testing.
dprankee0547cd2015-09-15 01:27:40741 if self.args.dryrun or self.args.verbose or force_verbose:
dprankefe4602312015-04-08 16:20:35742 self.PrintCmd(cmd)
743 if self.args.dryrun:
744 return 0, '', ''
dprankee0547cd2015-09-15 01:27:40745
dprankeedc49c382015-08-14 02:32:59746 ret, out, err = self.Call(cmd, env=env)
dprankee0547cd2015-09-15 01:27:40747 if self.args.verbose or force_verbose:
dprankefe4602312015-04-08 16:20:35748 if out:
dprankeee5b51f62015-04-09 00:03:22749 self.Print(out, end='')
dprankefe4602312015-04-08 16:20:35750 if err:
dprankeee5b51f62015-04-09 00:03:22751 self.Print(err, end='', file=sys.stderr)
dprankefe4602312015-04-08 16:20:35752 return ret, out, err
753
dprankeedc49c382015-08-14 02:32:59754 def Call(self, cmd, env=None):
dprankefe4602312015-04-08 16:20:35755 p = subprocess.Popen(cmd, shell=False, cwd=self.chromium_src_dir,
dprankeedc49c382015-08-14 02:32:59756 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
757 env=env)
dprankefe4602312015-04-08 16:20:35758 out, err = p.communicate()
759 return p.returncode, out, err
760
761 def ExpandUser(self, path):
762 # This function largely exists so it can be overridden for testing.
763 return os.path.expanduser(path)
764
765 def Exists(self, path):
766 # This function largely exists so it can be overridden for testing.
767 return os.path.exists(path)
768
dprankec3441d12015-06-23 23:01:35769 def MaybeMakeDirectory(self, path):
770 try:
771 os.makedirs(path)
772 except OSError, e:
773 if e.errno != errno.EEXIST:
774 raise
775
dpranke8c2cfd32015-09-17 20:12:33776 def PathJoin(self, *comps):
777 # This function largely exists so it can be overriden for testing.
778 return os.path.join(*comps)
779
dprankefe4602312015-04-08 16:20:35780 def ReadFile(self, path):
781 # This function largely exists so it can be overriden for testing.
782 with open(path) as fp:
783 return fp.read()
784
dprankef61de2f2015-05-14 04:09:56785 def RemoveFile(self, path):
786 # This function largely exists so it can be overriden for testing.
787 os.remove(path)
788
dprankec161aa92015-09-14 20:21:13789 def RemoveDirectory(self, abs_path):
dpranke8c2cfd32015-09-17 20:12:33790 if self.platform == 'win32':
dprankec161aa92015-09-14 20:21:13791 # In other places in chromium, we often have to retry this command
792 # because we're worried about other processes still holding on to
793 # file handles, but when MB is invoked, it will be early enough in the
794 # build that their should be no other processes to interfere. We
795 # can change this if need be.
796 self.Run(['cmd.exe', '/c', 'rmdir', '/q', '/s', abs_path])
797 else:
798 shutil.rmtree(abs_path, ignore_errors=True)
799
dprankef61de2f2015-05-14 04:09:56800 def TempFile(self, mode='w'):
801 # This function largely exists so it can be overriden for testing.
802 return tempfile.NamedTemporaryFile(mode=mode, delete=False)
803
dprankee0547cd2015-09-15 01:27:40804 def WriteFile(self, path, contents, force_verbose=False):
dprankefe4602312015-04-08 16:20:35805 # This function largely exists so it can be overriden for testing.
dprankee0547cd2015-09-15 01:27:40806 if self.args.dryrun or self.args.verbose or force_verbose:
dpranked5b2b9432015-06-23 16:55:30807 self.Print('\nWriting """\\\n%s""" to %s.\n' % (contents, path))
dprankefe4602312015-04-08 16:20:35808 with open(path, 'w') as fp:
809 return fp.write(contents)
810
dprankef61de2f2015-05-14 04:09:56811
dprankefe4602312015-04-08 16:20:35812class MBErr(Exception):
813 pass
814
815
816if __name__ == '__main__':
817 try:
818 sys.exit(main(sys.argv[1:]))
819 except MBErr as e:
820 print(e)
821 sys.exit(1)
822 except KeyboardInterrupt:
823 print("interrupted, exiting", stream=sys.stderr)
824 sys.exit(130)