Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 1 | #!/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 | ''' |
| 7 | Build r8lib.jar using src/main/keep.txt and test that d8_api_usage_sample.jar |
| 8 | works with the minified R8. |
| 9 | ''' |
| 10 | |
| 11 | import argparse |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 12 | import gradle |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 13 | import jdk |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 14 | import os |
| 15 | import subprocess |
| 16 | import toolhelper |
| 17 | import utils |
| 18 | |
| 19 | parser = argparse.ArgumentParser(description=__doc__.strip(), |
| 20 | formatter_class=argparse.RawTextHelpFormatter) |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 21 | parser.add_argument('-e', '--exclude_deps', action='store_true', |
| 22 | help='Create lib jar without dependencies') |
| 23 | parser.add_argument('-k', '--keep', default=utils.R8LIB_KEEP_RULES, |
| 24 | help='Keep rules file for lib') |
| 25 | parser.add_argument('-n', '--no_relocate', action='store_true', |
| 26 | help='Create lib jar without relocating libraries') |
| 27 | parser.add_argument('-o', '--out', default=None, |
| 28 | help='Output for built library') |
| 29 | parser.add_argument('-t', '--target', default='r8', |
| 30 | help='Compile target for library') |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 31 | |
| 32 | API_LEVEL = 26 |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 33 | DEPS_JAR = os.path.join(utils.LIBS, 'deps.jar') |
| 34 | SAMPLE_JAR = os.path.join(utils.REPO_ROOT, 'tests', 'd8_api_usage_sample.jar') |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 35 | |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 36 | def 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 Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 58 | if output_path is None: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 59 | output_path = target + 'lib.jar' |
Tamas Kenez | 54b939a | 2018-12-07 15:55:26 +0100 | [diff] [blame] | 60 | output_map_path = output_path + '.map' |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 61 | toolhelper.run( |
| 62 | 'r8', |
| 63 | ('--release', |
| 64 | '--classfile', |
| 65 | '--lib', utils.RT_JAR, |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 66 | '--lib', temp_deps, |
| 67 | temp_lib, |
Mathias Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 68 | '--output', output_path, |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 69 | '--pg-conf', keep_rules_path, |
| 70 | '--pg-map-output', output_map_path), |
Mathias Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 71 | **kwargs) |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 72 | if exclude_deps: |
| 73 | return [output_path, temp_deps] |
| 74 | else: |
| 75 | return [output_path] |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 76 | |
| 77 | |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 78 | def test_d8sample(paths): |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 79 | with utils.TempDir() as path: |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 80 | args = [jdk.GetJavaExecutable(), |
| 81 | '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 82 | 'com.android.tools.apiusagesample.D8ApiUsageSample', |
| 83 | '--output', path, |
| 84 | '--min-api', str(API_LEVEL), |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 85 | '--lib', utils.get_android_jar(API_LEVEL), |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 86 | '--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-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 93 | def test_r8command(paths): |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 94 | with utils.TempDir() as path: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 95 | # 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 Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 98 | # SAMPLE_JAR first on the classpath to use its version of R8CommandParser. |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 99 | args = [jdk.GetJavaExecutable(), |
| 100 | '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 101 | 'com.android.tools.r8.R8CommandParser', |
| 102 | '--output', path + "/output.zip", |
| 103 | '--min-api', str(API_LEVEL), |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 104 | '--lib', utils.get_android_jar(API_LEVEL), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 105 | '--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-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 111 | def test_r8cfcommand(paths): |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 112 | with utils.TempDir() as path: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 113 | # 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 Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 116 | # SAMPLE_JAR first on the classpath to use its version of R8CommandParser. |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 117 | args = [jdk.GetJavaExecutable(), |
| 118 | '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 119 | '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 Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 128 | def main(): |
| 129 | # Handle --help |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 130 | 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 Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 140 | |
| 141 | |
| 142 | if __name__ == '__main__': |
| 143 | main() |