show-headers revision 1.1 1 1.1 mrg #! /usr/bin/python2
2 1.1 mrg import os.path
3 1.1 mrg import sys
4 1.1 mrg import shlex
5 1.1 mrg import re
6 1.1 mrg
7 1.1 mrg from headerutils import *
8 1.1 mrg
9 1.1 mrg
10 1.1 mrg tabstop = 2
11 1.1 mrg padding = " "
12 1.1 mrg seen = { }
13 1.1 mrg output = list()
14 1.1 mrg summary = list()
15 1.1 mrg sawcore = False
16 1.1 mrg
17 1.1 mrg # list of headers to emphasize
18 1.1 mrg highlight = list ()
19 1.1 mrg
20 1.1 mrg bld_dir = ""
21 1.1 mrg # search path for headers
22 1.1 mrg incl_dirs = ["../include", "../libcpp/include", "common", "c-family", "c", "cp", "config" ]
23 1.1 mrg # extra search paths to look in *after* the directory the source file is in.
24 1.1 mrg
25 1.1 mrg # append (1) to the end of the first line which includes INC in list INC.
26 1.1 mrg def append_1 (output, inc):
27 1.1 mrg for n,t in enumerate (output):
28 1.1 mrg idx = t.find(inc)
29 1.1 mrg if idx != -1:
30 1.1 mrg eos = idx + len (inc)
31 1.1 mrg t = t[:eos] + " (1)" + t[eos+1:]
32 1.1 mrg output[n] = t
33 1.1 mrg return
34 1.1 mrg
35 1.1 mrg # These headers show up as duplicates in rtl.h due to conditional code arund the includes
36 1.1 mrg rtl_core = [ "machmode.h" , "signop.h" , "wide-int.h" , "double-int.h" , "real.h" , "fixed-value.h" , "statistics.h" , "vec.h" , "hash-table.h" , "hash-set.h" , "input.h" , "is-a.h" ]
37 1.1 mrg
38 1.1 mrg def find_include_data (inc):
39 1.1 mrg global sawcore
40 1.1 mrg for x in incl_dirs:
41 1.1 mrg nm = x+"/"+inc
42 1.1 mrg if os.path.exists (nm):
43 1.1 mrg info = find_unique_include_list (nm)
44 1.1 mrg # rtl.h mimics coretypes for GENERATOR FILES, remove if coretypes.h seen.
45 1.1 mrg if inc == "coretypes.h":
46 1.1 mrg sawcore = True
47 1.1 mrg elif inc == "rtl.h" and sawcore:
48 1.1 mrg for i in rtl_core:
49 1.1 mrg if i in info:
50 1.1 mrg info.remove (i)
51 1.1 mrg return info
52 1.1 mrg return list()
53 1.1 mrg
54 1.1 mrg def process_include (inc, indent):
55 1.1 mrg if inc[-2:] != ".h":
56 1.1 mrg return
57 1.1 mrg bname = os.path.basename (inc)
58 1.1 mrg if bname in highlight:
59 1.1 mrg arrow = " <<-------"
60 1.1 mrg if bname not in summary:
61 1.1 mrg summary.append (bname)
62 1.1 mrg else:
63 1.1 mrg arrow = ""
64 1.1 mrg if seen.get(inc) == None:
65 1.1 mrg seen[inc] = 1
66 1.1 mrg output.append (padding[:indent*tabstop] + bname + arrow)
67 1.1 mrg info = find_include_data (inc)
68 1.1 mrg for y in info:
69 1.1 mrg process_include (y, indent+1)
70 1.1 mrg else:
71 1.1 mrg seen[inc] += 1
72 1.1 mrg if (seen[inc] == 2):
73 1.1 mrg append_1(output, inc)
74 1.1 mrg output.append (padding[:indent*tabstop] + bname + " ("+str(seen[inc])+")" + arrow)
75 1.1 mrg
76 1.1 mrg
77 1.1 mrg
78 1.1 mrg extradir = list()
79 1.1 mrg usage = False
80 1.1 mrg src = list()
81 1.1 mrg
82 1.1 mrg for x in sys.argv[1:]:
83 1.1 mrg if x[0:2] == "-i":
84 1.1 mrg bld = x[2:]
85 1.1 mrg extradir.append (bld)
86 1.1 mrg elif x[0:2] == "-s":
87 1.1 mrg highlight.append (os.path.basename (x[2:]))
88 1.1 mrg elif x[0:2] == "-h":
89 1.1 mrg usage = True
90 1.1 mrg else:
91 1.1 mrg src.append (x)
92 1.1 mrg
93 1.1 mrg if len(src) != 1:
94 1.1 mrg usage = True
95 1.1 mrg elif not os.path.exists (src[0]):
96 1.1 mrg print src[0] + ": Requested source file does not exist.\n"
97 1.1 mrg usage = True
98 1.1 mrg
99 1.1 mrg if usage:
100 1.1 mrg print "show-headers [-idir] [-sfilen] file1 "
101 1.1 mrg print " "
102 1.1 mrg print " Show a hierarchical visual format how many times each header file"
103 1.1 mrg print " is included in a source file. Should be run from the source directory"
104 1.1 mrg print " files from find-include-depends"
105 1.1 mrg print " -s : search for a header, and point it out."
106 1.1 mrg print " -i : Specifies additonal directories to search for includes."
107 1.1 mrg sys.exit(0)
108 1.1 mrg
109 1.1 mrg
110 1.1 mrg
111 1.1 mrg if extradir:
112 1.1 mrg incl_dirs = extradir + incl_dirs;
113 1.1 mrg
114 1.1 mrg blddir = find_gcc_bld_dir ("../..")
115 1.1 mrg
116 1.1 mrg if blddir:
117 1.1 mrg print "Using build directory: " + blddir
118 1.1 mrg incl_dirs.insert (0, blddir)
119 1.1 mrg else:
120 1.1 mrg print "Could not find a build directory, better results if you specify one with -i"
121 1.1 mrg
122 1.1 mrg # search path is now ".", blddir, extradirs_from_-i, built_in_incl_dirs
123 1.1 mrg incl_dirs.insert (0, ".")
124 1.1 mrg
125 1.1 mrg # if source is in a subdirectory, prepend the subdirectory to the search list
126 1.1 mrg x = src[0]
127 1.1 mrg srcpath = os.path.dirname(x)
128 1.1 mrg if srcpath:
129 1.1 mrg incl_dirs.insert (0, srcpath)
130 1.1 mrg
131 1.1 mrg output = list()
132 1.1 mrg sawcore = False
133 1.1 mrg
134 1.1 mrg data = open (x).read().splitlines()
135 1.1 mrg for line in data:
136 1.1 mrg d = find_pound_include (line, True, True)
137 1.1 mrg if d and d[-2:] == ".h":
138 1.1 mrg process_include (d, 1)
139 1.1 mrg
140 1.1 mrg print "\n" + x
141 1.1 mrg for line in output:
142 1.1 mrg print line
143 1.1 mrg
144 1.1 mrg if highlight:
145 1.1 mrg print " "
146 1.1 mrg for h in summary:
147 1.1 mrg print h + " is included by source file."
148 1.1 mrg for h in highlight:
149 1.1 mrg if h not in summary:
150 1.1 mrg print h + " is not included by source file."
151 1.1 mrg
152