blob: f6ae8f5cac4166221e2dc1108050a4502a9daadd [file] [log] [blame]
wychen037f6e9e2017-01-10 17:14:561#!/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
8This script gets all the header files from ninja_deps, which is from the true
9dependency generated by the compiler, and report if they don't exist in GN.
10"""
11
12import argparse
13import json
14import os
15import re
wychen03629112017-05-25 20:37:1816import shutil
wychen037f6e9e2017-01-10 17:14:5617import subprocess
18import sys
wychen03629112017-05-25 20:37:1819import tempfile
wychenef74ec992017-04-27 06:28:2520from multiprocessing import Process, Queue
wychen037f6e9e2017-01-10 17:14:5621
nodir6a40e9402017-06-07 05:49:0322SRC_DIR = os.path.abspath(
23 os.path.join(os.path.abspath(os.path.dirname(__file__)), os.path.pardir))
24DEPOT_TOOLS_DIR = os.path.join(SRC_DIR, 'third_party', 'depot_tools')
25
wychen037f6e9e2017-01-10 17:14:5626
wychen8cc31232017-06-13 10:21:2327def GetHeadersFromNinja(out_dir, skip_obj, q):
wychen037f6e9e2017-01-10 17:14:5628 """Return all the header files from ninja_deps"""
wychenef74ec992017-04-27 06:28:2529
30 def NinjaSource():
nodir6a40e9402017-06-07 05:49:0331 cmd = [os.path.join(DEPOT_TOOLS_DIR, 'ninja'), '-C', out_dir, '-t', 'deps']
wychenef74ec992017-04-27 06:28:2532 # A negative bufsize means to use the system default, which usually
33 # means fully buffered.
34 popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=-1)
35 for line in iter(popen.stdout.readline, ''):
36 yield line.rstrip()
37
38 popen.stdout.close()
39 return_code = popen.wait()
40 if return_code:
41 raise subprocess.CalledProcessError(return_code, cmd)
42
wychen09692cd2017-05-26 01:57:1643 ans, err = set(), None
44 try:
wychen8cc31232017-06-13 10:21:2345 ans = ParseNinjaDepsOutput(NinjaSource(), out_dir, skip_obj)
wychen09692cd2017-05-26 01:57:1646 except Exception as e:
47 err = str(e)
48 q.put((ans, err))
wychen037f6e9e2017-01-10 17:14:5649
50
wychen8cc31232017-06-13 10:21:2351def ParseNinjaDepsOutput(ninja_out, out_dir, skip_obj):
wychen037f6e9e2017-01-10 17:14:5652 """Parse ninja output and get the header files"""
wychen8cc31232017-06-13 10:21:2353 all_headers = {}
wychen037f6e9e2017-01-10 17:14:5654
wychen97580de2017-06-13 00:52:4455 # Ninja always uses "/", even on Windows.
56 prefix = '../../'
wychen037f6e9e2017-01-10 17:14:5657
58 is_valid = False
wychen8cc31232017-06-13 10:21:2359 obj_file = ''
wychenef74ec992017-04-27 06:28:2560 for line in ninja_out:
wychen037f6e9e2017-01-10 17:14:5661 if line.startswith(' '):
62 if not is_valid:
63 continue
64 if line.endswith('.h') or line.endswith('.hh'):
65 f = line.strip()
66 if f.startswith(prefix):
67 f = f[6:] # Remove the '../../' prefix
68 # build/ only contains build-specific files like build_config.h
69 # and buildflag.h, and system header files, so they should be
70 # skipped.
wychen0735fd762017-06-03 07:53:2671 if f.startswith(out_dir) or f.startswith('out'):
72 continue
wychen037f6e9e2017-01-10 17:14:5673 if not f.startswith('build'):
wychen8cc31232017-06-13 10:21:2374 all_headers.setdefault(f, [])
75 if not skip_obj:
76 all_headers[f].append(obj_file)
wychen037f6e9e2017-01-10 17:14:5677 else:
78 is_valid = line.endswith('(VALID)')
wychen8cc31232017-06-13 10:21:2379 obj_file = line.split(':')[0]
wychen037f6e9e2017-01-10 17:14:5680
81 return all_headers
82
83
wychenef74ec992017-04-27 06:28:2584def GetHeadersFromGN(out_dir, q):
wychen037f6e9e2017-01-10 17:14:5685 """Return all the header files from GN"""
wychen03629112017-05-25 20:37:1886
87 tmp = None
wychen09692cd2017-05-26 01:57:1688 ans, err = set(), None
wychen03629112017-05-25 20:37:1889 try:
wychen97580de2017-06-13 00:52:4490 # Argument |dir| is needed to make sure it's on the same drive on Windows.
91 # dir='' means dir='.', but doesn't introduce an unneeded prefix.
92 tmp = tempfile.mkdtemp(dir='')
wychen03629112017-05-25 20:37:1893 shutil.copy2(os.path.join(out_dir, 'args.gn'),
94 os.path.join(tmp, 'args.gn'))
95 # Do "gn gen" in a temp dir to prevent dirtying |out_dir|.
wychen97580de2017-06-13 00:52:4496 gn_exe = 'gn.bat' if sys.platform == 'win32' else 'gn'
nodir6a40e9402017-06-07 05:49:0397 subprocess.check_call([
wychen8cc31232017-06-13 10:21:2398 os.path.join(DEPOT_TOOLS_DIR, gn_exe), 'gen', tmp, '--ide=json', '-q'])
wychen03629112017-05-25 20:37:1899 gn_json = json.load(open(os.path.join(tmp, 'project.json')))
wychen09692cd2017-05-26 01:57:16100 ans = ParseGNProjectJSON(gn_json, out_dir, tmp)
101 except Exception as e:
102 err = str(e)
wychen03629112017-05-25 20:37:18103 finally:
104 if tmp:
105 shutil.rmtree(tmp)
wychen09692cd2017-05-26 01:57:16106 q.put((ans, err))
wychen037f6e9e2017-01-10 17:14:56107
108
wychen03629112017-05-25 20:37:18109def ParseGNProjectJSON(gn, out_dir, tmp_out):
wychen037f6e9e2017-01-10 17:14:56110 """Parse GN output and get the header files"""
111 all_headers = set()
112
113 for _target, properties in gn['targets'].iteritems():
wychen55235782017-04-28 01:59:15114 sources = properties.get('sources', [])
115 public = properties.get('public', [])
116 # Exclude '"public": "*"'.
117 if type(public) is list:
118 sources += public
119 for f in sources:
wychen037f6e9e2017-01-10 17:14:56120 if f.endswith('.h') or f.endswith('.hh'):
121 if f.startswith('//'):
122 f = f[2:] # Strip the '//' prefix.
wychen03629112017-05-25 20:37:18123 if f.startswith(tmp_out):
124 f = out_dir + f[len(tmp_out):]
wychen037f6e9e2017-01-10 17:14:56125 all_headers.add(f)
126
127 return all_headers
128
129
wychenef74ec992017-04-27 06:28:25130def GetDepsPrefixes(q):
wychen037f6e9e2017-01-10 17:14:56131 """Return all the folders controlled by DEPS file"""
wychen09692cd2017-05-26 01:57:16132 prefixes, err = set(), None
133 try:
wychen97580de2017-06-13 00:52:44134 gclient_exe = 'gclient.bat' if sys.platform == 'win32' else 'gclient'
nodir6a40e9402017-06-07 05:49:03135 gclient_out = subprocess.check_output([
wychen97580de2017-06-13 00:52:44136 os.path.join(DEPOT_TOOLS_DIR, gclient_exe),
137 'recurse', '--no-progress', '-j1',
138 'python', '-c', 'import os;print os.environ["GCLIENT_DEP_PATH"]'],
139 universal_newlines=True)
wychen09692cd2017-05-26 01:57:16140 for i in gclient_out.split('\n'):
141 if i.startswith('src/'):
142 i = i[4:]
143 prefixes.add(i)
144 except Exception as e:
145 err = str(e)
146 q.put((prefixes, err))
wychen037f6e9e2017-01-10 17:14:56147
148
wychen0735fd762017-06-03 07:53:26149def IsBuildClean(out_dir):
nodir6a40e9402017-06-07 05:49:03150 cmd = [os.path.join(DEPOT_TOOLS_DIR, 'ninja'), '-C', out_dir, '-n']
wychen67aabe02017-06-17 00:12:04151 try:
152 out = subprocess.check_output(cmd)
153 return 'no work to do.' in out
154 except Exception as e:
155 print e
156 return False
wychen0735fd762017-06-03 07:53:26157
wychen037f6e9e2017-01-10 17:14:56158def ParseWhiteList(whitelist):
159 out = set()
160 for line in whitelist.split('\n'):
161 line = re.sub(r'#.*', '', line).strip()
162 if line:
163 out.add(line)
164 return out
165
166
wychene7a3d6482017-04-29 07:12:17167def FilterOutDepsedRepo(files, deps):
168 return {f for f in files if not any(f.startswith(d) for d in deps)}
169
170
171def GetNonExistingFiles(lst):
172 out = set()
173 for f in lst:
174 if not os.path.isfile(f):
175 out.add(f)
176 return out
177
178
wychen037f6e9e2017-01-10 17:14:56179def main():
wychen0735fd762017-06-03 07:53:26180
181 def DumpJson(data):
182 if args.json:
183 with open(args.json, 'w') as f:
184 json.dump(data, f)
185
186 def PrintError(msg):
187 DumpJson([])
188 parser.error(msg)
189
wychen03629112017-05-25 20:37:18190 parser = argparse.ArgumentParser(description='''
191 NOTE: Use ninja to build all targets in OUT_DIR before running
192 this script.''')
193 parser.add_argument('--out-dir', metavar='OUT_DIR', default='out/Release',
194 help='output directory of the build')
195 parser.add_argument('--json',
196 help='JSON output filename for missing headers')
197 parser.add_argument('--whitelist', help='file containing whitelist')
wychen0735fd762017-06-03 07:53:26198 parser.add_argument('--skip-dirty-check', action='store_true',
199 help='skip checking whether the build is dirty')
wychen8cc31232017-06-13 10:21:23200 parser.add_argument('--verbose', action='store_true',
201 help='print more diagnostic info')
wychen037f6e9e2017-01-10 17:14:56202
203 args, _extras = parser.parse_known_args()
204
wychen03629112017-05-25 20:37:18205 if not os.path.isdir(args.out_dir):
206 parser.error('OUT_DIR "%s" does not exist.' % args.out_dir)
207
wychen0735fd762017-06-03 07:53:26208 if not args.skip_dirty_check and not IsBuildClean(args.out_dir):
209 dirty_msg = 'OUT_DIR looks dirty. You need to build all there.'
210 if args.json:
211 # Assume running on the bots. Silently skip this step.
212 # This is possible because "analyze" step can be wrong due to
213 # underspecified header files. See crbug.com/725877
214 print dirty_msg
215 DumpJson([])
216 return 0
217 else:
218 # Assume running interactively.
219 parser.error(dirty_msg)
220
wychenef74ec992017-04-27 06:28:25221 d_q = Queue()
wychen8cc31232017-06-13 10:21:23222 d_p = Process(target=GetHeadersFromNinja, args=(args.out_dir, True, d_q,))
wychenef74ec992017-04-27 06:28:25223 d_p.start()
224
225 gn_q = Queue()
226 gn_p = Process(target=GetHeadersFromGN, args=(args.out_dir, gn_q,))
227 gn_p.start()
228
229 deps_q = Queue()
230 deps_p = Process(target=GetDepsPrefixes, args=(deps_q,))
231 deps_p.start()
232
wychen09692cd2017-05-26 01:57:16233 d, d_err = d_q.get()
234 gn, gn_err = gn_q.get()
wychen8cc31232017-06-13 10:21:23235 missing = set(d.keys()) - gn
wychene7a3d6482017-04-29 07:12:17236 nonexisting = GetNonExistingFiles(gn)
wychen037f6e9e2017-01-10 17:14:56237
wychen09692cd2017-05-26 01:57:16238 deps, deps_err = deps_q.get()
wychene7a3d6482017-04-29 07:12:17239 missing = FilterOutDepsedRepo(missing, deps)
240 nonexisting = FilterOutDepsedRepo(nonexisting, deps)
wychen037f6e9e2017-01-10 17:14:56241
wychenef74ec992017-04-27 06:28:25242 d_p.join()
243 gn_p.join()
244 deps_p.join()
245
wychen09692cd2017-05-26 01:57:16246 if d_err:
wychen0735fd762017-06-03 07:53:26247 PrintError(d_err)
wychen09692cd2017-05-26 01:57:16248 if gn_err:
wychen0735fd762017-06-03 07:53:26249 PrintError(gn_err)
wychen09692cd2017-05-26 01:57:16250 if deps_err:
wychen0735fd762017-06-03 07:53:26251 PrintError(deps_err)
wychen03629112017-05-25 20:37:18252 if len(GetNonExistingFiles(d)) > 0:
wychen0735fd762017-06-03 07:53:26253 print 'Non-existing files in ninja deps:', GetNonExistingFiles(d)
254 PrintError('Found non-existing files in ninja deps. You should ' +
255 'build all in OUT_DIR.')
wychen03629112017-05-25 20:37:18256 if len(d) == 0:
wychen0735fd762017-06-03 07:53:26257 PrintError('OUT_DIR looks empty. You should build all there.')
wychen03629112017-05-25 20:37:18258 if any((('/gen/' in i) for i in nonexisting)):
wychen0735fd762017-06-03 07:53:26259 PrintError('OUT_DIR looks wrong. You should build all there.')
wychen03629112017-05-25 20:37:18260
wychen037f6e9e2017-01-10 17:14:56261 if args.whitelist:
262 whitelist = ParseWhiteList(open(args.whitelist).read())
263 missing -= whitelist
wychen0735fd762017-06-03 07:53:26264 nonexisting -= whitelist
wychen037f6e9e2017-01-10 17:14:56265
266 missing = sorted(missing)
wychene7a3d6482017-04-29 07:12:17267 nonexisting = sorted(nonexisting)
wychen037f6e9e2017-01-10 17:14:56268
wychen0735fd762017-06-03 07:53:26269 DumpJson(sorted(missing + nonexisting))
wychen037f6e9e2017-01-10 17:14:56270
wychene7a3d6482017-04-29 07:12:17271 if len(missing) == 0 and len(nonexisting) == 0:
wychen037f6e9e2017-01-10 17:14:56272 return 0
273
wychene7a3d6482017-04-29 07:12:17274 if len(missing) > 0:
275 print '\nThe following files should be included in gn files:'
276 for i in missing:
277 print i
278
279 if len(nonexisting) > 0:
280 print '\nThe following non-existing files should be removed from gn files:'
281 for i in nonexisting:
282 print i
283
wychen8cc31232017-06-13 10:21:23284 if args.verbose:
285 # Only get detailed obj dependency here since it is slower.
286 GetHeadersFromNinja(args.out_dir, False, d_q)
287 d, d_err = d_q.get()
288 print '\nDetailed dependency info:'
289 for f in missing:
290 print f
291 for cc in d[f]:
292 print ' ', cc
293
294 print '\nMissing headers sorted by number of affected object files:'
295 count = {k: len(v) for (k, v) in d.iteritems()}
296 for f in sorted(count, key=count.get, reverse=True):
297 if f in missing:
298 print count[f], f
299
wychen037f6e9e2017-01-10 17:14:56300 return 1
301
302
303if __name__ == '__main__':
304 sys.exit(main())