1#!/usr/bin/env python
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Tool to prioritize which modules to convert to Soong.
18
19Generally, you'd use this through the make integration, which automatically
20generates the CSV input file that this tool expects:
21
22  $ m $OUT/soong_to_convert.txt
23  $ less $OUT/soong_to_convert.txt
24
25The output is a list of modules that are probably ready to convert to Soong:
26
27  # Blocked on Module (potential problems)
28           283 libEGL (srcs_dotarm)
29           246 libicuuc (dotdot_incs dotdot_srcs)
30           221 libspeexresampler
31           215 libcamera_metadata
32               ...
33             0 zram-perf (dotdot_incs)
34
35The number at the beginning of the line shows how many native modules depend
36on that module.
37
38All of their dependencies have been satisfied, and any potential problems
39that Make can detect are listed in parenthesis after the module:
40
41  dotdot_srcs: LOCAL_SRC_FILES contains paths outside $(LOCAL_PATH)
42  dotdot_incs: LOCAL_C_INCLUDES contains paths include '..'
43  srcs_dotarm: LOCAL_SRC_FILES contains source files like <...>.c.arm
44  aidl: LOCAL_SRC_FILES contains .aidl sources
45  objc: LOCAL_SRC_FILES contains Objective-C sources
46  proto: LOCAL_SRC_FILES contains .proto sources
47  rs: LOCAL_SRC_FILES contains renderscript sources
48  vts: LOCAL_SRC_FILES contains .vts sources
49
50Not all problems can be discovered, but this is a starting point.
51
52"""
53
54from __future__ import print_function
55
56import csv
57import sys
58
59def count_deps(depsdb, module, seen):
60    """Based on the depsdb, count the number of transitive dependencies.
61
62    You can pass in an reversed dependency graph to conut the number of
63    modules that depend on the module."""
64    count = 0
65    seen.append(module)
66    if module in depsdb:
67        for dep in depsdb[module]:
68            if dep in seen:
69                continue
70            count += 1 + count_deps(depsdb, dep, seen)
71    return count
72
73def process(reader):
74    """Read the input file and produce a list of modules ready to move to Soong
75    """
76    problems = dict()
77    deps = dict()
78    reverse_deps = dict()
79
80    for (module, problem, dependencies) in reader:
81        problems[module] = problem
82        deps[module] = [d for d in dependencies.strip().split(' ') if d != ""]
83        for dep in deps[module]:
84            if not dep in reverse_deps:
85                reverse_deps[dep] = []
86            reverse_deps[dep].append(module)
87
88    results = []
89    for module in problems:
90        # Only display actionable conversions, ones without missing dependencies
91        if len(deps[module]) != 0:
92            continue
93
94        extra = ""
95        if len(problems[module]) > 0:
96            extra = " ({})".format(problems[module])
97        results.append((count_deps(reverse_deps, module, []), module + extra))
98
99    return sorted(results, key=lambda result: (-result[0], result[1]))
100
101def display(results):
102    """Displays the results"""
103    count_header = "# Blocked on"
104    count_width = len(count_header)
105    print("{} Module (potential problems)".format(count_header))
106    for (count, module) in results:
107        print("{:>{}} {}".format(count, count_width, module))
108
109def main(filename):
110    """Read the CSV file, print the results"""
111    with open(filename, 'rb') as csvfile:
112        results = process(csv.reader(csvfile))
113
114    display(results)
115
116if __name__ == "__main__":
117    if len(sys.argv) != 2:
118        print("usage: soong_conversion.py <file>", file=sys.stderr)
119        sys.exit(1)
120
121    main(sys.argv[1])
122