Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/env python
      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: jyrki (at] google.com (Jyrki Alakuijala)
      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:
     12   tools/include_tracer.py chrome/browser/ui/browser.h
     13 """
     14 
     15 import os
     16 import sys
     17 
     18 # Created by copying the command line for prerender_browsertest.cc, replacing
     19 # spaces with newlines, and dropping everything except -F and -I switches.
     20 # TODO(port): Add windows, linux directories.
     21 INCLUDE_PATHS = [
     22   '',
     23   'gpu',
     24   'skia/config',
     25   'skia/ext',
     26   'testing/gmock/include',
     27   'testing/gtest/include',
     28   'third_party/GTM',
     29   'third_party/WebKit/Source',
     30   'third_party/WebKit/Source/wtf',
     31   'third_party/WebKit/Source/core',
     32   'third_party/WebKit/Source/core/accessibility',
     33   'third_party/WebKit/Source/core/accessibility/chromium',
     34   'third_party/WebKit/Source/core/bindings',
     35   'third_party/WebKit/Source/core/bindings/generic',
     36   'third_party/WebKit/Source/core/bindings/v8',
     37   'third_party/WebKit/Source/core/bindings/v8/custom',
     38   'third_party/WebKit/Source/core/bindings/v8/specialization',
     39   'third_party/WebKit/Source/core/bridge',
     40   'third_party/WebKit/Source/core/bridge/jni',
     41   'third_party/WebKit/Source/core/bridge/jni/v8',
     42   'third_party/WebKit/Source/core/css',
     43   'third_party/WebKit/Source/core/dom',
     44   'third_party/WebKit/Source/core/dom/default',
     45   'third_party/WebKit/Source/core/editing',
     46   'third_party/WebKit/Source/core/fileapi',
     47   'third_party/WebKit/Source/core/history',
     48   'third_party/WebKit/Source/core/html',
     49   'third_party/WebKit/Source/core/html/canvas',
     50   'third_party/WebKit/Source/core/html/parser',
     51   'third_party/WebKit/Source/core/html/shadow',
     52   'third_party/WebKit/Source/core/inspector',
     53   'third_party/WebKit/Source/core/loader',
     54   'third_party/WebKit/Source/core/loader/appcache',
     55   'third_party/WebKit/Source/core/loader/archive',
     56   'third_party/WebKit/Source/core/loader/cache',
     57   'third_party/WebKit/Source/core/loader/icon',
     58   'third_party/WebKit/Source/core/mathml',
     59   'third_party/WebKit/Source/core/notifications',
     60   'third_party/WebKit/Source/core/page',
     61   'third_party/WebKit/Source/core/page/animation',
     62   'third_party/WebKit/Source/core/page/chromium',
     63   'third_party/WebKit/Source/core/platform',
     64   'third_party/WebKit/Source/core/platform/animation',
     65   'third_party/WebKit/Source/core/platform/audio',
     66   'third_party/WebKit/Source/core/platform/audio/chromium',
     67   'third_party/WebKit/Source/core/platform/audio/mac',
     68   'third_party/WebKit/Source/core/platform/chromium',
     69   'third_party/WebKit/Source/core/platform/cocoa',
     70   'third_party/WebKit/Source/core/platform/graphics',
     71   'third_party/WebKit/Source/core/platform/graphics/cg',
     72   'third_party/WebKit/Source/core/platform/graphics/chromium',
     73   'third_party/WebKit/Source/core/platform/graphics/cocoa',
     74   'third_party/WebKit/Source/core/platform/graphics/filters',
     75   'third_party/WebKit/Source/core/platform/graphics/gpu',
     76   'third_party/WebKit/Source/core/platform/graphics/mac',
     77   'third_party/WebKit/Source/core/platform/graphics/opentype',
     78   'third_party/WebKit/Source/core/platform/graphics/skia',
     79   'third_party/WebKit/Source/core/platform/graphics/transforms',
     80   'third_party/WebKit/Source/core/platform/image-decoders',
     81   'third_party/WebKit/Source/core/platform/image-decoders/bmp',
     82   'third_party/WebKit/Source/core/platform/image-decoders/gif',
     83   'third_party/WebKit/Source/core/platform/image-decoders/ico',
     84   'third_party/WebKit/Source/core/platform/image-decoders/jpeg',
     85   'third_party/WebKit/Source/core/platform/image-decoders/png',
     86   'third_party/WebKit/Source/core/platform/image-decoders/skia',
     87   'third_party/WebKit/Source/core/platform/image-decoders/webp',
     88   'third_party/WebKit/Source/core/platform/image-decoders/xbm',
     89   'third_party/WebKit/Source/core/platform/image-encoders/skia',
     90   'third_party/WebKit/Source/core/platform/mac',
     91   'third_party/WebKit/Source/core/platform/mock',
     92   'third_party/WebKit/Source/core/platform/network',
     93   'third_party/WebKit/Source/core/platform/network/chromium',
     94   'third_party/WebKit/Source/core/platform/sql',
     95   'third_party/WebKit/Source/core/platform/text',
     96   'third_party/WebKit/Source/core/platform/text/mac',
     97   'third_party/WebKit/Source/core/platform/text/transcoder',
     98   'third_party/WebKit/Source/core/plugins',
     99   'third_party/WebKit/Source/core/plugins/chromium',
    100   'third_party/WebKit/Source/core/rendering',
    101   'third_party/WebKit/Source/core/rendering/style',
    102   'third_party/WebKit/Source/core/rendering/svg',
    103   'third_party/WebKit/Source/core/storage',
    104   'third_party/WebKit/Source/core/storage/chromium',
    105   'third_party/WebKit/Source/core/svg',
    106   'third_party/WebKit/Source/core/svg/animation',
    107   'third_party/WebKit/Source/core/svg/graphics',
    108   'third_party/WebKit/Source/core/svg/graphics/filters',
    109   'third_party/WebKit/Source/core/svg/properties',
    110   'third_party/WebKit/Source/core/webaudio',
    111   'third_party/WebKit/Source/core/websockets',
    112   'third_party/WebKit/Source/core/workers',
    113   'third_party/WebKit/Source/core/xml',
    114   'third_party/WebKit/Source/public',
    115   'third_party/WebKit/Source/web',
    116   'third_party/cld',
    117   'third_party/icu/public/common',
    118   'third_party/icu/public/i18n',
    119   'third_party/npapi',
    120   'third_party/npapi/bindings',
    121   'third_party/protobuf',
    122   'third_party/protobuf/src',
    123   'third_party/skia/gpu/include',
    124   'third_party/skia/include/config',
    125   'third_party/skia/include/core',
    126   'third_party/skia/include/effects',
    127   'third_party/skia/include/gpu',
    128   'third_party/skia/include/pdf',
    129   'third_party/skia/include/ports',
    130   'v8/include',
    131   'xcodebuild/Debug/include',
    132   'xcodebuild/DerivedSources/Debug/chrome',
    133   'xcodebuild/DerivedSources/Debug/policy',
    134   'xcodebuild/DerivedSources/Debug/protoc_out',
    135   'xcodebuild/DerivedSources/Debug/webkit',
    136   'xcodebuild/DerivedSources/Debug/webkit/bindings',
    137 ]
    138 
    139 
    140 def Walk(seen, filename, parent, indent):
    141   """Returns the size of |filename| plus the size of all files included by
    142   |filename| and prints the include tree of |filename| to stdout. Every file
    143   is visited at most once.
    144   """
    145   total_bytes = 0
    146 
    147   # .proto(devel) filename translation
    148   if filename.endswith('.pb.h'):
    149     basename = filename[:-5]
    150     if os.path.exists(basename + '.proto'):
    151       filename = basename + '.proto'
    152     else:
    153       print 'could not find ', filename
    154 
    155   # Show and count files only once.
    156   if filename in seen:
    157     return total_bytes
    158   seen.add(filename)
    159 
    160   # Display the paths.
    161   print ' ' * indent + filename
    162 
    163   # Skip system includes.
    164   if filename[0] == '<':
    165     return total_bytes
    166 
    167   # Find file in all include paths.
    168   resolved_filename = filename
    169   for root in INCLUDE_PATHS + [os.path.dirname(parent)]:
    170     if os.path.exists(os.path.join(root, filename)):
    171       resolved_filename = os.path.join(root, filename)
    172       break
    173 
    174   # Recurse.
    175   if os.path.exists(resolved_filename):
    176     lines = open(resolved_filename).readlines()
    177   else:
    178     print ' ' * (indent + 2) + "-- not found"
    179     lines = []
    180   for line in lines:
    181     line = line.strip()
    182     if line.startswith('#include "'):
    183       total_bytes += Walk(
    184           seen, line.split('"')[1], resolved_filename, indent + 2)
    185     elif line.startswith('#include '):
    186       include = '<' + line.split('<')[1].split('>')[0] + '>'
    187       total_bytes += Walk(
    188           seen, include, resolved_filename, indent + 2)
    189     elif line.startswith('import '):
    190       total_bytes += Walk(
    191           seen, line.split('"')[1], resolved_filename, indent + 2)
    192   return total_bytes + len("".join(lines))
    193 
    194 
    195 def main():
    196   bytes = Walk(set(), sys.argv[1], '', 0)
    197   print
    198   print float(bytes) / (1 << 20), "megabytes of chrome source"
    199 
    200 
    201 if __name__ == '__main__':
    202   sys.exit(main())
    203