[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 2 | # Copyright (c) 2011 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 | # based on an almost identical script by: [email protected] (Jyrki Alakuijala) |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 6 | |
| 7 | """Prints out include dependencies in chrome. |
| 8 | |
| 9 | Since it ignores defines, it gives just a rough estimation of file size. |
| 10 | |
| 11 | Usage: |
Kent Tamura | a28b0bb | 2018-06-05 05:59:46 | [diff] [blame] | 12 | tools/include_tracer.py -Iout/Default/gen chrome/browser/ui/browser.h |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 13 | """ |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 14 | |
Raul Tambre | f3d9412e | 2019-09-24 05:31:44 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | |
Kent Tamura | a28b0bb | 2018-06-05 05:59:46 | [diff] [blame] | 17 | import argparse |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 18 | import os |
Kent Tamura | 654d87ef | 2018-06-05 05:31:54 | [diff] [blame] | 19 | import re |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 20 | import sys |
| 21 | |
| 22 | # Created by copying the command line for prerender_browsertest.cc, replacing |
| 23 | # spaces with newlines, and dropping everything except -F and -I switches. |
| 24 | # TODO(port): Add windows, linux directories. |
| 25 | INCLUDE_PATHS = [ |
| 26 | '', |
| 27 | 'gpu', |
| 28 | 'skia/config', |
| 29 | 'skia/ext', |
| 30 | 'testing/gmock/include', |
| 31 | 'testing/gtest/include', |
[email protected] | a023dca | 2013-12-18 03:58:36 | [diff] [blame] | 32 | 'third_party/google_toolbox_for_mac/src', |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 33 | 'third_party/icu/public/common', |
| 34 | 'third_party/icu/public/i18n', |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 35 | 'third_party/protobuf', |
| 36 | 'third_party/protobuf/src', |
| 37 | 'third_party/skia/gpu/include', |
| 38 | 'third_party/skia/include/config', |
| 39 | 'third_party/skia/include/core', |
| 40 | 'third_party/skia/include/effects', |
| 41 | 'third_party/skia/include/gpu', |
| 42 | 'third_party/skia/include/pdf', |
| 43 | 'third_party/skia/include/ports', |
| 44 | 'v8/include', |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 45 | ] |
| 46 | |
| 47 | |
Kent Tamura | a28b0bb | 2018-06-05 05:59:46 | [diff] [blame] | 48 | def Walk(include_dirs, seen, filename, parent, indent): |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 49 | """Returns the size of |filename| plus the size of all files included by |
| 50 | |filename| and prints the include tree of |filename| to stdout. Every file |
| 51 | is visited at most once. |
| 52 | """ |
| 53 | total_bytes = 0 |
| 54 | |
| 55 | # .proto(devel) filename translation |
| 56 | if filename.endswith('.pb.h'): |
| 57 | basename = filename[:-5] |
| 58 | if os.path.exists(basename + '.proto'): |
| 59 | filename = basename + '.proto' |
| 60 | else: |
Raul Tambre | f3d9412e | 2019-09-24 05:31:44 | [diff] [blame] | 61 | print('could not find ', filename) |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 62 | |
| 63 | # Show and count files only once. |
| 64 | if filename in seen: |
| 65 | return total_bytes |
| 66 | seen.add(filename) |
| 67 | |
| 68 | # Display the paths. |
Raul Tambre | f3d9412e | 2019-09-24 05:31:44 | [diff] [blame] | 69 | print(' ' * indent + filename) |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 70 | |
| 71 | # Skip system includes. |
| 72 | if filename[0] == '<': |
| 73 | return total_bytes |
| 74 | |
| 75 | # Find file in all include paths. |
| 76 | resolved_filename = filename |
Kent Tamura | a28b0bb | 2018-06-05 05:59:46 | [diff] [blame] | 77 | for root in INCLUDE_PATHS + [os.path.dirname(parent)] + include_dirs: |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 78 | if os.path.exists(os.path.join(root, filename)): |
| 79 | resolved_filename = os.path.join(root, filename) |
| 80 | break |
| 81 | |
| 82 | # Recurse. |
| 83 | if os.path.exists(resolved_filename): |
| 84 | lines = open(resolved_filename).readlines() |
| 85 | else: |
Raul Tambre | f3d9412e | 2019-09-24 05:31:44 | [diff] [blame] | 86 | print(' ' * (indent + 2) + "-- not found") |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 87 | lines = [] |
| 88 | for line in lines: |
| 89 | line = line.strip() |
Kent Tamura | 654d87ef | 2018-06-05 05:31:54 | [diff] [blame] | 90 | match = re.match(r'#include\s+(\S+).*', line) |
| 91 | if match: |
| 92 | include = match.group(1) |
| 93 | if include.startswith('"'): |
| 94 | include = include[1:-1] |
Kent Tamura | a28b0bb | 2018-06-05 05:59:46 | [diff] [blame] | 95 | total_bytes += Walk( |
| 96 | include_dirs, seen, include, resolved_filename, indent + 2) |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 97 | elif line.startswith('import '): |
| 98 | total_bytes += Walk( |
Kent Tamura | a28b0bb | 2018-06-05 05:59:46 | [diff] [blame] | 99 | include_dirs, seen, line.split('"')[1], resolved_filename, indent + 2) |
[email protected] | 833b584ce | 2011-04-18 22:04:09 | [diff] [blame] | 100 | return total_bytes + len("".join(lines)) |
| 101 | |
| 102 | |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 103 | def main(): |
Kent Tamura | a28b0bb | 2018-06-05 05:59:46 | [diff] [blame] | 104 | parser = argparse.ArgumentParser() |
| 105 | parser.add_argument('-I', action='append', dest='include_dirs') |
| 106 | parser.add_argument('source_file') |
| 107 | options = parser.parse_args(sys.argv[1:]) |
| 108 | if not options.include_dirs: |
| 109 | options.include_dirs = [] |
| 110 | |
| 111 | bytes = Walk(options.include_dirs, set(), options.source_file, '', 0) |
Raul Tambre | f3d9412e | 2019-09-24 05:31:44 | [diff] [blame] | 112 | print() |
| 113 | print(float(bytes) / (1 << 20), "megabytes of chrome source") |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 114 | |
| 115 | |
| 116 | if __name__ == '__main__': |
| 117 | sys.exit(main()) |