Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
|
iannucci
2016/03/09 19:01:52
let's add this under the findit module in the 'res
stgao
2016/03/09 20:09:11
Nice! I didn't know about self.resource.
| |
| 2 # Copyright (c) 2016 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 """A tool to check whether a target exists in the ninja build graph or not. | |
| 7 | |
| 8 Before this is run, ninja build files should already be generated or updated. | |
| 9 | |
| 10 For a list of command line options, call this script with '--help'. | |
| 11 """ | |
| 12 | |
| 13 import json | |
| 14 import optparse | |
| 15 import os | |
| 16 import subprocess | |
| 17 import sys | |
| 18 | |
| 19 | |
| 20 def targets_exist(targets, target_build_dir): | |
| 21 """Returns True if the given targets exist in the ninja build file. | |
| 22 | |
| 23 Args: | |
| 24 targets (list): A list of target names. | |
| 25 target_build_dir (str): The path to the target build directory. | |
| 26 """ | |
| 27 print 'Checking targets: %s' % ', '.join(targets) | |
| 28 cmd = ['ninja', '-C', target_build_dir, '-t', 'query'] | |
|
Nico
2016/03/08 19:41:36
(i'm assuming you checked that this does the right
stgao
2016/03/09 20:09:11
Yes, it works as the code expected here. I checked
| |
| 29 cmd.extend(targets) | |
| 30 with open(os.devnull, 'w') as f: # Mute normal output, only care about error. | |
| 31 p = subprocess.Popen(cmd, stdout=f) | |
| 32 return_code = p.wait() | |
| 33 print 'return code: %s' % return_code | |
| 34 return return_code == 0 | |
| 35 | |
| 36 | |
| 37 def check_targets(targets, target_build_dir): | |
| 38 """Returns a dict of two lists to indicate which targets exist or not. | |
| 39 | |
| 40 Args: | |
| 41 targets (list): A list of target names. | |
| 42 target_build_dir (str): The path to the target build directory. | |
| 43 """ | |
| 44 # Check all the targets at once first in one ninja command. | |
| 45 # If a target among a few doesn't exist, ninja will exit immediately and not | |
| 46 # process the remaining targets. In that case, we have to check them one by | |
| 47 # one instead. | |
| 48 all_existing = targets_exist(targets, target_build_dir) | |
| 49 if all_existing: # All targets exists. | |
| 50 return {'found': targets, 'not_found': []} | |
| 51 elif len(targets) == 1: # None of the targets exists. | |
| 52 return {'found': [], 'not_found': targets} | |
| 53 else: # Some targets do not exist. | |
| 54 found = [] | |
| 55 not_found = [] | |
| 56 for target in targets: | |
| 57 if targets_exist([target], target_build_dir): | |
| 58 found.append(target) | |
| 59 else: | |
| 60 not_found.append(target) | |
| 61 | |
| 62 return { | |
| 63 'found': found, | |
| 64 'not_found': not_found, | |
| 65 } | |
| 66 | |
| 67 | |
| 68 def parse_args(): | |
| 69 option_parser = optparse.OptionParser() | |
| 70 option_parser.add_option('--target-build-dir', | |
| 71 help='The target build directory. eg: out/Release.') | |
| 72 option_parser.add_option('--target', action='append', default=[], | |
| 73 help='Specify the target to be checked. eg: ' | |
| 74 'browser_tests, obj/path/to/Source.o, or ' | |
| 75 'gen/path/to/generated.cc, etc.') | |
| 76 option_parser.add_option('--json-output', | |
| 77 help='Optional. Specify a file to dump the result ' | |
| 78 'as json.') | |
| 79 options, _ = option_parser.parse_args() | |
| 80 return options.target_build_dir, options.target, options.json_output | |
| 81 | |
| 82 | |
| 83 def main(): | |
| 84 target_build_dir, targets, json_output = parse_args() | |
| 85 if not (target_build_dir and targets): | |
| 86 print 'Invalid parameter.' | |
| 87 print 'target-build-dir: %s' % target_build_dir | |
| 88 print 'targets:' | |
| 89 for target in targets: | |
| 90 print ' %s' % target | |
| 91 return 1 | |
| 92 | |
| 93 result = check_targets(targets, target_build_dir) | |
| 94 if json_output: | |
| 95 with open(json_output, 'wb') as f: | |
| 96 json.dump(result, f, indent=2) | |
| 97 else: | |
| 98 print json.dumps(result, indent=2) | |
| 99 | |
| 100 return 0 | |
| 101 | |
| 102 | |
| 103 if __name__ == '__main__': | |
| 104 sys.exit(main()) | |
| OLD | NEW |