wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 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 |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 16 | import shutil |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 17 | import subprocess |
| 18 | import sys |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 19 | import tempfile |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 20 | from multiprocessing import Process, Queue |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 21 | |
nodir | 6a40e940 | 2017-06-07 05:49:03 | [diff] [blame^] | 22 | SRC_DIR = os.path.abspath( |
| 23 | os.path.join(os.path.abspath(os.path.dirname(__file__)), os.path.pardir)) |
| 24 | DEPOT_TOOLS_DIR = os.path.join(SRC_DIR, 'third_party', 'depot_tools') |
| 25 | |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 26 | |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 27 | def GetHeadersFromNinja(out_dir, q): |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 28 | """Return all the header files from ninja_deps""" |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 29 | |
| 30 | def NinjaSource(): |
nodir | 6a40e940 | 2017-06-07 05:49:03 | [diff] [blame^] | 31 | cmd = [os.path.join(DEPOT_TOOLS_DIR, 'ninja'), '-C', out_dir, '-t', 'deps'] |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 32 | # 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 | |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 43 | ans, err = set(), None |
| 44 | try: |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 45 | ans = ParseNinjaDepsOutput(NinjaSource(), out_dir) |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 46 | except Exception as e: |
| 47 | err = str(e) |
| 48 | q.put((ans, err)) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 49 | |
| 50 | |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 51 | def ParseNinjaDepsOutput(ninja_out, out_dir): |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 52 | """Parse ninja output and get the header files""" |
| 53 | all_headers = set() |
| 54 | |
| 55 | prefix = '..' + os.sep + '..' + os.sep |
| 56 | |
| 57 | is_valid = False |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 58 | for line in ninja_out: |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 59 | if line.startswith(' '): |
| 60 | if not is_valid: |
| 61 | continue |
| 62 | if line.endswith('.h') or line.endswith('.hh'): |
| 63 | f = line.strip() |
| 64 | if f.startswith(prefix): |
| 65 | f = f[6:] # Remove the '../../' prefix |
| 66 | # build/ only contains build-specific files like build_config.h |
| 67 | # and buildflag.h, and system header files, so they should be |
| 68 | # skipped. |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 69 | if f.startswith(out_dir) or f.startswith('out'): |
| 70 | continue |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 71 | if not f.startswith('build'): |
| 72 | all_headers.add(f) |
| 73 | else: |
| 74 | is_valid = line.endswith('(VALID)') |
| 75 | |
| 76 | return all_headers |
| 77 | |
| 78 | |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 79 | def GetHeadersFromGN(out_dir, q): |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 80 | """Return all the header files from GN""" |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 81 | |
| 82 | tmp = None |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 83 | ans, err = set(), None |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 84 | try: |
| 85 | tmp = tempfile.mkdtemp() |
| 86 | shutil.copy2(os.path.join(out_dir, 'args.gn'), |
| 87 | os.path.join(tmp, 'args.gn')) |
| 88 | # Do "gn gen" in a temp dir to prevent dirtying |out_dir|. |
nodir | 6a40e940 | 2017-06-07 05:49:03 | [diff] [blame^] | 89 | subprocess.check_call([ |
| 90 | os.path.join(DEPOT_TOOLS_DIR, 'gn'), 'gen', tmp, '--ide=json', '-q']) |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 91 | gn_json = json.load(open(os.path.join(tmp, 'project.json'))) |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 92 | ans = ParseGNProjectJSON(gn_json, out_dir, tmp) |
| 93 | except Exception as e: |
| 94 | err = str(e) |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 95 | finally: |
| 96 | if tmp: |
| 97 | shutil.rmtree(tmp) |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 98 | q.put((ans, err)) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 99 | |
| 100 | |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 101 | def ParseGNProjectJSON(gn, out_dir, tmp_out): |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 102 | """Parse GN output and get the header files""" |
| 103 | all_headers = set() |
| 104 | |
| 105 | for _target, properties in gn['targets'].iteritems(): |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 106 | sources = properties.get('sources', []) |
| 107 | public = properties.get('public', []) |
| 108 | # Exclude '"public": "*"'. |
| 109 | if type(public) is list: |
| 110 | sources += public |
| 111 | for f in sources: |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 112 | if f.endswith('.h') or f.endswith('.hh'): |
| 113 | if f.startswith('//'): |
| 114 | f = f[2:] # Strip the '//' prefix. |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 115 | if f.startswith(tmp_out): |
| 116 | f = out_dir + f[len(tmp_out):] |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 117 | all_headers.add(f) |
| 118 | |
| 119 | return all_headers |
| 120 | |
| 121 | |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 122 | def GetDepsPrefixes(q): |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 123 | """Return all the folders controlled by DEPS file""" |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 124 | prefixes, err = set(), None |
| 125 | try: |
nodir | 6a40e940 | 2017-06-07 05:49:03 | [diff] [blame^] | 126 | gclient_out = subprocess.check_output([ |
| 127 | os.path.join(DEPOT_TOOLS_DIR, 'gclient'), |
| 128 | 'recurse', '--no-progress', '-j1', |
| 129 | 'python', '-c', 'import os;print os.environ["GCLIENT_DEP_PATH"]']) |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 130 | for i in gclient_out.split('\n'): |
| 131 | if i.startswith('src/'): |
| 132 | i = i[4:] |
| 133 | prefixes.add(i) |
| 134 | except Exception as e: |
| 135 | err = str(e) |
| 136 | q.put((prefixes, err)) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 137 | |
| 138 | |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 139 | def IsBuildClean(out_dir): |
nodir | 6a40e940 | 2017-06-07 05:49:03 | [diff] [blame^] | 140 | cmd = [os.path.join(DEPOT_TOOLS_DIR, 'ninja'), '-C', out_dir, '-n'] |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 141 | out = subprocess.check_output(cmd) |
| 142 | return 'no work to do.' in out |
| 143 | |
| 144 | |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 145 | def ParseWhiteList(whitelist): |
| 146 | out = set() |
| 147 | for line in whitelist.split('\n'): |
| 148 | line = re.sub(r'#.*', '', line).strip() |
| 149 | if line: |
| 150 | out.add(line) |
| 151 | return out |
| 152 | |
| 153 | |
wychen | e7a3d648 | 2017-04-29 07:12:17 | [diff] [blame] | 154 | def FilterOutDepsedRepo(files, deps): |
| 155 | return {f for f in files if not any(f.startswith(d) for d in deps)} |
| 156 | |
| 157 | |
| 158 | def GetNonExistingFiles(lst): |
| 159 | out = set() |
| 160 | for f in lst: |
| 161 | if not os.path.isfile(f): |
| 162 | out.add(f) |
| 163 | return out |
| 164 | |
| 165 | |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 166 | def main(): |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 167 | |
| 168 | def DumpJson(data): |
| 169 | if args.json: |
| 170 | with open(args.json, 'w') as f: |
| 171 | json.dump(data, f) |
| 172 | |
| 173 | def PrintError(msg): |
| 174 | DumpJson([]) |
| 175 | parser.error(msg) |
| 176 | |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 177 | parser = argparse.ArgumentParser(description=''' |
| 178 | NOTE: Use ninja to build all targets in OUT_DIR before running |
| 179 | this script.''') |
| 180 | parser.add_argument('--out-dir', metavar='OUT_DIR', default='out/Release', |
| 181 | help='output directory of the build') |
| 182 | parser.add_argument('--json', |
| 183 | help='JSON output filename for missing headers') |
| 184 | parser.add_argument('--whitelist', help='file containing whitelist') |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 185 | parser.add_argument('--skip-dirty-check', action='store_true', |
| 186 | help='skip checking whether the build is dirty') |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 187 | |
| 188 | args, _extras = parser.parse_known_args() |
| 189 | |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 190 | if not os.path.isdir(args.out_dir): |
| 191 | parser.error('OUT_DIR "%s" does not exist.' % args.out_dir) |
| 192 | |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 193 | if not args.skip_dirty_check and not IsBuildClean(args.out_dir): |
| 194 | dirty_msg = 'OUT_DIR looks dirty. You need to build all there.' |
| 195 | if args.json: |
| 196 | # Assume running on the bots. Silently skip this step. |
| 197 | # This is possible because "analyze" step can be wrong due to |
| 198 | # underspecified header files. See crbug.com/725877 |
| 199 | print dirty_msg |
| 200 | DumpJson([]) |
| 201 | return 0 |
| 202 | else: |
| 203 | # Assume running interactively. |
| 204 | parser.error(dirty_msg) |
| 205 | |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 206 | d_q = Queue() |
| 207 | d_p = Process(target=GetHeadersFromNinja, args=(args.out_dir, d_q,)) |
| 208 | d_p.start() |
| 209 | |
| 210 | gn_q = Queue() |
| 211 | gn_p = Process(target=GetHeadersFromGN, args=(args.out_dir, gn_q,)) |
| 212 | gn_p.start() |
| 213 | |
| 214 | deps_q = Queue() |
| 215 | deps_p = Process(target=GetDepsPrefixes, args=(deps_q,)) |
| 216 | deps_p.start() |
| 217 | |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 218 | d, d_err = d_q.get() |
| 219 | gn, gn_err = gn_q.get() |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 220 | missing = d - gn |
wychen | e7a3d648 | 2017-04-29 07:12:17 | [diff] [blame] | 221 | nonexisting = GetNonExistingFiles(gn) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 222 | |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 223 | deps, deps_err = deps_q.get() |
wychen | e7a3d648 | 2017-04-29 07:12:17 | [diff] [blame] | 224 | missing = FilterOutDepsedRepo(missing, deps) |
| 225 | nonexisting = FilterOutDepsedRepo(nonexisting, deps) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 226 | |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 227 | d_p.join() |
| 228 | gn_p.join() |
| 229 | deps_p.join() |
| 230 | |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 231 | if d_err: |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 232 | PrintError(d_err) |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 233 | if gn_err: |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 234 | PrintError(gn_err) |
wychen | 09692cd | 2017-05-26 01:57:16 | [diff] [blame] | 235 | if deps_err: |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 236 | PrintError(deps_err) |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 237 | if len(GetNonExistingFiles(d)) > 0: |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 238 | print 'Non-existing files in ninja deps:', GetNonExistingFiles(d) |
| 239 | PrintError('Found non-existing files in ninja deps. You should ' + |
| 240 | 'build all in OUT_DIR.') |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 241 | if len(d) == 0: |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 242 | PrintError('OUT_DIR looks empty. You should build all there.') |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 243 | if any((('/gen/' in i) for i in nonexisting)): |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 244 | PrintError('OUT_DIR looks wrong. You should build all there.') |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 245 | |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 246 | if args.whitelist: |
| 247 | whitelist = ParseWhiteList(open(args.whitelist).read()) |
| 248 | missing -= whitelist |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 249 | nonexisting -= whitelist |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 250 | |
| 251 | missing = sorted(missing) |
wychen | e7a3d648 | 2017-04-29 07:12:17 | [diff] [blame] | 252 | nonexisting = sorted(nonexisting) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 253 | |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 254 | DumpJson(sorted(missing + nonexisting)) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 255 | |
wychen | e7a3d648 | 2017-04-29 07:12:17 | [diff] [blame] | 256 | if len(missing) == 0 and len(nonexisting) == 0: |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 257 | return 0 |
| 258 | |
wychen | e7a3d648 | 2017-04-29 07:12:17 | [diff] [blame] | 259 | if len(missing) > 0: |
| 260 | print '\nThe following files should be included in gn files:' |
| 261 | for i in missing: |
| 262 | print i |
| 263 | |
| 264 | if len(nonexisting) > 0: |
| 265 | print '\nThe following non-existing files should be removed from gn files:' |
| 266 | for i in nonexisting: |
| 267 | print i |
| 268 | |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 269 | return 1 |
| 270 | |
| 271 | |
| 272 | if __name__ == '__main__': |
| 273 | sys.exit(main()) |