Home | History | Annotate | Download | only in tools
      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   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 """
     53 
     54 from __future__ import print_function
     55 
     56 import csv
     57 import sys
     58 
     59 def 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 
     73 def 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     module_types = dict()
     80 
     81     for (module, module_type, problem, dependencies) in reader:
     82         module_types[module] = module_type
     83         problems[module] = problem
     84         deps[module] = [d for d in dependencies.strip().split(' ') if d != ""]
     85         for dep in deps[module]:
     86             if not dep in reverse_deps:
     87                 reverse_deps[dep] = []
     88             reverse_deps[dep].append(module)
     89 
     90     results = []
     91     for module in problems:
     92         # Only display actionable conversions, ones without missing dependencies
     93         if len(deps[module]) != 0:
     94             continue
     95 
     96         extra = ""
     97         if len(problems[module]) > 0:
     98             extra = " ({})".format(problems[module])
     99         results.append((count_deps(reverse_deps, module, []), module + extra, module_types[module]))
    100 
    101     return sorted(results, key=lambda result: (-result[0], result[1]))
    102 
    103 def filter(results, module_type):
    104     return [x for x in results if x[2] == module_type]
    105 
    106 def display(results):
    107     """Displays the results"""
    108     count_header = "# Blocked on"
    109     count_width = len(count_header)
    110     print("{} Module (potential problems)".format(count_header))
    111     for (count, module, module_type) in results:
    112         print("{:>{}} {}".format(count, count_width, module))
    113 
    114 def main(filename):
    115     """Read the CSV file, print the results"""
    116     with open(filename, 'rb') as csvfile:
    117         results = process(csv.reader(csvfile))
    118 
    119     native_results = filter(results, "native")
    120     java_results = filter(results, "java")
    121 
    122     print("native modules ready to convert")
    123     display(native_results)
    124 
    125     print("")
    126     print("java modules ready to convert")
    127     display(java_results)
    128 
    129 if __name__ == "__main__":
    130     if len(sys.argv) != 2:
    131         print("usage: soong_conversion.py <file>", file=sys.stderr)
    132         sys.exit(1)
    133 
    134     main(sys.argv[1])
    135