blob: 38b375789aae99e7dabbde35cec2808417a7f814 [file] [log] [blame]
Zachary Turner1d752972017-03-06 17:41:00 +00001import argparse
Zachary Turnere030d102017-03-06 17:40:36 +00002import os
3import re
4
5from use_lldb_suite import lldb_root
6
Zachary Turner1d752972017-03-06 17:41:00 +00007parser = argparse.ArgumentParser(
8 description='Analyze LLDB project #include dependencies.')
9parser.add_argument('--show-counts', default=False, action='store_true',
10 help='When true, show the number of dependencies from each subproject')
11args = parser.parse_args()
12
Zachary Turnere030d102017-03-06 17:40:36 +000013src_dir = os.path.join(lldb_root, "source")
14inc_dir = os.path.join(lldb_root, "include")
15
16src_map = {}
17
Zachary Turner1d752972017-03-06 17:41:00 +000018include_regex = re.compile('#include \"((lldb|Plugins|clang)(.*/)+).*\"')
19
20def normalize_host(str):
21 if str.startswith("lldb/Host"):
22 return "lldb/Host"
23 return str
Zachary Turnere030d102017-03-06 17:40:36 +000024
25def scan_deps(this_dir, file):
Zachary Turner1d752972017-03-06 17:41:00 +000026 global src_map
27 deps = {}
28 this_dir = normalize_host(this_dir)
29 if this_dir in src_map:
30 deps = src_map[this_dir]
31
Zachary Turnere030d102017-03-06 17:40:36 +000032 with open(file) as f:
33 for line in list(f):
34 m = include_regex.match(line)
Zachary Turner1d752972017-03-06 17:41:00 +000035 if m is None:
36 continue
37 relative = m.groups()[0].rstrip("/")
38 if relative == this_dir:
39 continue
40 relative = normalize_host(relative)
41 if relative in deps:
42 deps[relative] += 1
43 else:
44 deps[relative] = 1
45 if this_dir not in src_map and len(deps) > 0:
46 src_map[this_dir] = deps
Zachary Turnere030d102017-03-06 17:40:36 +000047
48for (base, dirs, files) in os.walk(inc_dir):
49 dir = os.path.basename(base)
50 relative = os.path.relpath(base, inc_dir)
51 inc_files = filter(lambda x : os.path.splitext(x)[1] in [".h"], files)
Zachary Turnere030d102017-03-06 17:40:36 +000052 relative = relative.replace("\\", "/")
53 for inc in inc_files:
54 inc_path = os.path.join(base, inc)
Zachary Turner1d752972017-03-06 17:41:00 +000055 scan_deps(relative, inc_path)
Zachary Turnere030d102017-03-06 17:40:36 +000056
57for (base, dirs, files) in os.walk(src_dir):
58 dir = os.path.basename(base)
59 relative = os.path.relpath(base, src_dir)
60 src_files = filter(lambda x : os.path.splitext(x)[1] in [".cpp", ".h", ".mm"], files)
Zachary Turnere030d102017-03-06 17:40:36 +000061 norm_base_path = os.path.normpath(os.path.join("lldb", relative))
62 norm_base_path = norm_base_path.replace("\\", "/")
63 for src in src_files:
64 src_path = os.path.join(base, src)
Zachary Turner1d752972017-03-06 17:41:00 +000065 scan_deps(norm_base_path, src_path)
Zachary Turnere030d102017-03-06 17:40:36 +000066 pass
67
68items = list(src_map.iteritems())
69items.sort(lambda A, B : cmp(A[0], B[0]))
70
71for (path, deps) in items:
72 print path + ":"
Zachary Turner1d752972017-03-06 17:41:00 +000073 sorted_deps = list(deps.iteritems())
74 if args.show_counts:
75 sorted_deps.sort(lambda A, B: cmp(A[1], B[1]))
76 for dep in sorted_deps:
77 print "\t{} [{}]".format(dep[0], dep[1])
78 else:
79 sorted_deps.sort(lambda A, B: cmp(A[0], B[0]))
80 for dep in sorted_deps:
81 print "\t{}".format(dep[0])
Zachary Turner5013eb92017-03-03 22:40:46 +000082pass