Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 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 """Find header files missing in GN. | |
| 7 | |
| 8 This script gets all the header files from ninja_deps, which is from the true | |
| 9 dependency generated by the compiler, and report if they don't exist in GN. | |
| 10 """ | |
| 11 | |
| 12 import argparse | |
| 13 import json | |
| 14 import os | |
| 15 import subprocess | |
| 16 import sys | |
| 17 | |
| 18 | |
| 19 def get_d_headers(out_dir): | |
| 20 """Return all the header files from ninja_deps""" | |
| 21 ninja_out = subprocess.check_output(['ninja', '-C', out_dir, '-t', 'deps']) | |
|
wychen
2016/12/28 15:33:50
Note to self:
Might need universal_newlines=True i
| |
| 22 return extract_d_headers(ninja_out) | |
| 23 | |
| 24 | |
| 25 def extract_d_headers(ninja_out): | |
| 26 """Extract header files from ninja output""" | |
| 27 all_headers = set() | |
| 28 | |
| 29 prefix = '..' + os.sep + '..' + os.sep | |
| 30 | |
| 31 is_valid = False | |
| 32 for line in ninja_out.split('\n'): | |
| 33 if line.startswith(' '): | |
| 34 if not is_valid: | |
| 35 continue | |
| 36 if line.endswith('.h') or line.endswith('.hh'): | |
| 37 f = line.strip() | |
| 38 if f.startswith(prefix): | |
| 39 f = f[6:] # Remove the '../../' prefix | |
| 40 # build/ only contains build-specific files like build_config.h | |
| 41 # and buildflag.h, and system header files, so they should be | |
| 42 # skipped. | |
| 43 if not f.startswith('build'): | |
| 44 all_headers.add(f) | |
| 45 else: | |
| 46 is_valid = line.endswith('(VALID)') | |
| 47 | |
| 48 return all_headers | |
| 49 | |
| 50 | |
| 51 def get_gn_headers(out_dir): | |
| 52 """Return all the header files from GN""" | |
| 53 subprocess.check_call(['gn', 'gen', out_dir, '--ide=json', '-q']) | |
| 54 gn_json = json.load(open(os.path.join(out_dir, 'project.json'))) | |
| 55 return extract_gn_headers(gn_json) | |
| 56 | |
| 57 | |
| 58 def extract_gn_headers(gn): | |
| 59 """Extract header files from GN output""" | |
| 60 all_headers = set() | |
| 61 | |
| 62 for target, properties in gn['targets'].iteritems(): | |
| 63 for f in properties.get('sources', []): | |
| 64 if f.endswith('.h') or f.endswith('.hh'): | |
| 65 if f.startswith('//'): | |
| 66 f = f[2:] # Strip the '//' prefix. | |
| 67 all_headers.add(f) | |
| 68 | |
| 69 return all_headers | |
| 70 | |
| 71 | |
| 72 def main(): | |
| 73 parser = argparse.ArgumentParser() | |
| 74 parser.add_argument('--out-dir', default='out/Release') | |
| 75 parser.add_argument('--json') | |
| 76 parser.add_argument('--whitelist') | |
| 77 parser.add_argument('args', nargs=argparse.REMAINDER) | |
| 78 | |
| 79 args, extras = parser.parse_known_args() | |
| 80 | |
| 81 d = get_d_headers(args.out_dir) | |
| 82 gn = get_gn_headers(args.out_dir) | |
| 83 missing = d - gn | |
| 84 | |
| 85 if args.whitelist: | |
| 86 whitelist = set(open(args.whitelist).read().split('\n')) | |
| 87 missing -= whitelist | |
| 88 | |
| 89 missing = sorted(missing) | |
| 90 | |
| 91 if args.json: | |
| 92 with open(args.json, 'w') as f: | |
| 93 json.dump(missing, f) | |
| 94 | |
| 95 if len(missing) == 0: | |
| 96 return 0 | |
| 97 | |
| 98 print 'The following files should be included in gn files:' | |
| 99 for i in missing: | |
| 100 print i | |
| 101 return 1 | |
| 102 | |
| 103 if __name__ == '__main__': | |
| 104 sys.exit(main()) | |
| OLD | NEW |