dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1 | #!/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 | |
| 8 | MB is a wrapper script for GYP and GN that can be used to generate build files |
| 9 | for sets of canned configurations and analyze them. |
| 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | |
| 14 | import argparse |
| 15 | import ast |
dpranke | c3441d1 | 2015-06-23 23:01:35 | [diff] [blame] | 16 | import errno |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 17 | import json |
| 18 | import os |
dpranke | 68d1cb18 | 2015-09-17 23:30:00 | [diff] [blame] | 19 | import pipes |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 20 | import pprint |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 21 | import re |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 22 | import shutil |
| 23 | import sys |
| 24 | import subprocess |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 25 | import tempfile |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 26 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 27 | def main(args): |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 28 | mbw = MetaBuildWrapper() |
| 29 | mbw.ParseArgs(args) |
| 30 | return mbw.args.func() |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 31 | |
| 32 | |
| 33 | class 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') |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 40 | self.executable = sys.executable |
dpranke | d1fba48 | 2015-04-14 20:54:51 | [diff] [blame] | 41 | self.platform = sys.platform |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 42 | self.sep = os.sep |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 43 | 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)') |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 68 | subp.add_argument('-v', '--verbose', action='store_true', |
| 69 | help='verbose logging') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 70 | |
| 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) |
tsergeant | f061c99 | 2015-07-16 01:34:00 | [diff] [blame] | 78 | subp.add_argument('--swarming-targets-file', |
| 79 | help='save runtime dependencies for targets listed ' |
| 80 | 'in file.') |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 81 | subp.add_argument('path', nargs=1, |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 82 | 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) |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 94 | subp.add_argument('--swarming-targets-file', |
| 95 | help='save runtime dependencies for targets listed ' |
| 96 | 'in file.') |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 97 | subp.add_argument('path', nargs=1, |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 98 | 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') |
dpranke | a5a77ca | 2015-07-16 23:24:17 | [diff] [blame] | 109 | 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)') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 113 | 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() |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 134 | |
| 135 | self.ClobberIfNeeded(vals) |
| 136 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 137 | if vals['type'] == 'gn': |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 138 | return self.RunGNGen(vals) |
dpranke | 08d2ab1 | 2015-04-24 21:54:20 | [diff] [blame] | 139 | if vals['type'] == 'gyp': |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 140 | return self.RunGYPGen(vals) |
dpranke | 08d2ab1 | 2015-04-24 21:54:20 | [diff] [blame] | 141 | |
| 142 | raise MBErr('Unknown meta-build type "%s"' % vals['type']) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 143 | |
| 144 | def CmdLookup(self): |
| 145 | vals = self.GetConfig() |
| 146 | if vals['type'] == 'gn': |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 147 | cmd = self.GNCmd('gen', '_path_', vals['gn_args']) |
| 148 | env = None |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 149 | elif vals['type'] == 'gyp': |
dpranke | edc49c38 | 2015-08-14 02:32:59 | [diff] [blame] | 150 | if vals['gyp_crosscompile']: |
| 151 | self.Print('GYP_CROSSCOMPILE=1') |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 152 | cmd, env = self.GYPCmd('_path_', vals['gyp_defines']) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 153 | else: |
| 154 | raise MBErr('Unknown meta-build type "%s"' % vals['type']) |
| 155 | |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 156 | self.PrintCmd(cmd, env) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 157 | return 0 |
| 158 | |
| 159 | def CmdHelp(self): |
| 160 | if self.args.subcommand: |
| 161 | self.ParseArgs([self.args.subcommand, '--help']) |
| 162 | else: |
| 163 | self.ParseArgs(['--help']) |
| 164 | |
| 165 | def CmdValidate(self): |
| 166 | errs = [] |
| 167 | |
| 168 | # Read the file to make sure it parses. |
| 169 | self.ReadConfigFile() |
| 170 | |
| 171 | # Figure out the whole list of configs and ensure that no config is |
| 172 | # listed in more than one category. |
| 173 | all_configs = {} |
| 174 | for config in self.common_dev_configs: |
| 175 | all_configs[config] = 'common_dev_configs' |
| 176 | for config in self.private_configs: |
| 177 | if config in all_configs: |
| 178 | errs.append('config "%s" listed in "private_configs" also ' |
| 179 | 'listed in "%s"' % (config, all_configs['config'])) |
| 180 | else: |
| 181 | all_configs[config] = 'private_configs' |
| 182 | for config in self.unsupported_configs: |
| 183 | if config in all_configs: |
| 184 | errs.append('config "%s" listed in "unsupported_configs" also ' |
| 185 | 'listed in "%s"' % (config, all_configs['config'])) |
| 186 | else: |
| 187 | all_configs[config] = 'unsupported_configs' |
| 188 | |
| 189 | for master in self.masters: |
| 190 | for builder in self.masters[master]: |
| 191 | config = self.masters[master][builder] |
| 192 | if config in all_configs and all_configs[config] not in self.masters: |
| 193 | errs.append('Config "%s" used by a bot is also listed in "%s".' % |
| 194 | (config, all_configs[config])) |
| 195 | else: |
| 196 | all_configs[config] = master |
| 197 | |
| 198 | # Check that every referenced config actually exists. |
| 199 | for config, loc in all_configs.items(): |
| 200 | if not config in self.configs: |
| 201 | errs.append('Unknown config "%s" referenced from "%s".' % |
| 202 | (config, loc)) |
| 203 | |
| 204 | # Check that every actual config is actually referenced. |
| 205 | for config in self.configs: |
| 206 | if not config in all_configs: |
| 207 | errs.append('Unused config "%s".' % config) |
| 208 | |
| 209 | # Figure out the whole list of mixins, and check that every mixin |
| 210 | # listed by a config or another mixin actually exists. |
| 211 | referenced_mixins = set() |
| 212 | for config, mixins in self.configs.items(): |
| 213 | for mixin in mixins: |
| 214 | if not mixin in self.mixins: |
| 215 | errs.append('Unknown mixin "%s" referenced by config "%s".' % |
| 216 | (mixin, config)) |
| 217 | referenced_mixins.add(mixin) |
| 218 | |
| 219 | for mixin in self.mixins: |
| 220 | for sub_mixin in self.mixins[mixin].get('mixins', []): |
| 221 | if not sub_mixin in self.mixins: |
| 222 | errs.append('Unknown mixin "%s" referenced by mixin "%s".' % |
| 223 | (sub_mixin, mixin)) |
| 224 | referenced_mixins.add(sub_mixin) |
| 225 | |
| 226 | # Check that every mixin defined is actually referenced somewhere. |
| 227 | for mixin in self.mixins: |
| 228 | if not mixin in referenced_mixins: |
| 229 | errs.append('Unreferenced mixin "%s".' % mixin) |
| 230 | |
| 231 | if errs: |
dpranke | 4323c8063 | 2015-08-10 22:53:54 | [diff] [blame] | 232 | raise MBErr(('mb config file %s has problems:' % self.args.config_file) + |
dpranke | a3326787 | 2015-08-12 15:45:17 | [diff] [blame] | 233 | '\n ' + '\n '.join(errs)) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 234 | |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 235 | self.Print('mb config file %s looks ok.' % self.args.config_file) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 236 | return 0 |
| 237 | |
| 238 | def GetConfig(self): |
| 239 | self.ReadConfigFile() |
| 240 | config = self.ConfigFromArgs() |
| 241 | if not config in self.configs: |
| 242 | raise MBErr('Config "%s" not found in %s' % |
| 243 | (config, self.args.config_file)) |
| 244 | |
| 245 | return self.FlattenConfig(config) |
| 246 | |
| 247 | def ReadConfigFile(self): |
| 248 | if not self.Exists(self.args.config_file): |
| 249 | raise MBErr('config file not found at %s' % self.args.config_file) |
| 250 | |
| 251 | try: |
| 252 | contents = ast.literal_eval(self.ReadFile(self.args.config_file)) |
| 253 | except SyntaxError as e: |
| 254 | raise MBErr('Failed to parse config file "%s": %s' % |
| 255 | (self.args.config_file, e)) |
| 256 | |
| 257 | self.common_dev_configs = contents['common_dev_configs'] |
| 258 | self.configs = contents['configs'] |
| 259 | self.masters = contents['masters'] |
| 260 | self.mixins = contents['mixins'] |
| 261 | self.private_configs = contents['private_configs'] |
| 262 | self.unsupported_configs = contents['unsupported_configs'] |
| 263 | |
| 264 | def ConfigFromArgs(self): |
| 265 | if self.args.config: |
| 266 | if self.args.master or self.args.builder: |
| 267 | raise MBErr('Can not specific both -c/--config and -m/--master or ' |
| 268 | '-b/--builder') |
| 269 | |
| 270 | return self.args.config |
| 271 | |
| 272 | if not self.args.master or not self.args.builder: |
| 273 | raise MBErr('Must specify either -c/--config or ' |
| 274 | '(-m/--master and -b/--builder)') |
| 275 | |
| 276 | if not self.args.master in self.masters: |
| 277 | raise MBErr('Master name "%s" not found in "%s"' % |
| 278 | (self.args.master, self.args.config_file)) |
| 279 | |
| 280 | if not self.args.builder in self.masters[self.args.master]: |
| 281 | raise MBErr('Builder name "%s" not found under masters[%s] in "%s"' % |
| 282 | (self.args.builder, self.args.master, self.args.config_file)) |
| 283 | |
| 284 | return self.masters[self.args.master][self.args.builder] |
| 285 | |
| 286 | def FlattenConfig(self, config): |
| 287 | mixins = self.configs[config] |
| 288 | vals = { |
| 289 | 'type': None, |
| 290 | 'gn_args': [], |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 291 | 'gyp_defines': '', |
dpranke | edc49c38 | 2015-08-14 02:32:59 | [diff] [blame] | 292 | 'gyp_crosscompile': False, |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | visited = [] |
| 296 | self.FlattenMixins(mixins, vals, visited) |
| 297 | return vals |
| 298 | |
| 299 | def FlattenMixins(self, mixins, vals, visited): |
| 300 | for m in mixins: |
| 301 | if m not in self.mixins: |
| 302 | raise MBErr('Unknown mixin "%s"' % m) |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 303 | |
| 304 | # TODO: check for cycles in mixins. |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 305 | |
| 306 | visited.append(m) |
| 307 | |
| 308 | mixin_vals = self.mixins[m] |
| 309 | if 'type' in mixin_vals: |
| 310 | vals['type'] = mixin_vals['type'] |
| 311 | if 'gn_args' in mixin_vals: |
| 312 | if vals['gn_args']: |
| 313 | vals['gn_args'] += ' ' + mixin_vals['gn_args'] |
| 314 | else: |
| 315 | vals['gn_args'] = mixin_vals['gn_args'] |
dpranke | edc49c38 | 2015-08-14 02:32:59 | [diff] [blame] | 316 | if 'gyp_crosscompile' in mixin_vals: |
| 317 | vals['gyp_crosscompile'] = mixin_vals['gyp_crosscompile'] |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 318 | if 'gyp_defines' in mixin_vals: |
| 319 | if vals['gyp_defines']: |
| 320 | vals['gyp_defines'] += ' ' + mixin_vals['gyp_defines'] |
| 321 | else: |
| 322 | vals['gyp_defines'] = mixin_vals['gyp_defines'] |
| 323 | if 'mixins' in mixin_vals: |
| 324 | self.FlattenMixins(mixin_vals['mixins'], vals, visited) |
| 325 | return vals |
| 326 | |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 327 | def ClobberIfNeeded(self, vals): |
| 328 | path = self.args.path[0] |
| 329 | build_dir = self.ToAbsPath(path) |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 330 | mb_type_path = self.PathJoin(build_dir, 'mb_type') |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 331 | needs_clobber = False |
| 332 | new_mb_type = vals['type'] |
| 333 | if self.Exists(build_dir): |
| 334 | if self.Exists(mb_type_path): |
| 335 | old_mb_type = self.ReadFile(mb_type_path) |
| 336 | if old_mb_type != new_mb_type: |
| 337 | self.Print("Build type mismatch: was %s, will be %s, clobbering %s" % |
| 338 | (old_mb_type, new_mb_type, path)) |
| 339 | needs_clobber = True |
| 340 | else: |
| 341 | # There is no 'mb_type' file in the build directory, so this probably |
| 342 | # means that the prior build(s) were not done through mb, and we |
| 343 | # have no idea if this was a GYP build or a GN build. Clobber it |
| 344 | # to be safe. |
| 345 | self.Print("%s/mb_type missing, clobbering to be safe" % path) |
| 346 | needs_clobber = True |
| 347 | |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 348 | if self.args.dryrun: |
| 349 | return |
| 350 | |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 351 | if needs_clobber: |
| 352 | self.RemoveDirectory(build_dir) |
| 353 | |
| 354 | self.MaybeMakeDirectory(build_dir) |
| 355 | self.WriteFile(mb_type_path, new_mb_type) |
| 356 | |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 357 | def RunGNGen(self, vals): |
| 358 | path = self.args.path[0] |
| 359 | |
dpranke | b218d91 | 2015-09-18 19:07:00 | [diff] [blame] | 360 | cmd = self.GNCmd('gen', path, vals['gn_args'], extra_args=['--check']) |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 361 | |
| 362 | swarming_targets = [] |
| 363 | if self.args.swarming_targets_file: |
| 364 | # We need GN to generate the list of runtime dependencies for |
| 365 | # the compile targets listed (one per line) in the file so |
| 366 | # we can run them via swarming. We use ninja_to_gn.pyl to convert |
| 367 | # the compile targets to the matching GN labels. |
| 368 | contents = self.ReadFile(self.args.swarming_targets_file) |
| 369 | swarming_targets = contents.splitlines() |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 370 | gn_isolate_map = ast.literal_eval(self.ReadFile(self.PathJoin( |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 371 | self.chromium_src_dir, 'testing', 'buildbot', 'gn_isolate_map.pyl'))) |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 372 | gn_labels = [] |
| 373 | for target in swarming_targets: |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 374 | if not target in gn_isolate_map: |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 375 | raise MBErr('test target "%s" not found in %s' % |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 376 | (target, '//testing/buildbot/gn_isolate_map.pyl')) |
| 377 | gn_labels.append(gn_isolate_map[target]['label']) |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 378 | |
| 379 | gn_runtime_deps_path = self.ToAbsPath(path, 'runtime_deps') |
dpranke | c3441d1 | 2015-06-23 23:01:35 | [diff] [blame] | 380 | |
| 381 | # Since GN hasn't run yet, the build directory may not even exist. |
| 382 | self.MaybeMakeDirectory(self.ToAbsPath(path)) |
| 383 | |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 384 | self.WriteFile(gn_runtime_deps_path, '\n'.join(gn_labels) + '\n') |
| 385 | cmd.append('--runtime-deps-list-file=%s' % gn_runtime_deps_path) |
| 386 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 387 | ret, _, _ = self.Run(cmd) |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 388 | if ret: |
| 389 | # If `gn gen` failed, we should exit early rather than trying to |
| 390 | # generate isolates. Run() will have already logged any error output. |
| 391 | self.Print('GN gen failed: %d' % ret) |
| 392 | return ret |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 393 | |
| 394 | for target in swarming_targets: |
dpranke | dbdd9d8 | 2015-08-12 21:18:18 | [diff] [blame] | 395 | if gn_isolate_map[target]['type'] == 'gpu_browser_test': |
| 396 | runtime_deps_target = 'browser_tests' |
dpranke | 6abd865 | 2015-08-28 03:21:11 | [diff] [blame] | 397 | elif gn_isolate_map[target]['type'] == 'script': |
| 398 | # For script targets, the build target is usually a group, |
| 399 | # for which gn generates the runtime_deps next to the stamp file |
| 400 | # for the label, which lives under the obj/ directory. |
| 401 | label = gn_isolate_map[target]['label'] |
| 402 | runtime_deps_target = 'obj/%s.stamp' % label.replace(':', '/') |
dpranke | 34bd39d | 2015-06-24 02:36:52 | [diff] [blame] | 403 | else: |
dpranke | dbdd9d8 | 2015-08-12 21:18:18 | [diff] [blame] | 404 | runtime_deps_target = target |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 405 | if self.platform == 'win32': |
dpranke | dbdd9d8 | 2015-08-12 21:18:18 | [diff] [blame] | 406 | deps_path = self.ToAbsPath(path, |
| 407 | runtime_deps_target + '.exe.runtime_deps') |
| 408 | else: |
| 409 | deps_path = self.ToAbsPath(path, |
| 410 | runtime_deps_target + '.runtime_deps') |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 411 | if not self.Exists(deps_path): |
| 412 | raise MBErr('did not generate %s' % deps_path) |
| 413 | |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 414 | command, extra_files = self.GetIsolateCommand(target, vals, |
| 415 | gn_isolate_map) |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 416 | |
| 417 | runtime_deps = self.ReadFile(deps_path).splitlines() |
| 418 | |
| 419 | isolate_path = self.ToAbsPath(path, target + '.isolate') |
| 420 | self.WriteFile(isolate_path, |
| 421 | pprint.pformat({ |
| 422 | 'variables': { |
| 423 | 'command': command, |
| 424 | 'files': sorted(runtime_deps + extra_files), |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 425 | } |
| 426 | }) + '\n') |
| 427 | |
| 428 | self.WriteJSON( |
| 429 | { |
| 430 | 'args': [ |
| 431 | '--isolated', |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 432 | self.ToSrcRelPath('%s%s%s.isolated' % (path, self.sep, target)), |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 433 | '--isolate', |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 434 | self.ToSrcRelPath('%s%s%s.isolate' % (path, self.sep, target)), |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 435 | ], |
| 436 | 'dir': self.chromium_src_dir, |
| 437 | 'version': 1, |
| 438 | }, |
| 439 | isolate_path + 'd.gen.json', |
| 440 | ) |
| 441 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 442 | return ret |
| 443 | |
dpranke | b218d91 | 2015-09-18 19:07:00 | [diff] [blame] | 444 | def GNCmd(self, subcommand, path, gn_args='', extra_args=None): |
dpranke | d1fba48 | 2015-04-14 20:54:51 | [diff] [blame] | 445 | if self.platform == 'linux2': |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 446 | subdir = 'linux64' |
dpranke | d1fba48 | 2015-04-14 20:54:51 | [diff] [blame] | 447 | elif self.platform == 'darwin': |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 448 | subdir = 'mac' |
dpranke | d1fba48 | 2015-04-14 20:54:51 | [diff] [blame] | 449 | else: |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 450 | subdir = 'win' |
| 451 | gn_path = self.PathJoin(self.chromium_src_dir, 'buildtools', subdir, 'gn') |
dpranke | d1fba48 | 2015-04-14 20:54:51 | [diff] [blame] | 452 | |
| 453 | cmd = [gn_path, subcommand, path] |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 454 | gn_args = gn_args.replace("$(goma_dir)", self.args.goma_dir) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 455 | if gn_args: |
| 456 | cmd.append('--args=%s' % gn_args) |
dpranke | b218d91 | 2015-09-18 19:07:00 | [diff] [blame] | 457 | if extra_args: |
| 458 | cmd.extend(extra_args) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 459 | return cmd |
| 460 | |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 461 | def RunGYPGen(self, vals): |
| 462 | path = self.args.path[0] |
| 463 | |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 464 | output_dir = self.ParseGYPConfigPath(path) |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 465 | cmd, env = self.GYPCmd(output_dir, vals['gyp_defines']) |
dpranke | edc49c38 | 2015-08-14 02:32:59 | [diff] [blame] | 466 | if vals['gyp_crosscompile']: |
dpranke | edc49c38 | 2015-08-14 02:32:59 | [diff] [blame] | 467 | env['GYP_CROSSCOMPILE'] = '1' |
| 468 | ret, _, _ = self.Run(cmd, env=env) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 469 | return ret |
| 470 | |
| 471 | def RunGYPAnalyze(self, vals): |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 472 | output_dir = self.ParseGYPConfigPath(self.args.path[0]) |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 473 | if self.args.verbose: |
Dirk Pranke | 953b27b | 2015-08-11 03:59:13 | [diff] [blame] | 474 | inp = self.ReadInputJSON(['files', 'targets']) |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 475 | self.Print() |
| 476 | self.Print('analyze input:') |
| 477 | self.PrintJSON(inp) |
| 478 | self.Print() |
| 479 | |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 480 | cmd, env = self.GYPCmd(output_dir, vals['gyp_defines']) |
dpranke | 1d30631 | 2015-08-11 21:17:33 | [diff] [blame] | 481 | cmd.extend(['-f', 'analyzer', |
| 482 | '-G', 'config_path=%s' % self.args.input_path[0], |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 483 | '-G', 'analyzer_output_path=%s' % self.args.output_path[0]]) |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 484 | ret, _, _ = self.Run(cmd, env=env) |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 485 | if not ret and self.args.verbose: |
| 486 | outp = json.loads(self.ReadFile(self.args.output_path[0])) |
| 487 | self.Print() |
| 488 | self.Print('analyze output:') |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 489 | self.PrintJSON(outp) |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 490 | self.Print() |
| 491 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 492 | return ret |
| 493 | |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 494 | def GetIsolateCommand(self, target, vals, gn_isolate_map): |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 495 | # This needs to mirror the settings in //build/config/ui.gni: |
| 496 | # use_x11 = is_linux && !use_ozone. |
| 497 | # TODO(dpranke): Figure out how to keep this in sync better. |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 498 | use_x11 = (self.platform == 'linux2' and |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 499 | not 'target_os="android"' in vals['gn_args'] and |
| 500 | not 'use_ozone=true' in vals['gn_args']) |
| 501 | |
| 502 | asan = 'is_asan=true' in vals['gn_args'] |
| 503 | msan = 'is_msan=true' in vals['gn_args'] |
| 504 | tsan = 'is_tsan=true' in vals['gn_args'] |
| 505 | |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 506 | executable_suffix = '.exe' if self.platform == 'win32' else '' |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 507 | |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 508 | test_type = gn_isolate_map[target]['type'] |
| 509 | cmdline = [] |
| 510 | extra_files = [] |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 511 | |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 512 | if use_x11 and test_type == 'windowed_test_launcher': |
| 513 | extra_files = [ |
| 514 | 'xdisplaycheck', |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 515 | '../../testing/test_env.py', |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 516 | '../../testing/xvfb.py', |
| 517 | ] |
| 518 | cmdline = [ |
| 519 | '../../testing/xvfb.py', |
| 520 | '.', |
| 521 | './' + str(target), |
| 522 | '--brave-new-test-launcher', |
| 523 | '--test-launcher-bot-mode', |
| 524 | '--asan=%d' % asan, |
| 525 | '--msan=%d' % msan, |
| 526 | '--tsan=%d' % tsan, |
| 527 | ] |
| 528 | elif test_type in ('windowed_test_launcher', 'console_test_launcher'): |
| 529 | extra_files = [ |
| 530 | '../../testing/test_env.py' |
| 531 | ] |
| 532 | cmdline = [ |
| 533 | '../../testing/test_env.py', |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 534 | './' + str(target) + executable_suffix, |
| 535 | '--brave-new-test-launcher', |
| 536 | '--test-launcher-bot-mode', |
| 537 | '--asan=%d' % asan, |
| 538 | '--msan=%d' % msan, |
| 539 | '--tsan=%d' % tsan, |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 540 | ] |
dpranke | dbdd9d8 | 2015-08-12 21:18:18 | [diff] [blame] | 541 | elif test_type == 'gpu_browser_test': |
| 542 | extra_files = [ |
| 543 | '../../testing/test_env.py' |
| 544 | ] |
| 545 | gtest_filter = gn_isolate_map[target]['gtest_filter'] |
| 546 | cmdline = [ |
| 547 | '../../testing/test_env.py', |
dpranke | 6abd865 | 2015-08-28 03:21:11 | [diff] [blame] | 548 | './browser_tests' + executable_suffix, |
dpranke | dbdd9d8 | 2015-08-12 21:18:18 | [diff] [blame] | 549 | '--test-launcher-bot-mode', |
| 550 | '--enable-gpu', |
| 551 | '--test-launcher-jobs=1', |
| 552 | '--gtest_filter=%s' % gtest_filter, |
| 553 | ] |
dpranke | 6abd865 | 2015-08-28 03:21:11 | [diff] [blame] | 554 | elif test_type == 'script': |
| 555 | extra_files = [ |
| 556 | '../../testing/test_env.py' |
| 557 | ] |
| 558 | cmdline = [ |
| 559 | '../../testing/test_env.py', |
| 560 | ] + ['../../' + self.ToSrcRelPath(gn_isolate_map[target]['script'])] |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 561 | elif test_type in ('raw'): |
| 562 | extra_files = [] |
| 563 | cmdline = [ |
| 564 | './' + str(target) + executable_suffix, |
| 565 | ] + gn_isolate_map[target].get('args') |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 566 | |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 567 | else: |
| 568 | self.WriteFailureAndRaise('No command line for %s found (test type %s).' |
| 569 | % (target, test_type), output_path=None) |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 570 | |
| 571 | return cmdline, extra_files |
| 572 | |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 573 | def ToAbsPath(self, build_path, *comps): |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 574 | return self.PathJoin(self.chromium_src_dir, |
| 575 | self.ToSrcRelPath(build_path), |
| 576 | *comps) |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 577 | |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 578 | def ToSrcRelPath(self, path): |
| 579 | """Returns a relative path from the top of the repo.""" |
| 580 | # TODO: Support normal paths in addition to source-absolute paths. |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 581 | assert(path.startswith('//')) |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 582 | return path[2:].replace('/', self.sep) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 583 | |
| 584 | def ParseGYPConfigPath(self, path): |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 585 | rpath = self.ToSrcRelPath(path) |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 586 | output_dir, _, _ = rpath.rpartition(self.sep) |
| 587 | return output_dir |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 588 | |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 589 | def GYPCmd(self, output_dir, gyp_defines): |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 590 | goma_dir = self.args.goma_dir |
| 591 | |
| 592 | # GYP uses shlex.split() to split the gyp defines into separate arguments, |
| 593 | # so we can support backslashes and and spaces in arguments by quoting |
| 594 | # them, even on Windows, where this normally wouldn't work. |
| 595 | if '\\' in goma_dir or ' ' in goma_dir: |
| 596 | goma_dir = "'%s'" % goma_dir |
| 597 | gyp_defines = gyp_defines.replace("$(goma_dir)", goma_dir) |
| 598 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 599 | cmd = [ |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 600 | self.executable, |
| 601 | self.PathJoin('build', 'gyp_chromium'), |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 602 | '-G', |
| 603 | 'output_dir=' + output_dir, |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 604 | ] |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 605 | env = os.environ.copy() |
| 606 | env['GYP_DEFINES'] = gyp_defines |
| 607 | return cmd, env |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 608 | |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 609 | def RunGNAnalyze(self, vals): |
| 610 | # analyze runs before 'gn gen' now, so we need to run gn gen |
| 611 | # in order to ensure that we have a build directory. |
| 612 | ret = self.RunGNGen(vals) |
| 613 | if ret: |
| 614 | return ret |
| 615 | |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 616 | inp = self.ReadInputJSON(['files', 'targets']) |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 617 | if self.args.verbose: |
| 618 | self.Print() |
| 619 | self.Print('analyze input:') |
| 620 | self.PrintJSON(inp) |
| 621 | self.Print() |
| 622 | |
| 623 | output_path = self.args.output_path[0] |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 624 | |
| 625 | # Bail out early if a GN file was modified, since 'gn refs' won't know |
| 626 | # what to do about it. |
| 627 | if any(f.endswith('.gn') or f.endswith('.gni') for f in inp['files']): |
Dirk Pranke | c965fa3 | 2015-04-14 23:46:29 | [diff] [blame] | 628 | self.WriteJSON({'status': 'Found dependency (all)'}, output_path) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 629 | return 0 |
| 630 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 631 | # Bail out early if 'all' was asked for, since 'gn refs' won't recognize it. |
| 632 | if 'all' in inp['targets']: |
dpranke | 7673466 | 2015-04-16 02:17:50 | [diff] [blame] | 633 | self.WriteJSON({'status': 'Found dependency (all)'}, output_path) |
| 634 | return 0 |
| 635 | |
dpranke | 7c5f614d | 2015-07-22 23:43:39 | [diff] [blame] | 636 | # This shouldn't normally happen, but could due to unusual race conditions, |
| 637 | # like a try job that gets scheduled before a patch lands but runs after |
| 638 | # the patch has landed. |
| 639 | if not inp['files']: |
| 640 | self.Print('Warning: No files modified in patch, bailing out early.') |
| 641 | self.WriteJSON({'targets': [], |
| 642 | 'build_targets': [], |
| 643 | 'status': 'No dependency'}, output_path) |
| 644 | return 0 |
| 645 | |
Dirk Pranke | 12ee2db | 2015-04-14 23:15:32 | [diff] [blame] | 646 | ret = 0 |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 647 | response_file = self.TempFile() |
| 648 | response_file.write('\n'.join(inp['files']) + '\n') |
| 649 | response_file.close() |
| 650 | |
| 651 | matching_targets = [] |
| 652 | try: |
dpranke | d1fba48 | 2015-04-14 20:54:51 | [diff] [blame] | 653 | cmd = self.GNCmd('refs', self.args.path[0]) + [ |
dpranke | 067d014 | 2015-05-14 22:52:45 | [diff] [blame] | 654 | '@%s' % response_file.name, '--all', '--as=output'] |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 655 | ret, out, _ = self.Run(cmd, force_verbose=False) |
dpranke | 0b3b788 | 2015-04-24 03:38:12 | [diff] [blame] | 656 | if ret and not 'The input matches no targets' in out: |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 657 | self.WriteFailureAndRaise('gn refs returned %d: %s' % (ret, out), |
| 658 | output_path) |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 659 | build_dir = self.ToSrcRelPath(self.args.path[0]) + self.sep |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 660 | for output in out.splitlines(): |
| 661 | build_output = output.replace(build_dir, '') |
| 662 | if build_output in inp['targets']: |
| 663 | matching_targets.append(build_output) |
dpranke | 067d014 | 2015-05-14 22:52:45 | [diff] [blame] | 664 | |
| 665 | cmd = self.GNCmd('refs', self.args.path[0]) + [ |
| 666 | '@%s' % response_file.name, '--all'] |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 667 | ret, out, _ = self.Run(cmd, force_verbose=False) |
dpranke | 067d014 | 2015-05-14 22:52:45 | [diff] [blame] | 668 | if ret and not 'The input matches no targets' in out: |
| 669 | self.WriteFailureAndRaise('gn refs returned %d: %s' % (ret, out), |
| 670 | output_path) |
| 671 | for label in out.splitlines(): |
| 672 | build_target = label[2:] |
newt | 309af8f | 2015-08-25 22:10:20 | [diff] [blame] | 673 | # We want to accept 'chrome/android:chrome_public_apk' and |
| 674 | # just 'chrome_public_apk'. This may result in too many targets |
dpranke | 067d014 | 2015-05-14 22:52:45 | [diff] [blame] | 675 | # getting built, but we can adjust that later if need be. |
| 676 | for input_target in inp['targets']: |
| 677 | if (input_target == build_target or |
| 678 | build_target.endswith(':' + input_target)): |
| 679 | matching_targets.append(input_target) |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 680 | finally: |
| 681 | self.RemoveFile(response_file.name) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 682 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 683 | if matching_targets: |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 684 | # TODO: it could be that a target X might depend on a target Y |
| 685 | # and both would be listed in the input, but we would only need |
| 686 | # to specify target X as a build_target (whereas both X and Y are |
| 687 | # targets). I'm not sure if that optimization is generally worth it. |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 688 | self.WriteJSON({'targets': sorted(set(matching_targets)), |
| 689 | 'build_targets': sorted(set(matching_targets)), |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 690 | 'status': 'Found dependency'}, output_path) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 691 | else: |
| 692 | self.WriteJSON({'targets': [], |
| 693 | 'build_targets': [], |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 694 | 'status': 'No dependency'}, output_path) |
| 695 | |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 696 | if self.args.verbose: |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 697 | outp = json.loads(self.ReadFile(output_path)) |
| 698 | self.Print() |
| 699 | self.Print('analyze output:') |
| 700 | self.PrintJSON(outp) |
| 701 | self.Print() |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 702 | |
| 703 | return 0 |
| 704 | |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 705 | def ReadInputJSON(self, required_keys): |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 706 | path = self.args.input_path[0] |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 707 | output_path = self.args.output_path[0] |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 708 | if not self.Exists(path): |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 709 | self.WriteFailureAndRaise('"%s" does not exist' % path, output_path) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 710 | |
| 711 | try: |
| 712 | inp = json.loads(self.ReadFile(path)) |
| 713 | except Exception as e: |
| 714 | self.WriteFailureAndRaise('Failed to read JSON input from "%s": %s' % |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 715 | (path, e), output_path) |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 716 | |
| 717 | for k in required_keys: |
| 718 | if not k in inp: |
| 719 | self.WriteFailureAndRaise('input file is missing a "%s" key' % k, |
| 720 | output_path) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 721 | |
| 722 | return inp |
| 723 | |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 724 | def WriteFailureAndRaise(self, msg, output_path): |
| 725 | if output_path: |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 726 | self.WriteJSON({'error': msg}, output_path, force_verbose=True) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 727 | raise MBErr(msg) |
| 728 | |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 729 | def WriteJSON(self, obj, path, force_verbose=False): |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 730 | try: |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 731 | self.WriteFile(path, json.dumps(obj, indent=2, sort_keys=True) + '\n', |
| 732 | force_verbose=force_verbose) |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 733 | except Exception as e: |
| 734 | raise MBErr('Error %s writing to the output path "%s"' % |
| 735 | (e, path)) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 736 | |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 737 | def PrintCmd(self, cmd, env): |
| 738 | if self.platform == 'win32': |
| 739 | env_prefix = 'set ' |
| 740 | env_quoter = QuoteForSet |
| 741 | shell_quoter = QuoteForCmd |
| 742 | else: |
| 743 | env_prefix = '' |
| 744 | env_quoter = pipes.quote |
| 745 | shell_quoter = pipes.quote |
| 746 | |
| 747 | def print_env(var): |
| 748 | if env and var in env: |
| 749 | self.Print('%s%s=%s' % (env_prefix, var, env_quoter(env[var]))) |
| 750 | |
| 751 | print_env('GYP_CROSSCOMPILE') |
| 752 | print_env('GYP_DEFINES') |
| 753 | |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 754 | if cmd[0] == self.executable: |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 755 | cmd = ['python'] + cmd[1:] |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 756 | self.Print(*[shell_quoter(arg) for arg in cmd]) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 757 | |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 758 | def PrintJSON(self, obj): |
| 759 | self.Print(json.dumps(obj, indent=2, sort_keys=True)) |
| 760 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 761 | def Print(self, *args, **kwargs): |
| 762 | # This function largely exists so it can be overridden for testing. |
| 763 | print(*args, **kwargs) |
| 764 | |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 765 | def Run(self, cmd, env=None, force_verbose=True): |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 766 | # This function largely exists so it can be overridden for testing. |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 767 | if self.args.dryrun or self.args.verbose or force_verbose: |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 768 | self.PrintCmd(cmd, env) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 769 | if self.args.dryrun: |
| 770 | return 0, '', '' |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 771 | |
dpranke | edc49c38 | 2015-08-14 02:32:59 | [diff] [blame] | 772 | ret, out, err = self.Call(cmd, env=env) |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 773 | if self.args.verbose or force_verbose: |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 774 | if out: |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 775 | self.Print(out, end='') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 776 | if err: |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 777 | self.Print(err, end='', file=sys.stderr) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 778 | return ret, out, err |
| 779 | |
dpranke | edc49c38 | 2015-08-14 02:32:59 | [diff] [blame] | 780 | def Call(self, cmd, env=None): |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 781 | p = subprocess.Popen(cmd, shell=False, cwd=self.chromium_src_dir, |
dpranke | edc49c38 | 2015-08-14 02:32:59 | [diff] [blame] | 782 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 783 | env=env) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 784 | out, err = p.communicate() |
| 785 | return p.returncode, out, err |
| 786 | |
| 787 | def ExpandUser(self, path): |
| 788 | # This function largely exists so it can be overridden for testing. |
| 789 | return os.path.expanduser(path) |
| 790 | |
| 791 | def Exists(self, path): |
| 792 | # This function largely exists so it can be overridden for testing. |
| 793 | return os.path.exists(path) |
| 794 | |
dpranke | c3441d1 | 2015-06-23 23:01:35 | [diff] [blame] | 795 | def MaybeMakeDirectory(self, path): |
| 796 | try: |
| 797 | os.makedirs(path) |
| 798 | except OSError, e: |
| 799 | if e.errno != errno.EEXIST: |
| 800 | raise |
| 801 | |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 802 | def PathJoin(self, *comps): |
| 803 | # This function largely exists so it can be overriden for testing. |
| 804 | return os.path.join(*comps) |
| 805 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 806 | def ReadFile(self, path): |
| 807 | # This function largely exists so it can be overriden for testing. |
| 808 | with open(path) as fp: |
| 809 | return fp.read() |
| 810 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 811 | def RemoveFile(self, path): |
| 812 | # This function largely exists so it can be overriden for testing. |
| 813 | os.remove(path) |
| 814 | |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 815 | def RemoveDirectory(self, abs_path): |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 816 | if self.platform == 'win32': |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 817 | # In other places in chromium, we often have to retry this command |
| 818 | # because we're worried about other processes still holding on to |
| 819 | # file handles, but when MB is invoked, it will be early enough in the |
| 820 | # build that their should be no other processes to interfere. We |
| 821 | # can change this if need be. |
| 822 | self.Run(['cmd.exe', '/c', 'rmdir', '/q', '/s', abs_path]) |
| 823 | else: |
| 824 | shutil.rmtree(abs_path, ignore_errors=True) |
| 825 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 826 | def TempFile(self, mode='w'): |
| 827 | # This function largely exists so it can be overriden for testing. |
| 828 | return tempfile.NamedTemporaryFile(mode=mode, delete=False) |
| 829 | |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 830 | def WriteFile(self, path, contents, force_verbose=False): |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 831 | # This function largely exists so it can be overriden for testing. |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 832 | if self.args.dryrun or self.args.verbose or force_verbose: |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 833 | self.Print('\nWriting """\\\n%s""" to %s.\n' % (contents, path)) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 834 | with open(path, 'w') as fp: |
| 835 | return fp.write(contents) |
| 836 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 837 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 838 | class MBErr(Exception): |
| 839 | pass |
| 840 | |
| 841 | |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame^] | 842 | # See http://goo.gl/l5NPDW and http://goo.gl/4Diozm for the painful |
| 843 | # details of this next section, which handles escaping command lines |
| 844 | # so that they can be copied and pasted into a cmd window. |
| 845 | UNSAFE_FOR_SET = set('^<>&|') |
| 846 | UNSAFE_FOR_CMD = UNSAFE_FOR_SET.union(set('()%')) |
| 847 | ALL_META_CHARS = UNSAFE_FOR_CMD.union(set('"')) |
| 848 | |
| 849 | |
| 850 | def QuoteForSet(arg): |
| 851 | if any(a in UNSAFE_FOR_SET for a in arg): |
| 852 | arg = ''.join('^' + a if a in UNSAFE_FOR_SET else a for a in arg) |
| 853 | return arg |
| 854 | |
| 855 | |
| 856 | def QuoteForCmd(arg): |
| 857 | # First, escape the arg so that CommandLineToArgvW will parse it properly. |
| 858 | # From //tools/gyp/pylib/gyp/msvs_emulation.py:23. |
| 859 | if arg == '' or ' ' in arg or '"' in arg: |
| 860 | quote_re = re.compile(r'(\\*)"') |
| 861 | arg = '"%s"' % (quote_re.sub(lambda mo: 2 * mo.group(1) + '\\"', arg)) |
| 862 | |
| 863 | # Then check to see if the arg contains any metacharacters other than |
| 864 | # double quotes; if it does, quote everything (including the double |
| 865 | # quotes) for safety. |
| 866 | if any(a in UNSAFE_FOR_CMD for a in arg): |
| 867 | arg = ''.join('^' + a if a in ALL_META_CHARS else a for a in arg) |
| 868 | return arg |
| 869 | |
| 870 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 871 | if __name__ == '__main__': |
| 872 | try: |
| 873 | sys.exit(main(sys.argv[1:])) |
| 874 | except MBErr as e: |
| 875 | print(e) |
| 876 | sys.exit(1) |
| 877 | except KeyboardInterrupt: |
| 878 | print("interrupted, exiting", stream=sys.stderr) |
| 879 | sys.exit(130) |