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