1#!/usr/bin/env python3
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"""
53import csv
54import sys
55
56def count_deps(depsdb, module, seen):
57    """Based on the depsdb, count the number of transitive dependencies.
58
59    You can pass in an reversed dependency graph to conut the number of
60    modules that depend on the module."""
61    count = 0
62    seen.append(module)
63    if module in depsdb:
64        for dep in depsdb[module]:
65            if dep in seen:
66                continue
67            count += 1 + count_deps(depsdb, dep, seen)
68    return count
69
70def process(reader):
71    """Read the input file and produce a list of modules ready to move to Soong
72    """
73    problems = dict()
74    deps = dict()
75    reverse_deps = dict()
76    module_types = dict()
77
78    for (module, module_type, problem, dependencies, makefiles, installed) in reader:
79        module_types[module] = module_type
80        problems[module] = problem
81        deps[module] = [d for d in dependencies.strip().split(' ') if d != ""]
82        for dep in deps[module]:
83            if not dep in reverse_deps:
84                reverse_deps[dep] = []
85            reverse_deps[dep].append(module)
86
87    results = []
88    for module in problems:
89        # Only display actionable conversions, ones without missing dependencies
90        if len(deps[module]) != 0:
91            continue
92
93        extra = ""
94        if len(problems[module]) > 0:
95            extra = " ({})".format(problems[module])
96        results.append((count_deps(reverse_deps, module, []), module + extra, module_types[module]))
97
98    return sorted(results, key=lambda result: (-result[0], result[1]))
99
100def filter(results, module_type):
101    return [x for x in results if x[2] == module_type]
102
103def display(results):
104    """Displays the results"""
105    count_header = "# Blocked on"
106    count_width = len(count_header)
107    print("{} Module (potential problems)".format(count_header))
108    for (count, module, module_type) in results:
109        print("{:>{}} {}".format(count, count_width, module))
110
111def main(filename):
112    """Read the CSV file, print the results"""
113    with open(filename, 'r') as csvfile:
114        results = process(csv.reader(csvfile))
115
116    native_results = filter(results, "native")
117    java_results = filter(results, "java")
118
119    print("native modules ready to convert")
120    display(native_results)
121
122    print("")
123    print("java modules ready to convert")
124    display(java_results)
125
126if __name__ == "__main__":
127    if len(sys.argv) != 2:
128        print("usage: soong_conversion.py <file>", file=sys.stderr)
129        sys.exit(1)
130
131    main(sys.argv[1])
132