1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import shutil
7import subprocess
8import sys
9
10
11BASE_DIR = os.path.dirname(os.path.abspath(__file__))
12
13
14def main():
15  if len(sys.argv) != 2:
16    print 'usage: %s <output.html>' % sys.argv[0]
17    return 1
18  env = os.environ.copy()
19  env['GYP_GENERATORS'] = 'dump_dependency_json'
20  print 'Dumping dependencies...'
21  popen = subprocess.Popen(
22      ['python', 'build/gyp_chromium'],
23      shell=True, env=env)
24  popen.communicate()
25  if popen.returncode != 0:
26    return popen.returncode
27  print 'Finding problems...'
28  popen = subprocess.Popen(
29      ['python', 'tools/gyp-explain.py', '--dot',
30       'chrome.gyp:browser#', 'core.gyp:webcore#'],
31      stdout=subprocess.PIPE,
32      shell=True)
33  out, _ = popen.communicate()
34  if popen.returncode != 0:
35    return popen.returncode
36
37  # Break into pairs to uniq to make graph less of a mess.
38  print 'Simplifying...'
39  deduplicated = set()
40  lines = out.splitlines()[2:-1]
41  for line in lines:
42    line = line.strip('\r\n ;')
43    pairs = line.split(' -> ')
44    for i in range(len(pairs) - 1):
45      deduplicated.add('%s -> %s;' % (pairs[i], pairs[i + 1]))
46  graph = 'strict digraph {\n' + '\n'.join(sorted(deduplicated)) + '\n}'
47
48  print 'Writing report to %s...' % sys.argv[1]
49  path_count = len(out.splitlines())
50  with open(os.path.join(BASE_DIR, 'viz.js', 'viz.js')) as f:
51    viz_js = f.read()
52  with open(sys.argv[1], 'w') as f:
53    f.write(PREFIX % path_count)
54    f.write(graph)
55    f.write(SUFFIX % viz_js)
56  print 'Done.'
57
58
59PREFIX = r'''<!DOCTYPE html>
60<html>
61  <head>
62    <meta charset="utf-8">
63    <title>Undesirable Dependencies</title>
64  </head>
65  <body>
66    <h1>Undesirable Dependencies</h1>
67<h2>browser &rarr; webcore</h2>
68<h3>%d paths</h3>
69    <script type="text/vnd.graphviz" id="graph">
70'''
71
72
73SUFFIX = r'''
74    </script>
75    <script>%s</script>
76    <div id="output">Rendering...</div>
77    <script>
78      setTimeout(function() {
79          document.getElementById("output").innerHTML =
80              Viz(document.getElementById("graph").innerHTML, "svg");
81        }, 1);
82    </script>
83  </body>
84</html>
85'''
86
87
88if __name__ == '__main__':
89  sys.exit(main())
90