Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 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 | |
| 19 | Generally, you'd use this through the make integration, which automatically |
| 20 | generates the CSV input file that this tool expects: |
| 21 | |
| 22 | $ m $OUT/soong_to_convert.txt |
| 23 | $ less $OUT/soong_to_convert.txt |
| 24 | |
| 25 | The 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 | |
| 35 | The number at the beginning of the line shows how many native modules depend |
| 36 | on that module. |
| 37 | |
| 38 | All of their dependencies have been satisfied, and any potential problems |
| 39 | that 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 | dbus: LOCAL_SRC_FILES contains .dbus-xml sources |
| 46 | objc: LOCAL_SRC_FILES contains Objective-C sources |
| 47 | proto: LOCAL_SRC_FILES contains .proto sources |
| 48 | rs: LOCAL_SRC_FILES contains renderscript sources |
| 49 | vts: LOCAL_SRC_FILES contains .vts sources |
| 50 | |
| 51 | Not all problems can be discovered, but this is a starting point. |
| 52 | |
| 53 | """ |
| 54 | |
| 55 | from __future__ import print_function |
| 56 | |
| 57 | import csv |
| 58 | import sys |
| 59 | |
| 60 | def count_deps(depsdb, module, seen): |
| 61 | """Based on the depsdb, count the number of transitive dependencies. |
| 62 | |
| 63 | You can pass in an reversed dependency graph to conut the number of |
| 64 | modules that depend on the module.""" |
| 65 | count = 0 |
| 66 | seen.append(module) |
| 67 | if module in depsdb: |
| 68 | for dep in depsdb[module]: |
| 69 | if dep in seen: |
| 70 | continue |
| 71 | count += 1 + count_deps(depsdb, dep, seen) |
| 72 | return count |
| 73 | |
| 74 | def process(reader): |
| 75 | """Read the input file and produce a list of modules ready to move to Soong |
| 76 | """ |
| 77 | problems = dict() |
| 78 | deps = dict() |
| 79 | reverse_deps = dict() |
| 80 | |
| 81 | for (module, problem, dependencies) in reader: |
| 82 | problems[module] = problem |
| 83 | deps[module] = [d for d in dependencies.strip().split(' ') if d != ""] |
| 84 | for dep in deps[module]: |
| 85 | if not dep in reverse_deps: |
| 86 | reverse_deps[dep] = [] |
| 87 | reverse_deps[dep].append(module) |
| 88 | |
| 89 | results = [] |
| 90 | for module in problems: |
| 91 | # Only display actionable conversions, ones without missing dependencies |
| 92 | if len(deps[module]) != 0: |
| 93 | continue |
| 94 | |
| 95 | extra = "" |
| 96 | if len(problems[module]) > 0: |
| 97 | extra = " ({})".format(problems[module]) |
| 98 | results.append((count_deps(reverse_deps, module, []), module + extra)) |
| 99 | |
| 100 | return sorted(results, key=lambda result: (-result[0], result[1])) |
| 101 | |
| 102 | def display(results): |
| 103 | """Displays the results""" |
| 104 | count_header = "# Blocked on" |
| 105 | count_width = len(count_header) |
| 106 | print("{} Module (potential problems)".format(count_header)) |
| 107 | for (count, module) in results: |
| 108 | print("{:>{}} {}".format(count, count_width, module)) |
| 109 | |
| 110 | def main(filename): |
| 111 | """Read the CSV file, print the results""" |
| 112 | with open(filename, 'rb') as csvfile: |
| 113 | results = process(csv.reader(csvfile)) |
| 114 | |
| 115 | display(results) |
| 116 | |
| 117 | if __name__ == "__main__": |
| 118 | if len(sys.argv) != 2: |
| 119 | print("usage: soong_conversion.py <file>", file=sys.stderr) |
| 120 | sys.exit(1) |
| 121 | |
| 122 | main(sys.argv[1]) |