blob: 5368f6f4c1d49b3aca51e649c107522bfb21131b [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
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020013import os
14import subprocess
15import toolhelper
16import utils
17
18parser = argparse.ArgumentParser(description=__doc__.strip(),
19 formatter_class=argparse.RawTextHelpFormatter)
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020020parser.add_argument('-e', '--exclude_deps', action='store_true',
21 help='Create lib jar without dependencies')
22parser.add_argument('-k', '--keep', default=utils.R8LIB_KEEP_RULES,
23 help='Keep rules file for lib')
24parser.add_argument('-n', '--no_relocate', action='store_true',
25 help='Create lib jar without relocating libraries')
26parser.add_argument('-o', '--out', default=None,
27 help='Output for built library')
28parser.add_argument('-t', '--target', default='r8',
29 help='Compile target for library')
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020030
31API_LEVEL = 26
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020032DEPS_JAR = os.path.join(utils.LIBS, 'deps.jar')
33SAMPLE_JAR = os.path.join(utils.REPO_ROOT, 'tests', 'd8_api_usage_sample.jar')
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020034
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020035def build_r8lib(target, exclude_deps, no_relocate, keep_rules_path,
36 output_path, **kwargs):
37 # Clean the build directory to ensure no repackaging of any existing
38 # lib or deps.
39 gradle.RunGradle(['clean'])
40 lib_args = [target]
41 deps_args = ['repackageDeps']
42 if exclude_deps:
43 lib_args.append('-Pexclude_deps')
44 if no_relocate:
45 lib_args.append('-Plib_no_relocate')
46 deps_args.append('-Plib_no_relocate')
47 # Produce the r8lib target to be processed later.
48 gradle.RunGradle(lib_args)
49 target_lib = os.path.join(utils.LIBS, target + '.jar')
50 temp_lib = os.path.join(utils.LIBS, target + '_to_process.jar')
51 os.rename(target_lib, temp_lib)
52 # Produce the dependencies needed for running r8 on lib.jar.
53 gradle.RunGradle(deps_args)
54 temp_deps = os.path.join(utils.LIBS, target + 'lib_deps.jar')
55 os.rename(DEPS_JAR, temp_deps)
56 # Produce R8 for compiling lib
Mathias Rav56df69d2018-06-07 12:38:21 +020057 if output_path is None:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020058 output_path = target + 'lib.jar'
Tamas Kenez54b939a2018-12-07 15:55:26 +010059 output_map_path = output_path + '.map'
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020060 toolhelper.run(
61 'r8',
62 ('--release',
63 '--classfile',
64 '--lib', utils.RT_JAR,
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020065 '--lib', temp_deps,
66 temp_lib,
Mathias Rav56df69d2018-06-07 12:38:21 +020067 '--output', output_path,
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020068 '--pg-conf', keep_rules_path,
69 '--pg-map-output', output_map_path),
Mathias Rav56df69d2018-06-07 12:38:21 +020070 **kwargs)
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020071 if exclude_deps:
72 return [output_path, temp_deps]
73 else:
74 return [output_path]
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020075
76
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020077def test_d8sample(paths):
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020078 with utils.TempDir() as path:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020079 args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020080 'com.android.tools.apiusagesample.D8ApiUsageSample',
81 '--output', path,
82 '--min-api', str(API_LEVEL),
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020083 '--lib', utils.get_android_jar(API_LEVEL),
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020084 '--classpath', utils.R8_JAR,
85 '--main-dex-list', '/dev/null',
86 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
87 utils.PrintCmd(args)
88 subprocess.check_call(args)
89
90
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020091def test_r8command(paths):
Mathias Rave3f3c522018-05-30 08:22:17 +020092 with utils.TempDir() as path:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020093 # SAMPLE_JAR and LIB_JAR should not have any classes in common, since e.g.
94 # R8CommandParser should have been minified in LIB_JAR.
95 # Just in case R8CommandParser is also present in LIB_JAR, we put
Mathias Rave3f3c522018-05-30 08:22:17 +020096 # SAMPLE_JAR first on the classpath to use its version of R8CommandParser.
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +020097 args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rave3f3c522018-05-30 08:22:17 +020098 'com.android.tools.r8.R8CommandParser',
99 '--output', path + "/output.zip",
100 '--min-api', str(API_LEVEL),
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200101 '--lib', utils.get_android_jar(API_LEVEL),
Mathias Rave3f3c522018-05-30 08:22:17 +0200102 '--main-dex-list', '/dev/null',
103 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
104 utils.PrintCmd(args)
105 subprocess.check_call(args)
106
107
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200108def test_r8cfcommand(paths):
Mathias Rave3f3c522018-05-30 08:22:17 +0200109 with utils.TempDir() as path:
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200110 # SAMPLE_JAR and LIB_JAR should not have any classes in common, since e.g.
111 # R8CommandParser should have been minified in LIB_JAR.
112 # Just in case R8CommandParser is also present in LIB_JAR, we put
Mathias Rave3f3c522018-05-30 08:22:17 +0200113 # SAMPLE_JAR first on the classpath to use its version of R8CommandParser.
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200114 args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)),
Mathias Rave3f3c522018-05-30 08:22:17 +0200115 'com.android.tools.r8.R8CommandParser',
116 '--classfile',
117 '--output', path + "/output.jar",
118 '--lib', utils.RT_JAR,
119 os.path.join(utils.BUILD, 'test/examples/hello.jar')]
120 utils.PrintCmd(args)
121 subprocess.check_call(args)
122
123
Mathias Rav3fb4a3a2018-05-29 15:41:36 +0200124def main():
125 # Handle --help
Morten Krogh-Jespersen00699af2018-10-09 10:54:42 +0200126 args = parser.parse_args()
127 output_paths = build_r8lib(
128 args.target, args.exclude_deps, args.no_relocate, args.keep, args.out)
129 if args.target == 'r8':
130 gradle.RunGradle(['buildExampleJars'])
131 test_r8command(output_paths)
132 test_r8cfcommand(output_paths)
133 if args.target == 'd8':
134 gradle.RunGradle(['buildExampleJars'])
135 test_d8sample(output_paths)
Mathias Rav3fb4a3a2018-05-29 15:41:36 +0200136
137
138if __name__ == '__main__':
139 main()