OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2017 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 re | |
16 import subprocess | |
17 import sys | |
18 | |
19 | |
20 def GetHeadersFromNinja(out_dir): | |
21 """Return all the header files from ninja_deps""" | |
22 ninja_out = subprocess.check_output(['ninja', '-C', out_dir, '-t', 'deps']) | |
23 return ParseNinjaDepsOutput(ninja_out) | |
24 | |
25 | |
26 def ParseNinjaDepsOutput(ninja_out): | |
27 """Parse ninja output and get the header files""" | |
28 all_headers = set() | |
29 | |
30 prefix = '..' + os.sep + '..' + os.sep | |
31 | |
32 is_valid = False | |
33 for line in ninja_out.split('\n'): | |
34 if line.startswith(' '): | |
35 if not is_valid: | |
36 continue | |
37 if line.endswith('.h') or line.endswith('.hh'): | |
38 f = line.strip() | |
39 if f.startswith(prefix): | |
40 f = f[6:] # Remove the '../../' prefix | |
41 # build/ only contains build-specific files like build_config.h | |
42 # and buildflag.h, and system header files, so they should be | |
43 # skipped. | |
44 if not f.startswith('build'): | |
45 all_headers.add(f) | |
46 else: | |
47 is_valid = line.endswith('(VALID)') | |
48 | |
49 return all_headers | |
50 | |
51 | |
52 def GetHeadersFromGN(out_dir): | |
53 """Return all the header files from GN""" | |
54 subprocess.check_call(['gn', 'gen', out_dir, '--ide=json', '-q']) | |
55 gn_json = json.load(open(os.path.join(out_dir, 'project.json'))) | |
56 return ParseGNProjectJSON(gn_json) | |
57 | |
58 | |
59 def ParseGNProjectJSON(gn): | |
60 """Parse GN output and get the header files""" | |
61 all_headers = set() | |
62 | |
63 for _target, properties in gn['targets'].iteritems(): | |
64 for f in properties.get('sources', []): | |
65 if f.endswith('.h') or f.endswith('.hh'): | |
66 if f.startswith('//'): | |
67 f = f[2:] # Strip the '//' prefix. | |
68 all_headers.add(f) | |
69 | |
70 return all_headers | |
71 | |
72 | |
73 def GetDepsPrefixes(deps_file): | |
74 """Return all the folders controlled by DEPS file""" | |
75 return ParseDeps(open(deps_file).read()) | |
76 | |
77 | |
78 def ParseDeps(deps_content): | |
79 """Parse the DEPS file and get the folder names""" | |
Dirk Pranke
2017/01/09 20:46:24
It occurred to me that this won't actually work ri
wychen
2017/01/10 15:47:42
Nice catch! Relying on gclient now.
| |
80 | |
81 # Load the DEPS file and stub out the Var() function. | |
82 deps = None | |
83 deps_os = None | |
84 var_def = 'def Var(a): return ""\n' | |
85 exec(var_def + deps_content) | |
86 | |
87 all_prefixes = set() | |
88 for f in deps.keys(): | |
89 if f.startswith('src/'): | |
90 f = f[4:] | |
91 all_prefixes.add(f) | |
92 for i in deps_os.keys(): | |
93 for f in deps_os[i].keys(): | |
94 if f.startswith('src/'): | |
95 f = f[4:] | |
96 all_prefixes.add(f) | |
97 return all_prefixes | |
98 | |
99 | |
100 def StartsWithAny(prefixes, string): | |
101 for p in prefixes: | |
102 if string.startswith(p): | |
103 return True | |
104 return False | |
105 | |
106 | |
107 def ParseWhiteList(whitelist): | |
108 out = set() | |
109 for line in whitelist.split('\n'): | |
110 line = re.sub(r'#.*', '', line).strip() | |
111 if line != '': | |
112 out.add(line) | |
113 return out | |
114 | |
115 | |
116 def main(): | |
117 parser = argparse.ArgumentParser() | |
118 parser.add_argument('--out-dir', default='out/Release') | |
119 parser.add_argument('--deps-file', default='DEPS') | |
120 parser.add_argument('--json') | |
121 parser.add_argument('--whitelist') | |
122 parser.add_argument('args', nargs=argparse.REMAINDER) | |
123 | |
124 args, _extras = parser.parse_known_args() | |
125 | |
126 d = GetHeadersFromNinja(args.out_dir) | |
127 gn = GetHeadersFromGN(args.out_dir) | |
128 missing = d - gn | |
129 | |
130 deps = GetDepsPrefixes(args.deps_file) | |
131 missing = set(filter(lambda x: not StartsWithAny(deps, x), missing)) | |
Dirk Pranke
2017/01/09 20:46:24
missing = {x for x in deps
if no
wychen
2017/01/10 15:47:42
Done.
| |
132 | |
133 if args.whitelist: | |
134 whitelist = ParseWhiteList(open(args.whitelist).read()) | |
135 missing -= whitelist | |
136 | |
137 missing = sorted(missing) | |
138 | |
139 if args.json: | |
140 with open(args.json, 'w') as f: | |
141 json.dump(missing, f) | |
142 | |
143 if len(missing) == 0: | |
144 return 0 | |
145 | |
146 print 'The following files should be included in gn files:' | |
147 for i in missing: | |
148 print i | |
149 return 1 | |
150 | |
151 | |
152 if __name__ == '__main__': | |
153 sys.exit(main()) | |
OLD | NEW |