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 |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 13 | import os |
| 14 | import subprocess |
| 15 | import toolhelper |
| 16 | import utils |
| 17 | |
| 18 | parser = argparse.ArgumentParser(description=__doc__.strip(), |
| 19 | formatter_class=argparse.RawTextHelpFormatter) |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 20 | parser.add_argument('-e', '--exclude_deps', action='store_true', |
| 21 | help='Create lib jar without dependencies') |
| 22 | parser.add_argument('-k', '--keep', default=utils.R8LIB_KEEP_RULES, |
| 23 | help='Keep rules file for lib') |
| 24 | parser.add_argument('-n', '--no_relocate', action='store_true', |
| 25 | help='Create lib jar without relocating libraries') |
| 26 | parser.add_argument('-o', '--out', default=None, |
| 27 | help='Output for built library') |
| 28 | parser.add_argument('-t', '--target', default='r8', |
| 29 | help='Compile target for library') |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 30 | |
| 31 | API_LEVEL = 26 |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 32 | DEPS_JAR = os.path.join(utils.LIBS, 'deps.jar') |
| 33 | 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] | 34 | |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 35 | def 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 Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 57 | if output_path is None: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 58 | output_path = target + 'lib.jar' |
Tamas Kenez | 54b939a | 2018-12-07 15:55:26 +0100 | [diff] [blame] | 59 | output_map_path = output_path + '.map' |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 60 | toolhelper.run( |
| 61 | 'r8', |
| 62 | ('--release', |
| 63 | '--classfile', |
| 64 | '--lib', utils.RT_JAR, |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 65 | '--lib', temp_deps, |
| 66 | temp_lib, |
Mathias Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 67 | '--output', output_path, |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 68 | '--pg-conf', keep_rules_path, |
| 69 | '--pg-map-output', output_map_path), |
Mathias Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 70 | **kwargs) |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 71 | if exclude_deps: |
| 72 | return [output_path, temp_deps] |
| 73 | else: |
| 74 | return [output_path] |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 75 | |
| 76 | |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 77 | def test_d8sample(paths): |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 78 | with utils.TempDir() as path: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 79 | args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 80 | 'com.android.tools.apiusagesample.D8ApiUsageSample', |
| 81 | '--output', path, |
| 82 | '--min-api', str(API_LEVEL), |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 83 | '--lib', utils.get_android_jar(API_LEVEL), |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 84 | '--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-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 91 | def test_r8command(paths): |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 92 | with utils.TempDir() as path: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 93 | # 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 Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 96 | # SAMPLE_JAR first on the classpath to use its version of R8CommandParser. |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 97 | args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 98 | 'com.android.tools.r8.R8CommandParser', |
| 99 | '--output', path + "/output.zip", |
| 100 | '--min-api', str(API_LEVEL), |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 101 | '--lib', utils.get_android_jar(API_LEVEL), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 102 | '--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-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 108 | def test_r8cfcommand(paths): |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 109 | with utils.TempDir() as path: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 110 | # 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 Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 113 | # SAMPLE_JAR first on the classpath to use its version of R8CommandParser. |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 114 | args = ['java', '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 115 | '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 Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 124 | def main(): |
| 125 | # Handle --help |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 126 | 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 Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 136 | |
| 137 | |
| 138 | if __name__ == '__main__': |
| 139 | main() |