Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 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. |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 5 | ''' |
| 6 | Compare the R8 API used by the API usage sample to the API kept by @Keep. |
| 7 | ''' |
| 8 | |
| 9 | import argparse |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 10 | import jdk |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 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): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 23 | if output_dir is None: |
| 24 | output_dir = '' |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 25 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 26 | javaExecutable = jdk.GetJavaExecutable() |
| 27 | printseeds_path = os.path.join(output_dir, 'keep-seeds.txt') |
| 28 | printseeds_args = [ |
| 29 | javaExecutable, |
| 30 | '-jar', |
| 31 | utils.R8_JAR, |
| 32 | 'printseeds', |
| 33 | utils.RT_JAR, |
| 34 | utils.R8_JAR, |
| 35 | utils.R8LIB_KEEP_RULES, |
| 36 | ] |
| 37 | write_sorted_lines(printseeds_args, printseeds_path) |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 38 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 39 | printuses_path = os.path.join(output_dir, 'sample-uses.txt') |
| 40 | printuses_args = [ |
| 41 | javaExecutable, |
| 42 | '-jar', |
| 43 | utils.R8_JAR, |
| 44 | 'printuses', |
| 45 | utils.RT_JAR, |
| 46 | utils.R8_JAR, |
| 47 | API_SAMPLE_JAR, |
| 48 | ] |
| 49 | write_sorted_lines(printuses_args, printuses_path) |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 50 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 51 | print_diff(printseeds_path, printuses_path) |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 52 | |
| 53 | |
| 54 | def write_sorted_lines(cmd_args, output_path): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 55 | utils.PrintCmd(cmd_args) |
| 56 | output_lines = subprocess.check_output(cmd_args).splitlines(True) |
| 57 | print("Write output to %s" % output_path) |
| 58 | output_lines.sort() |
| 59 | with open(output_path, 'w') as fp: |
| 60 | for line in output_lines: |
| 61 | fp.write(line) |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 62 | |
| 63 | |
| 64 | def print_diff(printseeds_path, printuses_path): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 65 | with open(printseeds_path) as fp: |
| 66 | seeds = set(fp.read().splitlines()) |
| 67 | with open(printuses_path) as fp: |
| 68 | uses = set(fp.read().splitlines()) |
| 69 | only_in_seeds = seeds - uses |
| 70 | only_in_uses = uses - seeds |
| 71 | if only_in_seeds: |
| 72 | print("%s lines with '-' are marked @Keep " % len(only_in_seeds) + |
| 73 | "but not used by sample.") |
| 74 | if only_in_uses: |
| 75 | print("%s lines with '+' are used by sample " % len(only_in_uses) + |
| 76 | "but are missing @Keep annotations.") |
| 77 | for line in sorted(only_in_seeds): |
| 78 | print('-' + line) |
| 79 | for line in sorted(only_in_uses): |
| 80 | print('+' + line) |
| 81 | if not only_in_seeds and not only_in_uses: |
| 82 | print('Sample uses the entire set of members marked @Keep. Well done!') |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 83 | |
| 84 | |
| 85 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 86 | main(**vars(parser.parse_args())) |