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): | |
|
Dirk Pranke
2017/01/09 06:47:49
Chromium Python style is (sadly) to use InitialCap
wychen
2017/01/09 11:26:38
Done.
| |
| 20 """Return all the header files from ninja_deps""" | |
| 21 ninja_out = subprocess.check_output(['ninja', '-C', out_dir, '-t', 'deps']) | |
| 22 return extract_d_headers(ninja_out) | |
| 23 | |
| 24 | |
| 25 def extract_d_headers(ninja_out): | |
|
Dirk Pranke
2017/01/09 06:47:49
extract_d_headers is a weird function name. It's n
wychen
2017/01/09 11:26:38
Done.
| |
| 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): | |
|
Dirk Pranke
2017/01/09 06:47:49
I'd call this something more like "GetHeadersFromG
wychen
2017/01/09 11:26:38
I use ParseGNProjectJSON() so it's parallel to Par
| |
| 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 get_deps_prefixes(deps_file): | |
| 73 """Return all the folders controlled by DEPS file""" | |
| 74 return extract_deps_prefixes(open(deps_file).read()) | |
| 75 | |
| 76 | |
| 77 def extract_deps_prefixes(deps): | |
| 78 var_def = 'def Var(a): return ""\n' | |
| 79 | |
| 80 exec(var_def + deps) | |
|
Dirk Pranke
2017/01/09 06:47:49
Please add a comment about what lines 78-80 are do
wychen
2017/01/09 11:26:38
Done.
| |
| 81 all_prefixes = set() | |
| 82 for f in deps.keys(): | |
| 83 if f.startswith('src/'): | |
| 84 f = f[4:] | |
| 85 all_prefixes.add(f) | |
| 86 for i in deps_os.keys(): | |
| 87 for f in deps_os[i].keys(): | |
| 88 if f.startswith('src/'): | |
| 89 f = f[4:] | |
| 90 all_prefixes.add(f) | |
| 91 return all_prefixes | |
| 92 | |
| 93 | |
| 94 def starts_with_any(prefixes, string): | |
| 95 for p in prefixes: | |
| 96 if string.startswith(p): | |
| 97 return True | |
| 98 return False | |
| 99 | |
| 100 | |
| 101 def main(): | |
| 102 parser = argparse.ArgumentParser() | |
| 103 parser.add_argument('--out-dir', default='out/Release') | |
| 104 parser.add_argument('--deps-file', default='DEPS') | |
| 105 parser.add_argument('--json') | |
| 106 parser.add_argument('--whitelist') | |
| 107 parser.add_argument('args', nargs=argparse.REMAINDER) | |
| 108 | |
| 109 args, _ = parser.parse_known_args() | |
| 110 | |
| 111 d = get_d_headers(args.out_dir) | |
| 112 gn = get_gn_headers(args.out_dir) | |
| 113 missing = d - gn | |
| 114 | |
| 115 deps = get_deps_prefixes(args.deps_file) | |
| 116 missing = set(filter(lambda x: not starts_with_any(deps, x), missing)) | |
| 117 | |
| 118 if args.whitelist: | |
| 119 whitelist = set(open(args.whitelist).read().split('\n')) | |
|
Dirk Pranke
2017/01/09 06:47:49
Please change this to be able to handle a file for
wychen
2017/01/09 11:26:38
Great idea!
| |
| 120 missing -= whitelist | |
| 121 | |
| 122 missing = sorted(missing) | |
| 123 | |
| 124 if args.json: | |
| 125 with open(args.json, 'w') as f: | |
| 126 json.dump(missing, f) | |
| 127 | |
| 128 if len(missing) == 0: | |
| 129 return 0 | |
| 130 | |
| 131 print 'The following files should be included in gn files:' | |
| 132 for i in missing: | |
| 133 print i | |
| 134 return 1 | |
| 135 | |
| 136 | |
| 137 if __name__ == '__main__': | |
| 138 sys.exit(main()) | |
| OLD | NEW |