blob: 2d9b0d20ea0de1911c25920602d9b78749ddcd18 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Mathias Ravb46dc002018-06-06 09:37:11 +02002# 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'''
7Compare the R8 API used by the API usage sample to the API kept by @Keep.
8'''
9
10import argparse
Ian Zerny3f54e222019-02-12 10:51:17 +010011import jdk
Mathias Ravb46dc002018-06-06 09:37:11 +020012import os
13import subprocess
14import utils
15
16parser = argparse.ArgumentParser(description=__doc__.strip(),
17 formatter_class=argparse.RawTextHelpFormatter)
18parser.add_argument('-o', '--output-dir')
19
20API_SAMPLE_JAR = 'tests/d8_api_usage_sample.jar'
21
22
23def main(output_dir=None):
24 if output_dir is None:
25 output_dir = ''
26
Ian Zerny3f54e222019-02-12 10:51:17 +010027 javaExecutable = jdk.GetJavaExecutable()
Mathias Ravb46dc002018-06-06 09:37:11 +020028 printseeds_path = os.path.join(output_dir, 'keep-seeds.txt')
29 printseeds_args = [
Ian Zerny3f54e222019-02-12 10:51:17 +010030 javaExecutable, '-jar', utils.R8_JAR, 'printseeds',
Mathias Ravb46dc002018-06-06 09:37:11 +020031 utils.RT_JAR, utils.R8_JAR, utils.R8LIB_KEEP_RULES,
32 ]
33 write_sorted_lines(printseeds_args, printseeds_path)
34
35 printuses_path = os.path.join(output_dir, 'sample-uses.txt')
36 printuses_args = [
Ian Zerny3f54e222019-02-12 10:51:17 +010037 javaExecutable, '-jar', utils.R8_JAR, 'printuses',
Mathias Ravb46dc002018-06-06 09:37:11 +020038 utils.RT_JAR, utils.R8_JAR, API_SAMPLE_JAR,
39 ]
40 write_sorted_lines(printuses_args, printuses_path)
41
42 print_diff(printseeds_path, printuses_path)
43
44
45def write_sorted_lines(cmd_args, output_path):
46 utils.PrintCmd(cmd_args)
47 output_lines = subprocess.check_output(cmd_args).splitlines(True)
48 print("Write output to %s" % output_path)
49 output_lines.sort()
50 with open(output_path, 'w') as fp:
51 for line in output_lines:
52 fp.write(line)
53
54
55def print_diff(printseeds_path, printuses_path):
56 with open(printseeds_path) as fp:
57 seeds = set(fp.read().splitlines())
58 with open(printuses_path) as fp:
59 uses = set(fp.read().splitlines())
60 only_in_seeds = seeds - uses
61 only_in_uses = uses - seeds
62 if only_in_seeds:
63 print("%s lines with '-' are marked @Keep " % len(only_in_seeds) +
64 "but not used by sample.")
65 if only_in_uses:
66 print("%s lines with '+' are used by sample " % len(only_in_uses) +
67 "but are missing @Keep annotations.")
68 for line in sorted(only_in_seeds):
69 print('-' + line)
70 for line in sorted(only_in_uses):
71 print('+' + line)
72 if not only_in_seeds and not only_in_uses:
73 print('Sample uses the entire set of members marked @Keep. Well done!')
74
75
76if __name__ == '__main__':
77 main(**vars(parser.parse_args()))