Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | ''' |
| 7 | Compare the R8 API used by the API usage sample to the API kept by @Keep. |
| 8 | ''' |
| 9 | |
| 10 | import argparse |
| 11 | import os |
| 12 | import subprocess |
| 13 | import utils |
| 14 | |
| 15 | parser = argparse.ArgumentParser(description=__doc__.strip(), |
| 16 | formatter_class=argparse.RawTextHelpFormatter) |
| 17 | parser.add_argument('-o', '--output-dir') |
| 18 | |
| 19 | API_SAMPLE_JAR = 'tests/d8_api_usage_sample.jar' |
| 20 | |
| 21 | |
| 22 | def main(output_dir=None): |
| 23 | if output_dir is None: |
| 24 | output_dir = '' |
| 25 | |
| 26 | printseeds_path = os.path.join(output_dir, 'keep-seeds.txt') |
| 27 | printseeds_args = [ |
| 28 | 'java', '-jar', utils.R8_JAR, 'printseeds', |
| 29 | utils.RT_JAR, utils.R8_JAR, utils.R8LIB_KEEP_RULES, |
| 30 | ] |
| 31 | write_sorted_lines(printseeds_args, printseeds_path) |
| 32 | |
| 33 | printuses_path = os.path.join(output_dir, 'sample-uses.txt') |
| 34 | printuses_args = [ |
| 35 | 'java', '-jar', utils.R8_JAR, 'printuses', |
| 36 | utils.RT_JAR, utils.R8_JAR, API_SAMPLE_JAR, |
| 37 | ] |
| 38 | write_sorted_lines(printuses_args, printuses_path) |
| 39 | |
| 40 | print_diff(printseeds_path, printuses_path) |
| 41 | |
| 42 | |
| 43 | def write_sorted_lines(cmd_args, output_path): |
| 44 | utils.PrintCmd(cmd_args) |
| 45 | output_lines = subprocess.check_output(cmd_args).splitlines(True) |
| 46 | print("Write output to %s" % output_path) |
| 47 | output_lines.sort() |
| 48 | with open(output_path, 'w') as fp: |
| 49 | for line in output_lines: |
| 50 | fp.write(line) |
| 51 | |
| 52 | |
| 53 | def print_diff(printseeds_path, printuses_path): |
| 54 | with open(printseeds_path) as fp: |
| 55 | seeds = set(fp.read().splitlines()) |
| 56 | with open(printuses_path) as fp: |
| 57 | uses = set(fp.read().splitlines()) |
| 58 | only_in_seeds = seeds - uses |
| 59 | only_in_uses = uses - seeds |
| 60 | if only_in_seeds: |
| 61 | print("%s lines with '-' are marked @Keep " % len(only_in_seeds) + |
| 62 | "but not used by sample.") |
| 63 | if only_in_uses: |
| 64 | print("%s lines with '+' are used by sample " % len(only_in_uses) + |
| 65 | "but are missing @Keep annotations.") |
| 66 | for line in sorted(only_in_seeds): |
| 67 | print('-' + line) |
| 68 | for line in sorted(only_in_uses): |
| 69 | print('+' + line) |
| 70 | if not only_in_seeds and not only_in_uses: |
| 71 | print('Sample uses the entire set of members marked @Keep. Well done!') |
| 72 | |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | main(**vars(parser.parse_args())) |