blob: 448a0beca38a9aa89b5d8551a963555dc1dd8ef1 [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.
Mathias Ravb46dc002018-06-06 09:37:11 +02005'''
6Compare the R8 API used by the API usage sample to the API kept by @Keep.
7'''
8
9import argparse
Ian Zerny3f54e222019-02-12 10:51:17 +010010import jdk
Mathias Ravb46dc002018-06-06 09:37:11 +020011import os
12import subprocess
13import utils
14
15parser = argparse.ArgumentParser(description=__doc__.strip(),
16 formatter_class=argparse.RawTextHelpFormatter)
17parser.add_argument('-o', '--output-dir')
18
19API_SAMPLE_JAR = 'tests/d8_api_usage_sample.jar'
20
21
22def main(output_dir=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020023 if output_dir is None:
24 output_dir = ''
Mathias Ravb46dc002018-06-06 09:37:11 +020025
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020026 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 Ravb46dc002018-06-06 09:37:11 +020038
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020039 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 Ravb46dc002018-06-06 09:37:11 +020050
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020051 print_diff(printseeds_path, printuses_path)
Mathias Ravb46dc002018-06-06 09:37:11 +020052
53
54def write_sorted_lines(cmd_args, output_path):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020055 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 Ravb46dc002018-06-06 09:37:11 +020062
63
64def print_diff(printseeds_path, printuses_path):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020065 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 Ravb46dc002018-06-06 09:37:11 +020083
84
85if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020086 main(**vars(parser.parse_args()))