blob: 54cf915ac6c5615d45cde366bfd39eaa2eefbce1 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Mathias Rav3fb4a3a2018-05-29 15:41:36 +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'''
7Build r8lib.jar using src/main/keep.txt and test that d8_api_usage_sample.jar
8works with the minified R8.
9'''
10
11import argparse
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020012import gradle
Ian Zerny3f54e222019-02-12 10:51:17 +010013import jdk
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020014import os
15import subprocess
16import toolhelper
17import utils
18
19parser = argparse.ArgumentParser(description=__doc__.strip(),
20 formatter_class=argparse.RawTextHelpFormatter)
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020021parser.add_argument('-e', '--exclude_deps', action='store_true',
22 help='Create lib jar without dependencies')
23parser.add_argument('-k', '--keep', default=utils.R8LIB_KEEP_RULES,
24 help='Keep rules file for lib')
25parser.add_argument('-n', '--no_relocate', action='store_true',
26 help='Create lib jar without relocating libraries')
27parser.add_argument('-o', '--out', default=None,
28 help='Output for built library')
29parser.add_argument('-t', '--target', default='r8',
30 help='Compile target for library')
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020031
32API_LEVEL = 26
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020033DEPS_JAR = os.path.join(utils.LIBS, 'deps.jar')
34SAMPLE_JAR = os.path.join(utils.REPO_ROOT, 'tests', 'd8_api_usage_sample.jar')
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020035
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020036def build_r8lib(target, exclude_deps, no_relocate, keep_rules_path,
37 output_path, **kwargs):
38 # Clean the build directory to ensure no repackaging of any existing
39 # lib or deps.
40 gradle.RunGradle(['clean'])
41 lib_args = [target]
42 deps_args = ['repackageDeps']
43 if exclude_deps:
44 lib_args.append('-Pexclude_deps')
45 if no_relocate:
46 lib_args.append('-Plib_no_relocate')
47 deps_args.append('-Plib_no_relocate')
48 # Produce the r8lib target to be processed later.
49 gradle.RunGradle(lib_args)
50 target_lib = os.path.join(utils.LIBS, target + '.jar')
51 temp_lib = os.path.join(utils.LIBS, target + '_to_process.jar')
52 os.rename(target_lib, temp_lib)
53 # Produce the dependencies needed for running r8 on lib.jar.
54 gradle.RunGradle(deps_args)
55 temp_deps = os.path.join(utils.LIBS, target + 'lib_deps.jar')
56 os.rename(DEPS_JAR, temp_deps)
57 # Produce R8 for compiling lib
Mathias Rav56df69d2018-06-07 12:38:21 +020058 if output_path is None:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020059 output_path = target + 'lib.jar'
Tamas Kenez54b939a2018-12-07 15:55:26 +010060 output_map_path = output_path + '.map'
Morten Krogh-Jesperseneda55812023-04-25 09:26:07 +020061 r8_output_map_path = output_path + '_map.zip'
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020062 toolhelper.run(
63 'r8',
64 ('--release',
65 '--classfile',
66 '--lib', utils.RT_JAR,
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020067 '--lib', temp_deps,
68 temp_lib,
Mathias Rav56df69d2018-06-07 12:38:21 +020069 '--output', output_path,
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020070 '--pg-conf', keep_rules_path,
Morten Krogh-Jesperseneda55812023-04-25 09:26:07 +020071 '--pg-map-output', output_map_path,
72 '--partition-map-output', r8_output_map_path),
Mathias Rav56df69d2018-06-07 12:38:21 +020073 **kwargs)
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020074 if exclude_deps:
75 return [output_path, temp_deps]
76 else:
77 return [output_path]
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020078
79
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020080def test_d8sample(paths):
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020081 with utils.TempDir() as path:
Ian Zerny3f54e222019-02-12 10:51:17 +010082 args = [jdk.GetJavaExecutable(),
83 '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020084 'com.android.tools.apiusagesample.D8ApiUsageSample',
85 '--output', path,
86 '--min-api', str(API_LEVEL),
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020087 '--lib', utils.get_android_jar(API_LEVEL),
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020088 '--classpath', utils.R8_JAR,
89 '--main-dex-list', '/dev/null',
90 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
91 utils.PrintCmd(args)
92 subprocess.check_call(args)
93
94
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020095def test_r8command(paths):
Mathias Rave3f3c522018-05-30 08:22:17 +020096 with utils.TempDir() as path:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020097 # SAMPLE_JAR and LIB_JAR should not have any classes in common, since e.g.
98 # R8CommandParser should have been minified in LIB_JAR.
99 # Just in case R8CommandParser is also present in LIB_JAR, we put
Mathias Rave3f3c522018-05-30 08:22:17 +0200100 # SAMPLE_JAR first on the classpath to use its version of R8CommandParser.
Ian Zerny3f54e222019-02-12 10:51:17 +0100101 args = [jdk.GetJavaExecutable(),
102 '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rave3f3c522018-05-30 08:22:17 +0200103 'com.android.tools.r8.R8CommandParser',
104 '--output', path + "/output.zip",
105 '--min-api', str(API_LEVEL),
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200106 '--lib', utils.get_android_jar(API_LEVEL),
Mathias Rave3f3c522018-05-30 08:22:17 +0200107 '--main-dex-list', '/dev/null',
108 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
109 utils.PrintCmd(args)
110 subprocess.check_call(args)
111
112
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200113def test_r8cfcommand(paths):
Mathias Rave3f3c522018-05-30 08:22:17 +0200114 with utils.TempDir() as path:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200115 # SAMPLE_JAR and LIB_JAR should not have any classes in common, since e.g.
116 # R8CommandParser should have been minified in LIB_JAR.
117 # Just in case R8CommandParser is also present in LIB_JAR, we put
Mathias Rave3f3c522018-05-30 08:22:17 +0200118 # SAMPLE_JAR first on the classpath to use its version of R8CommandParser.
Ian Zerny3f54e222019-02-12 10:51:17 +0100119 args = [jdk.GetJavaExecutable(),
120 '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rave3f3c522018-05-30 08:22:17 +0200121 'com.android.tools.r8.R8CommandParser',
122 '--classfile',
123 '--output', path + "/output.jar",
124 '--lib', utils.RT_JAR,
125 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
126 utils.PrintCmd(args)
127 subprocess.check_call(args)
128
129
Mathias Rav3fb4a3a2018-05-29 15:41:36 +0200130def main():
131 # Handle --help
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200132 args = parser.parse_args()
133 output_paths = build_r8lib(
134 args.target, args.exclude_deps, args.no_relocate, args.keep, args.out)
135 if args.target == 'r8':
136 gradle.RunGradle(['buildExampleJars'])
137 test_r8command(output_paths)
138 test_r8cfcommand(output_paths)
139 if args.target == 'd8':
140 gradle.RunGradle(['buildExampleJars'])
141 test_d8sample(output_paths)
Mathias Rav3fb4a3a2018-05-29 15:41:36 +0200142
143
144if __name__ == '__main__':
145 main()