blob: 980715e2ccb8a17b8af4bbd21c1c2ddc7db9d2d2 [file] [log] [blame]
Mathias Rav3fb4a3a2018-05-29 15:41:36 +02001#!/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'''
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'
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020061 toolhelper.run(
62 'r8',
63 ('--release',
64 '--classfile',
65 '--lib', utils.RT_JAR,
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020066 '--lib', temp_deps,
67 temp_lib,
Mathias Rav56df69d2018-06-07 12:38:21 +020068 '--output', output_path,
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020069 '--pg-conf', keep_rules_path,
70 '--pg-map-output', output_map_path),
Mathias Rav56df69d2018-06-07 12:38:21 +020071 **kwargs)
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020072 if exclude_deps:
73 return [output_path, temp_deps]
74 else:
75 return [output_path]
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020076
77
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020078def test_d8sample(paths):
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020079 with utils.TempDir() as path:
Ian Zerny3f54e222019-02-12 10:51:17 +010080 args = [jdk.GetJavaExecutable(),
81 '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020082 'com.android.tools.apiusagesample.D8ApiUsageSample',
83 '--output', path,
84 '--min-api', str(API_LEVEL),
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020085 '--lib', utils.get_android_jar(API_LEVEL),
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020086 '--classpath', utils.R8_JAR,
87 '--main-dex-list', '/dev/null',
88 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
89 utils.PrintCmd(args)
90 subprocess.check_call(args)
91
92
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020093def test_r8command(paths):
Mathias Rave3f3c522018-05-30 08:22:17 +020094 with utils.TempDir() as path:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020095 # SAMPLE_JAR and LIB_JAR should not have any classes in common, since e.g.
96 # R8CommandParser should have been minified in LIB_JAR.
97 # Just in case R8CommandParser is also present in LIB_JAR, we put
Mathias Rave3f3c522018-05-30 08:22:17 +020098 # SAMPLE_JAR first on the classpath to use its version of R8CommandParser.
Ian Zerny3f54e222019-02-12 10:51:17 +010099 args = [jdk.GetJavaExecutable(),
100 '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rave3f3c522018-05-30 08:22:17 +0200101 'com.android.tools.r8.R8CommandParser',
102 '--output', path + "/output.zip",
103 '--min-api', str(API_LEVEL),
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200104 '--lib', utils.get_android_jar(API_LEVEL),
Mathias Rave3f3c522018-05-30 08:22:17 +0200105 '--main-dex-list', '/dev/null',
106 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
107 utils.PrintCmd(args)
108 subprocess.check_call(args)
109
110
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200111def test_r8cfcommand(paths):
Mathias Rave3f3c522018-05-30 08:22:17 +0200112 with utils.TempDir() as path:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200113 # SAMPLE_JAR and LIB_JAR should not have any classes in common, since e.g.
114 # R8CommandParser should have been minified in LIB_JAR.
115 # Just in case R8CommandParser is also present in LIB_JAR, we put
Mathias Rave3f3c522018-05-30 08:22:17 +0200116 # SAMPLE_JAR first on the classpath to use its version of R8CommandParser.
Ian Zerny3f54e222019-02-12 10:51:17 +0100117 args = [jdk.GetJavaExecutable(),
118 '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rave3f3c522018-05-30 08:22:17 +0200119 'com.android.tools.r8.R8CommandParser',
120 '--classfile',
121 '--output', path + "/output.jar",
122 '--lib', utils.RT_JAR,
123 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
124 utils.PrintCmd(args)
125 subprocess.check_call(args)
126
127
Mathias Rav3fb4a3a2018-05-29 15:41:36 +0200128def main():
129 # Handle --help
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200130 args = parser.parse_args()
131 output_paths = build_r8lib(
132 args.target, args.exclude_deps, args.no_relocate, args.keep, args.out)
133 if args.target == 'r8':
134 gradle.RunGradle(['buildExampleJars'])
135 test_r8command(output_paths)
136 test_r8cfcommand(output_paths)
137 if args.target == 'd8':
138 gradle.RunGradle(['buildExampleJars'])
139 test_d8sample(output_paths)
Mathias Rav3fb4a3a2018-05-29 15:41:36 +0200140
141
142if __name__ == '__main__':
143 main()