| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 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'] |
| 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 with a list of existing targets. |
| 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 found = [] |
| 45 |
| 46 targets = sorted(set(targets)) |
| 47 # Check all the targets at once first in one ninja command. |
| 48 # If a target among a few doesn't exist, ninja will exit with 1 immediately |
| 49 # and not process the remaining targets. In that case, we have to check them |
| 50 # one by one instead. |
| 51 all_existing = targets_exist(targets, target_build_dir) |
| 52 |
| 53 if all_existing: # All targets exists. |
| 54 found = targets |
| 55 elif len(targets) > 1: # At least one target does not exist. |
| 56 for target in targets: |
| 57 if targets_exist([target], target_build_dir): |
| 58 found.append(target) |
| 59 |
| 60 return {'found': found} |
| 61 |
| 62 |
| 63 def parse_args(): |
| 64 option_parser = optparse.OptionParser() |
| 65 option_parser.add_option('--target-build-dir', |
| 66 help='The target build directory. eg: out/Release.') |
| 67 option_parser.add_option('--target', action='append', default=[], |
| 68 help='Specify the target to be checked. eg: ' |
| 69 'browser_tests, obj/path/to/Source.o, or ' |
| 70 'gen/path/to/generated.cc, etc.') |
| 71 option_parser.add_option('--json-output', |
| 72 help='Optional. Specify a file to dump the result ' |
| 73 'as json.') |
| 74 options, _ = option_parser.parse_args() |
| 75 return options.target_build_dir, options.target, options.json_output |
| 76 |
| 77 |
| 78 def main(): |
| 79 target_build_dir, targets, json_output = parse_args() |
| 80 if not (target_build_dir and targets): |
| 81 print 'Invalid parameter.' |
| 82 print 'target-build-dir: %s' % target_build_dir |
| 83 print 'targets:' |
| 84 for target in targets: |
| 85 print ' %s' % target |
| 86 return 1 |
| 87 |
| 88 result = check_targets(targets, target_build_dir) |
| 89 if json_output: |
| 90 with open(json_output, 'wb') as f: |
| 91 json.dump(result, f, indent=2) |
| 92 else: |
| 93 print json.dumps(result, indent=2) |
| 94 |
| 95 return 0 |
| 96 |
| 97 |
| 98 if __name__ == '__main__': |
| 99 sys.exit(main()) |
| OLD | NEW |