Index: build/check_gn_headers.py |
diff --git a/build/check_gn_headers.py b/build/check_gn_headers.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..8ca196f1f2432219209a3b530046a5b99b528829 |
--- /dev/null |
+++ b/build/check_gn_headers.py |
@@ -0,0 +1,104 @@ |
+#!/usr/bin/env python |
+# Copyright 2016 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Find header files missing in GN. |
+ |
+This script gets all the header files from ninja_deps, which is from the true |
+dependency generated by the compiler, and report if they don't exist in GN. |
+""" |
+ |
+import argparse |
+import json |
+import os |
+import subprocess |
+import sys |
+ |
+ |
+def get_d_headers(out_dir): |
+ """Return all the header files from ninja_deps""" |
+ 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
|
+ return extract_d_headers(ninja_out) |
+ |
+ |
+def extract_d_headers(ninja_out): |
+ """Extract header files from ninja output""" |
+ all_headers = set() |
+ |
+ prefix = '..' + os.sep + '..' + os.sep |
+ |
+ is_valid = False |
+ for line in ninja_out.split('\n'): |
+ if line.startswith(' '): |
+ if not is_valid: |
+ continue |
+ if line.endswith('.h') or line.endswith('.hh'): |
+ f = line.strip() |
+ if f.startswith(prefix): |
+ f = f[6:] # Remove the '../../' prefix |
+ # build/ only contains build-specific files like build_config.h |
+ # and buildflag.h, and system header files, so they should be |
+ # skipped. |
+ if not f.startswith('build'): |
+ all_headers.add(f) |
+ else: |
+ is_valid = line.endswith('(VALID)') |
+ |
+ return all_headers |
+ |
+ |
+def get_gn_headers(out_dir): |
+ """Return all the header files from GN""" |
+ subprocess.check_call(['gn', 'gen', out_dir, '--ide=json', '-q']) |
+ gn_json = json.load(open(os.path.join(out_dir, 'project.json'))) |
+ return extract_gn_headers(gn_json) |
+ |
+ |
+def extract_gn_headers(gn): |
+ """Extract header files from GN output""" |
+ all_headers = set() |
+ |
+ for target, properties in gn['targets'].iteritems(): |
+ for f in properties.get('sources', []): |
+ if f.endswith('.h') or f.endswith('.hh'): |
+ if f.startswith('//'): |
+ f = f[2:] # Strip the '//' prefix. |
+ all_headers.add(f) |
+ |
+ return all_headers |
+ |
+ |
+def main(): |
+ parser = argparse.ArgumentParser() |
+ parser.add_argument('--out-dir', default='out/Release') |
+ parser.add_argument('--json') |
+ parser.add_argument('--whitelist') |
+ parser.add_argument('args', nargs=argparse.REMAINDER) |
+ |
+ args, extras = parser.parse_known_args() |
+ |
+ d = get_d_headers(args.out_dir) |
+ gn = get_gn_headers(args.out_dir) |
+ missing = d - gn |
+ |
+ if args.whitelist: |
+ whitelist = set(open(args.whitelist).read().split('\n')) |
+ missing -= whitelist |
+ |
+ missing = sorted(missing) |
+ |
+ if args.json: |
+ with open(args.json, 'w') as f: |
+ json.dump(missing, f) |
+ |
+ if len(missing) == 0: |
+ return 0 |
+ |
+ print 'The following files should be included in gn files:' |
+ for i in missing: |
+ print i |
+ return 1 |
+ |
+if __name__ == '__main__': |
+ sys.exit(main()) |