Cole Faust | 5865420 | 2022-11-04 17:35:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 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 |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 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 | |
| 50 | Not all problems can be discovered, but this is a starting point. |
| 51 | |
| 52 | """ |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 53 | import csv |
| 54 | import sys |
| 55 | |
| 56 | def 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 | |
| 70 | def 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() |
Colin Cross | 3277ba3 | 2017-12-06 14:37:06 -0800 | [diff] [blame] | 76 | module_types = dict() |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 77 | |
Joe Onorato | 02fb89a | 2020-06-27 00:10:23 -0700 | [diff] [blame] | 78 | for (module, module_type, problem, dependencies, makefiles, installed) in reader: |
Colin Cross | 3277ba3 | 2017-12-06 14:37:06 -0800 | [diff] [blame] | 79 | module_types[module] = module_type |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 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]) |
Colin Cross | 3277ba3 | 2017-12-06 14:37:06 -0800 | [diff] [blame] | 96 | results.append((count_deps(reverse_deps, module, []), module + extra, module_types[module])) |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 97 | |
| 98 | return sorted(results, key=lambda result: (-result[0], result[1])) |
| 99 | |
Colin Cross | 3277ba3 | 2017-12-06 14:37:06 -0800 | [diff] [blame] | 100 | def filter(results, module_type): |
| 101 | return [x for x in results if x[2] == module_type] |
| 102 | |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 103 | def 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)) |
Colin Cross | 3277ba3 | 2017-12-06 14:37:06 -0800 | [diff] [blame] | 108 | for (count, module, module_type) in results: |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 109 | print("{:>{}} {}".format(count, count_width, module)) |
| 110 | |
| 111 | def main(filename): |
| 112 | """Read the CSV file, print the results""" |
Cole Faust | 5865420 | 2022-11-04 17:35:37 -0700 | [diff] [blame] | 113 | with open(filename, 'r') as csvfile: |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 114 | results = process(csv.reader(csvfile)) |
| 115 | |
Colin Cross | 3277ba3 | 2017-12-06 14:37:06 -0800 | [diff] [blame] | 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) |
Dan Willemsen | fc92fb2 | 2016-08-26 13:27:13 -0700 | [diff] [blame] | 125 | |
| 126 | if __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]) |